From bffffab7f24f4f7bec7201284995aaf6d302fa2f Mon Sep 17 00:00:00 2001 From: Ellis Hughes Date: Thu, 28 Oct 2021 10:30:10 -0700 Subject: [PATCH 1/3] Giving informative error message when root cannot be found --- R/file_and_path_utils.R | 14 +++++- tests/testthat/test-find_config.R | 76 +++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/test-find_config.R diff --git a/R/file_and_path_utils.R b/R/file_and_path_utils.R index 0b86af6..d9dd49e 100644 --- a/R/file_and_path_utils.R +++ b/R/file_and_path_utils.R @@ -58,8 +58,18 @@ vt_path <- function(...){ #' #' @export vt_find_config <- function(){ - - root <- find_root(has_file(".here") | is_rstudio_project | is_r_package | is_vcs_root) + + tryCatch({ + root <- find_root(has_file(".here") | is_rstudio_project | is_r_package | is_vcs_root) + }, error = function(e){ + abort( + paste0( + "Could not find root directory found. ", + "Is your working directory inside a package, validation packet, or project?\n" + ), + class = "vt.validation_root_missing" + ) + }) tryCatch({ diff --git a/tests/testthat/test-find_config.R b/tests/testthat/test-find_config.R new file mode 100644 index 0000000..3cade3f --- /dev/null +++ b/tests/testthat/test-find_config.R @@ -0,0 +1,76 @@ +test_that("Find config when within a package with validation", { + + withr::with_tempdir({ + quiet <- capture.output({ + vt_create_package( + "example.package", + open = FALSE) + }) + + + withr::with_dir(new = "example.package", { + expect_equal( + vt_find_config(), + file.path(getwd(), "vignettes","validation","validation.yml") + ) + }) + }) +}) + +test_that("Find config when within a package with validation when working dir is non-standard", { + + withr::with_tempdir({ + quiet <- capture.output({ + vt_create_package( + "example.package", + working_dir = "inst", + open = FALSE) + }) + + + withr::with_dir(new = "example.package", { + expect_equal( + vt_find_config(), + file.path(getwd(), "inst","validation","validation.yml") + ) + }) + }) +}) + +test_that("Find config when within a validation packet", { + + withr::with_tempdir({ + quiet <- capture.output({ + vt_create_packet("example_packet", + target = "example.package", + open = FALSE) + }) + + + withr::with_dir(new = "example_packet", { + expect_equal( + vt_find_config(), + file.path(getwd(), "validation","validation.yml") + ) + }) + + }) + +}) + + +test_that("Informative error when outside a packet or package", { + + withr::with_tempdir({ + + expect_error( + vt_find_config(), + paste0( + "Could not find root directory found. ", + "Is your working directory inside a package, validation packet, or project?\n" + ), + fixed = TRUE) + }) + +}) + From 34df0b0b5113cf68c8042ef817b70f3380037723 Mon Sep 17 00:00:00 2001 From: Ellis Hughes Date: Thu, 28 Oct 2021 13:30:02 -0700 Subject: [PATCH 2/3] Correct sentence structure --- R/file_and_path_utils.R | 2 +- tests/testthat/test-find_config.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/file_and_path_utils.R b/R/file_and_path_utils.R index d9dd49e..ccbb221 100644 --- a/R/file_and_path_utils.R +++ b/R/file_and_path_utils.R @@ -64,7 +64,7 @@ vt_find_config <- function(){ }, error = function(e){ abort( paste0( - "Could not find root directory found. ", + "Could not find root directory. ", "Is your working directory inside a package, validation packet, or project?\n" ), class = "vt.validation_root_missing" diff --git a/tests/testthat/test-find_config.R b/tests/testthat/test-find_config.R index 3cade3f..73c57d6 100644 --- a/tests/testthat/test-find_config.R +++ b/tests/testthat/test-find_config.R @@ -66,7 +66,7 @@ test_that("Informative error when outside a packet or package", { expect_error( vt_find_config(), paste0( - "Could not find root directory found. ", + "Could not find root directory. ", "Is your working directory inside a package, validation packet, or project?\n" ), fixed = TRUE) From 7a56d39fec0a435336546cc614ee66f2fbe6c197 Mon Sep 17 00:00:00 2001 From: Ellis Hughes Date: Thu, 28 Oct 2021 14:13:19 -0700 Subject: [PATCH 3/3] NOrmalize paths for error messages? --- tests/testthat/test-find_config.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-find_config.R b/tests/testthat/test-find_config.R index 73c57d6..782b1b6 100644 --- a/tests/testthat/test-find_config.R +++ b/tests/testthat/test-find_config.R @@ -11,7 +11,7 @@ test_that("Find config when within a package with validation", { withr::with_dir(new = "example.package", { expect_equal( vt_find_config(), - file.path(getwd(), "vignettes","validation","validation.yml") + normalizePath(file.path(getwd(), "vignettes","validation","validation.yml"),winslash = "/") ) }) }) @@ -31,7 +31,9 @@ test_that("Find config when within a package with validation when working dir is withr::with_dir(new = "example.package", { expect_equal( vt_find_config(), - file.path(getwd(), "inst","validation","validation.yml") + normalizePath( + file.path(getwd(), "inst","validation","validation.yml"), + winslash = "/") ) }) }) @@ -50,7 +52,9 @@ test_that("Find config when within a validation packet", { withr::with_dir(new = "example_packet", { expect_equal( vt_find_config(), - file.path(getwd(), "validation","validation.yml") + normalizePath( + file.path(getwd(), "validation","validation.yml"), + winslash = "/") ) })