forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsconfig.spec.ts
More file actions
93 lines (77 loc) · 3.12 KB
/
tsconfig.spec.ts
File metadata and controls
93 lines (77 loc) · 3.12 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
import * as fs from "fs-extra";
import * as os from "os";
import * as path from "path";
import { locateConfigFile } from "../../src/cli/tsconfig";
import { normalizeSlashes } from "../../src/utils";
let temp: string;
beforeEach(async () => {
temp = await fs.mkdtemp(path.join(os.tmpdir(), "tstl-test-"));
process.chdir(temp);
});
const originalWorkingDirectory = process.cwd();
afterEach(async () => {
process.chdir(originalWorkingDirectory);
// TODO [node@12]: `rmdir` has `recursive` option
await fs.remove(temp);
});
const locate = (project: string | undefined, fileNames: string[] = []) =>
locateConfigFile({ errors: [], fileNames, options: { project } });
const normalize = (name: string) => normalizeSlashes(path.resolve(temp, name));
describe("specified", () => {
for (const separator of process.platform === "win32" ? ["/", "\\"] : ["/"]) {
for (const pointsTo of ["file", "directory"] as const) {
const findAndExpect = (project: string, expected: string) => {
project = project.replace(/[\\/]/g, separator);
if (pointsTo === "directory") {
project = path.dirname(project);
}
expect(locate(project)).toBe(normalize(expected));
};
test(`relative to ${pointsTo} separated with '${separator}'`, async () => {
await fs.outputFile("tsconfig.json", "");
await fs.mkdir("src");
process.chdir("src");
findAndExpect("../tsconfig.json", "tsconfig.json");
});
test(`absolute to ${pointsTo} separated with '${separator}'`, async () => {
await fs.outputFile("tsconfig.json", "");
findAndExpect(path.resolve("tsconfig.json"), "tsconfig.json");
});
}
}
test.each(["", ".", "./"])("current directory (%p)", async () => {
await fs.outputFile("tsconfig.json", "");
expect(locate(".")).toBe(normalize("tsconfig.json"));
});
});
describe("inferred", () => {
test("in current directory", async () => {
await fs.outputFile("tsconfig.json", "");
expect(locate(undefined)).toBe(normalize("tsconfig.json"));
});
test("in parent directory", async () => {
await fs.outputFile("tsconfig.json", "");
await fs.mkdir("src");
process.chdir("src");
expect(locate(undefined)).toBe(normalize("tsconfig.json"));
});
test("not found", () => {
expect(locate(undefined)).toBeUndefined();
});
test("does not attempt when has files", async () => {
await fs.outputFile("tsconfig.json", "");
expect(locate(undefined, [""])).toBeUndefined();
});
});
describe("errors", () => {
test("specified file does not exist", () => {
expect([locate("tsconfig.json")]).toHaveDiagnostics();
});
test("specified directory does not exist", () => {
expect([locate("project")]).toHaveDiagnostics();
});
test("cannot be mixed", async () => {
await fs.outputFile("tsconfig.json", "");
expect([locate("tsconfig.json", [""])]).toHaveDiagnostics();
});
});