Skip to content

Commit 5b4350a

Browse files
committed
Add tests
1 parent 0586aee commit 5b4350a

File tree

5 files changed

+175
-37
lines changed

5 files changed

+175
-37
lines changed

package-lock.json

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@
4444
"typescript": "^3.6.2"
4545
},
4646
"devDependencies": {
47+
"@types/fs-extra": "^8.0.1",
4748
"@types/glob": "^7.1.1",
4849
"@types/jest": "^24.0.15",
4950
"@types/node": "^11.13.14",
5051
"@types/resolve": "0.0.8",
5152
"fengari": "^0.1.4",
53+
"fs-extra": "^8.1.0",
5254
"javascript-stringify": "^2.0.0",
5355
"jest": "^24.8.0",
5456
"jest-circus": "^24.8.0",

src/CommandLineParser.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as path from "path";
22
import * as ts from "typescript";
33
import { CompilerOptions, LuaLibImportKind, LuaTarget } from "./CompilerOptions";
44
import * as diagnosticFactories from "./diagnostics";
5+
import { normalizeSlashes } from "./utils";
56

67
export interface ParsedCommandLine extends ts.ParsedCommandLine {
78
options: CompilerOptions;
@@ -256,3 +257,35 @@ export function createDiagnosticReporter(pretty: boolean, system = ts.sys): ts.D
256257
reporter(diagnostic);
257258
};
258259
}
260+
261+
export function locateConfigFile(commandLine: ParsedCommandLine): ts.Diagnostic | string | undefined {
262+
const { project } = commandLine.options;
263+
if (!project) {
264+
if (commandLine.fileNames.length > 0) {
265+
return undefined;
266+
}
267+
268+
const searchPath = normalizeSlashes(ts.sys.getCurrentDirectory());
269+
return ts.findConfigFile(searchPath, ts.sys.fileExists);
270+
}
271+
272+
if (commandLine.fileNames.length !== 0) {
273+
return diagnosticFactories.optionProjectCannotBeMixedWithSourceFilesOnACommandLine();
274+
}
275+
276+
const fileOrDirectory = normalizeSlashes(path.resolve(ts.sys.getCurrentDirectory(), project));
277+
if (!fileOrDirectory || ts.sys.directoryExists(fileOrDirectory)) {
278+
const configFileName = path.posix.join(fileOrDirectory, "tsconfig.json");
279+
if (ts.sys.fileExists(configFileName)) {
280+
return configFileName;
281+
} else {
282+
return diagnosticFactories.cannotFindATsconfigJsonAtTheSpecifiedDirectory(project);
283+
}
284+
} else {
285+
if (ts.sys.fileExists(fileOrDirectory)) {
286+
return fileOrDirectory;
287+
} else {
288+
return diagnosticFactories.theSpecifiedPathDoesNotExist(project);
289+
}
290+
}
291+
}

src/tstl.ts

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as ts from "typescript";
44
import * as tstl from ".";
55
import * as CommandLineParser from "./CommandLineParser";
66
import * as diagnosticFactories from "./diagnostics";
7-
import { normalizeSlashes } from "./utils";
87

