Debian Codegraph @

tests/testthat/test-profile.R

raw
1
2 test_that("renv/profile is read and used to select a profile", {
3 skip_on_cran()
4
5 project <- renv_tests_scope()
6
7 renv_scope_envvars(RENV_PROFILE = NULL)
8 init(profile = "testing")
9
10 # make sure we have renv installed for this test
11 libpaths <- the$default_libpaths[[".libPaths()"]]
12 source <- find.package("renv", lib.loc = libpaths)
13 renv_imbue_self(project)
14
15 # check that profile was written
16 expect_true(file.exists("renv/profile"))
17
18 # check that its contents equal 'testing'
19 contents <- readLines("renv/profile")
20 expect_equal(contents, "testing")
21
22 # check that an R session launched here gets that profile
23 renv_scope_envvars(R_PROFILE_USER = NULL)
24 script <- renv_test_code({
25 writeLines(Sys.getenv("RENV_PROFILE"))
26 })
27
28 args <- c("-s", "-f", shQuote(script))
29 output <- renv_system_exec(R(), args, action = "reading profile")
30
31 })
32
33 test_that("a profile changes the default library / lockfile path", {
34 skip_on_cran()
35
36 renv_tests_scope()
37 renv_scope_envvars(RENV_PROFILE = "testing")
38
39 project <- getwd()
40 init()
41
42 # NOTE: renv/profile should not be written here as we've only forced
43 # activation via an environment variable and not explicitly via API
44 profile <- file.path(project, "renv/profile")
45 expect_false(file.exists(profile))
46
47 # however, other paths should resolve relative to the active profile
48 prefix <- "renv/profiles/testing"
49
50 expect_equal(
51 renv_path_normalize(paths$lockfile(project = project)),
52 renv_path_normalize(file.path(project, prefix, "renv.lock"))
53 )
54
55 expect_equal(
56 renv_path_normalize(paths$library(project = project)),
57 renv_path_normalize(file.path(project, prefix, "renv/library", renv_platform_prefix()))
58 )
59
60 expect_equal(
61 renv_path_normalize(paths$settings(project = project)),
62 renv_path_normalize(file.path(project, prefix, "renv/settings.json"))
63 )
64
65 })
66
67 test_that("profile-specific dependencies can be written", {
68
69 renv_tests_scope()
70
71 # initialize project with 'testing' profile
72 renv_scope_envvars(RENV_PROFILE = "testing")
73 init()
74
75 # have this profile depend on 'toast'
76 path <- renv_paths_renv("_dependencies.R")
77 ensure_parent_directory(path)
78 writeLines("library(toast)", con = path)
79
80 # validate the dependency is included
81 deps <- dependencies()
82 expect_true("toast" %in% deps$Package)
83
84 # switch to other profile
85 renv_scope_envvars(RENV_PROFILE = "other")
86
87 # 'toast' is no longer required
88 deps <- dependencies()
89 expect_false("toast" %in% deps$Package)
90
91 })
92
93 test_that("profile-specific dependencies can be declared in DESCRIPTION", {
94 renv_tests_scope()
95
96 renv_scope_envvars(RENV_PROFILE = "testing")
97 init()
98
99 writeLines(
100 "Config/renv/profiles/testing/dependencies: toast",
101 con = "DESCRIPTION"
102 )
103
104 deps <- dependencies()
105 expect_true("toast" %in% deps$Package)
106
107 })
108
109 test_that("profile-specific remotes are parsed", {
110 project <- renv_tests_scope()
111
112 renv_scope_envvars(RENV_PROFILE = "testing")
113 init()
114
115 desc <- heredoc('
116 Type: Project
117 Config/renv/profiles/testing/dependencies: bread
118 Config/renv/profiles/testing/remotes: [email protected]
119 ')
120
121 writeLines(desc, con = "DESCRIPTION")
122 remotes <- renv_project_remotes(project)
123
124 actual <- resolve(remotes$bread)
125 expected <- list(Package = "bread", Version = "0.1.0", Source = "Repository")
126 expect_equal(actual, expected)
127
128 })