Debian Codegraph @

tests/testthat/test-fs_path.R

raw
1 describe("as_fs_path", {
2 it("returns a new fs_path object from character inputs", {
3 x <- as_fs_path("foo/bar")
4 expect_s3_class(x, "fs_path")
5 expect_s3_class(x, "character")
6 expect_equal(length(x), 1)
7
8 x <- as_fs_path(c("foo/bar", "foo", NA))
9 expect_s3_class(x, "fs_path")
10 expect_s3_class(x, "character")
11 expect_equal(length(x), 3)
12 expect_equal(as.character(x), c("foo/bar", "foo", NA))
13 })
14
15 it("fails with non-character inputs", {
16 expect_snapshot(error = TRUE, {
17 as_fs_path(1)
18 as_fs_path(TRUE)
19 })
20 })
21
22 it("preserves the class with both subset and subset2", {
23 expect_s3_class(as_fs_path("foo")[1], "fs_path")
24 expect_s3_class(as_fs_path("foo")[[1]], "fs_path")
25 })
26 })
27
28 describe("colourise_fs_path", {
29 it("returns an appropriately colored fs_path object", {
30 withr::local_envvar(c("LS_COLORS" = gnu_ls_defaults))
31 local_reproducible_output(crayon = TRUE)
32
33 with_dir_tree(
34 list(
35 "foo/bar" = "test",
36 "file.zip" = "test",
37 "file.R" = "test",
38 "file.Rmd" = "test"
39 ),
40 {
41 link_create(path_abs("foo/bar"), "baz")
42
43 # folder
44 expect_equal(colourise_fs_path("foo"), "\033[01;34mfoo\033[0m")
45
46 # file
47 expect_equal(colourise_fs_path("foo/bar"), "foo/bar")
48
49 # symlink
50 expect_equal(colourise_fs_path("baz"), "\033[01;36mbaz\033[0m")
51
52 # extension
53 expect_equal(
54 colourise_fs_path("file.zip"),
55 "\033[01;31mfile.zip\033[0m"
56 )
57
58 # R
59 expect_equal(colourise_fs_path("file.R"), "\033[32mfile.R\033[0m")
60
61 # Rmd
62 expect_equal(colourise_fs_path("file.Rmd"), "\033[32mfile.Rmd\033[0m")
63 }
64 )
65 })
66
67 it("returns uncolored result if LS_COLORS is malformed", {
68 # This has an empty leading :
69 withr::local_envvar(c(
70 "LS_COLORS" = ":di=1;38;5;39:ex=1;38;5;34:ln=1;38;5;45:*.py=1;38;5;220:*.pdf=1;38;5;202:*.tex=1;38;5;196"
71 ))
72
73 expect_equal(colourise_fs_path("foo"), "foo")
74 })
75 })
76
77 describe("multicol", {
78 files <- c(
79 "DESCRIPTION",
80 "LICENSE.md",
81 "NAMESPACE",
82 "R",
83 "README.Rmd",
84 "README.md",
85 "_pkgdown.yml",
86 "appveyor.yml",
87 "codecov.yml",
88 "doc",
89 "docs",
90 "fs.Rcheck",
91 "fs.Rproj",
92 "fs_0.0.0.9000.tar.gz",
93 "man",
94 "man-roxygen",
95 "script.R",
96 "src",
97 "tests",
98 "tools",
99 "foo",
100 "foo/bar",
101 "foo/bar/baz",
102 "foo/bar/baz/bar/baz"
103 )
104
105 it("Uses 1 column when width is less than the max size", {
106 withr::with_options(c(width = 10), {
107 expect_equal(
108 max(nchar(multicol(files), keepNA = FALSE)),
109 max(nchar(files, keepNA = FALSE)) + 1
110 )
111 })
112 })
113
114 it("Uses 2 column when width is less than the max size * 2", {
115 withr::with_options(c(width = 50), {
116 expect_equal(max(nchar(multicol(files), keepNA = FALSE)), 43)
117 })
118 })
119
120 it("Uses 3 column when width is less than the max size * 3", {
121 withr::with_options(c(width = 70), {
122 expect_equal(max(nchar(multicol(files), keepNA = FALSE)), 64)
123 })
124 })
125
126 it("Uses 4 column when width is less than the max size * 4", {
127 withr::with_options(c(width = 90), {
128 expect_equal(max(nchar(multicol(files), keepNA = FALSE)), 85)
129 })
130 })
131
132 it("works with NA values", {
133 withr::with_options(c(width = 10), {
134 expect_equal(multicol(NA_character_), "NA\n")
135 expect_equal(multicol(c("foo", NA_character_)), "foo NA \n")
136 })
137 })
138
139 it("Ignores colors when calculating width", {
140 withr::with_options(c(crayon.enabled = TRUE, width = 90), {
141 expect_equal(max(nchar(multicol(files), keepNA = FALSE)), 85)
142 })
143 })
144 })
145
146 test_that("common type of character and fs_path is fs_path", {
147 expect_identical(
148 vctrs::vec_ptype2(character(), fs_path()),
149 fs_path()[0]
150 )
151 expect_identical(
152 vctrs::vec_ptype2(fs_path(), character()),
153 fs_path()[0]
154 )
155 })
156
157 test_that("fs_path and character are coercible", {
158 expect_identical(
159 vctrs::vec_cast("foo", fs_path()),
160 fs_path("foo")
161 )
162 expect_identical(
163 vctrs::vec_cast(fs_path("foo"), character()),
164 "foo"
165 )
166 expect_identical(
167 vctrs::vec_cast(fs_path("foo"), fs_path()),
168 fs_path("foo")
169 )
170 })
171
172 test_that("can concatenate fs_path", {
173 expect_identical(
174 vctrs::vec_c(fs_path("foo"), fs_path("bar")),
175 as_fs_path(c("foo", "bar"))
176 )
177 })