98
function createWatchStatusReporter(options?: ts.CompilerOptions): ts.WatchStatusReporter {
109
return (ts as any).createWatchStatusReporter(ts.sys, shouldBePretty(options));
@@ -21,41 +20,6 @@ function updateReportDiagnostic(options?: ts.CompilerOptions): void {
2120
reportDiagnostic = tstl.createDiagnosticReporter(shouldBePretty(options));
2221
}
2322

24-
function locateConfigFile(commandLine: tstl.ParsedCommandLine): string | undefined {
25-
const { project } = commandLine.options;
26-
if (!project) {
27-
if (commandLine.fileNames.length === 0) {
28-
const searchPath = normalizeSlashes(ts.sys.getCurrentDirectory());
29-
return ts.findConfigFile(searchPath, ts.sys.fileExists);
30-
}
31-
return;
32-
}
33-
34-
if (commandLine.fileNames.length !== 0) {
35-
reportDiagnostic(diagnosticFactories.optionProjectCannotBeMixedWithSourceFilesOnACommandLine());
36-
ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
37-
return;
38-
}
39-
40-
const fileOrDirectory = normalizeSlashes(path.resolve(ts.sys.getCurrentDirectory(), project));
41-
if (!fileOrDirectory || ts.sys.directoryExists(fileOrDirectory)) {
42-
const configFileName = path.posix.join(fileOrDirectory, "tsconfig.json");
43-
if (ts.sys.fileExists(configFileName)) {
44-
return configFileName;
45-
} else {
46-
reportDiagnostic(diagnosticFactories.cannotFindATsconfigJsonAtTheSpecifiedDirectory(project));
47-
ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
48-
}
49-
} else {
50-
if (ts.sys.fileExists(fileOrDirectory)) {
51-
return fileOrDirectory;
52-
} else {
53-
reportDiagnostic(diagnosticFactories.theSpecifiedPathDoesNotExist(project));
54-
ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
55-
}
56-
}
57-
}
58-
5923
function executeCommandLine(args: string[]): void {
6024
if (args.length > 0 && args[0].startsWith("-")) {
6125
const firstOption = args[0].slice(args[0].startsWith("--") ? 2 : 1).toLowerCase();
@@ -89,7 +53,12 @@ function executeCommandLine(args: string[]): void {
8953
return ts.sys.exit(ts.ExitStatus.Success);
9054
}
9155

92-
const configFileName = locateConfigFile(commandLine);
56+
const configFileName = CommandLineParser.locateConfigFile(commandLine);
57+
if (typeof configFileName === "object") {
58+
reportDiagnostic(configFileName);
59+
return ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
60+
}
61+
9362
const commandLineOptions = commandLine.options;
9463
if (configFileName) {
9564
const configParseResult = CommandLineParser.parseConfigFileWithSystem(configFileName, commandLineOptions);

test/cli/tsconfig.spec.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import * as fs from "fs-extra";
2+
import * as os from "os";
3+
import * as path from "path";
4+
import { locateConfigFile } from "../../src/CommandLineParser";
5+
import { normalizeSlashes } from "../../src/utils";
6+
7+
let temp: string;
8+
beforeEach(async () => {
9+
temp = await fs.mkdtemp(path.join(os.tmpdir(), "tstl-test-"));
10+
process.chdir(temp);
11+
});
12+
13+
afterEach(async () => {
14+
// TODO [node@12]: `rmdir` has `recursive` option
15+
await fs.remove(temp);
16+
});
17+
18+
const locate = (project: string | undefined, fileNames: string[] = []) =>
19+
locateConfigFile({ errors: [], fileNames, options: { project } });
20+
21+
const normalize = (name: string) => normalizeSlashes(path.resolve(temp, name));
22+
23+
describe("specified", () => {
24+
for (const separator of process.platform === "win32" ? ["/", "\\"] : ["/"]) {
25+
for (const pointsTo of ["file", "directory"] as const) {
26+
const findAndExpect = (project: string, expected: string) => {
27+
project = project.replace(/[\\/]/g, separator);
28+
if (pointsTo === "directory") {
29+
project = path.dirname(project);
30+
}
31+
32+
expect(locate(project)).toBe(normalize(expected));
33+
};
34+
35+
test(`relative to ${pointsTo} separated with '${separator}'`, async () => {
36+
await fs.outputFile("tsconfig.json", "");
37+
await fs.mkdir("src");
38+
process.chdir("src");
39+
findAndExpect("../tsconfig.json", "tsconfig.json");
40+
});
41+
42+
test(`absolute to ${pointsTo} separated with '${separator}'`, async () => {
43+
await fs.outputFile("tsconfig.json", "");
44+
findAndExpect(path.resolve("tsconfig.json"), "tsconfig.json");
45+
});
46+
}
47+
}
48+
49+
test.each(["", ".", "./"])("current directory (%p)", async () => {
50+
await fs.outputFile("tsconfig.json", "");
51+
expect(locate(".")).toBe(normalize("tsconfig.json"));
52+
});
53+
});
54+
55+
describe("inferred", () => {
56+
test("in current directory", async () => {
57+
await fs.outputFile("tsconfig.json", "");
58+
expect(locate(undefined)).toBe(normalize("tsconfig.json"));
59+
});
60+
61+
test("in parent directory", async () => {
62+
await fs.outputFile("tsconfig.json", "");
63+
await fs.mkdir("src");
64+
process.chdir("src");
65+
expect(locate(undefined)).toBe(normalize("tsconfig.json"));
66+
});
67+
68+
test("not found", () => {
69+
expect(locate(undefined)).toBe(undefined);
70+
});
71+
72+
test("does not attempt when has files", async () => {
73+
await fs.outputFile("tsconfig.json", "");
74+
expect(locate(undefined, [""])).toBe(undefined);
75+
});
76+
});
77+
78+
describe("errors", () => {
79+
test("specified file does not exist", () => {
80+
expect([locate("tsconfig.json")]).toHaveDiagnostics();
81+
});
82+
83+
test("specified directory does not exist", () => {
84+
expect([locate("project")]).toHaveDiagnostics();
85+
});
86+
87+
test("cannot be mixed", async () => {
88+
await fs.outputFile("tsconfig.json", "");
89+
expect([locate("tsconfig.json", [""])]).toHaveDiagnostics();
90+
});
91+
});

0 commit comments

Comments
 (0)