Skip to content

Commit 6e8957d

Browse files
author
Yui T
committed
Pass information of rwc's currentDirectory to the compiler host in the Harness
1 parent d391a8a commit 6e8957d

2 files changed

Lines changed: 28 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: 9 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.
@@ -45,14 +46,14 @@ module RWC {
4546
var opts: ts.ParsedCommandLine;
4647

4748
var ioLog: IOLog = JSON.parse(Harness.IO.readFile(jsonPath));
49+
currentDirectory = ioLog.currentDirectory;
4850
runWithIOLog(ioLog, () => {
4951
opts = ts.parseCommandLine(ioLog.arguments);
5052
assert.equal(opts.errors.length, 0);
5153
});
5254

5355
runWithIOLog(ioLog, () => {
5456
harnessCompiler.reset();
55-
5657
// Load the files
5758
ts.forEach(opts.filenames, fileName => {
5859
inputFiles.push(getHarnessCompilerInputUnit(fileName));
@@ -81,7 +82,11 @@ module RWC {
8182
// Emit the results
8283
compilerOptions = harnessCompiler.compileFiles(inputFiles, otherFiles, compileResult => {
8384
compilerResult = compileResult;
84-
}, /*settingsCallback*/ undefined, opts.options);
85+
},
86+
/*settingsCallback*/ undefined, opts.options,
87+
// Since all Rwc json file specified current directory in its json file, we need to pass this information to compilerHost
88+
// so that when the host is asked for current directory, it should give the value from json rather than from process
89+
currentDirectory);
8590
});
8691

8792
function getHarnessCompilerInputUnit(fileName: string) {
@@ -145,7 +150,8 @@ module RWC {
145150
it('has the expected errors in generated declaration files', () => {
146151
if (compilerOptions.declaration && !compilerResult.errors.length) {
147152
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);
153+
var declFileCompilationResult = Harness.Compiler.getCompiler().compileDeclarationFiles(inputFiles, otherFiles, compilerResult,
154+
/*settingscallback*/ undefined, compilerOptions, currentDirectory);
149155
if (declFileCompilationResult.declResult.errors.length === 0) {
150156
return null;
151157
}

0 commit comments

Comments
 (0)