From 42e3a5241db462539087ca7686c6aef758ce6eb5 Mon Sep 17 00:00:00 2001 From: Benjamin Kindle Date: Sun, 11 Apr 2021 12:23:42 -0400 Subject: [PATCH] test(compiler-cli): add integration test for relative rootDir (#41359) this will make it easier to detect regressions of the relative rootDir behavior PR Close #41359 --- packages/compiler-cli/test/ngtsc/env.ts | 15 ++++++++++----- .../compiler-cli/test/ngtsc/ngtsc_spec.ts | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/compiler-cli/test/ngtsc/env.ts b/packages/compiler-cli/test/ngtsc/env.ts index 4c68621b65..baad8f3286 100644 --- a/packages/compiler-cli/test/ngtsc/env.ts +++ b/packages/compiler-cli/test/ngtsc/env.ts @@ -30,6 +30,7 @@ export class NgtscTestEnvironment { private multiCompileHostExt: MultiCompileHostExt|null = null; private oldProgram: Program|null = null; private changedResources: Set|null = null; + private commandLineArgs = ['-p', this.basePath]; private constructor( private fs: FileSystem, readonly outDir: AbsoluteFsPath, readonly basePath: AbsoluteFsPath) {} @@ -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()`); @@ -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); @@ -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) { @@ -248,7 +253,7 @@ export class NgtscTestEnvironment { } async driveDiagnosticsAsync(): Promise> { - 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(); @@ -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 { - 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(); diff --git a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts index cf8700ab67..ae696115d8 100644 --- a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts +++ b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts @@ -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', '

Hello World

'); + 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';