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
This commit is contained in:
Alex Rickabaugh 2020-11-06 11:22:27 -08:00 committed by atscott
parent 3613e7c4e5
commit c243ff3b6b
4 changed files with 117 additions and 1 deletions

View File

@ -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",
],
)

View File

@ -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<T, U extends NgIterable<T>> {
$implicit: T;
ngForOf: U;
}
export interface TrackByFunction<T> {
(index: number, item: T): any;
}
export interface NgIfContext<T = unknown> {
$implicit: T;
ngIf: T;
}
/**
* A fake version of the NgFor directive.
*/
export declare class NgForOf<T, U extends NgIterable<T>> {
ngForOf: U&NgIterable<T>|null|undefined;
ngForTrackBy: TrackByFunction<T>;
ngForTemplate: TemplateRef<NgForOfContext<T, U>>;
static ɵdir: ɵɵDirectiveDefWithMeta < NgForOf<any, any>, '[ngFor][ngForOf]', never, {
'ngForOf': 'ngForOf';
'ngForTrackBy': 'ngForTrackBy';
'ngForTemplate': 'ngForTemplate';
}
, {}, never > ;
static ngTemplateContextGuard<T, U extends NgIterable<T>>(dir: NgForOf<T, U>, ctx: any):
ctx is NgForOfContext<T, U>;
}
export declare class NgIf<T = unknown> {
ngIf: T;
ngIfThen: TemplateRef<NgIfContext<T>>|null;
ngIfElse: TemplateRef<NgIfContext<T>>|null;
static ɵdir: ɵɵDirectiveDefWithMeta < NgIf<any>, '[ngIf]', never, {
'ngIf': 'ngIf';
'ngIfThen': 'ngIfThen';
'ngIfElse': 'ngIfElse';
}
, {}, never > ;
static ngTemplateContextGuard<T>(dir: NgIf<T>, ctx: any):
ctx is NgIfContext<Exclude<T, false|0|''|null|undefined>>;
}
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<DatePipe, 'date'>;
}
export declare class CommonModule {
static ɵmod:
ɵɵNgModuleDefWithMeta<CommonModule, [typeof NgForOf, typeof NgIf, typeof DatePipe], never, [
typeof NgForOf, typeof NgIf, typeof DatePipe
]>;
}

View File

@ -0,0 +1,5 @@
{
"name": "@angular/common",
"version": "0.0.0-FAKE-FOR-TESTS",
"description": "Fake version of common for tests"
}

View File

@ -43,7 +43,8 @@ const angularFolder = new CachedFolder(loadAngularFolder);
const rxjsFolder = new CachedFolder(() => loadFolder(resolveNpmTreeArtifact('rxjs'))); const rxjsFolder = new CachedFolder(() => loadFolder(resolveNpmTreeArtifact('rxjs')));
export function loadStandardTestFiles( 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 tmpFs = new MockFileSystemPosix(true);
const basePath = '/' as AbsoluteFsPath; const basePath = '/' as AbsoluteFsPath;
@ -57,6 +58,10 @@ export function loadStandardTestFiles(
tmpFs.mount(tmpFs.resolve('/node_modules/@angular'), angularFolder.get()); tmpFs.mount(tmpFs.resolve('/node_modules/@angular'), angularFolder.get());
} }
if (fakeCommon) {
loadFakeCommon(tmpFs, basePath);
}
if (rxjs) { if (rxjs) {
tmpFs.mount(tmpFs.resolve('/node_modules/rxjs'), rxjsFolder.get()); 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')); 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 { function loadFolder(path: string): Folder {
const tmpFs = new MockFileSystemPosix(true); const tmpFs = new MockFileSystemPosix(true);
// Note that we intentionally pass the native `path`, without resolving it through the file // Note that we intentionally pass the native `path`, without resolving it through the file