| 1 |
library(testit) |
| 2 |
|
| 3 |
assert('file_ext() and sans_ext() work', { |
| 4 |
p = c('abc.doc', 'def123.tex#', 'path/to/foo.Rmd', 'backup.ppt~', 'pkg.tar.xz') |
| 5 |
(file_ext(p) %==% c('doc', 'tex#', 'Rmd', 'ppt~', 'tar.xz')) |
| 6 |
(sans_ext(p) %==% c('abc', 'def123', 'path/to/foo', 'backup', 'pkg')) |
| 7 |
(file_ext(c('foo.bar.gz', 'foo', 'file.nb.html')) %==% c('gz', '', 'nb.html')) |
| 8 |
# some special extensions |
| 9 |
p = c('abc.c++', 'def.c--', 'ghi.e##', 'jkl.FB2K-COMPONENT', 'mno.WITNESS_CAMPAIGN', 'pqr.H!') |
| 10 |
(file_ext(p, '-+!_#') %==% c('c++', 'c--', 'e##', 'FB2K-COMPONENT', 'WITNESS_CAMPAIGN', 'H!')) |
| 11 |
# by default, these extensions are not recognized |
| 12 |
(file_ext(p) %==% character(length(p))) |
| 13 |
}) |
| 14 |
|
| 15 |
assert('with_ext() works for corner cases', { |
| 16 |
(with_ext(character(), 'abc') %==% character()) |
| 17 |
(with_ext('abc', character()) %==% 'abc') |
| 18 |
(with_ext(NA_character_, 'abc') %==% NA_character_) |
| 19 |
(has_error(with_ext('abc', NA_character_))) |
| 20 |
(with_ext('abc', c('d', 'e')) %==% c('abc.d', 'abc.e')) |
| 21 |
(has_error(with_ext(c('a', 'b'), c('d', 'e', 'f')))) |
| 22 |
(with_ext(c('a', 'b'), c('d', 'e')) %==% c('a.d', 'b.e')) |
| 23 |
(with_ext(c('a', 'b'), c('d')) %==% c('a.d', 'b.d')) |
| 24 |
(with_ext(c('a', 'b', 'c'), c('', '.d', 'e.e')) %==% c('a', 'b.d', 'c.e.e')) |
| 25 |
}) |
| 26 |
|
| 27 |
assert('same_path() works', { |
| 28 |
(is.na(same_path('~/foo', NA_character_))) |
| 29 |
(is.na(same_path(NA_character_, '~/foo'))) |
| 30 |
(same_path('~/foo', file.path(Sys.getenv('HOME'), 'foo'))) |
| 31 |
(!same_path(tempdir(), 'foo')) |
| 32 |
}) |
| 33 |
|
| 34 |
assert('normalize_path() works', { |
| 35 |
f1 = tempfile() |
| 36 |
writeLines('test symlink', f1) |
| 37 |
f2 = paste0(f1, '~') |
| 38 |
res = file.symlink(f1, f2) # this may fail (on Windows), i.e., res = FALSE |
| 39 |
# resolve symlink by default |
| 40 |
(!res || basename(normalize_path(f2)) %==% basename(f1)) |
| 41 |
# do not resolve symlink |
| 42 |
(!res || basename(normalize_path(f2, resolve_symlink = FALSE)) %==% basename(f2)) |
| 43 |
# resolve_symlink = FALSE should work with inputs like . and .. |
| 44 |
(normalize_path(c('.', '..'), resolve_symlink = FALSE) %==% normalize_path(c('.', '..'))) |
| 45 |
}) |
| 46 |
|
| 47 |
assert('url_filename() returns the file names in URLs', { |
| 48 |
(url_filename('https://yihui.org/images/logo.png') %==% 'logo.png') |
| 49 |
(url_filename(c( |
| 50 |
'https://yihui.org/index.html', |
| 51 |
'https://yihui.org/index.html?foo=bar', |
| 52 |
'https://yihui.org/index.html#about' |
| 53 |
)) %==% rep('index.html', 3)) |
| 54 |
}) |
| 55 |
|
| 56 |
assert('is_abs_path() recognizes absolute paths on Windows and *nix', { |
| 57 |
(!is_abs_path('abc/def')) |
| 58 |
(is_abs_path(if (.Platform$OS.type == 'windows') { |
| 59 |
c('D:\\abc', '\\\\netdrive\\somewhere') |
| 60 |
} else '/abc/def')) |
| 61 |
}) |
| 62 |
|
| 63 |
assert('del_empty_dir() correctly deletes empty dirs', { |
| 64 |
# do nothing is NULL |
| 65 |
(del_empty_dir(NULL) %==% NULL) |
| 66 |
# remove if empty |
| 67 |
dir.create(temp_dir <- tempfile()) |
| 68 |
del_empty_dir(temp_dir) |
| 69 |
(!dir_exists(temp_dir)) |
| 70 |
# do not remove if not empty |
| 71 |
dir.create(temp_dir <- tempfile()) |
| 72 |
writeLines('test', tempfile(tmpdir = temp_dir)) |
| 73 |
(del_empty_dir(temp_dir) %==% NULL) |
| 74 |
(dir_exists(temp_dir)) |
| 75 |
unlink(temp_dir, recursive = TRUE) |
| 76 |
}) |
| 77 |
|
| 78 |
assert('mark_dirs add trailing / when necessary', { |
| 79 |
local({ |
| 80 |
dir.create(tmp_dir <- tempfile()) |
| 81 |
tmp_dir_slash = paste0(tmp_dir, '/') |
| 82 |
file.create(tmp_file <- tempfile(tmpdir = tmp_dir)) |
| 83 |
(mark_dirs(c(tmp_dir, tmp_file)) %==% c(tmp_dir_slash, tmp_file)) |
| 84 |
(mark_dirs(c(tmp_dir_slash, tmp_file)) %==% c(tmp_dir_slash, tmp_file)) |
| 85 |
unlink(tmp_dir, recursive = TRUE) |
| 86 |
}) |
| 87 |
}) |
| 88 |
|
| 89 |
assert('relative_path() works', { |
| 90 |
(relative_path(c('foo/bar.txt', 'foo/baz.txt'), 'foo/') %==% c('bar.txt', 'baz.txt')) |
| 91 |
(relative_path('foo/bar.txt', 'foo') %==% 'bar.txt') |
| 92 |
}) |
| 93 |
|
| 94 |
assert('proj_root() works', { |
| 95 |
# detect .Rproj root |
| 96 |
dir.create(tmp_dir <- tempfile()) |
| 97 |
tmp_dir_slash <- paste0(tmp_dir, '/') |
| 98 |
file.create(f1 <- file.path(tmp_dir, 'test.Rproj')) |
| 99 |
writeLines(c('Version: 1.2.3', 'test: 321'), f1) |
| 100 |
|
| 101 |
(same_path(proj_root(tmp_dir), tmp_dir) %==% TRUE) |
| 102 |
unlink(f1) |
| 103 |
|
| 104 |
# detect package root |
| 105 |
file.create(f2 <- file.path(tmp_dir, 'DESCRIPTION')) |
| 106 |
writeLines(c('Package: abc', 'test: 321'), f2) |
| 107 |
dir.create(tmp_dir_child <- tempfile(tmpdir = tmp_dir)) |
| 108 |
|
| 109 |
(same_path(proj_root(tmp_dir_child), tmp_dir) %==% TRUE) |
| 110 |
unlink(tmp_dir, recursive = TRUE) |
| 111 |
}) |
| 112 |
|
| 113 |
assert('file_rename() works', { |
| 114 |
# work in temp dir |
| 115 |
dir.create(tmp_dir <- tempfile()) |
| 116 |
owd = setwd(tmp_dir) |
| 117 |
|
| 118 |
# empty dir is not moved but deleted |
| 119 |
dir.create('dest') |
| 120 |
dir.create('empty') |
| 121 |
file_rename('empty', 'dest') |
| 122 |
(dir.exists(c('empty', 'dest')) %==% c(FALSE, TRUE)) |
| 123 |
|
| 124 |
# files are moved correctly |
| 125 |
dir.create('filled') |
| 126 |
dummy_files = c('dummy1', 'dummy2') |
| 127 |
file.create(file.path('filled', dummy_files)) |
| 128 |
file_rename('filled', 'dest1') |
| 129 |
(dir.exists('filled') %==% FALSE) |
| 130 |
(list.files('dest1') %==% dummy_files) |
| 131 |
|
| 132 |
# rename multiple dirs |
| 133 |
dir.create('dest2') |
| 134 |
file_rename(c('dest1', 'dest2'), c('dest3', 'dest4')) |
| 135 |
(dir.exists(sprintf('dest%d', 1:4)) %==% c(FALSE, FALSE, TRUE, TRUE)) |
| 136 |
|
| 137 |
# rename files |
| 138 |
file_rename(file.path('dest3', dummy_files), dummy_files) |
| 139 |
(length(list.files('dest3')) %==% 0L) |
| 140 |
(file.exists(dummy_files) %==% c(TRUE, TRUE)) |
| 141 |
|
| 142 |
# remove temp dir |
| 143 |
setwd(owd) |
| 144 |
unlink(tmp_dir, recursive = TRUE) |
| 145 |
}) |
| 146 |
|
| 147 |
assert('rename_seq() returns renamed file patterns', { |
| 148 |
dir.create(tmp_dir <- tempfile()) |
| 149 |
owd = setwd(tmp_dir) |
| 150 |
# no matching files -> returns character(0) |
| 151 |
(rename_seq() %==% character(0)) |
| 152 |
# create some files matching the pattern |
| 153 |
file.create(c('01-foo.Rmd', '02-bar.Rmd', '03-baz.Rmd')) |
| 154 |
res = rename_seq() |
| 155 |
(length(res) %==% 3L) |
| 156 |
(inherits(res, 'xfun_rename_seq')) |
| 157 |
# print method should work without error |
| 158 |
capture.output(print(res)) |
| 159 |
# dry_run = FALSE actually renames |
| 160 |
file.create(c('01-x.Rmd', '02-y.Rmd')) |
| 161 |
rename_seq(pattern = '^[0-9]+-[xy][.]Rmd$', dry_run = FALSE) |
| 162 |
(all(file.exists(c('1-x.Rmd', '2-y.Rmd')))) |
| 163 |
setwd(owd) |
| 164 |
unlink(tmp_dir, recursive = TRUE) |
| 165 |
}) |
| 166 |
|
| 167 |
assert('existing_files() with first = TRUE works', { |
| 168 |
f = tempfile(); writeLines('test', f) |
| 169 |
(existing_files(f, first = TRUE) %==% f) |
| 170 |
unlink(f) |
| 171 |
(has_error(existing_files(c('nonexistent1', 'nonexistent2'), first = TRUE))) |
| 172 |
(existing_files(c('nonexistent1', 'nonexistent2'), first = TRUE, error = FALSE) %==% character(0)) |
| 173 |
(has_error(existing_files('nonexistent', first = TRUE, error = 'Custom error msg'))) |
| 174 |
}) |
| 175 |
|
| 176 |
assert('is_web_path() detects web paths', { |
| 177 |
(is_web_path(c('https://www.r-project.org', 'http://example.com', |
| 178 |
'ftp://files.example.com', 'ftps://secure.example.com'))) |
| 179 |
(!is_web_path(c('www.r-project.org', '/local/path', 'relative/path'))) |
| 180 |
}) |
| 181 |
|
| 182 |
assert('is_rel_path() is the negation of is_abs_path()', { |
| 183 |
(is_rel_path('foo.txt')) |
| 184 |
(!is_rel_path(tempdir())) |
| 185 |
}) |
| 186 |
|
| 187 |
assert('with_ext() removes extensions when ext is empty string', { |
| 188 |
(with_ext(c('a.txt', 'b.R'), '') %==% c('a', 'b')) |
| 189 |
}) |
| 190 |
|
| 191 |
assert('relative_path() edge cases', { |
| 192 |
(has_error(relative_path('/tmp/some/file.txt', '/different/dir', use.. = FALSE))) |
| 193 |
d = normalize_path(tempdir()) |
| 194 |
(relative_path(d, d) %==% '.') |
| 195 |
}) |
| 196 |
|
| 197 |
assert('relative_path() with .. works across sibling directories', { |
| 198 |
d = tempfile(); dir.create(d) |
| 199 |
a = file.path(d, 'a'); dir.create(a) |
| 200 |
b = file.path(d, 'b'); dir.create(b) |
| 201 |
f = file.path(a, 'file.txt'); file.create(f) |
| 202 |
res = relative_path(f, b) |
| 203 |
(grepl('\\.\\.', res)) |
| 204 |
unlink(d, recursive = TRUE) |
| 205 |
}) |
| 206 |
|
| 207 |
assert('R_logo() finds an existing logo', { |
| 208 |
logo = R_logo() |
| 209 |
(length(logo) %==% 1L) |
| 210 |
(file.exists(logo)) |
| 211 |
}) |
| 212 |
|
| 213 |
assert('all_files() lists files recursively', { |
| 214 |
d = tempfile(); dir.create(d) |
| 215 |
sub = file.path(d, 'sub'); dir.create(sub) |
| 216 |
file.create(file.path(d, 'a.txt')) |
| 217 |
file.create(file.path(sub, 'b.R')) |
| 218 |
res = all_files('[.]R$', dir = d) |
| 219 |
(length(res) %==% 1L) |
| 220 |
(grepl('b.R$', res)) |
| 221 |
unlink(d, recursive = TRUE) |
| 222 |
}) |
| 223 |
|
| 224 |
assert('magic_path() finds a file recursively in subdirs', { |
| 225 |
d = tempfile(); dir.create(d) |
| 226 |
sub = file.path(d, 'a', 'b'); dir.create(sub, recursive = TRUE) |
| 227 |
f = file.path(sub, 'target.txt'); writeLines('found', f) |
| 228 |
res = magic_path('target.txt', root = d, relative = FALSE) |
| 229 |
(same_path(res, f)) |
| 230 |
unlink(d, recursive = TRUE) |
| 231 |
}) |
| 232 |
|
| 233 |
assert('magic_path() errors when file not found and error = TRUE', { |
| 234 |
d = tempfile(); dir.create(d) |
| 235 |
(has_error(magic_path('nonexistent.txt', root = d, error = TRUE))) |
| 236 |
unlink(d, recursive = TRUE) |
| 237 |
}) |
| 238 |
|
| 239 |
assert('magic_path() returns input path when not found and error = FALSE', { |
| 240 |
d = tempfile(); dir.create(d) |
| 241 |
(magic_path('nonexistent.txt', root = d, error = FALSE) %==% 'nonexistent.txt') |
| 242 |
unlink(d, recursive = TRUE) |
| 243 |
}) |
| 244 |
|
| 245 |
assert('magic_path() errors or warns on multiple matches', { |
| 246 |
d = tempfile(); dir.create(d) |
| 247 |
sub1 = file.path(d, 'a'); dir.create(sub1) |
| 248 |
sub2 = file.path(d, 'b'); dir.create(sub2) |
| 249 |
file.create(file.path(sub1, 'dup.txt')) |
| 250 |
file.create(file.path(sub2, 'dup.txt')) |
| 251 |
(has_error(magic_path('dup.txt', root = d, error = TRUE))) |
| 252 |
res = suppressMessages(magic_path('dup.txt', root = d, error = FALSE)) |
| 253 |
(file.exists(res)) |
| 254 |
unlink(d, recursive = TRUE) |
| 255 |
}) |
| 256 |
|
| 257 |
assert('from_root() errors when root is NULL', { |
| 258 |
(has_error(from_root('foo.R', root = NULL))) |
| 259 |
}) |
| 260 |
|
| 261 |
assert('from_root() returns relative path from project root', { |
| 262 |
d = tempfile(); dir.create(d) |
| 263 |
writeLines('Package: testpkg', file.path(d, 'DESCRIPTION')) |
| 264 |
sub = file.path(d, 'R'); dir.create(sub) |
| 265 |
file.create(file.path(sub, 'code.R')) |
| 266 |
owd = setwd(sub) |
| 267 |
res = from_root('R', 'code.R', root = d) |
| 268 |
(res %==% 'code.R') |
| 269 |
setwd(owd) |
| 270 |
unlink(d, recursive = TRUE) |
| 271 |
}) |