From 076dde482045de6ff7189d75459a5aab756b4336 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 22 Aug 2019 12:59:08 -0700 Subject: [PATCH] Test with --out as well --- .../tsserver/projectReferenceErrors.ts | 399 +++++++++++------- 1 file changed, 244 insertions(+), 155 deletions(-) diff --git a/src/testRunner/unittests/tsserver/projectReferenceErrors.ts b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts index 62468fcc6f7..ac023743da3 100644 --- a/src/testRunner/unittests/tsserver/projectReferenceErrors.ts +++ b/src/testRunner/unittests/tsserver/projectReferenceErrors.ts @@ -3,37 +3,6 @@ namespace ts.projectSystem { const projectLocation = "/user/username/projects/myproject"; const dependecyLocation = `${projectLocation}/dependency`; const usageLocation = `${projectLocation}/usage`; - const dependencyTs: File = { - path: `${dependecyLocation}/fns.ts`, - content: `export function fn1() { } -export function fn2() { } -// Introduce error for fnErr import in main -// export function fnErr() { } -// Error in dependency ts file -export let x: string = 10;` - }; - const dependencyConfig: File = { - path: `${dependecyLocation}/tsconfig.json`, - content: JSON.stringify({ compilerOptions: { composite: true, declarationDir: "../decls" } }) - }; - const usageTs: File = { - path: `${usageLocation}/usage.ts`, - content: `import { - fn1, - fn2, - fnErr -} from '../decls/fns' -fn1(); -fn2(); -fnErr(); -` - }; - const usageConfig: File = { - path: `${usageLocation}/tsconfig.json`, - content: JSON.stringify({ - references: [{ path: "../dependency" }] - }) - }; interface CheckErrorsInFile { session: TestSession; @@ -75,9 +44,9 @@ fnErr(); } } - function verifyErrorsUsingGeterr({ openFiles, expectedGetErr }: VerifyScenario) { + function verifyErrorsUsingGeterr({ allFiles, openFiles, expectedGetErr }: VerifyScenario) { it("verifies the errors in open file", () => { - const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const host = createServerHost([...allFiles(), libFile]); const session = createSession(host, { canUseEvents: true, }); openFilesForSession(openFiles(), session); @@ -96,9 +65,9 @@ fnErr(); }); } - function verifyErrorsUsingGeterrForProject({ openFiles, expectedGetErrForProject }: VerifyScenario) { + function verifyErrorsUsingGeterrForProject({ allFiles, openFiles, expectedGetErrForProject }: VerifyScenario) { it("verifies the errors in projects", () => { - const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const host = createServerHost([...allFiles(), libFile]); const session = createSession(host, { canUseEvents: true, }); openFilesForSession(openFiles(), session); @@ -118,9 +87,9 @@ fnErr(); }); } - function verifyErrorsUsingSyncMethods({ openFiles, expectedSyncDiagnostics }: VerifyScenario) { + function verifyErrorsUsingSyncMethods({ allFiles, openFiles, expectedSyncDiagnostics }: VerifyScenario) { it("verifies the errors using sync commands", () => { - const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const host = createServerHost([...allFiles(), libFile]); const session = createSession(host); openFilesForSession(openFiles(), session); for (const { file, project, syntax, semantic, suggestion } of expectedSyncDiagnostics()) { @@ -152,9 +121,9 @@ fnErr(); }); } - function verifyConfigFileErrors({ openFiles, expectedConfigFileDiagEvents }: VerifyScenario) { + function verifyConfigFileErrors({ allFiles, openFiles, expectedConfigFileDiagEvents }: VerifyScenario) { it("verify config file errors", () => { - const host = createServerHost([dependencyTs, dependencyConfig, usageTs, usageConfig, libFile]); + const host = createServerHost([...allFiles(), libFile]); const { session, events } = createSessionWithEventTracking(host, server.ConfigFileDiagEvent); for (const file of openFiles()) { @@ -185,6 +154,7 @@ fnErr(); project?: string; } interface VerifyScenario { + allFiles: () => readonly File[]; openFiles: () => readonly File[]; expectedGetErr: () => readonly GetErrDiagnostics[]; expectedGetErrForProject: () => readonly GetErrForProjectDiagnostics[]; @@ -207,133 +177,252 @@ fnErr(); }; } - function usageDiagnostics(): GetErrDiagnostics { - return { - file: usageTs, - syntax: emptyArray, - semantic: [ - createDiagnostic( - { line: 4, offset: 5 }, - { line: 4, offset: 10 }, - Diagnostics.Module_0_has_no_exported_member_1, - [`"../dependency/fns"`, "fnErr"], - "error", - ) - ], - suggestion: emptyArray - }; - } - - function dependencyDiagnostics(): GetErrDiagnostics { - return { - file: dependencyTs, - syntax: emptyArray, - semantic: [ - createDiagnostic( - { line: 6, offset: 12 }, - { line: 6, offset: 13 }, - Diagnostics.Type_0_is_not_assignable_to_type_1, - ["10", "string"], - "error", - ) - ], - suggestion: emptyArray - }; - } - - function usageProjectDiagnostics(): GetErrForProjectDiagnostics { - return { - project: usageTs.path, - errors: [ - usageDiagnostics(), - emptyDiagnostics(dependencyTs) - ] - }; - } - - function dependencyProjectDiagnostics(): GetErrForProjectDiagnostics { - return { - project: dependencyTs.path, - errors: [ - dependencyDiagnostics() - ] - }; - } - function syncDiagnostics(diagnostics: GetErrDiagnostics, project: string): SyncDiagnostics { return { project, ...diagnostics }; } - function usageConfigDiag(): server.ConfigFileDiagEvent["data"] { - return { - triggerFile: usageTs.path, - configFileName: usageConfig.path, - diagnostics: emptyArray - }; + interface VerifyUsageAndDependency { + allFiles: readonly [File, File, File, File]; // dependencyTs, dependencyConfig, usageTs, usageConfig + usageDiagnostics(): GetErrDiagnostics; + dependencyDiagnostics(): GetErrDiagnostics; + + } + function verifyUsageAndDependency({ allFiles, usageDiagnostics, dependencyDiagnostics }: VerifyUsageAndDependency) { + const [dependencyTs, dependencyConfig, usageTs, usageConfig] = allFiles; + function usageProjectDiagnostics(): GetErrForProjectDiagnostics { + return { + project: usageTs.path, + errors: [ + usageDiagnostics(), + emptyDiagnostics(dependencyTs) + ] + }; + } + + function dependencyProjectDiagnostics(): GetErrForProjectDiagnostics { + return { + project: dependencyTs.path, + errors: [ + dependencyDiagnostics() + ] + }; + } + + function usageConfigDiag(): server.ConfigFileDiagEvent["data"] { + return { + triggerFile: usageTs.path, + configFileName: usageConfig.path, + diagnostics: emptyArray + }; + } + + function dependencyConfigDiag(): server.ConfigFileDiagEvent["data"] { + return { + triggerFile: dependencyTs.path, + configFileName: dependencyConfig.path, + diagnostics: emptyArray + }; + } + + describe("when dependency project is not open", () => { + verifyScenario({ + allFiles: () => allFiles, + openFiles: () => [usageTs], + expectedGetErr: () => [ + usageDiagnostics() + ], + expectedGetErrForProject: () => [ + usageProjectDiagnostics(), + { + project: dependencyTs.path, + errors: [ + emptyDiagnostics(dependencyTs), + usageDiagnostics() + ] + } + ], + expectedSyncDiagnostics: () => [ + // Without project + usageDiagnostics(), + emptyDiagnostics(dependencyTs), + // With project + syncDiagnostics(usageDiagnostics(), usageConfig.path), + syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), + ], + expectedConfigFileDiagEvents: () => [ + usageConfigDiag() + ], + }); + }); + + describe("when the depedency file is open", () => { + verifyScenario({ + allFiles: () => allFiles, + openFiles: () => [usageTs, dependencyTs], + expectedGetErr: () => [ + usageDiagnostics(), + dependencyDiagnostics(), + ], + expectedGetErrForProject: () => [ + usageProjectDiagnostics(), + dependencyProjectDiagnostics() + ], + expectedSyncDiagnostics: () => [ + // Without project + usageDiagnostics(), + dependencyDiagnostics(), + // With project + syncDiagnostics(usageDiagnostics(), usageConfig.path), + syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), + syncDiagnostics(dependencyDiagnostics(), dependencyConfig.path), + ], + expectedConfigFileDiagEvents: () => [ + usageConfigDiag(), + dependencyConfigDiag() + ], + }); + }); } - function dependencyConfigDiag(): server.ConfigFileDiagEvent["data"] { - return { - triggerFile: dependencyTs.path, - configFileName: dependencyConfig.path, - diagnostics: emptyArray + describe("with module scenario", () => { + const dependencyTs: File = { + path: `${dependecyLocation}/fns.ts`, + content: `export function fn1() { } +export function fn2() { } +// Introduce error for fnErr import in main +// export function fnErr() { } +// Error in dependency ts file +export let x: string = 10;` }; - } + const dependencyConfig: File = { + path: `${dependecyLocation}/tsconfig.json`, + content: JSON.stringify({ compilerOptions: { composite: true, declarationDir: "../decls" } }) + }; + const usageTs: File = { + path: `${usageLocation}/usage.ts`, + content: `import { + fn1, + fn2, + fnErr +} from '../decls/fns' +fn1(); +fn2(); +fnErr(); +` + }; + const usageConfig: File = { + path: `${usageLocation}/tsconfig.json`, + content: JSON.stringify({ + references: [{ path: "../dependency" }] + }) + }; + function usageDiagnostics(): GetErrDiagnostics { + return { + file: usageTs, + syntax: emptyArray, + semantic: [ + createDiagnostic( + { line: 4, offset: 5 }, + { line: 4, offset: 10 }, + Diagnostics.Module_0_has_no_exported_member_1, + [`"../dependency/fns"`, "fnErr"], + "error", + ) + ], + suggestion: emptyArray + }; + } - describe("when dependency project is not open", () => { - verifyScenario({ - openFiles: () => [usageTs], - expectedGetErr: () => [ - usageDiagnostics() - ], - expectedGetErrForProject: () => [ - usageProjectDiagnostics(), - { - project: dependencyTs.path, - errors: [ - emptyDiagnostics(dependencyTs), - usageDiagnostics() - ] - } - ], - expectedSyncDiagnostics: () => [ - // Without project - usageDiagnostics(), - emptyDiagnostics(dependencyTs), - // With project - syncDiagnostics(usageDiagnostics(), usageConfig.path), - syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), - ], - expectedConfigFileDiagEvents: () => [ - usageConfigDiag() - ], + function dependencyDiagnostics(): GetErrDiagnostics { + return { + file: dependencyTs, + syntax: emptyArray, + semantic: [ + createDiagnostic( + { line: 6, offset: 12 }, + { line: 6, offset: 13 }, + Diagnostics.Type_0_is_not_assignable_to_type_1, + ["10", "string"], + "error", + ) + ], + suggestion: emptyArray + }; + } + + verifyUsageAndDependency({ + allFiles: [dependencyTs, dependencyConfig, usageTs, usageConfig], + usageDiagnostics, + dependencyDiagnostics }); }); - describe("when the depedency file is open", () => { - verifyScenario({ - openFiles: () => [usageTs, dependencyTs], - expectedGetErr: () => [ - usageDiagnostics(), - dependencyDiagnostics(), - ], - expectedGetErrForProject: () => [ - usageProjectDiagnostics(), - dependencyProjectDiagnostics() - ], - expectedSyncDiagnostics: () => [ - // Without project - usageDiagnostics(), - dependencyDiagnostics(), - // With project - syncDiagnostics(usageDiagnostics(), usageConfig.path), - syncDiagnostics(emptyDiagnostics(dependencyTs), usageConfig.path), - syncDiagnostics(dependencyDiagnostics(), dependencyConfig.path), - ], - expectedConfigFileDiagEvents: () => [ - usageConfigDiag(), - dependencyConfigDiag() - ], + describe("with non module --out", () => { + const dependencyTs: File = { + path: `${dependecyLocation}/fns.ts`, + content: `function fn1() { } +function fn2() { } +// Introduce error for fnErr import in main +// function fnErr() { } +// Error in dependency ts file +let x: string = 10;` + }; + const dependencyConfig: File = { + path: `${dependecyLocation}/tsconfig.json`, + content: JSON.stringify({ compilerOptions: { composite: true, outFile: "../dependency.js" } }) + }; + const usageTs: File = { + path: `${usageLocation}/usage.ts`, + content: `fn1(); +fn2(); +fnErr(); +` + }; + const usageConfig: File = { + path: `${usageLocation}/tsconfig.json`, + content: JSON.stringify({ + compilerOptions: { outFile: "../usage.js" }, + references: [{ path: "../dependency" }] + }) + }; + function usageDiagnostics(): GetErrDiagnostics { + return { + file: usageTs, + syntax: emptyArray, + semantic: [ + createDiagnostic( + { line: 3, offset: 1 }, + { line: 3, offset: 6 }, + Diagnostics.Cannot_find_name_0, + ["fnErr"], + "error", + ) + ], + suggestion: emptyArray + }; + } + + function dependencyDiagnostics(): GetErrDiagnostics { + return { + file: dependencyTs, + syntax: emptyArray, + semantic: [ + createDiagnostic( + { line: 6, offset: 5 }, + { line: 6, offset: 6 }, + Diagnostics.Type_0_is_not_assignable_to_type_1, + ["10", "string"], + "error", + ) + ], + suggestion: emptyArray + }; + } + + verifyUsageAndDependency({ + allFiles: [dependencyTs, dependencyConfig, usageTs, usageConfig], + usageDiagnostics, + dependencyDiagnostics }); }); });