@@ -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