test(compiler-cli): ensure partial-evaluator tests are not brittle to case-sensitivity (#36859)

These tests were matching file-paths against what is retrieved from the
TS compiler. But the TS compiler paths have been canonicalised, so the
tests were brittle on case-insensitive file-systems.

PR Close #36859
This commit is contained in:
Pete Bacon Darwin 2020-05-06 22:21:38 +01:00 committed by Alex Rickabaugh
parent 8ce38cac0d
commit 9e43e4900e
1 changed files with 14 additions and 8 deletions

View File

@ -790,25 +790,28 @@ runInEachFileSystem(() => {
it('should track each time a source file is visited', () => { it('should track each time a source file is visited', () => {
const addDependency = const addDependency =
jasmine.createSpy<DependencyTracker['addDependency']>('DependencyTracker'); jasmine.createSpy<DependencyTracker['addDependency']>('DependencyTracker');
const {expression, checker} = makeExpression( const {expression, checker, program} = makeExpression(
`class A { static foo = 42; } function bar() { return A.foo; }`, 'bar()'); `class A { static foo = 42; } function bar() { return A.foo; }`, 'bar()');
const entryPath = getSourceFileOrError(program, _('/entry.ts')).fileName;
const evaluator = makeEvaluator(checker, {...fakeDepTracker, addDependency}); const evaluator = makeEvaluator(checker, {...fakeDepTracker, addDependency});
evaluator.evaluate(expression); evaluator.evaluate(expression);
expect(addDependency).toHaveBeenCalledTimes(2); // two declaration visited expect(addDependency).toHaveBeenCalledTimes(2); // two declaration visited
expect( expect(
addDependency.calls.allArgs().map( addDependency.calls.allArgs().map(
(args: Parameters<typeof addDependency>) => [args[0].fileName, args[1].fileName])) (args: Parameters<typeof addDependency>) => [args[0].fileName, args[1].fileName]))
.toEqual([[_('/entry.ts'), _('/entry.ts')], [_('/entry.ts'), _('/entry.ts')]]); .toEqual([[entryPath, entryPath], [entryPath, entryPath]]);
}); });
it('should track imported source files', () => { it('should track imported source files', () => {
const addDependency = const addDependency =
jasmine.createSpy<DependencyTracker['addDependency']>('DependencyTracker'); jasmine.createSpy<DependencyTracker['addDependency']>('DependencyTracker');
const {expression, checker} = const {expression, checker, program} =
makeExpression(`import {Y} from './other'; const A = Y;`, 'A', [ makeExpression(`import {Y} from './other'; const A = Y;`, 'A', [
{name: _('/other.ts'), contents: `export const Y = 'test';`}, {name: _('/other.ts'), contents: `export const Y = 'test';`},
{name: _('/not-visited.ts'), contents: `export const Z = 'nope';`} {name: _('/not-visited.ts'), contents: `export const Z = 'nope';`}
]); ]);
const entryPath = getSourceFileOrError(program, _('/entry.ts')).fileName;
const otherPath = getSourceFileOrError(program, _('/other.ts')).fileName;
const evaluator = makeEvaluator(checker, {...fakeDepTracker, addDependency}); const evaluator = makeEvaluator(checker, {...fakeDepTracker, addDependency});
evaluator.evaluate(expression); evaluator.evaluate(expression);
expect(addDependency).toHaveBeenCalledTimes(2); expect(addDependency).toHaveBeenCalledTimes(2);
@ -816,15 +819,15 @@ runInEachFileSystem(() => {
addDependency.calls.allArgs().map( addDependency.calls.allArgs().map(
(args: Parameters<typeof addDependency>) => [args[0].fileName, args[1].fileName])) (args: Parameters<typeof addDependency>) => [args[0].fileName, args[1].fileName]))
.toEqual([ .toEqual([
[_('/entry.ts'), _('/entry.ts')], [entryPath, entryPath],
[_('/entry.ts'), _('/other.ts')], [entryPath, otherPath],
]); ]);
}); });
it('should track files passed through during re-exports', () => { it('should track files passed through during re-exports', () => {
const addDependency = const addDependency =
jasmine.createSpy<DependencyTracker['addDependency']>('DependencyTracker'); jasmine.createSpy<DependencyTracker['addDependency']>('DependencyTracker');
const {expression, checker} = const {expression, checker, program} =
makeExpression(`import * as mod from './direct-reexport';`, 'mod.value.property', [ makeExpression(`import * as mod from './direct-reexport';`, 'mod.value.property', [
{name: _('/const.ts'), contents: 'export const value = {property: "test"};'}, {name: _('/const.ts'), contents: 'export const value = {property: "test"};'},
{ {
@ -841,16 +844,19 @@ runInEachFileSystem(() => {
}, },
]); ]);
const evaluator = makeEvaluator(checker, {...fakeDepTracker, addDependency}); const evaluator = makeEvaluator(checker, {...fakeDepTracker, addDependency});
const entryPath = getSourceFileOrError(program, _('/entry.ts')).fileName;
const directReexportPath = getSourceFileOrError(program, _('/direct-reexport.ts')).fileName;
const constPath = getSourceFileOrError(program, _('/const.ts')).fileName;
evaluator.evaluate(expression); evaluator.evaluate(expression);
expect(addDependency).toHaveBeenCalledTimes(2); expect(addDependency).toHaveBeenCalledTimes(2);
expect( expect(
addDependency.calls.allArgs().map( addDependency.calls.allArgs().map(
(args: Parameters<typeof addDependency>) => [args[0].fileName, args[1].fileName])) (args: Parameters<typeof addDependency>) => [args[0].fileName, args[1].fileName]))
.toEqual([ .toEqual([
[_('/entry.ts'), _('/direct-reexport.ts')], [entryPath, directReexportPath],
// Not '/indirect-reexport.ts' or '/def.ts'. // Not '/indirect-reexport.ts' or '/def.ts'.
// TS skips through them when finding the original symbol for `value` // TS skips through them when finding the original symbol for `value`
[_('/entry.ts'), _('/const.ts')], [entryPath, constPath],
]); ]);
}); });
}); });