2015-10-29 12:56:13 +01:00
|
|
|
/// <reference path="..\..\..\src\harness\harness.ts" />
|
|
|
|
/// <reference path="..\..\..\src\compiler\commandLineParser.ts" />
|
|
|
|
|
2015-11-01 15:31:16 +01:00
|
|
|
namespace ts {
|
|
|
|
describe('parseConfigFileTextToJson', () => {
|
|
|
|
function assertParseResult(jsonText: string, expectedConfigObject: { config?: any; error?: Diagnostic }) {
|
|
|
|
let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText);
|
|
|
|
assert.equal(JSON.stringify(parsed), JSON.stringify(expectedConfigObject));
|
|
|
|
}
|
2015-10-29 12:56:13 +01:00
|
|
|
|
2015-11-01 15:31:16 +01:00
|
|
|
function assertParseError(jsonText: string) {
|
|
|
|
let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText);
|
|
|
|
assert.isTrue(undefined === parsed.config);
|
|
|
|
assert.isTrue(undefined !== parsed.error);
|
|
|
|
}
|
2016-05-05 23:49:41 +02:00
|
|
|
|
2016-03-28 14:23:59 +02:00
|
|
|
function assertParseErrorWithExcludesKeyword(jsonText: string) {
|
|
|
|
let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText);
|
|
|
|
let parsedCommand = ts.parseJsonConfigFileContent(parsed, ts.sys, "tests/cases/unittests");
|
|
|
|
assert.isTrue(undefined !== parsedCommand.errors);
|
|
|
|
}
|
2015-10-29 12:56:13 +01:00
|
|
|
|
2016-05-05 23:49:41 +02:00
|
|
|
function assertParseFileList(jsonText: string, configFileName: string, basePath: string, allFileList: string[], expectedFileList: string[]) {
|
|
|
|
const json = JSON.parse(jsonText);
|
|
|
|
const host: ParseConfigHost = { readDirectory: mockReadDirectory };
|
|
|
|
const parsed = ts.parseJsonConfigFileContent(json, host, basePath, /*existingOptions*/ undefined, configFileName);
|
|
|
|
assert.isTrue(arrayIsEqualTo(parsed.fileNames.sort(), expectedFileList.sort()));
|
|
|
|
|
|
|
|
function mockReadDirectory(rootDir: string, extension: string, exclude: string[]): string[] {
|
|
|
|
const result: string[] = [];
|
|
|
|
const fullExcludeDirectories = ts.map(exclude, directory => combinePaths(rootDir, directory));
|
|
|
|
for (const file of allFileList) {
|
|
|
|
let shouldExclude = false;
|
|
|
|
for (const fullExcludeDirectorie of fullExcludeDirectories) {
|
|
|
|
if (file.indexOf(fullExcludeDirectorie) >= 0) {
|
|
|
|
shouldExclude = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shouldExclude) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (fileExtensionIs(file, extension)) {
|
|
|
|
result.push(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 15:31:16 +01:00
|
|
|
it("returns empty config for file with only whitespaces", () => {
|
|
|
|
assertParseResult("", { config : {} });
|
|
|
|
assertParseResult(" ", { config : {} });
|
|
|
|
});
|
2015-10-29 12:56:13 +01:00
|
|
|
|
2015-11-01 15:31:16 +01:00
|
|
|
it("returns empty config for file with comments only", () => {
|
|
|
|
assertParseResult("// Comment", { config: {} });
|
|
|
|
assertParseResult("/* Comment*/", { config: {} });
|
|
|
|
});
|
2015-10-29 12:56:13 +01:00
|
|
|
|
2015-11-01 15:31:16 +01:00
|
|
|
it("returns empty config when config is empty object", () => {
|
|
|
|
assertParseResult("{}", { config: {} });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns config object without comments", () => {
|
|
|
|
assertParseResult(
|
|
|
|
`{ // Excluded files
|
|
|
|
"exclude": [
|
|
|
|
// Exclude d.ts
|
|
|
|
"file.d.ts"
|
|
|
|
]
|
|
|
|
}`, { config: { exclude: ["file.d.ts"] } });
|
|
|
|
|
|
|
|
assertParseResult(
|
|
|
|
`{
|
|
|
|
/* Excluded
|
|
|
|
Files
|
|
|
|
*/
|
|
|
|
"exclude": [
|
|
|
|
/* multiline comments can be in the middle of a line */"file.d.ts"
|
|
|
|
]
|
|
|
|
}`, { config: { exclude: ["file.d.ts"] } });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("keeps string content untouched", () => {
|
|
|
|
assertParseResult(
|
|
|
|
`{
|
|
|
|
"exclude": [
|
|
|
|
"xx//file.d.ts"
|
|
|
|
]
|
|
|
|
}`, { config: { exclude: ["xx//file.d.ts"] } });
|
|
|
|
assertParseResult(
|
|
|
|
`{
|
|
|
|
"exclude": [
|
|
|
|
"xx/*file.d.ts*/"
|
|
|
|
]
|
|
|
|
}`, { config: { exclude: ["xx/*file.d.ts*/"] } });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("handles escaped characters in strings correctly", () => {
|
|
|
|
assertParseResult(
|
|
|
|
`{
|
|
|
|
"exclude": [
|
|
|
|
"xx\\"//files"
|
|
|
|
]
|
|
|
|
}`, { config: { exclude: ["xx\"//files"] } });
|
|
|
|
|
|
|
|
assertParseResult(
|
|
|
|
`{
|
|
|
|
"exclude": [
|
|
|
|
"xx\\\\" // end of line comment
|
|
|
|
]
|
|
|
|
}`, { config: { exclude: ["xx\\"] } });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns object with error when json is invalid", () => {
|
|
|
|
assertParseError("invalid");
|
|
|
|
});
|
2016-03-05 01:52:31 +01:00
|
|
|
|
|
|
|
it("returns object when users correctly specify library", () => {
|
|
|
|
assertParseResult(
|
|
|
|
`{
|
|
|
|
"compilerOptions": {
|
2016-03-15 19:44:33 +01:00
|
|
|
"lib": "es5"
|
2016-03-05 01:52:31 +01:00
|
|
|
}
|
2016-03-15 19:44:33 +01:00
|
|
|
}`, {
|
|
|
|
config: { compilerOptions: { lib: "es5" } }
|
|
|
|
});
|
|
|
|
|
2016-03-05 01:52:31 +01:00
|
|
|
assertParseResult(
|
|
|
|
`{
|
|
|
|
"compilerOptions": {
|
2016-03-15 19:44:33 +01:00
|
|
|
"lib": "es5,es6"
|
2016-03-05 01:52:31 +01:00
|
|
|
}
|
2016-03-15 19:44:33 +01:00
|
|
|
}`, {
|
|
|
|
config: { compilerOptions: { lib: "es5,es6" } }
|
|
|
|
});
|
2016-03-05 01:52:31 +01:00
|
|
|
});
|
2016-05-05 23:49:41 +02:00
|
|
|
|
2016-03-28 14:23:59 +02:00
|
|
|
it("returns error when tsconfig have excludes", () => {
|
|
|
|
assertParseErrorWithExcludesKeyword(
|
|
|
|
`{
|
|
|
|
"compilerOptions": {
|
|
|
|
"lib": "es5"
|
|
|
|
},
|
|
|
|
"excludes": [
|
|
|
|
"foge.ts"
|
|
|
|
]
|
|
|
|
}`);
|
|
|
|
});
|
2016-05-05 23:49:41 +02:00
|
|
|
|
|
|
|
it("ignore dotted files and folders", () => {
|
|
|
|
assertParseFileList(
|
|
|
|
`{}`,
|
|
|
|
"tsconfig.json",
|
|
|
|
"/apath",
|
|
|
|
["/apath/test.ts", "/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"],
|
|
|
|
["/apath/test.ts"]
|
|
|
|
)
|
2016-05-06 20:03:45 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("allow dotted files and folders when explicitly requested", () => {
|
|
|
|
assertParseFileList(
|
|
|
|
`{
|
|
|
|
"files": ["/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"]
|
|
|
|
}`,
|
|
|
|
"tsconfig.json",
|
|
|
|
"/apath",
|
|
|
|
["/apath/test.ts", "/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"],
|
|
|
|
["/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"]
|
|
|
|
)
|
|
|
|
});
|
2015-10-29 12:56:13 +01:00
|
|
|
});
|
|
|
|
}
|