66# ' `test_that()` calls, or used test_this tags. If you want automatic
77# ' analysis of test coverage, you must look in other packages such as `covr`.
88# '
9- # ' @template base_path
109# ' @param from_tags Logical scalar. Checks the files if your test directory for
1110# ' testthis tags. Specifically, if you have the comment `#* @testing myfunction`
1211# ' in any of your test files, myfunction will be marked as tested.
3433# ' }
3534# '
3635get_test_coverage <- function (
37- base_path = ' .' ,
3836 from_tags = TRUE ,
3937 from_desc = TRUE
4038){
41- all <- get_pkg_functions(base_path = base_path )
39+ all <- get_pkg_functions()
4240 tst <- get_pkg_tested_functions(
43- base_path = base_path ,
4441 from_tags = from_tags ,
4542 from_desc = from_desc
4643 )
47- ign <- get_pkg_testignore(base_path = base_path )
44+ ign <- get_pkg_testignore()
4845
4946
5047 res <- data.frame (
5148 fun = all ,
52- exp = all %in% get_pkg_exports(base_path = base_path ),
53- s3 = all %in% get_pkg_S3methods(base_path = base_path ),
49+ exp = all %in% get_pkg_exports(),
50+ s3 = all %in% get_pkg_S3methods(),
5451 tested = all %in% tst ,
5552 ignore = all %in% ign ,
5653 stringsAsFactors = FALSE
5754 )
5855
59- attr(res , ' package' ) <- devtools :: as.package( base_path ) $ package
56+ attr(res , ' package' ) <- usethis :: proj_get()
6057 test_coverage(res )
6158}
6259
@@ -170,15 +167,25 @@ print.Test_coverage <- function(x, ...){
170167# ' @inheritParams get_test_coverage
171168# ' @noRd
172169# ' @return `get_pkg_functions()` returns a character vector of *all* functions
173- # ' defined in `base_path` .
170+ # ' defined in package .
174171# '
175- get_pkg_functions <- function (base_path = ' .' ){
176- base_path <- devtools :: as.package(base_path )
172+ get_pkg_functions <- function (){
173+ pkg <- devtools :: as.package(usethis :: proj_get())
174+
175+ ns <- tryCatch(
176+ asNamespace(pkg $ package ),
177+ error = function (e ){
178+ stop(
179+ " Functions can only be detected for installed packages"
180+ )
181+ })
182+
177183 res <- as.character(unclass(
178184 utils :: lsf.str(
179- envir = asNamespace(base_path $ package ),
185+ envir = asNamespace(pkg $ package ),
180186 all = TRUE )
181187 ))
188+
182189 return (res )
183190}
184191
@@ -187,29 +194,41 @@ get_pkg_functions <- function(base_path = '.'){
187194
188195# ' @rdname get_pkg_functions
189196# ' @return `get_pkg_exports()` returns a character vector of functions *exported*
190- # ' from `base_path`s NAMESPACE.
197+ # ' from the current packages NAMESPACE.
191198# ' @noRd
192- get_pkg_exports <- function (base_path = ' . ' ){
193- base_path %> %
199+ get_pkg_exports <- function (){
200+ ns <- usethis :: proj_get() %> %
194201 devtools :: as.package() %> %
195- devtools :: parse_ns_file() %> %
196- magrittr :: extract2(' exports' )
202+ devtools :: parse_ns_file()
203+
204+ if (identical(ns $ exportPatterns , " ^[[:alpha:]]+" )){
205+ return (get_pkg_functions())
206+ } else {
207+ ns [[" exports" ]]
208+ }
197209}
198210
199211
200212
201213
202214# ' @rdname get_pkg_functions
203215# ' @return `get_pkg_S3methods()` returns a character vector of all *S3 methods*
204- # ' exported from `base_path`s NAMESPACE.
216+ # ' exported from the current packages NAMESPACE.
205217# ' @noRd
206- get_pkg_S3methods <- function (base_path = ' . ' ){
207- dd <- base_path %> %
218+ get_pkg_S3methods <- function (){
219+ ns <- usethis :: proj_get() %> %
208220 devtools :: as.package() %> %
209- devtools :: parse_ns_file() %> %
210- magrittr :: extract2(' S3methods' )
221+ devtools :: parse_ns_file()
222+
223+ if (identical(ns $ exportPatterns , " ^[[:alpha:]]+" )){
224+ warning(
225+ " Detecting exported functions only works if all functions are " ,
226+ " explicitly exported in the NAMESPACE file (for example via Roxygen)"
227+ )}
211228
212- apply(dd , 1 , function (x ) paste(stats :: na.omit(x ), collapse = ' .' ))
229+ ns %> %
230+ magrittr :: extract2(' S3methods' ) %> %
231+ apply(1 , function (x ) paste(stats :: na.omit(x ), collapse = ' .' ))
213232}
214233
215234
@@ -219,15 +238,15 @@ get_pkg_S3methods <- function(base_path = '.'){
219238# ' @return `get_pkg_tested_functions()` returns a character vector of all
220239# ' *functions for which unit tests exist*.
221240# ' @noRd
222- get_pkg_tested_functions <- function (base_path , from_tags , from_desc ){
241+ get_pkg_tested_functions <- function (from_tags , from_desc ){
223242 res <- vector()
224243
225244 if (from_tags ){
226- res <- c(res , get_pkg_tested_functions_from_tags(base_path ))
245+ res <- c(res , get_pkg_tested_functions_from_tags())
227246 }
228247
229248 if (from_desc ){
230- res <- c(res , get_pkg_tested_functions_from_desc(base_path ))
249+ res <- c(res , get_pkg_tested_functions_from_desc())
231250 }
232251
233252 return (res )
@@ -240,9 +259,8 @@ get_pkg_tested_functions <- function(base_path, from_tags, from_desc){
240259# ' @return `get_pkg_testignore()` returns a character vector of all
241260# ' functions listed in \file{tests/testthat/_testignore}.
242261# ' @noRd
243- get_pkg_testignore <- function (base_path ){
244- base_path <- devtools :: as.package(base_path )
245- tfile <- file.path(base_path $ path , ' tests' , ' testthat' , ' _testignore' )
262+ get_pkg_testignore <- function (){
263+ tfile <- file.path(usethis :: proj_get(), ' tests' , ' testthat' , ' _testignore' )
246264
247265 if (file.exists(tfile )){
248266 return (readLines(tfile ))
@@ -254,20 +272,20 @@ get_pkg_testignore <- function(base_path){
254272
255273
256274
257- get_pkg_tested_functions_from_tags <- function (base_path ){
258- taglists <- get_test_taglist(base_path )
275+ get_pkg_tested_functions_from_tags <- function (){
276+ taglists <- get_test_taglist()
259277 res <- sort(unlist(unique(lapply(taglists , get_tag , ' testing' ))))
260278 return (res )
261279}
262280
263281
264282
265283
266- get_pkg_tested_functions_from_desc <- function (base_path ){
267- ttfiles <- list_test_files(base_path , full_names = TRUE )
284+ get_pkg_tested_functions_from_desc <- function (){
285+ ttfiles <- list_test_files(full_names = TRUE )
268286 descs <- extract_test_that_desc(ttfiles )
269287
270- pkgfuns <- get_pkg_functions(base_path )
288+ pkgfuns <- get_pkg_functions()
271289 res <- rep(NA , length(pkgfuns ))
272290
273291 for (i in seq_along(pkgfuns )){
0 commit comments