Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions tests/testthat/test-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}
)
})
250 changes: 129 additions & 121 deletions tests/testthat/test-root.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

test_that("is_root_criterion", {
expect_true(is_root_criterion(has_file("DESCRIPTION")))
expect_false(is_root_criterion("DESCRIPTION"))
Expand Down Expand Up @@ -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
}
)
})

Expand All @@ -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"
)
}
)
})

Expand All @@ -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")
}
)
})

Expand All @@ -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"
)
}
)
})

Expand All @@ -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))
}
)
})

Expand All @@ -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"
)
}
)
})

Expand Down Expand Up @@ -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"
)
}
)
})

Expand All @@ -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"
)
}
)
})

Expand Down