Skip to content

Commit 63d11cf

Browse files
committed
Add tests
1 parent d888167 commit 63d11cf

File tree

1 file changed

+221
-0
lines changed

1 file changed

+221
-0
lines changed
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
test_that("project_path() works with explicit root", {
2+
temp_dir <- withr::local_tempdir()
3+
withr::local_dir(temp_dir)
4+
expect_identical(
5+
project_path("data", "file.csv", root = temp_dir),
6+
file.path("data", "file.csv")
7+
)
8+
expect_identical(
9+
project_path("outputs", "figures", "plot.png", root = temp_dir),
10+
file.path("outputs", "figures", "plot.png")
11+
)
12+
expect_identical(project_path(root = temp_dir), ".")
13+
})
14+
15+
test_that("project_path() uses Quarto environment variables", {
16+
temp_dir <- withr::local_tempdir()
17+
withr::local_dir(temp_dir)
18+
dir.create("project")
19+
withr::local_envvar(
20+
QUARTO_PROJECT_ROOT = xfun::normalize_path(file.path(temp_dir, "project"))
21+
)
22+
expect_identical(
23+
project_path("data", "file.csv"),
24+
file.path("project", "data", "file.csv")
25+
)
26+
withr::local_envvar(
27+
QUARTO_PROJECT_ROOT = "",
28+
QUARTO_PROJECT_DIR = xfun::normalize_path(file.path(temp_dir, "project"))
29+
)
30+
expect_identical(
31+
project_path("data", "file.csv"),
32+
file.path("project", "data", "file.csv")
33+
)
34+
})
35+
36+
test_that("project_path() detects Quarto project files", {
37+
skip_if_no_quarto()
38+
39+
project_dir <- local_quarto_project(type = "blog")
40+
# simulate running from a post directory withing .qmd
41+
post_dir <- file.path(project_dir, "posts", "welcome")
42+
withr::local_dir(post_dir)
43+
# data is at root of the project and path should be relative to that
44+
expect_identical(
45+
project_path("data", "file.csv"),
46+
"../../data/file.csv"
47+
)
48+
})
49+
50+
test_that("project_path() detects R package DESCRIPTION", {
51+
temp_dir <- withr::local_tempdir()
52+
desc_file <- file.path(temp_dir, "DESCRIPTION")
53+
writeLines(c("Package: testpkg", "Version: 1.0.0"), desc_file)
54+
withr::local_dir(temp_dir)
55+
dir.create("reports")
56+
withr::local_dir("reports")
57+
expect_identical(
58+
project_path("R", "functions.R"),
59+
"../R/functions.R"
60+
)
61+
})
62+
63+
test_that("project_path() detects .Rproj files", {
64+
temp_dir <- withr::local_tempdir()
65+
rproj_file <- file.path(temp_dir, "test.Rproj")
66+
writeLines("Version: 1.0", rproj_file)
67+
withr::local_dir(temp_dir)
68+
dir.create("reports")
69+
withr::local_dir("reports")
70+
expect_identical(
71+
project_path("analysis", "script.R"),
72+
"../analysis/script.R"
73+
)
74+
})
75+
76+
test_that("project_path() falls back to working directory with warning", {
77+
temp_dir <- withr::local_tempdir()
78+
withr::local_dir(temp_dir)
79+
withr::local_envvar(
80+
QUARTO_PROJECT_ROOT = "",
81+
QUARTO_PROJECT_DIR = ""
82+
)
83+
84+
expect_warning(
85+
expect_identical(
86+
project_path("data", "file.csv"),
87+
file.path("data", "file.csv")
88+
),
89+
"Failed to determine project root"
90+
)
91+
})
92+
93+
test_that("project_path() handles xfun::proj_root() errors gracefully", {
94+
temp_dir <- withr::local_tempdir()
95+
96+
# Mock xfun::proj_root to throw an error
97+
local_mocked_bindings(
98+
proj_root = function(path = ".", rules = xfun::root_rules) {
99+
stop("Test error")
100+
},
101+
.package = "xfun"
102+
)
103+
104+
withr::local_dir(temp_dir)
105+
withr::local_envvar(
106+
QUARTO_PROJECT_ROOT = "",
107+
QUARTO_PROJECT_DIR = ""
108+
)
109+
110+
expect_warning(
111+
expect_identical(
112+
project_path("data", "file.csv"),
113+
file.path("data", "file.csv")
114+
),
115+
"Failed to determine project root: Test error"
116+
)
117+
})
118+
119+
test_that("is_running_quarto_project() detects environment variables", {
120+
withr::with_envvar(
121+
list(
122+
QUARTO_PROJECT_ROOT = "",
123+
QUARTO_PROJECT_DIR = ""
124+
),
125+
expect_false(is_running_quarto_project())
126+
)
127+
128+
withr::with_envvar(
129+
list(
130+
QUARTO_PROJECT_ROOT = "/some/path",
131+
QUARTO_PROJECT_DIR = ""
132+
),
133+
expect_true(is_running_quarto_project())
134+
)
135+
136+
withr::with_envvar(
137+
list(
138+
QUARTO_PROJECT_ROOT = "",
139+
QUARTO_PROJECT_DIR = "/some/path"
140+
),
141+
expect_true(is_running_quarto_project())
142+
)
143+
144+
withr::with_envvar(
145+
list(
146+
QUARTO_PROJECT_ROOT = "/path1",
147+
QUARTO_PROJECT_DIR = "/path2"
148+
),
149+
expect_true(is_running_quarto_project())
150+
)
151+
})
152+
153+
test_that("is_quarto_project() detects Quarto project files", {
154+
skip_if_no_quarto()
155+
156+
temp_dir <- withr::local_tempdir()
157+
expect_false(is_quarto_project(temp_dir))
158+
159+
project_dir <- local_quarto_project(type = "default")
160+
expect_true(is_quarto_project(project_dir))
161+
162+
withr::local_dir(project_dir)
163+
expect_true(is_quarto_project())
164+
})
165+
166+
test_that("is_quarto_project() works with _quarto.yaml", {
167+
temp_dir <- withr::local_tempdir()
168+
169+
quarto_yaml <- file.path(temp_dir, "_quarto.yaml")
170+
writeLines("project:", quarto_yaml)
171+
172+
expect_true(is_quarto_project(temp_dir))
173+
174+
withr::local_dir(temp_dir)
175+
expect_true(is_quarto_project())
176+
})
177+
178+
test_that("is_quarto_project() handles errors gracefully", {
179+
# Mock xfun::proj_root to throw an error
180+
local_mocked_bindings(
181+
proj_root = function(path = ".", rules = xfun::root_rules) {
182+
stop("Test error")
183+
},
184+
.package = "xfun"
185+
)
186+
187+
temp_dir <- withr::local_tempdir()
188+
expect_false(is_quarto_project(temp_dir))
189+
})
190+
191+
test_that("project_path() prioritizes environment variables over file detection", {
192+
skip_if_no_quarto()
193+
194+
temp1 <- withr::local_tempdir()
195+
temp2 <- withr::local_tempdir()
196+
197+
quarto_create_project(
198+
name = "test_project",
199+
type = "default",
200+
dir = temp1,
201+
no_prompt = TRUE,
202+
quiet = TRUE
203+
)
204+
205+
withr::local_envvar(
206+
QUARTO_PROJECT_ROOT = "",
207+
QUARTO_PROJECT_DIR = ""
208+
)
209+
project_dir <- file.path(temp1, "test_project")
210+
dir.create(file.path(project_dir, "subfolder"))
211+
withr::local_dir(file.path(project_dir, "subfolder"))
212+
213+
expect_identical(project_path("test.txt"), "../test.txt")
214+
215+
# With env var, should use env var instead
216+
withr::local_envvar(QUARTO_PROJECT_ROOT = file.path(project_dir, "subfolder"))
217+
expect_identical(
218+
project_path("test.txt"),
219+
"test.txt"
220+
)
221+
})

0 commit comments

Comments
 (0)