/
test-proj.R
248 lines (201 loc) · 6.75 KB
/
test-proj.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
test_that("proj_set() errors on non-existent path", {
expect_usethis_error(
proj_set("abcedefgihklmnopqrstuv"),
"does not exist"
)
})
test_that("proj_set() errors if no criteria are fulfilled", {
tmpdir <- withr::local_tempdir(pattern = "i-am-not-a-project")
expect_usethis_error(
proj_set(tmpdir),
"does not appear to be inside a project or package"
)
})
test_that("proj_set() can be forced, even if no criteria are fulfilled", {
tmpdir <- withr::local_tempdir(pattern = "i-am-not-a-project")
expect_no_error(old <- proj_set(tmpdir, force = TRUE))
withr::defer(proj_set(old))
expect_identical(proj_get(), proj_path_prep(tmpdir))
})
test_that("is_package() detects package-hood", {
create_local_package()
expect_true(is_package())
create_local_project()
expect_false(is_package())
})
test_that("check_is_package() errors for non-package", {
create_local_project()
expect_usethis_error(check_is_package(), "not an R package")
})
test_that("check_is_package() can reveal who's asking", {
create_local_project()
expect_snapshot(
error = TRUE,
check_is_package("foo()"),
transform = scrub_testproj
)
})
test_that("proj_path() appends to the project path", {
create_local_project()
expect_equal(
proj_path("a", "b", "c"),
path(proj_get(), "a/b/c")
)
expect_identical(proj_path("a", "b", "c"), proj_path("a/b/c"))
})
test_that("proj_path() errors with absolute paths", {
create_local_project()
expect_snapshot(proj_path(c("/a", "b", "/c")), error = TRUE)
expect_snapshot(proj_path("/a", "b", "/c"), error = TRUE)
expect_snapshot(proj_path("/a", c("b", "/c")), error = TRUE)
})
test_that("proj_path() with no inputs returns result of length 1, not 0", {
create_local_project()
expect_equal(proj_path(), proj_get())
})
test_that("proj_rel_path() returns path part below the project", {
create_local_project()
expect_equal(proj_rel_path(proj_path("a/b/c")), "a/b/c")
})
test_that("proj_rel_path() returns path 'as is' if not in project", {
create_local_project()
expect_identical(proj_rel_path(path_temp()), path_temp())
})
test_that("proj_set() enforces proj path preparation policy", {
# specifically: check that proj_get() returns realized path
t <- withr::local_tempdir("proj-set-path-prep")
# a/b/d and a/b2/d identify same directory
a <- path_real(dir_create(path(t, "a")))
b <- dir_create(path(a, "b"))
b2 <- link_create(b, path(a, "b2"))
d <- dir_create(path(b, "d"))
# input path includes symbolic link
path_with_symlinks <- path(b2, "d")
expect_equal(path_rel(path_with_symlinks, a), path("b2/d"))
# force = TRUE
local_project(path_with_symlinks, force = TRUE)
expect_equal(path_rel(proj_get(), a), path("b/d"))
# force = FALSE
file_create(path(b, "d", ".here"))
proj_set(path_with_symlinks, force = FALSE)
expect_equal(path_rel(proj_get(), a), path("b/d"))
})
test_that("proj_path_prep() passes NULL through", {
expect_null(proj_path_prep(NULL))
})
test_that("is_in_proj() detects whether files are (or would be) in project", {
create_local_package()
## file does not exist but would be in project if created
expect_true(is_in_proj(proj_path("fiction")))
## file exists in project
expect_true(is_in_proj(proj_path("DESCRIPTION")))
## file does not exist and would not be in project if created
expect_false(is_in_proj(file_temp()))
## file exists and is not in project
expect_false(is_in_proj(path_temp()))
})
test_that("is_in_proj() does not activate a project", {
pkg <- create_local_package()
path <- proj_path("DESCRIPTION")
expect_true(is_in_proj(path))
local_project(NULL)
expect_false(is_in_proj(path))
expect_false(proj_active())
})
test_that("proj_sitrep() reports current working/project state", {
pkg <- create_local_package()
x <- proj_sitrep()
expect_s3_class(x, "sitrep")
expect_false(is.null(x[["working_directory"]]))
expect_identical(
fs::path_file(pkg),
fs::path_file(x[["active_usethis_proj"]])
)
})
test_that("with_project() runs code in temp proj, restores (lack of) proj", {
old_project <- proj_get_()
withr::defer(proj_set_(old_project))
temp_proj <- create_project(
file_temp(pattern = "TEMPPROJ"),
rstudio = FALSE, open = FALSE
)
proj_set_(NULL)
expect_identical(proj_get_(), NULL)
res <- with_project(path = temp_proj, proj_get_())
expect_identical(res, temp_proj)
expect_identical(proj_get_(), NULL)
})
test_that("with_project() runs code in temp proj, restores original proj", {
old_project <- proj_get_()
withr::defer(proj_set_(old_project))
host <- create_project(
file_temp(pattern = "host"),
rstudio = FALSE, open = FALSE
)
guest <- create_project(
file_temp(pattern = "guest"),
rstudio = FALSE, open = FALSE
)
proj_set(host)
expect_identical(proj_get_(), host)
res <- with_project(path = guest, proj_get_())
expect_identical(res, guest)
expect_identical(proj_get(), host)
})
test_that("with_project() works when temp proj == original proj", {
old_project <- proj_get_()
withr::defer(proj_set_(old_project))
host <- create_project(
file_temp(pattern = "host"),
rstudio = FALSE, open = FALSE
)
proj_set(host)
expect_identical(proj_get_(), host)
res <- with_project(path = host, proj_get_())
expect_identical(res, host)
expect_identical(proj_get(), host)
})
test_that("local_project() activates proj til scope ends", {
old_project <- proj_get_()
withr::defer(proj_set_(old_project))
new_proj <- file_temp(pattern = "localprojtest")
create_project(new_proj, rstudio = FALSE, open = FALSE)
proj_set_(NULL)
foo <- function() {
local_project(new_proj)
proj_sitrep()
}
res <- foo()
expect_identical(
res[["active_usethis_proj"]],
as.character(proj_path_prep(new_proj))
)
expect_null(proj_get_())
})
# https://github.com/r-lib/usethis/issues/954
test_that("proj_activate() works with relative path when RStudio is not detected", {
sandbox <- path_real(dir_create(file_temp("sandbox")))
withr::defer(dir_delete(sandbox))
orig_proj <- proj_get_()
withr::defer(proj_set(orig_proj, force = TRUE))
withr::local_dir(sandbox)
local_rstudio_available(FALSE)
rel_path_proj <- path_file(file_temp(pattern = "mno"))
out_path <- create_project(rel_path_proj, rstudio = FALSE, open = FALSE)
expect_no_error(
result <- proj_activate(rel_path_proj)
)
expect_true(result)
expect_equal(path_wd(), out_path)
expect_equal(proj_get(), out_path)
})
# https://github.com/r-lib/usethis/issues/1498
test_that("local_project()'s `quiet` argument works", {
temp_proj <- create_project(
file_temp(pattern = "TEMPPROJ"),
rstudio = FALSE, open = FALSE
)
withr::defer(dir_delete(temp_proj))
local_project(path = temp_proj, quiet = TRUE, force = TRUE, setwd = FALSE)
expect_true(getOption("usethis.quiet"))
})