From cccb60f011392ed850e2e75cd1a80012890667e7 Mon Sep 17 00:00:00 2001 From: IlyaZar Date: Wed, 5 Jul 2023 08:46:44 +0200 Subject: [PATCH 1/6] tests: add dummy files for use_{external,internal}_XXX_files() testing - add some dummy files to the inst/utils/ directory - for later: add tests that download exactly from that directory Refs: #1058 --- inst/utils/testfile_template_css.css | 13 +++++++++++++ inst/utils/testfile_template_html.html | 10 ++++++++++ inst/utils/testfile_template_js.js | 2 ++ inst/utils/testfile_template_plainfile.txt | 1 + 4 files changed, 26 insertions(+) create mode 100644 inst/utils/testfile_template_css.css create mode 100644 inst/utils/testfile_template_html.html create mode 100644 inst/utils/testfile_template_js.js create mode 100644 inst/utils/testfile_template_plainfile.txt diff --git a/inst/utils/testfile_template_css.css b/inst/utils/testfile_template_css.css new file mode 100644 index 00000000..2beaf256 --- /dev/null +++ b/inst/utils/testfile_template_css.css @@ -0,0 +1,13 @@ + body { + background-color: lightblue; +} + +h1 { + color: white; + text-align: center; +} + +p { + font-family: verdana; + font-size: 20px; +} diff --git a/inst/utils/testfile_template_html.html b/inst/utils/testfile_template_html.html new file mode 100644 index 00000000..c87692f8 --- /dev/null +++ b/inst/utils/testfile_template_html.html @@ -0,0 +1,10 @@ + + + + +

My First Heading

+ +

My first paragraph.

