Skip to content

Commit c0713fb

Browse files
committed
Use usethis::proj_get() instead of the base_path function argument for compatbility with usethis.
1 parent 40732de commit c0713fb

17 files changed

+164
-152
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: testthis
22
Type: Package
33
Title: Utils and 'RStudio' Addins to Make Testing Even More Fun
4-
Version: 1.0.2
4+
Version: 1.0.1.9000
55
Authors@R: person("Stefan", "Fleck", email = "[email protected]", role = c("aut", "cre"))
66
Maintainer: Stefan Fleck <[email protected]>
77
Description:

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# testthis 1.0.2
22

33
* added `test_all()` to run tests in all subdirectories
4+
* Use `usethis::proj_get()` instead of the `base_path` function argument for
5+
compatbility with usethis.
46

57

68
# testthis 1.0.1

R/Taglist.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ taglist <- function(dat){
2525

2626

2727

28-
get_rdir_taglist <- function(base_path = '.'){
29-
ttfiles <- list_rdir_files(base_path, full_names = TRUE)
28+
get_rdir_taglist <- function(){
29+
ttfiles <- list_rdir_files(full_names = TRUE)
3030
lapply(ttfiles, get_taglist)
3131
}
3232

3333

3434

3535

36-
get_test_taglist <- function(base_path = '.'){
37-
ttfiles <- list_test_files(base_path, full_names = TRUE)
36+
get_test_taglist <- function(){
37+
ttfiles <- list_test_files(full_names = TRUE)
3838
taglists <- lapply(ttfiles, get_taglist)
3939
}
4040

R/Test_coverage.R

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
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.
@@ -34,29 +33,27 @@
3433
#' }
3534
#'
3635
get_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)){

R/list_test_files.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
list_test_files <- function(
2-
base_path,
32
full_names = TRUE,
43
skip = FALSE,
54
recursive = TRUE
65
){
7-
pkg <- devtools::as.package(base_path)
8-
tpath <- file.path(pkg$path, 'tests', 'testthat')
6+
tpath <- file.path(usethis::proj_get(), 'tests', 'testthat')
97
res <- list.files(
108
tpath,
119
full.names = TRUE,
@@ -29,12 +27,10 @@ list_test_files <- function(
2927

3028

3129
list_rdir_files <- function(
32-
base_path,
3330
full_names = TRUE,
3431
skip = FALSE
3532
){
36-
base_path <- devtools::as.package(base_path)
37-
tpath <- file.path(base_path$path, 'R')
33+
tpath <- file.path(usethis::proj_get(), 'R')
3834
list.files(
3935
tpath,
4036
full.names = full_names

R/test_subdir.R

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#' own test subdirs instead.
3636
#'
3737
#' @inheritParams devtools::test
38-
#' @template base_path
3938
#' @param subdir subdir of `inst/tests/` or `tests/testthat` that contains the
4039
#' tests
4140
#' @param ... passed on to `devtools::test()`
@@ -44,37 +43,23 @@
4443
#' @return A [testthat_results] object (invisibly)
4544
#'
4645
#' @export
47-
test_subdir <- function(subdir, base_path = '.', ...){
48-
49-
find_test_dir_mock <- function (path){
50-
testthat <- file.path(path, "tests", "testthat", subdir)
51-
if (dir.exists(testthat))
52-
return(testthat)
53-
inst <- file.path(path, "inst", "tests", subdir)
54-
if (dir.exists(inst))
55-
return(inst)
56-
stop(
57-
sprintf("%s not found in any of the test dirs of %s", subdir, path),
58-
call. = FALSE
59-
)
60-
}
61-
46+
test_subdir <- function(subdir, ...){
6247

63-
invisible(testthat::with_mock(
64-
`devtools:::find_test_dir` = find_test_dir_mock,
65-
devtools::test(pkg = base_path, ...)
48+
browser()
49+
testthat::test_dir(file.path(
50+
usethis::proj_get(), testthat::test_path(), subdir
6651
))
52+
6753
}
6854

6955

7056

7157

7258
#' @rdname test_subdir
7359
#' @export
74-
test_integration <- function(base_path = '.', ...){
60+
test_integration <- function(...){
7561
test_subdir(
7662
subdir = options('testthis.integration_tests_path'),
77-
base_path = base_path,
7863
...)
7964
}
8065

@@ -83,10 +68,9 @@ test_integration <- function(base_path = '.', ...){
8368

8469
#' @rdname test_subdir
8570
#' @export
86-
test_acceptance <- function(base_path = '.', ...){
71+
test_acceptance <- function(...){
8772
test_subdir(
8873
subdir = options('testthis.acceptance_tests_path'),
89-
base_path = base_path,
9074
...)
9175
}
9276

@@ -95,10 +79,9 @@ test_acceptance <- function(base_path = '.', ...){
9579

9680
#' @rdname test_subdir
9781
#' @export
98-
test_manual <- function(base_path = '.', ...){
82+
test_manual <- function(...){
9983
test_subdir(
10084
subdir = options('testthis.manual_tests_path'),
101-
base_path = base_path,
10285
...)
10386
}
10487

@@ -110,10 +93,9 @@ test_manual <- function(base_path = '.', ...){
11093
#' @rdname test_subdir
11194
#' @export
11295
test_all <- function(
113-
base_path = ".",
11496
...
11597
){
116-
pkg_dir <- devtools::as.package(base_path)$path
98+
pkg_dir <- usethis::proj_get()
11799

118100
dirs <- basename(
119101
list.dirs(
@@ -133,6 +115,12 @@ test_all <- function(
133115

134116

135117

118+
119+
dir_mock <- function(path = ".", pattern = NULL, ...){
120+
list.files(path = path, pattern = pattern, recursive = TRUE)
121+
}
122+
123+
136124
find_test_scripts_mock <- function(
137125
path,
138126
filter = NULL,
@@ -152,5 +140,9 @@ find_test_scripts_mock <- function(
152140
invert = TRUE
153141
)
154142

155-
testthat:::filter_test_scripts(files, filter, invert, ...)
143+
if(!is.null(filter)){
144+
stop("filter not supported")
145+
}
146+
147+
files
156148
}

R/test_with_skip.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
#' See the devtools documentation for further info or
77
#' `vignette("testthis")` for infos on testthis tags.
88
#'
9-
#' @template base_path
109
#' @inheritParams devtools::test
1110
#'
1211
#' @export
13-
test_with_skip <- function(base_path = '.', ...){
12+
test_with_skip <- function(...){
1413
fltr <- list_test_files(
15-
base_path,
1614
full_names = FALSE,
1715
skip = TRUE
1816
)

0 commit comments

Comments
 (0)