From 8b00ba274ff6d63e12d86967a6ad1d4e045a2112 Mon Sep 17 00:00:00 2001 From: colin Date: Tue, 10 Dec 2024 10:01:12 +0100 Subject: [PATCH] test: refactoring some tests --- R/create_pipework.R | 70 +++++++------- tests/testthat/setup.R | 17 ++-- tests/testthat/test-R.R | 5 + tests/testthat/test-build_plumber_file.R | 3 + tests/testthat/test-create_pipework.R | 116 ++++++++--------------- tests/testthat/test-manage_endpoints.R | 15 +-- tests/testthat/test-utils.R | 37 ++++++++ 7 files changed, 133 insertions(+), 130 deletions(-) create mode 100644 tests/testthat/test-R.R create mode 100644 tests/testthat/test-build_plumber_file.R diff --git a/R/create_pipework.R b/R/create_pipework.R index 044f673..450be26 100644 --- a/R/create_pipework.R +++ b/R/create_pipework.R @@ -36,11 +36,6 @@ create_mariobox <- function( overwrite = FALSE, package_name = basename(path) ) { - # if (check_name) { - # cat_rule("Checking package name") - # getFromNamespace("check_package_name", "usethis")(package_name) - # cat_green_tick("Valid package name") - # } if (dir.exists(path)) { if (!isTRUE(overwrite)) { @@ -65,35 +60,10 @@ create_mariobox <- function( } cat_rule("Copying package skeleton") - marioboxexample_path <- mariobox_sys("marioboxexample") - dir_copy( - path = marioboxexample_path, - new_path = path, - overwrite = TRUE + copy_empty_mariobox( + path, + package_name ) - # Listing copied files ***from source directory*** - copied_files <- list.files( - path = marioboxexample_path, - full.names = FALSE, - all.files = TRUE, - recursive = TRUE - ) - # Going through copied files to replace package name - for (file in copied_files) { - copied_file <- file.path(path, file) - if (grepl("^REMOVEME", file)) { - file.rename( - from = copied_file, - to = file.path(path, gsub("REMOVEME", "", file)) - ) - copied_file <- file.path(path, gsub("REMOVEME", "", file)) - } - replace_word( - file = copied_file, - pattern = "marioboxexample", - replace = package_name - ) - } cat_green_tick("Copied app skeleton") cat_rule("Done") @@ -122,3 +92,37 @@ create_mariobox <- function( ) ) } + + +copy_empty_mariobox <- function(path, package_name){ + marioboxexample_path <- mariobox_sys("marioboxexample") + dir_copy( + path = marioboxexample_path, + new_path = path, + overwrite = TRUE + ) + # Listing copied files ***from source directory*** + copied_files <- list.files( + path = marioboxexample_path, + full.names = FALSE, + all.files = TRUE, + recursive = TRUE + ) + # Going through copied files to replace package name + for (file in copied_files) { + copied_file <- file.path(path, file) + if (grepl("^REMOVEME", file)) { + file.rename( + from = copied_file, + to = file.path(path, gsub("REMOVEME", "", file)) + ) + copied_file <- file.path(path, gsub("REMOVEME", "", file)) + } + replace_word( + file = copied_file, + pattern = "marioboxexample", + replace = package_name + ) + } + return(path) +} \ No newline at end of file diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index 50b2d14..a674b71 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -10,12 +10,7 @@ run_quietly_in_a_dummy_mariobox <- function(expr) { withr::with_options( c("usethis.quiet" = TRUE), { - dummy_mariobox <- tempdir() - dummy_mariobox <- create_mariobox( - dummy_mariobox, - overwrite = TRUE, - open = FALSE - ) + dummy_mariobox <- create_dummy_mariobox() withr::with_dir( dummy_mariobox, expr @@ -31,3 +26,13 @@ dir_remove <- function(path) { force = TRUE ) } + +create_dummy_mariobox <- function(){ + copy_empty_mariobox( + file.path( + tempdir(), + "dummymariobox" + ), + "dummymariobox" + ) +} \ No newline at end of file diff --git a/tests/testthat/test-R.R b/tests/testthat/test-R.R new file mode 100644 index 0000000..61b508a --- /dev/null +++ b/tests/testthat/test-R.R @@ -0,0 +1,5 @@ +test_that("",{ + expect_true( + (1 + 1) == 2 + ) +}) \ No newline at end of file diff --git a/tests/testthat/test-build_plumber_file.R b/tests/testthat/test-build_plumber_file.R new file mode 100644 index 0000000..8849056 --- /dev/null +++ b/tests/testthat/test-build_plumber_file.R @@ -0,0 +1,3 @@ +test_that("multiplication works", { + expect_equal(2 * 2, 4) +}) diff --git a/tests/testthat/test-create_pipework.R b/tests/testthat/test-create_pipework.R index 7806f35..db1d372 100644 --- a/tests/testthat/test-create_pipework.R +++ b/tests/testthat/test-create_pipework.R @@ -1,90 +1,50 @@ - is_properly_populated_mariobox <- function(path) { - # All files excepts *.Rproj which changes based on the project name - expected_files <- c( - "DESCRIPTION", - "LICENSE", - "LICENSE.md", - "dev/run_dev.R", - "inst/mariobox.yml", - "man/get_health.Rd", - "man/run_api.Rd", - "NAMESPACE", - "R/get_health.R", - "R/run_plumber.R", - "tests/testthat.R", - "tests/testthat/test-health.R", - "tests/testthat/test-run_plumber.R" + expected_files <- list.files( + mariobox_sys( + "marioboxexample" + ), + recursive = TRUE ) - if (rstudioapi::isAvailable()) { - expected_files <- c( - expected_files, - paste0(basename(path), ".Rproj") - ) - } - - actual_files <- list.files(path, recursive = TRUE) + expected_files <- expected_files[!grepl( + "Rproj", + expected_files + )] + expected_files <- expected_files[!grepl( + "vscode-R", + expected_files + )] - # browser() - # waldo::compare(sort(expected_files), sort(actual_files)) - identical(sort(expected_files), sort(actual_files)) -} + expected_files <- expected_files[!grepl( + "REMOVEME.Rbuildignore", + expected_files + )] -keep_only_non_pdf_related_warnings <- function(check_output_warnings) { - grep( - "pdf", - check_output_warnings, - ignore.case = TRUE, - invert = TRUE, - value = TRUE + actual_files <- list.files(path, recursive = TRUE) + actual_files <- actual_files[!grepl( + "vscode-R", + actual_files + )] + identical( + sort(expected_files), + sort(actual_files) ) } -path_dummy <- tempfile(pattern = "dummy") -dir.create(path_dummy) -dummy_mariobox_path <- file.path(path_dummy, "pipo") -path_pkg <- create_mariobox( - path = dummy_mariobox_path, - open = FALSE -) -test_that("create_mariobox() works", { - usethis::with_project(dummy_mariobox_path, { - usethis::use_mit_license(copyright_holder = "Babar") - - check_output <- rcmdcheck::rcmdcheck( - path = dummy_mariobox_path, - quiet = TRUE, - args = c("--no-manual") - ) - - expect_equal( - check_output[["errors"]], - character(0) - ) - expect_lte( - length( - keep_only_non_pdf_related_warnings(check_output[["warnings"]]) - ), - 1 - ) - if (length(check_output[["warnings"]]) == 1) { - expect_true(grepl("there is no package called", check_output[["warnings"]])) - } - expect_lte( - length(check_output[["notes"]]), - 1 - ) - if (length(check_output[["notes"]]) == 1) { - expect_true( - grepl("\\.here", check_output[["notes"]][1]) +test_that("copy_empty_mariobox works", { + on.exit( + { + dir_remove( + dummy_mariobox ) - } - expect_true( - is_properly_populated_mariobox(path_pkg) + }, + add = TRUE + ) + dummy_mariobox <- create_dummy_mariobox() + expect_true( + is_properly_populated_mariobox( + path = dummy_mariobox ) - }) + ) }) - -dir_remove(path_dummy) diff --git a/tests/testthat/test-manage_endpoints.R b/tests/testthat/test-manage_endpoints.R index 1d363b8..a407a71 100644 --- a/tests/testthat/test-manage_endpoints.R +++ b/tests/testthat/test-manage_endpoints.R @@ -1,15 +1,6 @@ test_that("Managing endpoints", { - path_dummy <- tempfile(pattern = "dummy") - dir.create(path_dummy) - - dummy_mariobox_path <- file.path(path_dummy, "pipo") - path_pkg <- create_mariobox( - path = dummy_mariobox_path, - open = FALSE - ) - - withr::with_dir(dummy_mariobox_path, { - # Adding endpoint + run_quietly_in_a_dummy_mariobox({ + # Adding endpoint mariobox_yaml_path <- "inst/mariobox.yml" endpoint_name <- "michel" r_file <- sprintf("R/get_%s.R", endpoint_name) @@ -127,6 +118,4 @@ test_that("Managing endpoints", { default_yaml ) }) - - dir_remove(path_dummy) }) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index f61758c..6f6f9e2 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -14,3 +14,40 @@ test_that("silent_lapply() works", { as.list(LETTERS) ) }) + + +test_that("replace_word replaces pattern correctly in a file", { + temp_file <- tempfile() + writeLines(c("Hello world", "Hello again"), temp_file) + replace_word(temp_file, "Hello", "Hi") + modified_content <- readLines(temp_file) + expect_equal(modified_content, c("Hi world", "Hi again")) + unlink(temp_file) +}) + +test_that("replace_word handles files with no matching pattern", { + temp_file <- tempfile() + writeLines(c("Goodbye world", "Goodbye again"), temp_file) + replace_word(temp_file, "Hello", "Hi") + modified_content <- readLines(temp_file) + expect_equal(modified_content, c("Goodbye world", "Goodbye again")) + unlink(temp_file) +}) + +test_that("replace_word works with empty files", { + temp_file <- tempfile() + file.create(temp_file) + replace_word(temp_file, "Hello", "Hi") + modified_content <- readLines(temp_file) + expect_equal(modified_content, character(0)) + unlink(temp_file) +}) + +test_that("replace_word works with complex patterns", { + temp_file <- tempfile() + writeLines(c("abc123", "xyz789"), temp_file) + replace_word(temp_file, "[a-z]{3}[0-9]{3}", "replaced") + modified_content <- readLines(temp_file) + expect_equal(modified_content, c("replaced", "replaced")) + unlink(temp_file) +})