Skip to content

Commit

Permalink
test: refactoring some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinFay committed Dec 10, 2024
1 parent 0cdcb66 commit 8b00ba2
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 130 deletions.
70 changes: 37 additions & 33 deletions R/create_pipework.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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")
Expand Down Expand Up @@ -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)
}
17 changes: 11 additions & 6 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,3 +26,13 @@ dir_remove <- function(path) {
force = TRUE
)
}

create_dummy_mariobox <- function(){
copy_empty_mariobox(
file.path(
tempdir(),
"dummymariobox"
),
"dummymariobox"
)
}
5 changes: 5 additions & 0 deletions tests/testthat/test-R.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test_that("",{
expect_true(
(1 + 1) == 2
)
})
3 changes: 3 additions & 0 deletions tests/testthat/test-build_plumber_file.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test_that("multiplication works", {
expect_equal(2 * 2, 4)
})
116 changes: 38 additions & 78 deletions tests/testthat/test-create_pipework.R
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 2 additions & 13 deletions tests/testthat/test-manage_endpoints.R
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -127,6 +118,4 @@ test_that("Managing endpoints", {
default_yaml
)
})

dir_remove(path_dummy)
})
37 changes: 37 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 8b00ba2

Please sign in to comment.