From e64aea1b87daa630ae32a3d91f4488ccd1970b14 Mon Sep 17 00:00:00 2001 From: statnmap Date: Fri, 10 Jan 2025 18:23:31 +0100 Subject: [PATCH] fix: test with interactivity required more info --- R/build_fusen_chunks.R | 17 +++++-- dev/dev_history_cran.R | 8 ++-- dev/flat_addins.Rmd | 31 +++++++------ dev/flat_register_config_file.Rmd | 48 +++++++++++++++---- dev/flat_sepuku.Rmd | 54 ++++++++++++---------- tests/testthat/test-build_fusen_chunks.R | 14 +----- tests/testthat/test-register_config_file.R | 48 +++++++++++++++---- tests/testthat/test-sepuku.R | 54 ++++++++++++---------- 8 files changed, 173 insertions(+), 101 deletions(-) diff --git a/R/build_fusen_chunks.R b/R/build_fusen_chunks.R index 877098cc..e7b107b7 100644 --- a/R/build_fusen_chunks.R +++ b/R/build_fusen_chunks.R @@ -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) } @@ -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" } @@ -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), diff --git a/dev/dev_history_cran.R b/dev/dev_history_cran.R index 23849e50..672c5c61 100644 --- a/dev/dev_history_cran.R +++ b/dev/dev_history_cran.R @@ -27,8 +27,8 @@ 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") @@ -36,10 +36,11 @@ 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 @@ -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() } diff --git a/dev/flat_addins.Rmd b/dev/flat_addins.Rmd index f14d9584..486b4004 100644 --- a/dev/flat_addins.Rmd +++ b/dev/flat_addins.Rmd @@ -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) } @@ -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" } @@ -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), @@ -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 @@ -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) @@ -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 @@ -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) diff --git a/dev/flat_register_config_file.Rmd b/dev/flat_register_config_file.Rmd index 703fab1f..cfc58113 100644 --- a/dev/flat_register_config_file.Rmd +++ b/dev/flat_register_config_file.Rmd @@ -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 @@ -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" @@ -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"]], @@ -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( @@ -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") @@ -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), @@ -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")) diff --git a/dev/flat_sepuku.Rmd b/dev/flat_sepuku.Rmd index 01b6ae8a..17fcedec 100644 --- a/dev/flat_sepuku.Rmd +++ b/dev/flat_sepuku.Rmd @@ -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) diff --git a/tests/testthat/test-build_fusen_chunks.R b/tests/testthat/test-build_fusen_chunks.R index a127f38c..d029cb04 100644 --- a/tests/testthat/test-build_fusen_chunks.R +++ b/tests/testthat/test-build_fusen_chunks.R @@ -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 @@ -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) @@ -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 @@ -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) diff --git a/tests/testthat/test-register_config_file.R b/tests/testthat/test-register_config_file.R index d168d965..34efa7dc 100644 --- a/tests/testthat/test-register_config_file.R +++ b/tests/testthat/test-register_config_file.R @@ -5,13 +5,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 @@ -26,17 +35,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" @@ -84,7 +101,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"]], @@ -98,7 +118,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( @@ -108,11 +131,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") @@ -478,6 +502,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), @@ -496,6 +523,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")) diff --git a/tests/testthat/test-sepuku.R b/tests/testthat/test-sepuku.R index 1476dffc..20b781f0 100644 --- a/tests/testthat/test-sepuku.R +++ b/tests/testthat/test-sepuku.R @@ -172,36 +172,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)