Debian Codegraph @

tests/testthat/test-create.R

raw
1 test_that("file_create works with new and existing files", {
2 x1 <- file_create(tempfile())
3
4 expect_true(file_exists(x1))
5 expect_error(file_create(x1), NA)
6 expect_error(file_create(NA), class = "invalid_argument")
7
8 unlink(x1)
9 })
10
11 test_that("dir_create works with new and existing files", {
12 x1 <- dir_create(tempfile())
13
14 expect_true(file_exists(x1))
15 expect_error(dir_create(x1), NA)
16 expect_error(dir_create(NA), class = "invalid_argument")
17
18 unlink(x1, recursive = TRUE)
19 })
20
21 test_that("file_create works with multiple path arguments", {
22 mkdirp(tmp <- tempfile())
23 on.exit(unlink(tmp, recursive = TRUE), add = TRUE)
24 x1 <- file_create(tmp, "foo")
25
26 expect_equal(x1, path(tmp, "foo"))
27 expect_true(file_exists(x1))
28 })
29
30 test_that("dir_create works with multiple path arguments", {
31 x1 <- dir_create(tempdir(), "foo", "bar")
32 x2 <- dir_create(tempdir(), "foo", "baz", recurse = FALSE)
33
34 expect_equal(x1, path(tempdir(), "foo", "bar"))
35 expect_equal(x2, path(tempdir(), "foo", "baz"))
36 expect_true(dir_exists(x1))
37 expect_true(dir_exists(x2))
38
39 unlink(x1)
40 unlink(x2)
41 })
42
43 test_that("dir_create sets the mode properly", {
44 skip_on_cran()
45 skip_on_os("windows")
46 x1 <- dir_create(tempfile(), mode = "775")
47
48 expect_true(file_exists(x1))
49 expect_equal(as.character(file_info(x1)$permissions), "rwxrwxr-x")
50
51 unlink(x1, recursive = TRUE)
52 })
53
54 test_that("dir_create works with absolute paths and recurse = FALSE (#204)", {
55 x1 <- dir_create(path_abs(tempfile()), recurse = FALSE)
56
57 expect_true(file_exists(x1))
58
59 unlink(x1, recursive = TRUE)
60 })
61
62 test_that("dir_create fails silently if the directory or link exists and fails if a file exists", {
63 with_dir_tree(c("foo/bar" = ""), {
64 expect_error_free(dir_create("foo"))
65 expect_error(dir_create("foo/bar"), class = "EEXIST")
66 })
67 })
68
69 test_that("dir_create fails with EACCES if it cannot create the directory", {
70 skip_on_os("windows")
71 skip_on_cran()
72 if (Sys.info()[["effective_user"]] == "root") skip("root user")
73
74 with_dir_tree("foo", {
75 # Set current directly as read-only
76 file_chmod(".", "u-w")
77 expect_error(dir_create("bar"), class = "EACCES")
78 })
79 })
80
81 test_that("link_create does not modify existing links", {
82 # Windows does not currently support file based symlinks
83 skip_on_os("windows")
84
85 x <- dir_create(tempfile())
86
87 dir_create(path(x, "dir1"))
88 dir_create(path(x, "dir2"))
89
90 link_create(path(x, "dir1"), path(x, "link"))
91 expect_error(
92 link_create(path(x, "dir2"), path(x, "link")),
93 "file already exists",
94 class = "EEXIST"
95 )
96 expect_equal(link_path(path(x, "link")), path(x, "dir1"))
97
98 expect_error(link_create(NA), class = "invalid_argument")
99 expect_error(link_create(path(x, "dir2"), NA), class = "invalid_argument")
100 })