Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/unit/paths/qualified-path.test.ts
6451 views
1
/*
2
* qualified-path.test.ts
3
*
4
* Copyright (C) 2022 Posit Software, PBC
5
*
6
*/
7
8
import { unitTest } from "../../test.ts";
9
10
import {
11
makePath,
12
PathInfo,
13
QualifiedPath,
14
} from "../../../src/core/qualified-path.ts";
15
import { assertEquals, assertRejects } from "testing/asserts";
16
import { isWindows } from "../../../src/deno_ral/platform.ts";
17
18
//deno-lint-ignore require-await
19
unitTest("qualified-path - basic", async () => {
20
const paths: PathInfo = {
21
currentFileDir: "/tmp/project/dir",
22
projectRoot: "/tmp/project",
23
};
24
25
const projectRelative = makePath("/dir/file1", paths);
26
const relative = makePath("file1", paths);
27
const absolute = makePath("/tmp/project/dir/file1", paths, true);
28
29
const expectedRelative = "file1";
30
const expectedProjectRelative = "/dir/file1";
31
const expectedAbsolute = "/tmp/project/dir/file1";
32
33
for (let path of [projectRelative, relative, absolute]) {
34
for (let i = 0; i < 30; ++i) {
35
const choices = [
36
(path: QualifiedPath) => path.asAbsolute(paths),
37
(path: QualifiedPath) => path.asRelative(paths),
38
(path: QualifiedPath) => path.asProjectRelative(paths),
39
];
40
path = choices[~~(Math.random() * choices.length)](path);
41
assertEquals(path.asAbsolute(paths).value, expectedAbsolute);
42
assertEquals(path.asRelative(paths).value, expectedRelative);
43
assertEquals(
44
path.asProjectRelative(paths).value,
45
expectedProjectRelative,
46
);
47
}
48
}
49
}, {
50
ignore: isWindows,
51
});
52
53
unitTest("qualified-path - validation", async () => {
54
const paths: PathInfo = {
55
currentFileDir: "/tmp/project/dir",
56
projectRoot: "/tmp/project",
57
};
58
59
makePath("../file1", paths);
60
61
//deno-lint-ignore require-await
62
await assertRejects(async () => {
63
// this should raise because it resolves outside of projectRoot.
64
return makePath("../../file1", paths);
65
});
66
}, {
67
ignore: isWindows,
68
});
69
70