Debian Codegraph @

tests/testthat/test-access.R

raw
1 describe("file_access", {
2 with_dir_tree(list("foo/bar" = "test"), {
3 it("errors on missing input", {
4 expect_error(file_access(NA), class = "invalid_argument")
5 })
6 it("checks for file existence", {
7 expect_equal(file_access("foo", "exists"), c(foo = TRUE))
8 })
9 it("checks for file read and write ability", {
10 skip_on_os(c("windows", "solaris"))
11 if (Sys.info()[["effective_user"]] == "root") skip("root user")
12 file_chmod("foo/bar", "-rw")
13 expect_equal(file_access("foo/bar", "read"), c("foo/bar" = FALSE))
14 expect_equal(file_access("foo/bar", "write"), c("foo/bar" = FALSE))
15
16 file_chmod("foo/bar", "+rw")
17 expect_equal(file_access("foo/bar", "read"), c("foo/bar" = TRUE))
18 expect_equal(file_access("foo/bar", "write"), c("foo/bar" = TRUE))
19 })
20 it("checks for file execute ability", {
21 skip_on_os(c("windows", "solaris"))
22 if (
23 Sys.info()[["sysname"]] == "DragonFly" &&
24 Sys.info()[["effective_user"]] == "root"
25 ) {
26 skip("file_access() is always executable for root on DragonFlyBSD")
27 }
28 file_chmod("foo/bar", "-x")
29 expect_equal(file_access("foo/bar", "execute"), c("foo/bar" = FALSE))
30 file_chmod("foo/bar", "+x")
31 expect_equal(file_access("foo/bar", "execute"), c("foo/bar" = TRUE))
32 })
33 })
34 })
35
36 with_dir_tree(list("foo/bar" = "test"), {
37 link_create(path_abs("foo"), "loo")
38 if (!is_windows()) {
39 link_create("foo", "relloo")
40 }
41
42 describe("file_exists", {
43 it("returns true for files that exist, false for those that do not", {
44 expect_equal(file_exists("foo"), c(foo = TRUE))
45 expect_equal(file_exists("missing"), c(missing = FALSE))
46 expect_equal(
47 file_exists(c("foo", "missing", "foo/bar", "loo")),
48 c(foo = TRUE, missing = FALSE, "foo/bar" = TRUE, "loo" = TRUE)
49 )
50 })
51 it("returns FALSE on missing input", {
52 expect_identical(file_exists(NA_character_), structure(names = NA, FALSE))
53 })
54 it("expands ~", {
55 withr::with_envvar(c("R_FS_HOME" = getwd()), {
56 expect_identical(file_exists("~/foo"), c("~/foo" = TRUE))
57 expect_identical(file_exists("~/missing"), c("~/missing" = FALSE))
58 })
59 })
60 })
61
62 describe("dir_exists", {
63 it("returns true for dir that exist, false for those that do not or are not directories", {
64 expect_equal(dir_exists("foo"), c(foo = TRUE))
65 expect_equal(dir_exists("missing"), c(missing = FALSE))
66 expect_equal(
67 dir_exists(c("foo", "missing", "foo/bar")),
68 c(foo = TRUE, missing = FALSE, "foo/bar" = FALSE)
69 )
70 })
71 it("returns true for links to directories, like -d in bash", {
72 expect_equal(dir_exists("loo"), c(loo = TRUE))
73 if (!is_windows()) {
74 expect_equal(dir_exists("relloo"), c(relloo = TRUE))
75 }
76 .old_wd <- setwd("foo")
77 expect_equal(dir_exists("../loo"), c("../loo" = TRUE))
78 if (!is_windows()) {
79 expect_equal(dir_exists("../relloo"), c("../relloo" = TRUE))
80 }
81 setwd(.old_wd)
82 })
83 it("returns FALSE on missing input", {
84 expect_identical(dir_exists(NA_character_), structure(names = NA, FALSE))
85 })
86 })
87
88 describe("link_exists", {
89 it("returns true for link that exist, false for those that do not or are not symlinks", {
90 expect_equal(link_exists("loo"), c(loo = TRUE))
91 expect_equal(link_exists("missing"), c(missing = FALSE))
92 expect_equal(
93 link_exists(c("foo", "missing", "foo/bar", "loo")),
94 c(foo = FALSE, missing = FALSE, "foo/bar" = FALSE, "loo" = TRUE)
95 )
96 })
97 it("returns FALSE on missing input", {
98 expect_identical(link_exists(NA_character_), structure(names = NA, FALSE))
99 })
100 })
101 })