diff --git a/tests/testthat/test-file.R b/tests/testthat/test-file.R index 4020ff36..15801822 100644 --- a/tests/testthat/test-file.R +++ b/tests/testthat/test-file.R @@ -9,32 +9,34 @@ test_that("has_file", { mockr::with_mock( is_root = function(x) x == stop_path, - expect_equal( - find_root_file("c", criterion = "b/a", path = path), - file.path(hierarchy(2L), "c") - ), - # Absolute paths are stripped - expect_equal( - find_root_file("/x", "y", criterion = "b/a", path = path), - file.path("/x", "y") - ), - expect_identical( - find_root_file("c", NA, criterion = "b/a", path = path), - NA_character_ - ), - expect_identical( - find_root_file("c", character(), criterion = "b/a", path = path), - character() - ), - expect_error( - find_root_file(letters[1:2], letters[1:3], criterion = "a", path = path) - ), - expect_error( - find_root_file(letters[1:2], character(), criterion = "a", path = path) - ), - expect_error( - find_root_file(c("b", "/x"), "c", criterion = "a", path = path), - "absolute and relative" - ) + { + expect_equal( + find_root_file("c", criterion = "b/a", path = path), + file.path(hierarchy(2L), "c") + ) + # Absolute paths are stripped + expect_equal( + find_root_file("/x", "y", criterion = "b/a", path = path), + file.path("/x", "y") + ) + expect_identical( + find_root_file("c", NA, criterion = "b/a", path = path), + NA_character_ + ) + expect_identical( + find_root_file("c", character(), criterion = "b/a", path = path), + character() + ) + expect_error( + find_root_file(letters[1:2], letters[1:3], criterion = "a", path = path) + ) + expect_error( + find_root_file(letters[1:2], character(), criterion = "a", path = path) + ) + expect_error( + find_root_file(c("b", "/x"), "c", criterion = "a", path = path), + "absolute and relative" + ) + } ) }) diff --git a/tests/testthat/test-root.R b/tests/testthat/test-root.R index 34cc65eb..11428faf 100644 --- a/tests/testthat/test-root.R +++ b/tests/testthat/test-root.R @@ -1,4 +1,3 @@ - test_that("is_root_criterion", { expect_true(is_root_criterion(has_file("DESCRIPTION"))) expect_false(is_root_criterion("DESCRIPTION")) @@ -56,32 +55,34 @@ test_that("has_file", { mockr::with_mock( is_root = function(x) x == stop_path, - expect_equal(find_root("a", path = path), hierarchy(3L)), - expect_equal(find_root("b", path = path), hierarchy(3L)), - expect_equal(find_root("b/a", path = path), hierarchy(2L)), - expect_equal(find_root("c", path = path), hierarchy(1L)), - expect_equal(find_root("d", path = path), hierarchy(4L)), - expect_equal(find_root(has_file("DESCRIPTION", "^Package: ", 1), path = path), hierarchy(1L)), - expect_equal(find_root(has_file("DESCRIPTION", "^Package: "), path = path), hierarchy(1L)), - expect_equal(find_root(has_file("DESCRIPTION", "package* does", fixed = TRUE), path = path), hierarchy(1L)), - expect_error( - find_root("test-root.R", path = path), - "No root directory found" - ), - expect_error( - find_root("rprojroot.Rproj", path = path), - "No root directory found" - ), - expect_error( - find_root(has_file("e", "f"), path = path), - "No root directory found" - ), - expect_error( - find_root(has_file("e", "f", 1), path = path), - "No root directory found" - ), - expect_error(has_file("/a"), "absolute"), - TRUE + { + expect_equal(find_root("a", path = path), hierarchy(3L)) + expect_equal(find_root("b", path = path), hierarchy(3L)) + expect_equal(find_root("b/a", path = path), hierarchy(2L)) + expect_equal(find_root("c", path = path), hierarchy(1L)) + expect_equal(find_root("d", path = path), hierarchy(4L)) + expect_equal(find_root(has_file("DESCRIPTION", "^Package: ", 1), path = path), hierarchy(1L)) + expect_equal(find_root(has_file("DESCRIPTION", "^Package: "), path = path), hierarchy(1L)) + expect_equal(find_root(has_file("DESCRIPTION", "package* does", fixed = TRUE), path = path), hierarchy(1L)) + expect_error( + find_root("test-root.R", path = path), + "No root directory found" + ) + expect_error( + find_root("rprojroot.Rproj", path = path), + "No root directory found" + ) + expect_error( + find_root(has_file("e", "f"), path = path), + "No root directory found" + ) + expect_error( + find_root(has_file("e", "f", 1), path = path), + "No root directory found" + ) + expect_error(has_file("/a"), "absolute") + TRUE + } ) }) @@ -96,38 +97,39 @@ test_that("has_file_pattern", { mockr::with_mock( is_root = function(x) x == stop_path, - expect_equal(find_root(has_file_pattern(glob2rx("a")), path = path), hierarchy(3L)), - expect_equal(find_root(has_file_pattern(glob2rx("b")), path = path), hierarchy(3L)), - expect_equal( - find_root(has_file_pattern("[ab]", "File b"), path = path), - hierarchy(3L) - ), - expect_equal( - find_root(has_file_pattern("[ab]", "File b in root"), path = path), - hierarchy(1L) - ), - expect_equal(find_root(has_file_pattern(glob2rx("c")), path = path), hierarchy(1L)), - expect_equal(find_root(has_file_pattern(glob2rx("d")), path = path), hierarchy(4L)), - expect_equal(find_root(has_file_pattern(glob2rx("DES*ION"), "^Package: ", 1), path = path), hierarchy(1L)), - expect_equal(find_root(has_file_pattern(glob2rx("DESCRI?TION"), "^Package: "), path = path), hierarchy(1L)), - expect_equal(find_root(has_file_pattern(glob2rx("D?SCRIPTI?N"), "package* does", fixed = TRUE), path = path), hierarchy(1L)), - expect_error( - find_root(has_file_pattern(glob2rx("test-root.R")), path = path), - "No root directory found" - ), - expect_error( - find_root(has_file_pattern(glob2rx("rprojroot.Rproj")), path = path), - "No root directory found" - ), - expect_error( - find_root(has_file_pattern(glob2rx("e"), "f"), path = path), - "No root directory found" - ), - expect_error( - find_root(has_file_pattern(glob2rx("e"), "f", 1), path = path), - "No root directory found" - ), - TRUE + { + expect_equal(find_root(has_file_pattern(glob2rx("a")), path = path), hierarchy(3L)) + expect_equal(find_root(has_file_pattern(glob2rx("b")), path = path), hierarchy(3L)) + expect_equal( + find_root(has_file_pattern("[ab]", "File b"), path = path), + hierarchy(3L) + ) + expect_equal( + find_root(has_file_pattern("[ab]", "File b in root"), path = path), + hierarchy(1L) + ) + expect_equal(find_root(has_file_pattern(glob2rx("c")), path = path), hierarchy(1L)) + expect_equal(find_root(has_file_pattern(glob2rx("d")), path = path), hierarchy(4L)) + expect_equal(find_root(has_file_pattern(glob2rx("DES*ION"), "^Package: ", 1), path = path), hierarchy(1L)) + expect_equal(find_root(has_file_pattern(glob2rx("DESCRI?TION"), "^Package: "), path = path), hierarchy(1L)) + expect_equal(find_root(has_file_pattern(glob2rx("D?SCRIPTI?N"), "package* does", fixed = TRUE), path = path), hierarchy(1L)) + expect_error( + find_root(has_file_pattern(glob2rx("test-root.R")), path = path), + "No root directory found" + ) + expect_error( + find_root(has_file_pattern(glob2rx("rprojroot.Rproj")), path = path), + "No root directory found" + ) + expect_error( + find_root(has_file_pattern(glob2rx("e"), "f"), path = path), + "No root directory found" + ) + expect_error( + find_root(has_file_pattern(glob2rx("e"), "f", 1), path = path), + "No root directory found" + ) + } ) }) @@ -142,19 +144,20 @@ test_that("has_dir", { mockr::with_mock( is_root = function(x) x == stop_path, - expect_equal(find_root(has_dir("a"), path = path), hierarchy(1L)), - expect_equal(find_root(has_dir("b"), path = path), hierarchy(2L)), - expect_equal(find_root(has_dir("c"), path = path), hierarchy(3L)), - expect_error( - find_root(has_dir("e"), path = path), - "No root directory found" - ), - expect_error( - find_root(has_dir("rprojroot.Rproj"), path = path), - "No root directory found" - ), - expect_error(has_dir("/a"), "absolute"), - TRUE + { + expect_equal(find_root(has_dir("a"), path = path), hierarchy(1L)) + expect_equal(find_root(has_dir("b"), path = path), hierarchy(2L)) + expect_equal(find_root(has_dir("c"), path = path), hierarchy(3L)) + expect_error( + find_root(has_dir("e"), path = path), + "No root directory found" + ) + expect_error( + find_root(has_dir("rprojroot.Rproj"), path = path), + "No root directory found" + ) + expect_error(has_dir("/a"), "absolute") + } ) }) @@ -169,18 +172,19 @@ test_that("has_basename", { mockr::with_mock( is_root = function(x) x == stop_path, - expect_equal(find_root(has_basename("a"), path = path), hierarchy(2L)), - expect_equal(find_root(has_basename("b"), path = path), hierarchy(3L)), - expect_equal(find_root(has_basename("c"), path = path), hierarchy(4L)), - expect_error( - find_root(has_basename("d"), path = path), - "No root directory found" - ), - expect_error( - find_root(has_basename("rprojroot.Rproj"), path = path), - "No root directory found" - ), - TRUE + { + expect_equal(find_root(has_basename("a"), path = path), hierarchy(2L)) + expect_equal(find_root(has_basename("b"), path = path), hierarchy(3L)) + expect_equal(find_root(has_basename("c"), path = path), hierarchy(4L)) + expect_error( + find_root(has_basename("d"), path = path), + "No root directory found" + ) + expect_error( + find_root(has_basename("rprojroot.Rproj"), path = path), + "No root directory found" + ) + } ) }) @@ -198,10 +202,11 @@ test_that("concrete criteria", { mockr::with_mock( is_root = function(x) x == stop_path, - expect_equal(find_root(is_rstudio_project, path = path), hierarchy(1L)), - expect_equal(find_root(is_remake_project, path = path), hierarchy(2L)), - expect_equal(find_root(is_projectile_project, path = path), hierarchy(3L)), - TRUE + { + expect_equal(find_root(is_rstudio_project, path = path), hierarchy(1L)) + expect_equal(find_root(is_remake_project, path = path), hierarchy(2L)) + expect_equal(find_root(is_projectile_project, path = path), hierarchy(3L)) + } ) }) @@ -219,17 +224,18 @@ test_that("is_svn_root", { mockr::with_mock( is_root = function(x) x == stop_path, - expect_equal(find_root(is_svn_root, path = path), hierarchy(1L)), - expect_equal(find_root(is_vcs_root, path = path), hierarchy(1L)), - expect_error( - find_root(is_svn_root, path = hierarchy(0L)), - "No root directory found" - ), - expect_error( - find_root(is_vcs_root, path = hierarchy(0L)), - "No root directory found" - ), - TRUE + { + expect_equal(find_root(is_svn_root, path = path), hierarchy(1L)) + expect_equal(find_root(is_vcs_root, path = path), hierarchy(1L)) + expect_error( + find_root(is_svn_root, path = hierarchy(0L)), + "No root directory found" + ) + expect_error( + find_root(is_vcs_root, path = hierarchy(0L)), + "No root directory found" + ) + } ) }) @@ -261,17 +267,18 @@ test_that("is_git_root", { mockr::with_mock( is_root = function(x) x == stop_path, - expect_equal(find_root(is_git_root, path = path), hierarchy(1L)), - expect_equal(find_root(is_vcs_root, path = path), hierarchy(1L)), - expect_error( - find_root(is_git_root, path = hierarchy(0L)), - "No root directory found" - ), - expect_error( - find_root(is_vcs_root, path = hierarchy(0L)), - "No root directory found" - ), - TRUE + { + expect_equal(find_root(is_git_root, path = path), hierarchy(1L)) + expect_equal(find_root(is_vcs_root, path = path), hierarchy(1L)) + expect_error( + find_root(is_git_root, path = hierarchy(0L)), + "No root directory found" + ) + expect_error( + find_root(is_vcs_root, path = hierarchy(0L)), + "No root directory found" + ) + } ) }) @@ -282,17 +289,18 @@ test_that("is_git_root for separated git directory", { mockr::with_mock( is_root = function(x) x == stop_path, - expect_equal(find_root(is_git_root, path = path), hierarchy(1L)), - expect_equal(find_root(is_vcs_root, path = path), hierarchy(1L)), - expect_error( - find_root(is_git_root, path = hierarchy(0L)), - "No root directory found" - ), - expect_error( - find_root(is_vcs_root, path = hierarchy(0L)), - "No root directory found" - ), - TRUE + { + expect_equal(find_root(is_git_root, path = path), hierarchy(1L)) + expect_equal(find_root(is_vcs_root, path = path), hierarchy(1L)) + expect_error( + find_root(is_git_root, path = hierarchy(0L)), + "No root directory found" + ) + expect_error( + find_root(is_vcs_root, path = hierarchy(0L)), + "No root directory found" + ) + } ) })