From c243ff3b6b9b11626c05293e962c18ca10ebe3a2 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Fri, 6 Nov 2020 11:22:27 -0800 Subject: [PATCH] test(compiler-cli): add a `fake_common` package alongside `fake_core` (#39594) ngtsc's testing infrastructure uses a mock version of @angular/core, which allows tests to run without requiring the real version of core to be built. This commit adds a mock version of @angular/common as well, as the language service tests are written to test against common. Only a handful of directives/pipes from common are currently supported. PR Close #39594 --- .../src/ngtsc/testing/fake_common/BUILD.bazel | 25 +++++++ .../src/ngtsc/testing/fake_common/index.ts | 72 +++++++++++++++++++ .../ngtsc/testing/fake_common/package.json | 5 ++ .../ngtsc/testing/src/mock_file_loading.ts | 16 ++++- 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 packages/compiler-cli/src/ngtsc/testing/fake_common/BUILD.bazel create mode 100644 packages/compiler-cli/src/ngtsc/testing/fake_common/index.ts create mode 100644 packages/compiler-cli/src/ngtsc/testing/fake_common/package.json diff --git a/packages/compiler-cli/src/ngtsc/testing/fake_common/BUILD.bazel b/packages/compiler-cli/src/ngtsc/testing/fake_common/BUILD.bazel new file mode 100644 index 0000000000..a6a4a1bd63 --- /dev/null +++ b/packages/compiler-cli/src/ngtsc/testing/fake_common/BUILD.bazel @@ -0,0 +1,25 @@ +package(default_visibility = ["//visibility:public"]) + +load("//tools:defaults.bzl", "ng_package", "ts_library") + +ts_library( + name = "fake_common", + srcs = [ + "index.ts", + ], + module_name = "@angular/common", + deps = [ + "//packages/compiler-cli/src/ngtsc/testing/fake_core", + ], +) + +ng_package( + name = "npm_package", + srcs = [ + "package.json", + ], + entry_point = ":index.ts", + deps = [ + ":fake_common", + ], +) diff --git a/packages/compiler-cli/src/ngtsc/testing/fake_common/index.ts b/packages/compiler-cli/src/ngtsc/testing/fake_common/index.ts new file mode 100644 index 0000000000..f2d0c5f257 --- /dev/null +++ b/packages/compiler-cli/src/ngtsc/testing/fake_common/index.ts @@ -0,0 +1,72 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {NgIterable, TemplateRef, ɵɵDirectiveDefWithMeta, ɵɵNgModuleDefWithMeta, ɵɵPipeDefWithMeta} from '@angular/core'; + +export interface NgForOfContext> { + $implicit: T; + ngForOf: U; +} + +export interface TrackByFunction { + (index: number, item: T): any; +} + +export interface NgIfContext { + $implicit: T; + ngIf: T; +} + +/** + * A fake version of the NgFor directive. + */ +export declare class NgForOf> { + ngForOf: U&NgIterable|null|undefined; + ngForTrackBy: TrackByFunction; + ngForTemplate: TemplateRef>; + + static ɵdir: ɵɵDirectiveDefWithMeta < NgForOf, '[ngFor][ngForOf]', never, { + 'ngForOf': 'ngForOf'; + 'ngForTrackBy': 'ngForTrackBy'; + 'ngForTemplate': 'ngForTemplate'; + } + , {}, never > ; + static ngTemplateContextGuard>(dir: NgForOf, ctx: any): + ctx is NgForOfContext; +} + +export declare class NgIf { + ngIf: T; + ngIfThen: TemplateRef>|null; + ngIfElse: TemplateRef>|null; + static ɵdir: ɵɵDirectiveDefWithMeta < NgIf, '[ngIf]', never, { + 'ngIf': 'ngIf'; + 'ngIfThen': 'ngIfThen'; + 'ngIfElse': 'ngIfElse'; + } + , {}, never > ; + static ngTemplateContextGuard(dir: NgIf, ctx: any): + ctx is NgIfContext>; +} + +export declare class DatePipe { + transform(value: Date|string|number, format?: string, timezone?: string, locale?: string): string + |null; + transform(value: null|undefined, format?: string, timezone?: string, locale?: string): null; + transform( + value: Date|string|number|null|undefined, format?: string, timezone?: string, + locale?: string): string|null; + static ɵpipe: ɵɵPipeDefWithMeta; +} + +export declare class CommonModule { + static ɵmod: + ɵɵNgModuleDefWithMeta; +} diff --git a/packages/compiler-cli/src/ngtsc/testing/fake_common/package.json b/packages/compiler-cli/src/ngtsc/testing/fake_common/package.json new file mode 100644 index 0000000000..18910da93e --- /dev/null +++ b/packages/compiler-cli/src/ngtsc/testing/fake_common/package.json @@ -0,0 +1,5 @@ +{ + "name": "@angular/common", + "version": "0.0.0-FAKE-FOR-TESTS", + "description": "Fake version of common for tests" +} \ No newline at end of file diff --git a/packages/compiler-cli/src/ngtsc/testing/src/mock_file_loading.ts b/packages/compiler-cli/src/ngtsc/testing/src/mock_file_loading.ts index a5d80e66a2..8915e105c1 100644 --- a/packages/compiler-cli/src/ngtsc/testing/src/mock_file_loading.ts +++ b/packages/compiler-cli/src/ngtsc/testing/src/mock_file_loading.ts @@ -43,7 +43,8 @@ const angularFolder = new CachedFolder(loadAngularFolder); const rxjsFolder = new CachedFolder(() => loadFolder(resolveNpmTreeArtifact('rxjs'))); export function loadStandardTestFiles( - {fakeCore = true, rxjs = false}: {fakeCore?: boolean, rxjs?: boolean} = {}): Folder { + {fakeCore = true, fakeCommon = false, rxjs = false}: + {fakeCore?: boolean, fakeCommon?: boolean, rxjs?: boolean} = {}): Folder { const tmpFs = new MockFileSystemPosix(true); const basePath = '/' as AbsoluteFsPath; @@ -57,6 +58,10 @@ export function loadStandardTestFiles( tmpFs.mount(tmpFs.resolve('/node_modules/@angular'), angularFolder.get()); } + if (fakeCommon) { + loadFakeCommon(tmpFs, basePath); + } + if (rxjs) { tmpFs.mount(tmpFs.resolve('/node_modules/rxjs'), rxjsFolder.get()); } @@ -77,6 +82,15 @@ export function loadFakeCore(fs: FileSystem, basePath: string = '/') { fs.resolve(basePath, 'node_modules/@angular/core')); } +export function loadFakeCommon(fs: FileSystem, basePath: string = '/') { + loadTestDirectory( + fs, + resolveNpmTreeArtifact( + 'angular/packages/compiler-cli/src/ngtsc/testing/fake_common/npm_package'), + fs.resolve(basePath, 'node_modules/@angular/common')); +} + + function loadFolder(path: string): Folder { const tmpFs = new MockFileSystemPosix(true); // Note that we intentionally pass the native `path`, without resolving it through the file