Skip to content

Commit 89ea33f

Browse files
committed
Export context_start_file()
Replacing exported context_name() since this is a higher level API that should be used by external reporters Fixes #1082
1 parent d07d805 commit 89ea33f

11 files changed

+47
-54
lines changed

DESCRIPTION

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ Collate:
6464
'compare-numeric.R'
6565
'compare-time.R'
6666
'context.R'
67-
'context_name.R'
6867
'deprec-condition.R'
6968
'describe.R'
7069
'edition.R'

NAMESPACE

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export(capture_warnings)
5858
export(check_reporter)
5959
export(compare)
6060
export(context)
61-
export(context_name)
61+
export(context_start_file)
6262
export(default_compact_reporter)
6363
export(default_reporter)
6464
export(describe)

NEWS.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# testthat (development version)
22

3+
* `context_start_file()` is now exported for external reporters (#983, #1082).
4+
It now only strips first instance of prefix/suffix (#1041, @stufield).
5+
36
* New `CompactProgressReporter` tweaks the output of `ProgressReporter` for
47
use with a single file, as in `devtools::test_file()`. You can pick a
58
different default by setting `testthat.default_compact_reporter` to
@@ -69,9 +72,6 @@
6972
`options(lifecycle_verbosity = "warning")` to ensure that lifecycle
7073
warnings are always generated in tests.
7174

72-
* `context_name()` is now exported (#983, @stufield) and only strips first
73-
instance of prefix/suffix (#1041, @stufield).
74-
7575
* `verify_output()` now uses the `pdf()` device instead of `png()`; that makes
7676
it work on systems without X11 (#1011).
7777

R/context.R

+19
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,22 @@ context_start <- function(desc) {
2929
get_reporter()$.start_context(desc)
3030
}
3131
}
32+
33+
#' Start test context from a file name
34+
#'
35+
#' For use in external reporters
36+
#'
37+
#' @param name file name
38+
#' @keywords internal
39+
#' @export
40+
context_start_file <- function(name) {
41+
context_start(context_name(name))
42+
}
43+
44+
context_name <- function(filename) {
45+
# Remove test- prefix
46+
filename <- sub("^test-", "", filename)
47+
# Remove terminal extension
48+
filename <- sub("[.][Rr]$", "", filename)
49+
filename
50+
}

R/context_name.R

-19
This file was deleted.

R/reporter-junit.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ JunitReporter <- R6::R6Class("JunitReporter",
7474

7575
start_test = function(context, test) {
7676
if (is.null(context)) {
77-
name <- context_name(self$file_name)
78-
context_start(name)
77+
context_start_file(self$file_name)
7978
}
8079
},
8180

R/reporter-progress.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ ProgressReporter <- R6::R6Class("ProgressReporter",
7676
self$ctxt_issues <- Stack$new()
7777
self$ctxt_start_time <- proc.time()
7878

79-
name <- context_name(self$file_name)
80-
context_start(name)
79+
context_start_file(self$file_name)
8180
},
8281

8382
start_context = function(context) {

man/context_name.Rd

-20
This file was deleted.

man/context_start_file.Rd

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-context.R

+7
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ test_that("contexts are opened, then closed", {
3434
expect_equal(report$test_count, 4)
3535
expect_equal(report$test_i, 0)
3636
})
37+
38+
test_that("context_name strips prefix and extensions correctly", {
39+
expect_equal(context_name("test-metrics.R"), "metrics") # uppercase
40+
expect_equal(context_name("test-metrics.r"), "metrics") # lowercase
41+
expect_equal(context_name("test-check.Rfile.R"), "check.Rfile") # suffix only
42+
expect_equal(context_name("test-test-test.R"), "test-test") # 1st prefix only
43+
})

tests/testthat/test-context_name.R

-6
This file was deleted.

0 commit comments

Comments
 (0)