forked from google/sqlcommenter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.spec.ts
More file actions
118 lines (102 loc) · 3.63 KB
/
path.spec.ts
File metadata and controls
118 lines (102 loc) · 3.63 KB
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
import { test } from "node:test";
import assert from "node:assert";
import { existsSync } from "node:fs";
import { join } from "node:path";
import {
findProjectRoot,
resolveFilePath,
applyWslPrefix,
_resetProjectRootCache,
} from "../src/path.js";
test("findProjectRoot", async (t) => {
t.afterEach(() => {
_resetProjectRootCache();
});
await t.test("returns a directory containing tsconfig.json", () => {
const root = findProjectRoot();
assert.ok(
existsSync(join(root, "tsconfig.json")),
`Expected ${root} to contain tsconfig.json`,
);
});
await t.test("caches the result across calls", () => {
const first = findProjectRoot();
const second = findProjectRoot();
assert.strictEqual(first, second);
});
});
test("resolveFilePath", async (t) => {
t.afterEach(() => {
_resetProjectRootCache();
});
await t.test("resolves path with src/ to project root", () => {
const projectRoot = findProjectRoot();
const result = resolveFilePath(
"/wrong/deploy/dir/src/routes/admin.ts:12:15",
);
assert.strictEqual(result, `${projectRoot}/src/routes/admin.ts:12:15`);
});
await t.test("leaves path without src/ unchanged", () => {
const result = resolveFilePath("/some/other/path/routes/admin.ts:5:10");
assert.strictEqual(result, "/some/other/path/routes/admin.ts:5:10");
});
await t.test("preserves line:column suffix", () => {
const projectRoot = findProjectRoot();
const result = resolveFilePath("/bad/path/src/index.ts:99:3");
assert.strictEqual(result, `${projectRoot}/src/index.ts:99:3`);
});
await t.test("uses first src/ occurrence", () => {
const projectRoot = findProjectRoot();
const result = resolveFilePath(
"/deploy/src/nested/src/routes/admin.ts:1:1",
);
assert.strictEqual(
result,
`${projectRoot}/src/nested/src/routes/admin.ts:1:1`,
);
});
await t.test("returns raw string if no line:column suffix", () => {
const result = resolveFilePath("/some/path/src/file.ts");
assert.strictEqual(result, "/some/path/src/file.ts");
});
});
test("applyWslPrefix", async (t) => {
const originalWslDistroName = process.env.WSL_DISTRO_NAME;
t.afterEach(() => {
if (originalWslDistroName === undefined) {
delete process.env.WSL_DISTRO_NAME;
} else {
process.env.WSL_DISTRO_NAME = originalWslDistroName;
}
});
await t.test("prefixes path when WSL_DISTRO_NAME is set", () => {
process.env.WSL_DISTRO_NAME = "Ubuntu-22.04";
const path = "/home/user/project/src/file.ts:1:1";
const result = applyWslPrefix(path);
assert.strictEqual(result, "//wsl.localhost/Ubuntu-22.04/home/user/project/src/file.ts:1:1");
});
await t.test("returns path unchanged when WSL_DISTRO_NAME is not set", () => {
delete process.env.WSL_DISTRO_NAME;
const path = "/home/user/project/src/file.ts:1:1";
const result = applyWslPrefix(path);
assert.strictEqual(result, path);
});
});
test("resolveFilePath with WSL", async (t) => {
const originalWslDistroName = process.env.WSL_DISTRO_NAME;
t.afterEach(() => {
_resetProjectRootCache();
if (originalWslDistroName === undefined) {
delete process.env.WSL_DISTRO_NAME;
} else {
process.env.WSL_DISTRO_NAME = originalWslDistroName;
}
});
await t.test("applies WSL prefix to resolved src/ paths", () => {
process.env.WSL_DISTRO_NAME = "Ubuntu-22.04";
const projectRoot = findProjectRoot();
const rawPath = "/wrong/deploy/dir/src/routes/admin.ts:12:15";
const result = resolveFilePath(rawPath);
assert.strictEqual(result, `//wsl.localhost/Ubuntu-22.04${projectRoot}/src/routes/admin.ts:12:15`);
});
});