Skip to content

Commit

Permalink
test(compiler-cli): add integration test for relative rootDir
Browse files Browse the repository at this point in the history
this will make it easier to detect regressions of the relative rootDir behavior
  • Loading branch information
literalpie committed Apr 11, 2021
1 parent 68840e2 commit 32cb493
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/compiler-cli/test/ngtsc/env.ts
Expand Up @@ -30,6 +30,7 @@ export class NgtscTestEnvironment {
private multiCompileHostExt: MultiCompileHostExt|null = null;
private oldProgram: Program|null = null;
private changedResources: Set<string>|null = null;
private commandLineArgs = ['-p', this.basePath];

private constructor(
private fs: FileSystem, readonly outDir: AbsoluteFsPath, readonly basePath: AbsoluteFsPath) {}
Expand Down Expand Up @@ -114,6 +115,10 @@ export class NgtscTestEnvironment {
setWrapHostForTest(makeWrapHost(new ResourceLoadingCompileHost(this.fs)));
}

addCommandLineArgs(...args: string[]): void {
this.commandLineArgs.push(...args);
}

flushWrittenFileTracking(): void {
if (this.multiCompileHostExt === null) {
throw new Error(`Not tracking written files - call enableMultipleCompilations()`);
Expand Down Expand Up @@ -214,7 +219,7 @@ export class NgtscTestEnvironment {
};
}
const exitCode = main(
['-p', this.basePath], errorSpy, undefined, customTransformers, reuseProgram,
this.commandLineArgs, errorSpy, undefined, customTransformers, reuseProgram,
this.changedResources);
expect(errorSpy).not.toHaveBeenCalled();
expect(exitCode).toBe(0);
Expand All @@ -236,7 +241,7 @@ export class NgtscTestEnvironment {
}

const diags = mainDiagnosticsForTest(
['-p', this.basePath], undefined, reuseProgram, this.changedResources);
this.commandLineArgs, undefined, reuseProgram, this.changedResources);


if (this.multiCompileHostExt !== null) {
Expand All @@ -248,7 +253,7 @@ export class NgtscTestEnvironment {
}

async driveDiagnosticsAsync(): Promise<ReadonlyArray<ts.Diagnostic>> {
const {rootNames, options} = readNgcCommandLineAndConfiguration(['-p', this.basePath]);
const {rootNames, options} = readNgcCommandLineAndConfiguration(this.commandLineArgs);
const host = createCompilerHost({options});
const program = createProgram({rootNames, host, options});
await program.loadNgStructureAsync();
Expand All @@ -258,14 +263,14 @@ export class NgtscTestEnvironment {
}

driveRoutes(entryPoint?: string): LazyRoute[] {
const {rootNames, options} = readNgcCommandLineAndConfiguration(['-p', this.basePath]);
const {rootNames, options} = readNgcCommandLineAndConfiguration(this.commandLineArgs);
const host = createCompilerHost({options});
const program = createProgram({rootNames, host, options});
return program.listLazyRoutes(entryPoint);
}

driveIndexer(): Map<DeclarationNode, IndexedComponent> {
const {rootNames, options} = readNgcCommandLineAndConfiguration(['-p', this.basePath]);
const {rootNames, options} = readNgcCommandLineAndConfiguration(this.commandLineArgs);
const host = createCompilerHost({options});
const program = createProgram({rootNames, host, options});
return (program as NgtscProgram).getIndexedComponents();
Expand Down
19 changes: 19 additions & 0 deletions packages/compiler-cli/test/ngtsc/ngtsc_spec.ts
Expand Up @@ -54,6 +54,25 @@ function allTests(os: string) {
env.tsconfig();
});

it('should accept relative file paths as command line argument', () => {
env.addCommandLineArgs('--rootDir', './rootDir');
env.write('rootDir/test.html', '<p>Hello World</p>');
env.write('rootDir/test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test-cmp',
templateUrl: 'test.html',
})
export class TestCmp {}
`);

env.driveMain();

const jsContents = env.getContents('test.js');
expect(jsContents).toContain('Hello World');
});

it('should compile Injectables without errors', () => {
env.write('test.ts', `
import {Injectable} from '@angular/core';
Expand Down

0 comments on commit 32cb493

Please sign in to comment.