Skip to content

Commit

Permalink
fix: test with interactivity required more info
Browse files Browse the repository at this point in the history
  • Loading branch information
statnmap committed Jan 10, 2025
1 parent 9a0fa3d commit e64aea1
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 101 deletions.
17 changes: 14 additions & 3 deletions R/build_fusen_chunks.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ add_fusen_chunks <- function(
# This will allow to interactively have the function name
if (is.null(function_name)) {
if (rstudioapi::hasFun("showPrompt")) {
function_name <- rstudioapi::showPrompt("{fusen}", "Enter the function name")
function_name <- rstudioapi::showPrompt(
"{fusen}",
"Enter the function name"
)
if (is.null(function_name)) {
return(NULL)
}
Expand All @@ -42,7 +45,12 @@ add_fusen_chunks <- function(

if (is.null(export)) {
if (rstudioapi::hasFun("showQuestion")) {
export <- rstudioapi::showQuestion("{fusen}", "Should the function be exported?", ok = "yes", cancel = "no")
export <- rstudioapi::showQuestion(
"{fusen}",
"Should the function be exported?",
ok = "yes",
cancel = "no"
)
} else {
export <- readline("Should the function be exported? (y/n) ") == "y"
}
Expand Down Expand Up @@ -111,7 +119,10 @@ build_fusen_chunks <- function(function_name, export = TRUE) {
sprintf("# %s", function_name),
" ",
sprintf("```{r development-%s}", function_name),
sprintf("# You can prepare the code of the %s() function here", function_name),
sprintf(
"# You can prepare the code of the %s() function here",
function_name
),
"```",
" ",
sprintf("```{r function-%s}", function_name),
Expand Down
8 changes: 5 additions & 3 deletions dev/dev_history_cran.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ pkgload::load_all()
# Unit tests open files interactively and close them
# The focus needs to be kept on what the test is doing
Sys.setenv("NOT_CRAN" = "true")
Sys.setenv("_R_CHECK_SYSTEM_CLOCK_" = 0)
testthat::test_dir("tests/testthat/")
Sys.setenv("_R_CHECK_SYSTEM_CLOCK_" = 0) # If clock not available
testthat::test_dir("tests/testthat/") # interactivity - answer yes
testthat::test_file("tests/testthat/test-inflate-part1.R")
testthat::test_file("tests/testthat/test-inflate-part2.R")
testthat::test_file("tests/testthat/test-inflate_all.R")
testthat::test_file("tests/testthat/test-inflate_all_utils.R")
testthat::test_file("tests/testthat/test-build_fusen_chunks.R") # Opens files
testthat::test_file("tests/testthat/test-add_flat_template.R")
testthat::test_file("tests/testthat/test-skeleton.R")
testthat::test_file("tests/testthat/test-register_config_file.R") # interactivity
testthat::test_file("tests/testthat/test-register_config_file.R") # interactivity - answer yes
testthat::test_file("tests/testthat/test-rename_flat_file.R")
testthat::test_file("tests/testthat/test-deprecate_flat_file.R")
testthat::test_file("tests/testthat/test-get_package_structure.R")
testthat::test_file("tests/testthat/test-sepuku.R") # interactivity - answer yes
Sys.setenv("NOT_CRAN" = "false")

# Test on Windows if directory is like `D:\\2025` messes with regex
Expand All @@ -64,6 +65,7 @@ if (.Platform$OS.type == "windows") {
# Back to normal
Sys.setenv(TMPDIR = tmpdir_orig) # Windows only
unlink(file.path(tmpdir_new, basename(tmpdir_orig)), recursive = TRUE)
unlink(list.files(tmpdir_new, pattern = "Rtmp", full.names = TRUE), recursive = TRUE)
tempdir(check = TRUE)
tempdir()
}
Expand Down
31 changes: 16 additions & 15 deletions dev/flat_addins.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ add_fusen_chunks <- function(
# This will allow to interactively have the function name
if (is.null(function_name)) {
if (rstudioapi::hasFun("showPrompt")) {
function_name <- rstudioapi::showPrompt("{fusen}", "Enter the function name")
function_name <- rstudioapi::showPrompt(
"{fusen}",
"Enter the function name"
)
if (is.null(function_name)) {
return(NULL)
}
Expand All @@ -59,7 +62,12 @@ add_fusen_chunks <- function(
if (is.null(export)) {
if (rstudioapi::hasFun("showQuestion")) {
export <- rstudioapi::showQuestion("{fusen}", "Should the function be exported?", ok = "yes", cancel = "no")
export <- rstudioapi::showQuestion(
"{fusen}",
"Should the function be exported?",
ok = "yes",
cancel = "no"
)
} else {
export <- readline("Should the function be exported? (y/n) ") == "y"
}
Expand Down Expand Up @@ -132,7 +140,10 @@ build_fusen_chunks <- function(function_name, export = TRUE) {
sprintf("# %s", function_name),
" ",
sprintf("```{r development-%s}", function_name),
sprintf("# You can prepare the code of the %s() function here", function_name),
sprintf(
"# You can prepare the code of the %s() function here",
function_name
),
"```",
" ",
sprintf("```{r function-%s}", function_name),
Expand Down Expand Up @@ -274,8 +285,7 @@ test_that("build_fusen_chunks add lines with export as expected", {
requireNamespace("rstudioapi") &&
rstudioapi::isAvailable() &&
rstudioapi::hasFun("navigateToFile") &&
rstudioapi::hasFun("documentSave") # &&
# rstudioapi::hasFun("documentClose")
rstudioapi::hasFun("documentSave")
) {
print("Test with RStudio")
# current position
Expand All @@ -286,10 +296,6 @@ test_that("build_fusen_chunks add lines with export as expected", {
Sys.sleep(1)
open_editor <- rstudioapi::getSourceEditorContext()
id <- open_editor$id
# the_open_path <- rstudioapi::documentPath(id)
# if (basename(the_open_path) != basename(path_dev_history)) {
# stop("Open the file was short to get the correct Id of the opened file")
# }
# add chunks
add_fusen_chunks(function_name = "zaza", export = TRUE)
rstudioapi::documentSave(id)
Expand Down Expand Up @@ -335,8 +341,7 @@ test_that("build_fusen_chunks add lines with noRd as expected", {
requireNamespace("rstudioapi") &&
rstudioapi::isAvailable() &&
rstudioapi::hasFun("navigateToFile") &&
rstudioapi::hasFun("documentSave") # &&
# rstudioapi::hasFun("documentClose")
rstudioapi::hasFun("documentSave")
) {
print("Test with RStudio")
# current position
Expand All @@ -347,10 +352,6 @@ test_that("build_fusen_chunks add lines with noRd as expected", {
Sys.sleep(1)
open_editor <- rstudioapi::getSourceEditorContext()
id <- open_editor$id
# the_open_path <- rstudioapi::documentPath(id)
# if (basename(the_open_path) != basename(path_dev_history)) {
# stop("Open the file was short to get the correct Id of the opened file")
# }
# add chunks
add_fusen_chunks(function_name = "zaza", export = FALSE)
rstudioapi::documentSave(id)
Expand Down
48 changes: 39 additions & 9 deletions dev/flat_register_config_file.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,22 @@ dir.create(dummypackage)
# {fusen} steps
fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package"))
dev_file <- suppressMessages(add_flat_template(pkg = dummypackage, overwrite = TRUE, open = FALSE))
dev_file <- suppressMessages(
add_flat_template(
pkg = dummypackage,
overwrite = TRUE,
open = FALSE
)
)
flat_file <- dev_file[grepl("flat_", dev_file)]
usethis::with_project(dummypackage, {
test_that("check_not_registered_files returns message if empty", {
expect_true(inherits(check_not_registered_files, "function"))
expect_message(check_not_registered_files(open = FALSE), "There are no files in the package")
expect_message(
check_not_registered_files(open = FALSE),
"There are no files in the package"
)
})
# Inflate once
Expand All @@ -388,17 +397,25 @@ usethis::with_project(dummypackage, {
)
test_that("guess_flat_origin works", {
guessed_path <- guess_flat_origin(file.path(dummypackage, "R", "my_median.R"))
guessed_path <- guess_flat_origin(
file.path(dummypackage, "R", "my_median.R")
)
# Relative path
expect_equal(guessed_path, file.path("dev", "flat_full.Rmd"))
guessed_path <- guess_flat_origin(file.path(dummypackage, "dev", "0-dev_history.Rmd"))
guessed_path <- guess_flat_origin(
file.path(dummypackage, "dev", "0-dev_history.Rmd")
)
expect_true(grepl("No existing source path found", guessed_path))
})
test_that("check_not_registered_files works", {
# All files were registered during inflate
expect_true(file.exists(file.path(dummypackage, "dev", "config_fusen.yaml")))
expect_true(
file.exists(
file.path(dummypackage, "dev", "config_fusen.yaml")
)
)
expect_message(
out_csv <- check_not_registered_files(open = FALSE),
regexp = "There are no unregistered files"
Expand Down Expand Up @@ -446,7 +463,10 @@ usethis::with_project(dummypackage, {
)
expect_equal(
out_config_content[["flat_full.Rmd"]][["tests"]],
c("tests/testthat/test-my_median.R", "tests/testthat/test-my_other_median.R")
c(
"tests/testthat/test-my_median.R",
"tests/testthat/test-my_other_median.R"
)
)
expect_equal(
out_config_content[["flat_full.Rmd"]][["vignettes"]],
Expand All @@ -460,7 +480,10 @@ usethis::with_project(dummypackage, {
cat("# test R file\n", file = file.path(dummypackage, "R", "to_keep.R"))
test_that("check_not_registered_files can help manually fill config", {
expect_message(out_csv <- check_not_registered_files(open = FALSE), "Some files in your package are not registered in the configuration file")
expect_message(
out_csv <- check_not_registered_files(open = FALSE),
"Some files in your package are not registered in the configuration file"
)
content_csv <- read.csv(out_csv, stringsAsFactors = FALSE)
expect_true(
grepl(
Expand All @@ -470,11 +493,12 @@ usethis::with_project(dummypackage, {
)
# Add in the yaml file with `df_to_config()`
keep_to_add_to_config <- content_csv[grepl("to_keep.R", content_csv[, "path"]), ]
keep_to_add_to_config <- content_csv[
grepl("to_keep.R", content_csv[, "path"]),
]
keep_to_add_to_config$origin <- "keep"
out_config <- df_to_config(keep_to_add_to_config)
# rstudioapi::navigateToFile(out_config)
out_config_content <- yaml::read_yaml(out_config)
expect_equal(out_config_content$keep$R, "R/to_keep.R")
Expand Down Expand Up @@ -1592,6 +1616,9 @@ withr::with_dir(temp_clean_inflate, {
expect_true(file.exists("tests/testthat/test-zaza.R"))
})
cli::cat_rule(
"Test: You should be asked a question: please answer 'y' during tests"
)
update_one_group_yaml(
all_files_new,
complete_yaml = yaml::read_yaml(config_file),
Expand All @@ -1610,6 +1637,9 @@ withr::with_dir(temp_clean_inflate, {
)
)
do_it <- readline("Test: Were you just asked to remove listed files? (y/n)") == "y"
expect_true(do_it)
test_that("update_one_group_yaml clean old files after interactive", {
expect_true(file.exists("R/to_add.R"))
expect_false(file.exists("R/to_remove.R"))
Expand Down
54 changes: 29 additions & 25 deletions dev/flat_sepuku.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -425,36 +425,40 @@ dummypackage <- tempfile(paste0(sample(letters, 10), collapse = ""))
dir.create(dummypackage)
fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package"))
usethis::with_project(dummypackage, {
# Add licence
usethis::use_mit_license("John Doe")
test_that("sepuku informs the user than modifying / deleting files can not be undone if force = FALSE", {
dev_file1 <- add_minimal_flat(
pkg = dummypackage,
flat_name = "flat1.Rmd",
open = FALSE
)
inflate(
pkg = dummypackage,
flat_file = dev_file1,
vignette_name = "Get started",
check = FALSE,
open_vignette = FALSE,
document = TRUE,
overwrite = "yes"
)
dev_file1 <- add_minimal_flat(
pkg = dummypackage,
flat_name = "flat1.Rmd",
open = FALSE
)
inflate(
pkg = dummypackage,
flat_file = dev_file1,
vignette_name = "Get started",
check = FALSE,
open_vignette = FALSE,
document = TRUE,
overwrite = "yes"
)
if (interactive()) {
test_that("sepuku tells the users that files will be modified or deleted and that it is irreversible", {
expect_message(
sepuku(force = FALSE),
"Some files are about to be deleted or modified. This operation is irreversible."
)
})
}
})
if (interactive()) {
cli::cat_rule(
"Test: You should be asked a question: please answer 'y' during tests"
)
test_that("sepuku tells the users that files will be modified or deleted and that it is irreversible", {
expect_message(
sepuku(force = FALSE),
"Some files are about to be deleted or modified. This operation is irreversible."
)
do_it <- readline("Test: Were you just asked you were sure what you were doing? (y/n)") == "y"
expect_true(do_it)
})
}
})
unlink(dummypackage, recursive = TRUE)
Expand Down
14 changes: 2 additions & 12 deletions tests/testthat/test-build_fusen_chunks.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ test_that("build_fusen_chunks add lines with export as expected", {
requireNamespace("rstudioapi") &&
rstudioapi::isAvailable() &&
rstudioapi::hasFun("navigateToFile") &&
rstudioapi::hasFun("documentSave") # &&
# rstudioapi::hasFun("documentClose")
rstudioapi::hasFun("documentSave")
) {
print("Test with RStudio")
# current position
Expand All @@ -115,10 +114,6 @@ test_that("build_fusen_chunks add lines with export as expected", {
Sys.sleep(1)
open_editor <- rstudioapi::getSourceEditorContext()
id <- open_editor$id
# the_open_path <- rstudioapi::documentPath(id)
# if (basename(the_open_path) != basename(path_dev_history)) {
# stop("Open the file was short to get the correct Id of the opened file")
# }
# add chunks
add_fusen_chunks(function_name = "zaza", export = TRUE)
rstudioapi::documentSave(id)
Expand Down Expand Up @@ -164,8 +159,7 @@ test_that("build_fusen_chunks add lines with noRd as expected", {
requireNamespace("rstudioapi") &&
rstudioapi::isAvailable() &&
rstudioapi::hasFun("navigateToFile") &&
rstudioapi::hasFun("documentSave") # &&
# rstudioapi::hasFun("documentClose")
rstudioapi::hasFun("documentSave")
) {
print("Test with RStudio")
# current position
Expand All @@ -176,10 +170,6 @@ test_that("build_fusen_chunks add lines with noRd as expected", {
Sys.sleep(1)
open_editor <- rstudioapi::getSourceEditorContext()
id <- open_editor$id
# the_open_path <- rstudioapi::documentPath(id)
# if (basename(the_open_path) != basename(path_dev_history)) {
# stop("Open the file was short to get the correct Id of the opened file")
# }
# add chunks
add_fusen_chunks(function_name = "zaza", export = FALSE)
rstudioapi::documentSave(id)
Expand Down
Loading

0 comments on commit e64aea1

Please sign in to comment.