Added environment variable support to tests

This commit is contained in:
Ron Buckton
2016-03-21 11:58:34 -07:00
parent 02ebfa5d11
commit 92fbe6b231
3 changed files with 28 additions and 23 deletions

View File

@@ -568,6 +568,10 @@ namespace Harness.LanguageService {
return this.host.getCurrentDirectory();
}
getEnvironmentVariable(name: string): string {
return ts.sys.getEnvironmentVariable(name);
}
readDirectory(path: string, extension?: string): string[] {
throw new Error("Not implemented Yet.");
}
@@ -644,4 +648,3 @@ namespace Harness.LanguageService {
getPreProcessedFileInfo(fileName: string, fileContents: string): ts.PreProcessedFileInfo { throw new Error("getPreProcessedFileInfo is not available using the server interface."); }
}
}

View File

@@ -46,6 +46,7 @@ module ts {
getCurrentDirectory: (): string => {
return "";
},
getEnvironmentVariable: (name: string) => "",
readDirectory: (path: string, extension?: string, exclude?: string[]): string[] => {
throw new Error("NYI");
},
@@ -79,8 +80,8 @@ module ts {
let projectService = new server.ProjectService(serverHost, logger);
let rootScriptInfo = projectService.openFile(rootFile, /* openedByClient */true);
let project = projectService.createInferredProject(rootScriptInfo);
project.setProjectOptions( {files: [rootScriptInfo.fileName], compilerOptions: {module: ts.ModuleKind.AMD} } );
return {
project.setProjectOptions( {files: [rootScriptInfo.fileName], compilerOptions: {module: ts.ModuleKind.AMD} } );
return {
project,
rootScriptInfo
};
@@ -97,22 +98,22 @@ module ts {
name: "c:/f1.ts",
content: `foo()`
};
let serverHost = createDefaultServerHost({ [root.name]: root, [imported.name]: imported });
let { project, rootScriptInfo } = createProject(root.name, serverHost);
// ensure that imported file was found
let diags = project.compilerService.languageService.getSemanticDiagnostics(imported.name);
assert.equal(diags.length, 1);
let originalFileExists = serverHost.fileExists;
{
// patch fileExists to make sure that disk is not touched
serverHost.fileExists = (fileName): boolean => {
assert.isTrue(false, "fileExists should not be called");
assert.isTrue(false, "fileExists should not be called");
return false;
};
let newContent = `import {x} from "f1"
var x: string = 1;`;
rootScriptInfo.editContent(0, rootScriptInfo.content.length, newContent);
@@ -133,7 +134,7 @@ module ts {
};
let newContent = `import {x} from "f2"`;
rootScriptInfo.editContent(0, rootScriptInfo.content.length, newContent);
try {
// trigger synchronization to make sure that LSHost will try to find 'f2' module on disk
project.compilerService.languageService.getSemanticDiagnostics(imported.name);
@@ -142,7 +143,7 @@ module ts {
catch(e) {
assert.isTrue(e.message.indexOf(`Could not find file: '${imported.name}'.`) === 0);
}
assert.isTrue(fileExistsIsCalled);
}
{
@@ -150,45 +151,45 @@ module ts {
serverHost.fileExists = (fileName): boolean => {
if (fileName === "lib.d.ts") {
return false;
}
}
fileExistsCalled = true;
assert.isTrue(fileName.indexOf('/f1.') !== -1);
return originalFileExists(fileName);
};
let newContent = `import {x} from "f1"`;
rootScriptInfo.editContent(0, rootScriptInfo.content.length, newContent);
project.compilerService.languageService.getSemanticDiagnostics(imported.name);
assert.isTrue(fileExistsCalled);
// setting compiler options discards module resolution cache
fileExistsCalled = false;
let opts = ts.clone(project.projectOptions);
opts.compilerOptions = ts.clone(opts.compilerOptions);
opts.compilerOptions.target = ts.ScriptTarget.ES5;
project.setProjectOptions(opts);
project.compilerService.languageService.getSemanticDiagnostics(imported.name);
assert.isTrue(fileExistsCalled);
}
});
it("loads missing files from disk", () => {
let root: File = {
name: 'c:/foo.ts',
content: `import {x} from "bar"`
};
let imported: File = {
name: 'c:/bar.d.ts',
content: `export var y = 1`
};
};
let fileMap: Map<File> = { [root.name]: root };
let serverHost = createDefaultServerHost(fileMap);
let originalFileExists = serverHost.fileExists;
let fileExistsCalledForBar = false;
serverHost.fileExists = fileName => {
if (fileName === "lib.d.ts") {
@@ -197,22 +198,22 @@ module ts {
if (!fileExistsCalledForBar) {
fileExistsCalledForBar = fileName.indexOf("/bar.") !== -1;
}
return originalFileExists(fileName);
};
let { project, rootScriptInfo } = createProject(root.name, serverHost);
let diags = project.compilerService.languageService.getSemanticDiagnostics(root.name);
assert.isTrue(fileExistsCalledForBar, "'fileExists' should be called");
assert.isTrue(diags.length === 1, "one diagnostic expected");
assert.isTrue(typeof diags[0].messageText === "string" && ((<string>diags[0].messageText).indexOf("Cannot find module") === 0), "should be 'cannot find module' message");
// assert that import will success once file appear on disk
fileMap[imported.name] = imported;
fileExistsCalledForBar = false;
rootScriptInfo.editContent(0, rootScriptInfo.content.length, `import {y} from "bar"`)
diags = project.compilerService.languageService.getSemanticDiagnostics(root.name);
assert.isTrue(fileExistsCalledForBar, "'fileExists' should be called");
assert.isTrue(diags.length === 0);

View File

@@ -17,6 +17,7 @@ namespace ts.server {
createDirectory(): void {},
getExecutingFilePath(): string { return void 0; },
getCurrentDirectory(): string { return void 0; },
getEnvironmentVariable(name: string): string { return ""; },
readDirectory(): string[] { return []; },
exit(): void {}
};