Skip to content

Commit 55b8037

Browse files
committed
Open test file if it already exists
Fixes #97 Also improve checks so you're less likely to create a bad file.
1 parent 5491eef commit 55b8037

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

R/test.R

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ use_test <- function(name = NULL, open = TRUE) {
3232
use_testthat()
3333
}
3434

35-
use_template("test-example.R",
36-
file.path("tests", "testthat", name),
37-
data = list(test_name = name),
38-
open = open
39-
)
35+
path <- file.path("tests", "testthat", name)
36+
37+
if (file.exists(file.path(proj_get(), path))) {
38+
edit_file(proj_get(), path)
39+
} else {
40+
use_template("test-example.R",
41+
path,
42+
data = list(test_name = name),
43+
open = open
44+
)
45+
}
46+
4047

4148
invisible(TRUE)
4249
}
@@ -55,14 +62,19 @@ find_test_name <- function(name = NULL) {
5562
return(paste0("test-", slug(name, ".R")))
5663
}
5764

58-
if (rstudioapi::isAvailable()) {
59-
active_file <- rstudioapi::getSourceEditorContext()$path
65+
if (!rstudioapi::isAvailable()) {
66+
stop("Argument `name` is missing, with no default", call. = FALSE)
67+
}
68+
active_file <- rstudioapi::getSourceEditorContext()$path
6069

61-
if (grepl("\\.[Rr]$", active_file)) {
62-
return(paste0("test-", basename(active_file)))
63-
}
70+
dir <- basename(dirname(active_file))
71+
if (dir != "R") {
72+
stop("Open file not in `R/` directory", call. = FALSE)
6473
}
6574

66-
stop("Argument `name` is missing, with no default", call. = FALSE)
75+
if (!grepl("\\.[Rr]$", active_file)) {
76+
stop("Open file is does not end in `.R`", call. = FALSE)
77+
}
6778

79+
paste0("test-", basename(active_file))
6880
}

0 commit comments

Comments
 (0)