Skip to content

Commit 713e545

Browse files
author
Yui
committed
Merge pull request microsoft#1675 from Microsoft/fixCurrentDirectoryForRwc
Pass information of RWC currentDirectory to the Harness
2 parents bfe63cc + 02b78da commit 713e545

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

src/harness/harness.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -810,14 +810,18 @@ module Harness {
810810
export function createCompilerHost(inputFiles: { unitName: string; content: string; }[],
811811
writeFile: (fn: string, contents: string, writeByteOrderMark: boolean) => void,
812812
scriptTarget: ts.ScriptTarget,
813-
useCaseSensitiveFileNames: boolean): ts.CompilerHost {
813+
useCaseSensitiveFileNames: boolean,
814+
// the currentDirectory is needed for rwcRunner to passed in specified current directory to compiler host
815+
currentDirectory?: string): ts.CompilerHost {
814816

815817
// Local get canonical file name function, that depends on passed in parameter for useCaseSensitiveFileNames
816818
function getCanonicalFileName(fileName: string): string {
817819
return useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
818820
}
819821

820822
var filemap: { [filename: string]: ts.SourceFile; } = {};
823+
var getCurrentDirectory = currentDirectory === undefined ? ts.sys.getCurrentDirectory : () => currentDirectory;
824+
821825
// Register input files
822826
function register(file: { unitName: string; content: string; }) {
823827
if (file.content !== undefined) {
@@ -828,11 +832,15 @@ module Harness {
828832
inputFiles.forEach(register);
829833

830834
return {
831-
getCurrentDirectory: ts.sys.getCurrentDirectory,
835+
getCurrentDirectory,
832836
getSourceFile: (fn, languageVersion) => {
833837
if (Object.prototype.hasOwnProperty.call(filemap, getCanonicalFileName(fn))) {
834838
return filemap[getCanonicalFileName(fn)];
835839
}
840+
else if (currentDirectory) {
841+
var canonicalAbsolutePath = getCanonicalFileName(ts.getNormalizedAbsolutePath(fn, currentDirectory));
842+
return Object.prototype.hasOwnProperty.call(filemap, getCanonicalFileName(canonicalAbsolutePath)) ? filemap[canonicalAbsolutePath] : undefined;
843+
}
836844
else if (fn === fourslashFilename) {
837845
var tsFn = 'tests/cases/fourslash/' + fourslashFilename;
838846
fourslashSourceFile = fourslashSourceFile || ts.createSourceFile(tsFn, Harness.IO.readFile(tsFn), scriptTarget);
@@ -909,7 +917,9 @@ module Harness {
909917
otherFiles: { unitName: string; content: string }[],
910918
onComplete: (result: CompilerResult, program: ts.Program) => void,
911919
settingsCallback?: (settings: ts.CompilerOptions) => void,
912-
options?: ts.CompilerOptions) {
920+
options?: ts.CompilerOptions,
921+
// Current directory is needed for rwcRunner to be able to use currentDirectory defined in json file
922+
currentDirectory?: string) {
913923

914924
options = options || { noResolve: false };
915925
options.target = options.target || ts.ScriptTarget.ES3;
@@ -1063,8 +1073,7 @@ module Harness {
10631073
var programFiles = inputFiles.map(file => file.unitName);
10641074
var program = ts.createProgram(programFiles, options, createCompilerHost(inputFiles.concat(otherFiles),
10651075
(fn, contents, writeByteOrderMark) => fileOutputs.push({ fileName: fn, code: contents, writeByteOrderMark: writeByteOrderMark }),
1066-
options.target,
1067-
useCaseSensitiveFileNames));
1076+
options.target, useCaseSensitiveFileNames, currentDirectory));
10681077

10691078
var checker = program.getTypeChecker(/*produceDiagnostics*/ true);
10701079

@@ -1095,7 +1104,9 @@ module Harness {
10951104
otherFiles: { unitName: string; content: string; }[],
10961105
result: CompilerResult,
10971106
settingsCallback?: (settings: ts.CompilerOptions) => void,
1098-
options?: ts.CompilerOptions) {
1107+
options?: ts.CompilerOptions,
1108+
// Current directory is needed for rwcRunner to be able to use currentDirectory defined in json file
1109+
currentDirectory?: string) {
10991110
if (options.declaration && result.errors.length === 0 && result.declFilesCode.length !== result.files.length) {
11001111
throw new Error('There were no errors and declFiles generated did not match number of js files generated');
11011112
}
@@ -1108,9 +1119,8 @@ module Harness {
11081119

11091120
ts.forEach(inputFiles, file => addDtsFile(file, declInputFiles));
11101121
ts.forEach(otherFiles, file => addDtsFile(file, declOtherFiles));
1111-
this.compileFiles(declInputFiles, declOtherFiles, function (compileResult) {
1112-
declResult = compileResult;
1113-
}, settingsCallback, options);
1122+
this.compileFiles(declInputFiles, declOtherFiles, function (compileResult) { declResult = compileResult; },
1123+
settingsCallback, options, currentDirectory);
11141124

11151125
return { declInputFiles, declOtherFiles, declResult };
11161126
}

src/harness/rwcRunner.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module RWC {
2828
var compilerOptions: ts.CompilerOptions;
2929
var baselineOpts: Harness.Baseline.BaselineOptions = { Subfolder: 'rwc' };
3030
var baseName = /(.*)\/(.*).json/.exec(ts.normalizeSlashes(jsonPath))[2];
31+
var currentDirectory: string;
3132

3233
after(() => {
3334
// Mocha holds onto the closure environment of the describe callback even after the test is done.
@@ -38,21 +39,22 @@ module RWC {
3839
compilerOptions = undefined;
3940
baselineOpts = undefined;
4041
baseName = undefined;
42+
currentDirectory = undefined;
4143
});
4244

4345
it('can compile', () => {
4446
var harnessCompiler = Harness.Compiler.getCompiler();
4547
var opts: ts.ParsedCommandLine;
4648

4749
var ioLog: IOLog = JSON.parse(Harness.IO.readFile(jsonPath));
50+
currentDirectory = ioLog.currentDirectory;
4851
runWithIOLog(ioLog, () => {
4952
opts = ts.parseCommandLine(ioLog.arguments);
5053
assert.equal(opts.errors.length, 0);
5154
});
5255

5356
runWithIOLog(ioLog, () => {
5457
harnessCompiler.reset();
55-
5658
// Load the files
5759
ts.forEach(opts.filenames, fileName => {
5860
inputFiles.push(getHarnessCompilerInputUnit(fileName));
@@ -81,7 +83,11 @@ module RWC {
8183
// Emit the results
8284
compilerOptions = harnessCompiler.compileFiles(inputFiles, otherFiles, compileResult => {
8385
compilerResult = compileResult;
84-
}, /*settingsCallback*/ undefined, opts.options);
86+
},
87+
/*settingsCallback*/ undefined, opts.options,
88+
// Since all Rwc json file specified current directory in its json file, we need to pass this information to compilerHost
89+
// so that when the host is asked for current directory, it should give the value from json rather than from process
90+
currentDirectory);
8591
});
8692

8793
function getHarnessCompilerInputUnit(fileName: string) {
@@ -145,7 +151,8 @@ module RWC {
145151
it('has the expected errors in generated declaration files', () => {
146152
if (compilerOptions.declaration && !compilerResult.errors.length) {
147153
Harness.Baseline.runBaseline('has the expected errors in generated declaration files', baseName + '.dts.errors.txt', () => {
148-
var declFileCompilationResult = Harness.Compiler.getCompiler().compileDeclarationFiles(inputFiles, otherFiles, compilerResult, /*settingscallback*/ undefined, compilerOptions);
154+
var declFileCompilationResult = Harness.Compiler.getCompiler().compileDeclarationFiles(inputFiles, otherFiles, compilerResult,
155+
/*settingscallback*/ undefined, compilerOptions, currentDirectory);
149156
if (declFileCompilationResult.declResult.errors.length === 0) {
150157
return null;
151158
}

0 commit comments

Comments
 (0)