Skip to content

Commit 92fbe6b

Browse files
committed
Added environment variable support to tests
1 parent 02ebfa5 commit 92fbe6b

3 files changed

Lines changed: 28 additions & 23 deletions

File tree

src/harness/harnessLanguageService.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,10 @@ namespace Harness.LanguageService {
568568
return this.host.getCurrentDirectory();
569569
}
570570

571+
getEnvironmentVariable(name: string): string {
572+
return ts.sys.getEnvironmentVariable(name);
573+
}
574+
571575
readDirectory(path: string, extension?: string): string[] {
572576
throw new Error("Not implemented Yet.");
573577
}
@@ -644,4 +648,3 @@ namespace Harness.LanguageService {
644648
getPreProcessedFileInfo(fileName: string, fileContents: string): ts.PreProcessedFileInfo { throw new Error("getPreProcessedFileInfo is not available using the server interface."); }
645649
}
646650
}
647-

tests/cases/unittests/cachingInServerLSHost.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module ts {
4646
getCurrentDirectory: (): string => {
4747
return "";
4848
},
49+
getEnvironmentVariable: (name: string) => "",
4950
readDirectory: (path: string, extension?: string, exclude?: string[]): string[] => {
5051
throw new Error("NYI");
5152
},
@@ -79,8 +80,8 @@ module ts {
7980
let projectService = new server.ProjectService(serverHost, logger);
8081
let rootScriptInfo = projectService.openFile(rootFile, /* openedByClient */true);
8182
let project = projectService.createInferredProject(rootScriptInfo);
82-
project.setProjectOptions( {files: [rootScriptInfo.fileName], compilerOptions: {module: ts.ModuleKind.AMD} } );
83-
return {
83+
project.setProjectOptions( {files: [rootScriptInfo.fileName], compilerOptions: {module: ts.ModuleKind.AMD} } );
84+
return {
8485
project,
8586
rootScriptInfo
8687
};
@@ -97,22 +98,22 @@ module ts {
9798
name: "c:/f1.ts",
9899
content: `foo()`
99100
};
100-
101+
101102
let serverHost = createDefaultServerHost({ [root.name]: root, [imported.name]: imported });
102103
let { project, rootScriptInfo } = createProject(root.name, serverHost);
103104

104105
// ensure that imported file was found
105106
let diags = project.compilerService.languageService.getSemanticDiagnostics(imported.name);
106107
assert.equal(diags.length, 1);
107-
108+
108109
let originalFileExists = serverHost.fileExists;
109110
{
110111
// patch fileExists to make sure that disk is not touched
111112
serverHost.fileExists = (fileName): boolean => {
112-
assert.isTrue(false, "fileExists should not be called");
113+
assert.isTrue(false, "fileExists should not be called");
113114
return false;
114115
};
115-
116+
116117
let newContent = `import {x} from "f1"
117118
var x: string = 1;`;
118119
rootScriptInfo.editContent(0, rootScriptInfo.content.length, newContent);
@@ -133,7 +134,7 @@ module ts {
133134
};
134135
let newContent = `import {x} from "f2"`;
135136
rootScriptInfo.editContent(0, rootScriptInfo.content.length, newContent);
136-
137+
137138
try {
138139
// trigger synchronization to make sure that LSHost will try to find 'f2' module on disk
139140
project.compilerService.languageService.getSemanticDiagnostics(imported.name);
@@ -142,53 +143,53 @@ module ts {
142143
catch(e) {
143144
assert.isTrue(e.message.indexOf(`Could not find file: '${imported.name}'.`) === 0);
144145
}
145-
146+
146147
assert.isTrue(fileExistsIsCalled);
147148
}
148149
{
149150
let fileExistsCalled = false;
150151
serverHost.fileExists = (fileName): boolean => {
151152
if (fileName === "lib.d.ts") {
152153
return false;
153-
}
154+
}
154155
fileExistsCalled = true;
155156
assert.isTrue(fileName.indexOf('/f1.') !== -1);
156157
return originalFileExists(fileName);
157158
};
158-
159+
159160
let newContent = `import {x} from "f1"`;
160161
rootScriptInfo.editContent(0, rootScriptInfo.content.length, newContent);
161162
project.compilerService.languageService.getSemanticDiagnostics(imported.name);
162163
assert.isTrue(fileExistsCalled);
163-
164+
164165
// setting compiler options discards module resolution cache
165166
fileExistsCalled = false;
166-
167+
167168
let opts = ts.clone(project.projectOptions);
168169
opts.compilerOptions = ts.clone(opts.compilerOptions);
169170
opts.compilerOptions.target = ts.ScriptTarget.ES5;
170171
project.setProjectOptions(opts);
171-
172+
172173
project.compilerService.languageService.getSemanticDiagnostics(imported.name);
173174
assert.isTrue(fileExistsCalled);
174175
}
175176
});
176-
177+
177178
it("loads missing files from disk", () => {
178179
let root: File = {
179180
name: 'c:/foo.ts',
180181
content: `import {x} from "bar"`
181182
};
182-
183+
183184
let imported: File = {
184185
name: 'c:/bar.d.ts',
185186
content: `export var y = 1`
186-
};
187-
187+
};
188+
188189
let fileMap: Map<File> = { [root.name]: root };
189190
let serverHost = createDefaultServerHost(fileMap);
190191
let originalFileExists = serverHost.fileExists;
191-
192+
192193
let fileExistsCalledForBar = false;
193194
serverHost.fileExists = fileName => {
194195
if (fileName === "lib.d.ts") {
@@ -197,22 +198,22 @@ module ts {
197198
if (!fileExistsCalledForBar) {
198199
fileExistsCalledForBar = fileName.indexOf("/bar.") !== -1;
199200
}
200-
201+
201202
return originalFileExists(fileName);
202203
};
203-
204+
204205
let { project, rootScriptInfo } = createProject(root.name, serverHost);
205206

206207
let diags = project.compilerService.languageService.getSemanticDiagnostics(root.name);
207208
assert.isTrue(fileExistsCalledForBar, "'fileExists' should be called");
208209
assert.isTrue(diags.length === 1, "one diagnostic expected");
209210
assert.isTrue(typeof diags[0].messageText === "string" && ((<string>diags[0].messageText).indexOf("Cannot find module") === 0), "should be 'cannot find module' message");
210-
211+
211212
// assert that import will success once file appear on disk
212213
fileMap[imported.name] = imported;
213214
fileExistsCalledForBar = false;
214215
rootScriptInfo.editContent(0, rootScriptInfo.content.length, `import {y} from "bar"`)
215-
216+
216217
diags = project.compilerService.languageService.getSemanticDiagnostics(root.name);
217218
assert.isTrue(fileExistsCalledForBar, "'fileExists' should be called");
218219
assert.isTrue(diags.length === 0);

tests/cases/unittests/session.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace ts.server {
1717
createDirectory(): void {},
1818
getExecutingFilePath(): string { return void 0; },
1919
getCurrentDirectory(): string { return void 0; },
20+
getEnvironmentVariable(name: string): string { return ""; },
2021
readDirectory(): string[] { return []; },
2122
exit(): void {}
2223
};

0 commit comments

Comments
 (0)