+ + + diff --git a/inst/utils/testfile_template_js.js b/inst/utils/testfile_template_js.js new file mode 100644 index 00000000..e282ac1d --- /dev/null +++ b/inst/utils/testfile_template_js.js @@ -0,0 +1,2 @@ +const myHeading = document.querySelector("h1"); +myHeading.textContent = "Hello world!"; diff --git a/inst/utils/testfile_template_plainfile.txt b/inst/utils/testfile_template_plainfile.txt new file mode 100644 index 00000000..3e715502 --- /dev/null +++ b/inst/utils/testfile_template_plainfile.txt @@ -0,0 +1 @@ +Some text. From 64e11e5056e2d696e156ede19f797b0940d2a556 Mon Sep 17 00:00:00 2001 From: IlyaZar Date: Wed, 5 Jul 2023 10:32:55 +0200 Subject: [PATCH 2/6] tests: add testing for external-funcs standard cases --- tests/testthat/test-use_files.R | 80 +++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tests/testthat/test-use_files.R diff --git a/tests/testthat/test-use_files.R b/tests/testthat/test-use_files.R new file mode 100644 index 00000000..2e0523be --- /dev/null +++ b/tests/testthat/test-use_files.R @@ -0,0 +1,80 @@ +test_that("use_external_XXX_files() function family works properly", { + path_dummy_golem <- tempfile(pattern = "dummygolem") + create_golem( + path = path_dummy_golem, + open = FALSE + ) + with_dir( + path_dummy_golem, + { + # I. test the external ".txt" file download + use_external_file( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_plainfile.txt", + name = "testfile_template_plainfile.txt" + ) + test_file_download <- readLines( + "inst/app/www/testfile_template_plainfile.txt" + ) + expect_identical( + test_file_download, + "Some text." + ) + + # II. test the external ".html" file download + use_external_html_template( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_html.html", + name = "testfile_template_plainfile.html" + ) + test_file_download <- readLines( + "inst/app/www/testfile_template_plainfile.html" + ) + expect_identical( + test_file_download, + c("", "", "", "", + "

My First Heading

", "", "

My first paragraph.

", "", + "","") + ) + + # III. test the external ".js" file download + use_external_js_file( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_js.js", + name = "testfile_template_js.js" + ) + test_file_download <- readLines( + "inst/app/www/testfile_template_js.js" + ) + expect_identical( + test_file_download, + c("const myHeading = document.querySelector(\"h1\");", + "myHeading.textContent = \"Hello world!\";") + ) + + # IV. test the external ".css" file download + use_external_css_file( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_css.css", + name = "testfile_template_css.css" + ) + test_file_download <- readLines( + "inst/app/www/testfile_template_css.css" + ) + expect_identical( + test_file_download, + c(" body {", + " background-color: lightblue;", + "}", + "", + "h1 {", + " color: white;", + " text-align: center;", + "}", + "", + "p {", + " font-family: verdana;", + " font-size: 20px;", + "}" + ) + ) + unlink(path_dummy_golem, recursive = TRUE) + } + ) +}) From 89b04ab777f3a054b53c23593b9c05e8c9ae62af Mon Sep 17 00:00:00 2001 From: IlyaZar Date: Wed, 5 Jul 2023 11:39:20 +0200 Subject: [PATCH 3/6] tests: add testing for internal-funcs standard cases - fix typo in testfile_template_html - use styler::style_file(..., style = grkstyle::grk_style_transformer) --- tests/testthat/test-use_files.R | 154 ++++++++++++++++++++++++++++++-- 1 file changed, 146 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test-use_files.R b/tests/testthat/test-use_files.R index 2e0523be..0f5b4eb6 100644 --- a/tests/testthat/test-use_files.R +++ b/tests/testthat/test-use_files.R @@ -23,16 +23,25 @@ test_that("use_external_XXX_files() function family works properly", { # II. test the external ".html" file download use_external_html_template( url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_html.html", - name = "testfile_template_plainfile.html" + name = "testfile_template_html.html" ) test_file_download <- readLines( - "inst/app/www/testfile_template_plainfile.html" + "inst/app/www/testfile_template_html.html" ) expect_identical( test_file_download, - c("", "", "", "", - "

My First Heading

", "", "

My first paragraph.

", "", - "","") + c( + "", + "", + "", + "", + "

My First Heading

", + "", + "

My first paragraph.

", + "", + "", + "" + ) ) # III. test the external ".js" file download @@ -45,8 +54,10 @@ test_that("use_external_XXX_files() function family works properly", { ) expect_identical( test_file_download, - c("const myHeading = document.querySelector(\"h1\");", - "myHeading.textContent = \"Hello world!\";") + c( + "const myHeading = document.querySelector(\"h1\");", + "myHeading.textContent = \"Hello world!\";" + ) ) # IV. test the external ".css" file download @@ -59,7 +70,134 @@ test_that("use_external_XXX_files() function family works properly", { ) expect_identical( test_file_download, - c(" body {", + c( + " body {", + " background-color: lightblue;", + "}", + "", + "h1 {", + " color: white;", + " text-align: center;", + "}", + "", + "p {", + " font-family: verdana;", + " font-size: 20px;", + "}" + ) + ) + unlink(path_dummy_golem, recursive = TRUE) + } + ) +}) +test_that("use_internal_XXX_files() function family works properly", { + path_dummy_golem <- tempfile(pattern = "dummygolem") + create_golem( + path = path_dummy_golem, + open = FALSE + ) + dir.create(file.path(path_dummy_golem, "tmp_dump_testfiles")) + file.copy( + from = file.path( + .libPaths()[1], + "golem", + "utils", + c( + "testfile_template_plainfile.txt", + "testfile_template_html.html", + "testfile_template_js.js", + "testfile_template_css.css" + ) + ), + to = file.path( + path_dummy_golem, + "tmp_dump_testfiles" + ) + ) + with_dir( + path_dummy_golem, + { + # I. test the internal ".txt" file usage + use_internal_file( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_plainfile.txt" + ), + name = "testfile_template_plainfile.txt" + ) + test_file_download <- readLines( + "inst/app/www/testfile_template_plainfile.txt" + ) + expect_identical( + test_file_download, + "Some text." + ) + + # II. test the internal ".html" file usage + use_internal_html_template( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_html.html" + ), + name = "testfile_template_html.html" + ) + test_file_download <- readLines( + "inst/app/www/testfile_template_html.html" + ) + expect_identical( + test_file_download, + c( + "", + "", + "", + "", + "

My First Heading

", + "", + "

My first paragraph.

", + "", + "", + "" + ) + ) + + # III. test the internal ".js" file usage + use_internal_js_file( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_js.js" + ), + name = "testfile_template_js.js" + ) + test_file_download <- readLines( + "inst/app/www/testfile_template_js.js" + ) + expect_identical( + test_file_download, + c( + "const myHeading = document.querySelector(\"h1\");", + "myHeading.textContent = \"Hello world!\";" + ) + ) + + # IV. test the internal ".css" file usage + use_internal_css_file( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_css.css" + ), + name = "testfile_template_css.css" + ) + test_file_download <- readLines( + "inst/app/www/testfile_template_css.css" + ) + expect_identical( + test_file_download, + c( + " body {", " background-color: lightblue;", "}", "", From 372d3aaef920c0fa50ae6b0e025b82a4968a8fc2 Mon Sep 17 00:00:00 2001 From: IlyaZar Date: Wed, 5 Jul 2023 12:56:42 +0200 Subject: [PATCH 4/6] fix: set default value for argument name before using it - the order needs a change at some places: first set a default value when name argument is missing - only then call check_name_length() to avoid errors of the type: "argument "name" is missing, with no default" - also, to avoid RStudio and other IDEs linting a missing argument, provide a default value "NULL" for argument 'name' Refs: #1060 --- R/use_files.R | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/R/use_files.R b/R/use_files.R index 1c322f5d..6148c70d 100644 --- a/R/use_files.R +++ b/R/use_files.R @@ -18,7 +18,7 @@ #' @return The path to the file, invisibly. use_external_js_file <- function( url, - name, + name = NULL, pkg = get_golem_wd(), dir = "inst/app/www", open = FALSE, @@ -85,7 +85,7 @@ use_external_js_file <- function( #' @rdname use_files use_external_css_file <- function( url, - name, + name = NULL, pkg = get_golem_wd(), dir = "inst/app/www", open = FALSE, @@ -211,18 +211,18 @@ use_external_html_template <- function( #' @rdname use_files use_external_file <- function( url, - name, + name = NULL, pkg = get_golem_wd(), dir = "inst/app/www", open = FALSE, dir_create = TRUE ) { - check_name_length(name) if (missing(name)) { name <- basename(url) } + check_name_length(name) old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) @@ -260,13 +260,12 @@ use_external_file <- function( #' @rdname use_files use_internal_js_file <- function( path, - name, + name = NULL, pkg = get_golem_wd(), dir = "inst/app/www", open = FALSE, dir_create = TRUE ) { - check_name_length(name) old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) @@ -274,6 +273,8 @@ use_internal_js_file <- function( name <- basename(path) } + check_name_length(name) + name <- file_path_sans_ext(name) new_file <- sprintf("%s.js", name) @@ -325,13 +326,12 @@ use_internal_js_file <- function( #' @rdname use_files use_internal_css_file <- function( path, - name, + name = NULL, pkg = get_golem_wd(), dir = "inst/app/www", open = FALSE, dir_create = TRUE ) { - check_name_length(name) old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) @@ -340,6 +340,8 @@ use_internal_css_file <- function( name <- basename(path) } + check_name_length(name) + name <- file_path_sans_ext(name) new_file <- sprintf("%s.css", name) @@ -449,7 +451,7 @@ use_internal_html_template <- function( #' @rdname use_files use_internal_file <- function( path, - name, + name = NULL, pkg = get_golem_wd(), dir = "inst/app/www", open = FALSE, From 21bccb1534d203a3091ec58018577356bb2f8524 Mon Sep 17 00:00:00 2001 From: IlyaZar Date: Wed, 5 Jul 2023 14:58:06 +0200 Subject: [PATCH 5/6] feat: improve error handling and allow codecovr. to be 100% - instead of throwing an error, return 'FALSE' is passed and cat_dir_necessary() prints an informative message to the user (with the same effect i.e. not creating a directory) - this behavior is a bit more userfriendly and also allows for a code coverage of 100%; the latter is not possible to implement since tests are run non-interactively --- R/use_files.R | 138 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 96 insertions(+), 42 deletions(-) diff --git a/R/use_files.R b/R/use_files.R index 6148c70d..416fd574 100644 --- a/R/use_files.R +++ b/R/use_files.R @@ -23,7 +23,7 @@ use_external_js_file <- function( dir = "inst/app/www", open = FALSE, dir_create = TRUE -) { + ) { old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) @@ -36,12 +36,19 @@ use_external_js_file <- function( name <- file_path_sans_ext(name) new_file <- sprintf("%s.js", name) - dir_created <- create_if_needed( - dir, - type = "directory" + dir_created <- tryCatch( + create_if_needed( + dir, + type = "directory" + ), + error = function(e) { + out <- FALSE + names(out) <- e[[1]] + return(out) + } ) - if (!dir_created) { + if (isFALSE(dir_created)) { cat_dir_necessary() return(invisible(FALSE)) } @@ -90,7 +97,7 @@ use_external_css_file <- function( dir = "inst/app/www", open = FALSE, dir_create = TRUE -) { + ) { old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) @@ -103,12 +110,19 @@ use_external_css_file <- function( name <- file_path_sans_ext(name) new_file <- sprintf("%s.css", name) - dir_created <- create_if_needed( - dir, - type = "directory" + dir_created <- tryCatch( + create_if_needed( + dir, + type = "directory" + ), + error = function(e) { + out <- FALSE + names(out) <- e[[1]] + return(out) + } ) - if (!dir_created) { + if (isFALSE(dir_created)) { cat_dir_necessary() return(invisible(FALSE)) } @@ -157,7 +171,7 @@ use_external_html_template <- function( dir = "inst/app/www", open = FALSE, dir_create = TRUE -) { + ) { old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) @@ -168,12 +182,19 @@ use_external_html_template <- function( check_name_length(name) - dir_created <- create_if_needed( - dir, - type = "directory" + dir_created <- tryCatch( + create_if_needed( + dir, + type = "directory" + ), + error = function(e) { + out <- FALSE + names(out) <- e[[1]] + return(out) + } ) - if (!dir_created) { + if (isFALSE(dir_created)) { cat_dir_necessary() return(invisible(FALSE)) } @@ -216,8 +237,7 @@ use_external_file <- function( dir = "inst/app/www", open = FALSE, dir_create = TRUE -) { - + ) { if (missing(name)) { name <- basename(url) } @@ -227,12 +247,19 @@ use_external_file <- function( old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) - dir_created <- create_if_needed( - dir, - type = "directory" + dir_created <- tryCatch( + create_if_needed( + dir, + type = "directory" + ), + error = function(e) { + out <- FALSE + names(out) <- e[[1]] + return(out) + } ) - if (!dir_created) { + if (isFALSE(dir_created)) { cat_dir_necessary() return(invisible(FALSE)) } @@ -265,7 +292,7 @@ use_internal_js_file <- function( dir = "inst/app/www", open = FALSE, dir_create = TRUE -) { + ) { old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) @@ -278,12 +305,19 @@ use_internal_js_file <- function( name <- file_path_sans_ext(name) new_file <- sprintf("%s.js", name) - dir_created <- create_if_needed( - dir, - type = "directory" + dir_created <- tryCatch( + create_if_needed( + dir, + type = "directory" + ), + error = function(e) { + out <- FALSE + names(out) <- e[[1]] + return(out) + } ) - if (!dir_created) { + if (isFALSE(dir_created)) { cat_dir_necessary() return(invisible(FALSE)) } @@ -331,8 +365,7 @@ use_internal_css_file <- function( dir = "inst/app/www", open = FALSE, dir_create = TRUE -) { - + ) { old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) @@ -345,12 +378,19 @@ use_internal_css_file <- function( name <- file_path_sans_ext(name) new_file <- sprintf("%s.css", name) - dir_created <- create_if_needed( - dir, - type = "directory" + dir_created <- tryCatch( + create_if_needed( + dir, + type = "directory" + ), + error = function(e) { + out <- FALSE + names(out) <- e[[1]] + return(out) + } ) - if (!dir_created) { + if (isFALSE(dir_created)) { cat_dir_necessary() return(invisible(FALSE)) } @@ -398,7 +438,7 @@ use_internal_html_template <- function( dir = "inst/app/www", open = FALSE, dir_create = TRUE -) { + ) { old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) @@ -409,12 +449,19 @@ use_internal_html_template <- function( file_path_sans_ext(name) ) - dir_created <- create_if_needed( - dir, - type = "directory" + dir_created <- tryCatch( + create_if_needed( + dir, + type = "directory" + ), + error = function(e) { + out <- FALSE + names(out) <- e[[1]] + return(out) + } ) - if (!dir_created) { + if (isFALSE(dir_created)) { cat_dir_necessary() return(invisible(FALSE)) } @@ -456,7 +503,7 @@ use_internal_file <- function( dir = "inst/app/www", open = FALSE, dir_create = TRUE -) { + ) { if (missing(name)) { name <- basename(path) } @@ -466,12 +513,19 @@ use_internal_file <- function( old <- setwd(fs_path_abs(pkg)) on.exit(setwd(old)) - dir_created <- create_if_needed( - dir, - type = "directory" + dir_created <- tryCatch( + create_if_needed( + dir, + type = "directory" + ), + error = function(e) { + out <- FALSE + names(out) <- e[[1]] + return(out) + } ) - if (!dir_created) { + if (isFALSE(dir_created)) { cat_dir_necessary() return(invisible(FALSE)) } From 8cb926201101ab7481276a22bfd98f3326629413 Mon Sep 17 00:00:00 2001 From: IlyaZar Date: Wed, 5 Jul 2023 14:59:18 +0200 Subject: [PATCH 6/6] tests: add corner cases for tests to make covr. 100% --- tests/testthat/test-use_files.R | 216 ++++++++++++++++++++++++++++++-- 1 file changed, 204 insertions(+), 12 deletions(-) diff --git a/tests/testthat/test-use_files.R b/tests/testthat/test-use_files.R index 0f5b4eb6..76d9ce21 100644 --- a/tests/testthat/test-use_files.R +++ b/tests/testthat/test-use_files.R @@ -8,9 +8,9 @@ test_that("use_external_XXX_files() function family works properly", { path_dummy_golem, { # I. test the external ".txt" file download + # I.A standard case use_external_file( - url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_plainfile.txt", - name = "testfile_template_plainfile.txt" + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_plainfile.txt" ) test_file_download <- readLines( "inst/app/www/testfile_template_plainfile.txt" @@ -19,8 +19,24 @@ test_that("use_external_XXX_files() function family works properly", { test_file_download, "Some text." ) + # I.B corner case: file already exists + expect_false( + use_external_file( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_plainfile.txt", + name = "testfile_template_plainfile.txt" + ) + ) + # I.C corner case: dir already exists + expect_false( + use_external_file( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_plainfile.txt", + name = "testfile_template_plainfile3.txt", + dir = "inst/app/www2" + ) + ) # II. test the external ".html" file download + # II.A standard case use_external_html_template( url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_html.html", name = "testfile_template_html.html" @@ -43,11 +59,26 @@ test_that("use_external_XXX_files() function family works properly", { "" ) ) + # II.B corner case: file already exists + expect_false( + use_external_html_template( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_html.html", + name = "testfile_template_html.html" + ) + ) + # II.C corner case: dir already exists + expect_false( + use_external_html_template( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_html.html", + name = "testfile_template_html2.html", + dir = "inst/app/www2" + ) + ) # III. test the external ".js" file download + # III.A standard case use_external_js_file( - url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_js.js", - name = "testfile_template_js.js" + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_js.js" ) test_file_download <- readLines( "inst/app/www/testfile_template_js.js" @@ -59,11 +90,33 @@ test_that("use_external_XXX_files() function family works properly", { "myHeading.textContent = \"Hello world!\";" ) ) + # III.B corner case: file already exists + expect_false( + use_external_js_file( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_js.js", + name = "testfile_template_js.js" + ) + ) + # III.C corner case: dir already exists + expect_false( + use_external_js_file( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_js.js", + name = "testfile_template_js2.js", + dir = "inst/app/www2" + ) + ) + # III.D corner case: URL does not have extension ".js" + expect_false( + use_external_js_file( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_js", + name = "testfile_template_js3.js" + ) + ) # IV. test the external ".css" file download + # IV.A standard case use_external_css_file( - url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_css.css", - name = "testfile_template_css.css" + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_css.css" ) test_file_download <- readLines( "inst/app/www/testfile_template_css.css" @@ -86,6 +139,29 @@ test_that("use_external_XXX_files() function family works properly", { "}" ) ) + # IV.B corner case: file already exists + expect_false( + use_external_css_file( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_css.css", + name = "testfile_template_css.css" + ) + ) + # IV.C corner case: dir already exists + expect_false( + use_external_css_file( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_css.css", + name = "testfile_template_css2.css", + dir = "inst/app/www2" + ) + ) + # IV.D corner case: URL does not have extension ".css" + expect_false( + use_external_css_file( + url = "https://raw.githubusercontent.com/ilyaZar/golem/fix-1058/inst/utils/testfile_template_css", + name = "testfile_template_css3.css" + ) + ) + unlink(path_dummy_golem, recursive = TRUE) } ) @@ -118,13 +194,13 @@ test_that("use_internal_XXX_files() function family works properly", { path_dummy_golem, { # I. test the internal ".txt" file usage + # I.A standard case use_internal_file( path = file.path( path_dummy_golem, "tmp_dump_testfiles", "testfile_template_plainfile.txt" - ), - name = "testfile_template_plainfile.txt" + ) ) test_file_download <- readLines( "inst/app/www/testfile_template_plainfile.txt" @@ -133,8 +209,32 @@ test_that("use_internal_XXX_files() function family works properly", { test_file_download, "Some text." ) + # I.B corner case: file already exists + expect_false( + use_internal_file( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_plainfile.txt" + ), + name = "testfile_template_plainfile.txt" + ) + ) + # I.C corner case: dir already exists + expect_false( + use_internal_file( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_plainfile.txt" + ), + name = "testfile_template_plainfile2.txt", + dir = "inst/app/www2" + ) + ) # II. test the internal ".html" file usage + # II.A standard case use_internal_html_template( path = file.path( path_dummy_golem, @@ -161,15 +261,38 @@ test_that("use_internal_XXX_files() function family works properly", { "" ) ) + # II.B corner case: file already exists + expect_false( + use_internal_html_template( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_html.html" + ), + name = "testfile_template_html.html" + ) + ) + # II.C corner case: dir already exists + expect_false( + use_internal_html_template( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_html.html" + ), + name = "testfile_template_html2.html", + dir = "inst/app/www2" + ) + ) # III. test the internal ".js" file usage + # III.A standard case use_internal_js_file( path = file.path( path_dummy_golem, "tmp_dump_testfiles", "testfile_template_js.js" - ), - name = "testfile_template_js.js" + ) ) test_file_download <- readLines( "inst/app/www/testfile_template_js.js" @@ -181,15 +304,49 @@ test_that("use_internal_XXX_files() function family works properly", { "myHeading.textContent = \"Hello world!\";" ) ) + # III.B corner case: file already exists + expect_false( + use_internal_js_file( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_js.js" + ), + name = "testfile_template_js.js" + ) + ) + # III.C corner case: dir already exists + expect_false( + use_internal_js_file( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_js2.js" + ), + name = "testfile_template_js.js", + dir = "inst/app/www2" + ) + ) + # III.D corner case: file path does not have extension ".js" + expect_false( + use_internal_js_file( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_js" + ), + name = "testfile_template_js3.js" + ) + ) # IV. test the internal ".css" file usage + # IV.A standard case use_internal_css_file( path = file.path( path_dummy_golem, "tmp_dump_testfiles", "testfile_template_css.css" - ), - name = "testfile_template_css.css" + ) ) test_file_download <- readLines( "inst/app/www/testfile_template_css.css" @@ -212,6 +369,41 @@ test_that("use_internal_XXX_files() function family works properly", { "}" ) ) + # IV.B corner case: file already exists + expect_false( + use_internal_css_file( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_css.css" + ), + name = "testfile_template_css.css" + ) + ) + # IV.C corner case: dir already exists + expect_false( + use_internal_css_file( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_css.css" + ), + name = "testfile_template_css2.css", + dir = "inst/app/www2" + ) + ) + # IV.D corner case: file path does not have extension ".css" + expect_false( + use_internal_css_file( + path = file.path( + path_dummy_golem, + "tmp_dump_testfiles", + "testfile_template_css" + ), + name = "testfile_template_css3.css" + ) + ) + unlink(path_dummy_golem, recursive = TRUE) } )