• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace project {
2    // Test case is json of below type in tests/cases/project/
3    interface ProjectRunnerTestCase {
4        scenario: string;
5        projectRoot: string; // project where it lives - this also is the current directory when compiling
6        inputFiles: readonly string[]; // list of input files to be given to program
7        resolveMapRoot?: boolean; // should we resolve this map root and give compiler the absolute disk path as map root?
8        resolveSourceRoot?: boolean; // should we resolve this source root and give compiler the absolute disk path as map root?
9        baselineCheck?: boolean; // Verify the baselines of output files, if this is false, we will write to output to the disk but there is no verification of baselines
10        runTest?: boolean; // Run the resulting test
11        bug?: string; // If there is any bug associated with this test case
12    }
13
14    interface ProjectRunnerTestCaseResolutionInfo extends ProjectRunnerTestCase {
15        // Apart from actual test case the results of the resolution
16        resolvedInputFiles: readonly string[]; // List of files that were asked to read by compiler
17        emittedFiles: readonly string[]; // List of files that were emitted by the compiler
18    }
19
20    interface CompileProjectFilesResult {
21        configFileSourceFiles: readonly ts.SourceFile[];
22        moduleKind: ts.ModuleKind;
23        program?: ts.Program;
24        compilerOptions?: ts.CompilerOptions;
25        errors: readonly ts.Diagnostic[];
26        sourceMapData?: readonly ts.SourceMapEmitResult[];
27    }
28
29    interface BatchCompileProjectTestCaseResult extends CompileProjectFilesResult {
30        outputFiles?: readonly documents.TextDocument[];
31    }
32
33    export class ProjectRunner extends Harness.RunnerBase {
34        public enumerateTestFiles() {
35            const all = this.enumerateFiles("tests/cases/project", /\.json$/, { recursive: true });
36            if (Harness.shards === 1) {
37                return all;
38            }
39            return all.filter((_val, idx) => idx % Harness.shards === (Harness.shardId - 1));
40        }
41
42        public kind(): Harness.TestRunnerKind {
43            return "project";
44        }
45
46        public initializeTests() {
47            describe("projects tests", () => {
48                const tests = this.tests.length === 0 ? this.enumerateTestFiles() : this.tests;
49                for (const test of tests) {
50                    this.runProjectTestCase(typeof test === "string" ? test : test.file);
51                }
52            });
53        }
54
55        private runProjectTestCase(testCaseFileName: string) {
56            for (const { name, payload } of ProjectTestCase.getConfigurations(testCaseFileName)) {
57                describe("Compiling project for " + payload.testCase.scenario + ": testcase " + testCaseFileName + (name ? ` (${name})` : ``), () => {
58                    let projectTestCase: ProjectTestCase | undefined;
59                    before(() => { projectTestCase = new ProjectTestCase(testCaseFileName, payload); });
60                    it(`Correct module resolution tracing for ${testCaseFileName}`, () => projectTestCase && projectTestCase.verifyResolution());
61                    it(`Correct errors for ${testCaseFileName}`, () => projectTestCase && projectTestCase.verifyDiagnostics());
62                    it(`Correct JS output for ${testCaseFileName}`, () => projectTestCase && projectTestCase.verifyJavaScriptOutput());
63                    // NOTE: This check was commented out in previous code. Leaving this here to eventually be restored if needed.
64                    // it(`Correct sourcemap content for ${testCaseFileName}`, () => projectTestCase && projectTestCase.verifySourceMapRecord());
65                    it(`Correct declarations for ${testCaseFileName}`, () => projectTestCase && projectTestCase.verifyDeclarations());
66                    after(() => { projectTestCase = undefined; });
67                });
68            }
69        }
70    }
71
72    class ProjectCompilerHost extends fakes.CompilerHost {
73        private _testCase: ProjectRunnerTestCase & ts.CompilerOptions;
74        private _projectParseConfigHost: ProjectParseConfigHost | undefined;
75
76        constructor(sys: fakes.System | vfs.FileSystem, compilerOptions: ts.CompilerOptions, _testCaseJustName: string, testCase: ProjectRunnerTestCase & ts.CompilerOptions, _moduleKind: ts.ModuleKind) {
77            super(sys, compilerOptions);
78            this._testCase = testCase;
79        }
80
81        public get parseConfigHost(): fakes.ParseConfigHost {
82            return this._projectParseConfigHost || (this._projectParseConfigHost = new ProjectParseConfigHost(this.sys, this._testCase));
83        }
84
85        public getDefaultLibFileName(_options: ts.CompilerOptions) {
86            return vpath.resolve(this.getDefaultLibLocation(), "lib.es5.d.ts");
87        }
88    }
89
90    class ProjectParseConfigHost extends fakes.ParseConfigHost {
91        private _testCase: ProjectRunnerTestCase & ts.CompilerOptions;
92
93        constructor(sys: fakes.System, testCase: ProjectRunnerTestCase & ts.CompilerOptions) {
94            super(sys);
95            this._testCase = testCase;
96        }
97
98        public readDirectory(path: string, extensions: string[], excludes: string[], includes: string[], depth: number): string[] {
99            const result = super.readDirectory(path, extensions, excludes, includes, depth);
100            const projectRoot = vpath.resolve(vfs.srcFolder, this._testCase.projectRoot);
101            return result.map(item => vpath.relative(
102                projectRoot,
103                vpath.resolve(projectRoot, item),
104                this.vfs.ignoreCase
105            ));
106        }
107    }
108
109    interface ProjectTestConfiguration {
110        name: string;
111        payload: ProjectTestPayload;
112    }
113
114    interface ProjectTestPayload {
115        testCase: ProjectRunnerTestCase & ts.CompilerOptions;
116        moduleKind: ts.ModuleKind;
117        vfs: vfs.FileSystem;
118    }
119
120    class ProjectTestCase {
121        private testCase: ProjectRunnerTestCase & ts.CompilerOptions;
122        private testCaseJustName: string;
123        private sys: fakes.System;
124        private compilerOptions: ts.CompilerOptions;
125        private compilerResult: BatchCompileProjectTestCaseResult;
126
127        constructor(testCaseFileName: string, { testCase, moduleKind, vfs }: ProjectTestPayload) {
128            this.testCase = testCase;
129            this.testCaseJustName = testCaseFileName.replace(/^.*[\\\/]/, "").replace(/\.json/, "");
130            this.compilerOptions = createCompilerOptions(testCase, moduleKind);
131            this.sys = new fakes.System(vfs);
132
133            let configFileName: string | undefined;
134            let inputFiles = testCase.inputFiles;
135            if (this.compilerOptions.project) {
136                // Parse project
137                configFileName = ts.normalizePath(ts.combinePaths(this.compilerOptions.project, "tsconfig.json"));
138                assert(!inputFiles || inputFiles.length === 0, "cannot specify input files and project option together");
139            }
140            else if (!inputFiles || inputFiles.length === 0) {
141                configFileName = ts.findConfigFile("", path => this.sys.fileExists(path));
142            }
143
144            let errors: ts.Diagnostic[] | undefined;
145            const configFileSourceFiles: ts.SourceFile[] = [];
146            if (configFileName) {
147                const result = ts.readJsonConfigFile(configFileName, path => this.sys.readFile(path));
148                configFileSourceFiles.push(result);
149                const configParseHost = new ProjectParseConfigHost(this.sys, this.testCase);
150                const configParseResult = ts.parseJsonSourceFileConfigFileContent(result, configParseHost, ts.getDirectoryPath(configFileName), this.compilerOptions);
151                inputFiles = configParseResult.fileNames;
152                this.compilerOptions = configParseResult.options;
153                errors = [...result.parseDiagnostics, ...configParseResult.errors];
154            }
155
156            const compilerHost = new ProjectCompilerHost(this.sys, this.compilerOptions, this.testCaseJustName, this.testCase, moduleKind);
157            const projectCompilerResult = this.compileProjectFiles(moduleKind, configFileSourceFiles, () => inputFiles, compilerHost, this.compilerOptions);
158
159            this.compilerResult = {
160                configFileSourceFiles,
161                moduleKind,
162                program: projectCompilerResult.program,
163                compilerOptions: this.compilerOptions,
164                sourceMapData: projectCompilerResult.sourceMapData,
165                outputFiles: compilerHost.outputs,
166                errors: errors ? ts.concatenate(errors, projectCompilerResult.errors) : projectCompilerResult.errors,
167            };
168        }
169
170        private get vfs() {
171            return this.sys.vfs;
172        }
173
174        public static getConfigurations(testCaseFileName: string): ProjectTestConfiguration[] {
175            let testCase: ProjectRunnerTestCase & ts.CompilerOptions;
176
177            let testFileText: string | undefined;
178            try {
179                testFileText = Harness.IO.readFile(testCaseFileName);
180            }
181            catch (e) {
182                assert(false, "Unable to open testcase file: " + testCaseFileName + ": " + e.message);
183            }
184
185            try {
186                testCase = <ProjectRunnerTestCase & ts.CompilerOptions>JSON.parse(testFileText!);
187            }
188            catch (e) {
189                throw assert(false, "Testcase: " + testCaseFileName + " does not contain valid json format: " + e.message);
190            }
191
192            const fs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false);
193            fs.mountSync(vpath.resolve(Harness.IO.getWorkspaceRoot(), "tests"), vpath.combine(vfs.srcFolder, "tests"), vfs.createResolver(Harness.IO));
194            fs.mkdirpSync(vpath.combine(vfs.srcFolder, testCase.projectRoot));
195            fs.chdir(vpath.combine(vfs.srcFolder, testCase.projectRoot));
196            fs.makeReadonly();
197
198            return [
199                { name: `@module: commonjs`, payload: { testCase, moduleKind: ts.ModuleKind.CommonJS, vfs: fs } },
200                { name: `@module: amd`, payload: { testCase, moduleKind: ts.ModuleKind.AMD, vfs: fs } }
201            ];
202        }
203
204        public verifyResolution() {
205            const cwd = this.vfs.cwd();
206            const ignoreCase = this.vfs.ignoreCase;
207            const resolutionInfo: ProjectRunnerTestCaseResolutionInfo & ts.CompilerOptions = JSON.parse(JSON.stringify(this.testCase));
208            resolutionInfo.resolvedInputFiles = this.compilerResult.program!.getSourceFiles()
209                .map(({ fileName: input }) =>
210                    vpath.beneath(vfs.builtFolder, input, this.vfs.ignoreCase) || vpath.beneath(vfs.testLibFolder, input, this.vfs.ignoreCase) ? Utils.removeTestPathPrefixes(input) :
211                    vpath.isAbsolute(input) ? vpath.relative(cwd, input, ignoreCase) :
212                    input);
213
214            resolutionInfo.emittedFiles = this.compilerResult.outputFiles!
215                .map(output => output.meta.get("fileName") || output.file)
216                .map(output => Utils.removeTestPathPrefixes(vpath.isAbsolute(output) ? vpath.relative(cwd, output, ignoreCase) : output));
217
218            const content = JSON.stringify(resolutionInfo, undefined, "    ");
219            Harness.Baseline.runBaseline(this.getBaselineFolder(this.compilerResult.moduleKind) + this.testCaseJustName + ".json", content);
220        }
221
222        public verifyDiagnostics() {
223            if (this.compilerResult.errors.length) {
224                Harness.Baseline.runBaseline(this.getBaselineFolder(this.compilerResult.moduleKind) + this.testCaseJustName + ".errors.txt", getErrorsBaseline(this.compilerResult));
225            }
226        }
227
228        public verifyJavaScriptOutput() {
229            if (this.testCase.baselineCheck) {
230                const errs: Error[] = [];
231                let nonSubfolderDiskFiles = 0;
232                for (const output of this.compilerResult.outputFiles!) {
233                    try {
234                        // convert file name to rooted name
235                        // if filename is not rooted - concat it with project root and then expand project root relative to current directory
236                        const fileName = output.meta.get("fileName") || output.file;
237                        const diskFileName = vpath.isAbsolute(fileName) ? fileName : vpath.resolve(this.vfs.cwd(), fileName);
238
239                        // compute file name relative to current directory (expanded project root)
240                        let diskRelativeName = vpath.relative(this.vfs.cwd(), diskFileName, this.vfs.ignoreCase);
241                        if (vpath.isAbsolute(diskRelativeName) || diskRelativeName.startsWith("../")) {
242                            // If the generated output file resides in the parent folder or is rooted path,
243                            // we need to instead create files that can live in the project reference folder
244                            // but make sure extension of these files matches with the fileName the compiler asked to write
245                            diskRelativeName = `diskFile${nonSubfolderDiskFiles}${vpath.extname(fileName, [".js.map", ".js", ".d.ts"], this.vfs.ignoreCase)}`;
246                            nonSubfolderDiskFiles++;
247                        }
248
249                        const content = Utils.removeTestPathPrefixes(output.text, /*retainTrailingDirectorySeparator*/ true);
250                        Harness.Baseline.runBaseline(this.getBaselineFolder(this.compilerResult.moduleKind) + diskRelativeName, content as string | null); // TODO: GH#18217
251                    }
252                    catch (e) {
253                        errs.push(e);
254                    }
255                }
256
257                if (errs.length) {
258                    throw Error(errs.join("\n     "));
259                }
260            }
261        }
262
263        public verifySourceMapRecord() {
264            // NOTE: This check was commented out in previous code. Leaving this here to eventually be restored if needed.
265            // if (compilerResult.sourceMapData) {
266            //     Harness.Baseline.runBaseline(getBaselineFolder(compilerResult.moduleKind) + testCaseJustName + ".sourcemap.txt", () => {
267            //         return Harness.SourceMapRecorder.getSourceMapRecord(compilerResult.sourceMapData, compilerResult.program,
268            //             ts.filter(compilerResult.outputFiles, outputFile => Harness.Compiler.isJS(outputFile.emittedFileName)));
269            //     });
270            // }
271        }
272
273        public verifyDeclarations() {
274            if (!this.compilerResult.errors.length && this.testCase.declaration) {
275                const dTsCompileResult = this.compileDeclarations(this.compilerResult);
276                if (dTsCompileResult && dTsCompileResult.errors.length) {
277                    Harness.Baseline.runBaseline(this.getBaselineFolder(this.compilerResult.moduleKind) + this.testCaseJustName + ".dts.errors.txt", getErrorsBaseline(dTsCompileResult));
278                }
279            }
280        }
281
282        // Project baselines verified go in project/testCaseName/moduleKind/
283        private getBaselineFolder(moduleKind: ts.ModuleKind) {
284            return "project/" + this.testCaseJustName + "/" + moduleNameToString(moduleKind) + "/";
285        }
286
287        private cleanProjectUrl(url: string) {
288            let diskProjectPath = ts.normalizeSlashes(Harness.IO.resolvePath(this.testCase.projectRoot)!);
289            let projectRootUrl = "file:///" + diskProjectPath;
290            const normalizedProjectRoot = ts.normalizeSlashes(this.testCase.projectRoot);
291            diskProjectPath = diskProjectPath.substr(0, diskProjectPath.lastIndexOf(normalizedProjectRoot));
292            projectRootUrl = projectRootUrl.substr(0, projectRootUrl.lastIndexOf(normalizedProjectRoot));
293            if (url && url.length) {
294                if (url.indexOf(projectRootUrl) === 0) {
295                    // replace the disk specific project url path into project root url
296                    url = "file:///" + url.substr(projectRootUrl.length);
297                }
298                else if (url.indexOf(diskProjectPath) === 0) {
299                    // Replace the disk specific path into the project root path
300                    url = url.substr(diskProjectPath.length);
301                    if (url.charCodeAt(0) !== ts.CharacterCodes.slash) {
302                        url = "/" + url;
303                    }
304                }
305            }
306
307            return url;
308        }
309
310        private compileProjectFiles(moduleKind: ts.ModuleKind, configFileSourceFiles: readonly ts.SourceFile[],
311            getInputFiles: () => readonly string[],
312            compilerHost: ts.CompilerHost,
313            compilerOptions: ts.CompilerOptions): CompileProjectFilesResult {
314
315            const program = ts.createProgram(getInputFiles(), compilerOptions, compilerHost);
316            const errors = ts.getPreEmitDiagnostics(program);
317
318            const { sourceMaps: sourceMapData, diagnostics: emitDiagnostics } = program.emit();
319
320            // Clean up source map data that will be used in baselining
321            if (sourceMapData) {
322                for (const data of sourceMapData) {
323                    data.sourceMap = {
324                        ...data.sourceMap,
325                        sources: data.sourceMap.sources.map(source => this.cleanProjectUrl(source)),
326                        sourceRoot: data.sourceMap.sourceRoot && this.cleanProjectUrl(data.sourceMap.sourceRoot)
327                    };
328                }
329            }
330
331            return {
332                configFileSourceFiles,
333                moduleKind,
334                program,
335                errors: ts.concatenate(errors, emitDiagnostics),
336                sourceMapData
337            };
338        }
339
340        private compileDeclarations(compilerResult: BatchCompileProjectTestCaseResult) {
341            if (!compilerResult.program) {
342                return;
343            }
344
345            const compilerOptions = compilerResult.program.getCompilerOptions();
346            const allInputFiles: documents.TextDocument[] = [];
347            const rootFiles: string[] = [];
348            ts.forEach(compilerResult.program.getSourceFiles(), sourceFile => {
349                if (sourceFile.isDeclarationFile) {
350                    if (!vpath.isDefaultLibrary(sourceFile.fileName)) {
351                        allInputFiles.unshift(new documents.TextDocument(sourceFile.fileName, sourceFile.text));
352                    }
353                    rootFiles.unshift(sourceFile.fileName);
354                }
355                else if (!(compilerOptions.outFile || compilerOptions.out)) {
356                    let emitOutputFilePathWithoutExtension: string | undefined;
357                    if (compilerOptions.outDir) {
358                        let sourceFilePath = ts.getNormalizedAbsolutePath(sourceFile.fileName, compilerResult.program!.getCurrentDirectory());
359                        sourceFilePath = sourceFilePath.replace(compilerResult.program!.getCommonSourceDirectory(), "");
360                        emitOutputFilePathWithoutExtension = ts.removeFileExtension(ts.combinePaths(compilerOptions.outDir, sourceFilePath));
361                    }
362                    else {
363                        emitOutputFilePathWithoutExtension = ts.removeFileExtension(sourceFile.fileName);
364                    }
365
366                    const outputDtsFileName = emitOutputFilePathWithoutExtension + ts.Extension.Dts;
367                    const file = findOutputDtsFile(outputDtsFileName);
368                    if (file) {
369                        allInputFiles.unshift(file);
370                        rootFiles.unshift(file.meta.get("fileName") || file.file);
371                    }
372                }
373                else {
374                    const outputDtsFileName = ts.removeFileExtension(compilerOptions.outFile || compilerOptions.out!) + ts.Extension.Dts;
375                    const outputDtsFile = findOutputDtsFile(outputDtsFileName)!;
376                    if (!ts.contains(allInputFiles, outputDtsFile)) {
377                        allInputFiles.unshift(outputDtsFile);
378                        rootFiles.unshift(outputDtsFile.meta.get("fileName") || outputDtsFile.file);
379                    }
380                }
381            });
382
383            const _vfs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false, {
384                documents: allInputFiles,
385                cwd: vpath.combine(vfs.srcFolder, this.testCase.projectRoot)
386            });
387
388            // Dont allow config files since we are compiling existing source options
389            const compilerHost = new ProjectCompilerHost(_vfs, compilerResult.compilerOptions!, this.testCaseJustName, this.testCase, compilerResult.moduleKind);
390            return this.compileProjectFiles(compilerResult.moduleKind, compilerResult.configFileSourceFiles, () => rootFiles, compilerHost, compilerResult.compilerOptions!);
391
392            function findOutputDtsFile(fileName: string) {
393                return ts.forEach(compilerResult.outputFiles, outputFile => outputFile.meta.get("fileName") === fileName ? outputFile : undefined);
394            }
395        }
396    }
397
398    function moduleNameToString(moduleKind: ts.ModuleKind) {
399        return moduleKind === ts.ModuleKind.AMD ? "amd" :
400            moduleKind === ts.ModuleKind.CommonJS ? "node" : "none";
401    }
402
403    function getErrorsBaseline(compilerResult: CompileProjectFilesResult) {
404        const inputSourceFiles = compilerResult.configFileSourceFiles.slice();
405        if (compilerResult.program) {
406            for (const sourceFile of compilerResult.program.getSourceFiles()) {
407                if (!Harness.isDefaultLibraryFile(sourceFile.fileName)) {
408                    inputSourceFiles.push(sourceFile);
409                }
410            }
411        }
412
413        const inputFiles = inputSourceFiles.map<Harness.Compiler.TestFile>(sourceFile => ({
414            unitName: ts.isRootedDiskPath(sourceFile.fileName) ?
415                Harness.RunnerBase.removeFullPaths(sourceFile.fileName) :
416                sourceFile.fileName,
417            content: sourceFile.text
418        }));
419
420        return Harness.Compiler.getErrorBaseline(inputFiles, compilerResult.errors);
421    }
422
423    function createCompilerOptions(testCase: ProjectRunnerTestCase & ts.CompilerOptions, moduleKind: ts.ModuleKind) {
424        // Set the special options that depend on other testcase options
425        const compilerOptions: ts.CompilerOptions = {
426            noErrorTruncation: false,
427            skipDefaultLibCheck: false,
428            moduleResolution: ts.ModuleResolutionKind.Classic,
429            module: moduleKind,
430            newLine: ts.NewLineKind.CarriageReturnLineFeed,
431            mapRoot: testCase.resolveMapRoot && testCase.mapRoot
432                ? vpath.resolve(vfs.srcFolder, testCase.mapRoot)
433                : testCase.mapRoot,
434
435            sourceRoot: testCase.resolveSourceRoot && testCase.sourceRoot
436                ? vpath.resolve(vfs.srcFolder, testCase.sourceRoot)
437                : testCase.sourceRoot
438        };
439
440        // Set the values specified using json
441        const optionNameMap = ts.arrayToMap(ts.optionDeclarations, option => option.name);
442        for (const name in testCase) {
443            if (name !== "mapRoot" && name !== "sourceRoot") {
444                const option = optionNameMap.get(name);
445                if (option) {
446                    const optType = option.type;
447                    let value = <any>testCase[name];
448                    if (!ts.isString(optType)) {
449                        const key = value.toLowerCase();
450                        const optTypeValue = optType.get(key);
451                        if (optTypeValue) {
452                            value = optTypeValue;
453                        }
454                    }
455                    compilerOptions[option.name] = value;
456                }
457            }
458        }
459
460        return compilerOptions;
461    }
462}
463