Debian Codegraph @

tests/testthat/test-paths.R

raw
1
2 test_that("all renv paths live within tempdir() during tests", {
3 renv_tests_scope()
4 info <- c(root = renv_paths_root(), tempdir = tempdir())
5 expect_true(renv_path_within(renv_paths_root(), tempdir()), info = info)
6 })
7
8 test_that("the cache path can be set through an environment variable", {
9 cachepath <- renv_scope_tempfile("renv-cache-")
10 renv_scope_envvars(RENV_PATHS_CACHE = cachepath)
11 expect_true(startsWith(renv_paths_cache(), cachepath))
12 })
13
14 test_that("we can construct paths to multiple files with path APIs", {
15 root <- renv_paths_root()
16 files <- renv_paths_root(c("A", "B", "C"), c("a", "b", "c"))
17 expected <- file.path(root, c("A/a", "B/b", "C/c"))
18 expect_equal(files, expected)
19 })
20
21 test_that("RENV_PATHS_PREFIX is respected", {
22
23 renv_scope_binding(the, "prefix", NULL)
24
25 os <- "ubuntu-bionic"
26 renv_scope_envvars(RENV_PATHS_PREFIX = os)
27
28 cachepath <- renv_paths_cache()
29 expect_true(grepl(os, cachepath, fixed = TRUE))
30
31 libpath <- renv_paths_library()
32 expect_true(grepl(os, libpath, fixed = TRUE))
33
34 prefix <- renv_platform_prefix()
35 expect_true(startsWith(prefix, os))
36
37 })
38
39 test_that("renv_path_normalize is correctly initialized", {
40
41 expect_identical(
42 renv_path_normalize,
43 if (renv_platform_windows())
44 renv_path_normalize_win32
45 else
46 renv_path_normalize_unix
47 )
48
49 })
50
51 test_that("UTF-8 paths can be normalized", {
52
53 skip_if(getRversion() < "4.0.0")
54
55 info <- l10n_info()
56 if (!identical(info$`UTF-8`, TRUE))
57 skip("locale is not UTF-8")
58
59 name <- enc2utf8("\u4f60\u597d") # nihao
60 root <- renv_path_normalize(tempdir())
61 path <- paste(root, name, sep = "/")
62 defer(unlink(path))
63 expect_true(file.create(path))
64
65 expected <- path
66 actual <- renv_path_normalize(path)
67 expect_equal(expected, actual)
68
69 })
70
71 test_that("our fallback R_user_dir() implementation is compatible", {
72 skip_if(getRversion() < "4.0.0")
73 actual <- renv_paths_root_default_impl_v2_fallback()
74 expected <- tools::R_user_dir("renv", which = "cache")
75 expect_true(renv_path_same(actual, expected))
76 })
77
78 test_that("path environment variables can use ';' as separator", {
79 renv_scope_envvars(RENV_PATHS_LOCAL = "a;b;c")
80 paths <- renv_paths_local()
81 expect_equal(paths, c("a", "b", "c"))
82 })
83
84 test_that("RENV_PATHS_LOCKFILE resolves relative paths against project", {
85
86 renv_tests_scope()
87 project <- getwd()
88
89 # relative path should be resolved against the project directory
90 renv_scope_envvars(RENV_PATHS_LOCKFILE = "custom/renv.lock")
91 result <- renv_paths_lockfile(project = project)
92 expect_equal(result, file.path(project, "custom/renv.lock"))
93
94 })
95
96 test_that("RENV_PATHS_LOCKFILE preserves absolute paths", {
97
98 renv_tests_scope()
99
100 abspath <- file.path(tempdir(), "my-renv.lock")
101 renv_scope_envvars(RENV_PATHS_LOCKFILE = abspath)
102 result <- renv_paths_lockfile(project = getwd())
103 expect_equal(result, abspath)
104
105 })
106
107 test_that("RENV_PATHS_LOCKFILE appends renv.lock for trailing slash", {
108
109 renv_tests_scope()
110 project <- getwd()
111
112 renv_scope_envvars(RENV_PATHS_LOCKFILE = "custom/")
113 result <- renv_paths_lockfile(project = project)
114 expect_equal(result, file.path(project, "custom/renv.lock"))
115
116 })