feat(misc): export mock builder context from workspace (#2221)

This commit is contained in:
Jason Jean 2019-12-19 18:30:21 -05:00 committed by GitHub
parent 6ef95bd454
commit cee6888a12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 135 additions and 274 deletions

View File

@ -5,7 +5,10 @@ import {
} from './execute.impl';
import { of, from } from 'rxjs';
import * as devkitArchitect from '@angular-devkit/architect';
import { MockBuilderContext, getMockContext } from '../../utils/testing';
import { MockBuilderContext } from '@nrwl/workspace/testing';
import { getMockContext } from '../../utils/testing';
jest.mock('child_process');
let { fork } = require('child_process');

View File

@ -1,13 +1,12 @@
import { join } from 'path';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { schema } from '@angular-devkit/core';
import { Rule, Tree } from '@angular-devkit/schematics';
import {
TestingArchitectHost,
TestLogger
} from '@angular-devkit/architect/testing';
import { schema, JsonObject } from '@angular-devkit/core';
import { Architect, BuilderContext, Target } from '@angular-devkit/architect';
import { ScheduleOptions } from '@angular-devkit/architect/src/api';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { Architect } from '@angular-devkit/architect';
import { TestingArchitectHost } from '@angular-devkit/architect/testing';
import { MockBuilderContext } from '@nrwl/workspace/testing';
const testRunner = new SchematicTestRunner(
'@nrwl/node',
@ -41,74 +40,3 @@ export async function getMockContext() {
await context.addBuilderFromPackage(join(__dirname, '../..'));
return context;
}
export class MockBuilderContext implements BuilderContext {
id: 0;
builder: any = {};
analytics = null;
target: Target = {
project: null,
target: null
};
get currentDirectory() {
return this.architectHost.currentDirectory;
}
get workspaceRoot() {
return this.architectHost.workspaceRoot;
}
logger = new TestLogger('test');
constructor(
private architect: Architect,
private architectHost: TestingArchitectHost
) {}
async addBuilderFromPackage(path: string) {
return await this.architectHost.addBuilderFromPackage(path);
}
async addTarget(target: Target, builderName: string) {
return await this.architectHost.addTarget(target, builderName);
}
getBuilderNameForTarget(target: Target) {
return this.architectHost.getBuilderNameForTarget(target);
}
scheduleTarget(
target: Target,
overrides?: JsonObject,
scheduleOptions?: ScheduleOptions
) {
return this.architect.scheduleTarget(target, overrides, scheduleOptions);
}
scheduleBuilder(
name: string,
overrides?: JsonObject,
scheduleOptions?: ScheduleOptions
) {
return this.architect.scheduleBuilder(name, overrides, scheduleOptions);
}
getTargetOptions(target: Target) {
return this.architectHost.getOptionsForTarget(target);
}
validateOptions<T extends JsonObject = JsonObject>(
options: JsonObject,
builderName: string
): Promise<T> {
return Promise.resolve<T>(options as T);
}
reportRunning() {}
reportStatus(status: string) {}
reportProgress(current: number, total?: number, status?: string) {}
addTeardown(teardown: () => Promise<void> | void) {}
}

View File

@ -1,8 +1,12 @@
import { run as storybookBuilder } from './build-storybook.impl';
import { MockBuilderContext, getMockContext } from '../../utils/testing';
import { join } from 'path';
import * as storybook from '@storybook/core/dist/server/build-static';
import { MockBuilderContext } from '@nrwl/workspace/testing';
import { getMockContext } from '../../utils/testing';
import { run as storybookBuilder } from './build-storybook.impl';
describe('Build storybook', () => {
let context: MockBuilderContext;

View File

@ -1,27 +1,18 @@
import { join, sep } from 'path';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import {
Tree,
Rule,
externalSchematic,
apply,
source
} from '@angular-devkit/schematics';
import { createEmptyWorkspace } from '@nrwl/workspace/src/utils/testing-utils';
import {
BuilderContext,
Target,
Architect,
ScheduleOptions
} from '@angular-devkit/architect';
import {
TestLogger,
TestingArchitectHost
} from '@angular-devkit/architect/testing';
import { JsonObject, json, schema } from '@angular-devkit/core';
import { tmpdir } from 'os';
import { mkdtempSync } from 'fs';
import { schema } from '@angular-devkit/core';
import { externalSchematic, Rule, Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { Architect } from '@angular-devkit/architect';
import { TestingArchitectHost } from '@angular-devkit/architect/testing';
import {
createEmptyWorkspace,
MockBuilderContext
} from '@nrwl/workspace/testing';
const testRunner = new SchematicTestRunner(
'@nrwl/storybook',
join(__dirname, '../../collection.json')
@ -134,83 +125,3 @@ export async function getMockContext() {
await context.addBuilderFromPackage(join(__dirname, '../..'));
return context;
}
export class MockBuilderContext implements BuilderContext {
id: 0;
builder: any = {};
analytics = null;
target: Target = {
project: null,
target: null
};
get currentDirectory() {
return this.architectHost.currentDirectory;
}
get workspaceRoot() {
return this.architectHost.workspaceRoot;
}
logger = new TestLogger('test');
constructor(
private architect: Architect,
private architectHost: TestingArchitectHost
) {}
async addBuilderFromPackage(path: string) {
return await this.architectHost.addBuilderFromPackage(path);
}
async addTarget(target: Target, builderName: string) {
return await this.architectHost.addTarget(target, builderName);
}
getBuilderNameForTarget(target: Target) {
return this.architectHost.getBuilderNameForTarget(target);
}
scheduleTarget(
target: Target,
overrides?: JsonObject,
scheduleOptions?: ScheduleOptions
) {
return this.architect.scheduleTarget(target, overrides, scheduleOptions);
}
scheduleBuilder(
name: string,
overrides?: JsonObject,
scheduleOptions?: ScheduleOptions
) {
return this.architect.scheduleBuilder(name, overrides, scheduleOptions);
}
getTargetOptions(target: Target) {
return this.architectHost.getOptionsForTarget(target);
}
validateOptions<T extends JsonObject = JsonObject>(
options: JsonObject,
builderName: string
): Promise<T> {
return Promise.resolve<T>(options as T);
}
reportRunning() {}
reportStatus(status: string) {}
reportProgress(current: number, total?: number, status?: string) {}
addTeardown(teardown: () => Promise<void> | void) {}
async getProjectMetadata(
target: Target | string
): Promise<json.JsonObject | null> {
return (
this.architectHost &&
this.architectHost.getProjectMetadata(target as string)
);
}
}

View File

@ -1,12 +1,16 @@
import { join } from 'path';
import { workspaces } from '@angular-devkit/core';
import { run, WebBuildBuilderOptions } from './build.impl';
import { of } from 'rxjs';
import * as buildWebpack from '@angular-devkit/build-webpack';
jest.mock('tsconfig-paths-webpack-plugin');
import TsConfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
import { MockBuilderContext } from '@nrwl/workspace/testing';
import { run, WebBuildBuilderOptions } from './build.impl';
import * as webConfigUtils from '../../utils/web.config';
import { getMockContext, MockBuilderContext } from '../../utils/testing';
import { join } from 'path';
import { getMockContext } from '../../utils/testing';
import * as indexHtmlUtils from '../../utils/third-party/cli-files/utilities/index-file/write-index-html';
describe('WebBuildBuilder', () => {

View File

@ -1,10 +1,14 @@
import { of } from 'rxjs';
import { join } from 'path';
import { workspaces } from '@angular-devkit/core';
import * as f from '@nrwl/workspace/src/utils/fileutils';
import { MockBuilderContext } from '@nrwl/workspace/testing';
import * as impl from './bundle.impl';
import * as rr from './run-rollup';
import { of } from 'rxjs';
import { getMockContext, MockBuilderContext } from '../../utils/testing';
import { workspaces } from '@angular-devkit/core';
import { join } from 'path';
import * as f from '@nrwl/workspace/src/utils/fileutils';
import { getMockContext } from '../../utils/testing';
import { BundleBuilderOptions } from '../../utils/types';
jest.mock('tsconfig-paths-webpack-plugin');

View File

@ -1,17 +1,12 @@
import { join } from 'path';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { schema } from '@angular-devkit/core';
import { Rule, Tree } from '@angular-devkit/schematics';
import {
BuilderContext,
Architect,
Target,
ScheduleOptions
} from '@angular-devkit/architect';
import {
TestingArchitectHost,
TestLogger
} from '@angular-devkit/architect/testing';
import { schema, JsonObject } from '@angular-devkit/core';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { Architect } from '@angular-devkit/architect';
import { TestingArchitectHost } from '@angular-devkit/architect/testing';
import { MockBuilderContext } from '@nrwl/workspace/testing';
const testRunner = new SchematicTestRunner(
'@nrwl/web',
@ -45,74 +40,3 @@ export async function getMockContext() {
await context.addBuilderFromPackage(join(__dirname, '../..'));
return context;
}
export class MockBuilderContext implements BuilderContext {
id: 0;
builder: any = {};
analytics = null;
target: Target = {
project: null,
target: null
};
get currentDirectory() {
return this.architectHost.currentDirectory;
}
get workspaceRoot() {
return this.architectHost.workspaceRoot;
}
logger = new TestLogger('test');
constructor(
private architect: Architect,
private architectHost: TestingArchitectHost
) {}
async addBuilderFromPackage(path: string) {
return await this.architectHost.addBuilderFromPackage(path);
}
async addTarget(target: Target, builderName: string) {
return await this.architectHost.addTarget(target, builderName);
}
getBuilderNameForTarget(target: Target) {
return this.architectHost.getBuilderNameForTarget(target);
}
scheduleTarget(
target: Target,
overrides?: JsonObject,
scheduleOptions?: ScheduleOptions
) {
return this.architect.scheduleTarget(target, overrides, scheduleOptions);
}
scheduleBuilder(
name: string,
overrides?: JsonObject,
scheduleOptions?: ScheduleOptions
) {
return this.architect.scheduleBuilder(name, overrides, scheduleOptions);
}
getTargetOptions(target: Target) {
return this.architectHost.getOptionsForTarget(target);
}
validateOptions<T extends JsonObject = JsonObject>(
options: JsonObject,
builderName: string
): Promise<T> {
return Promise.resolve<T>(options as T);
}
reportRunning() {}
reportStatus(status: string) {}
reportProgress(current: number, total?: number, status?: string) {}
addTeardown(teardown: () => Promise<void> | void) {}
}

View File

@ -4,6 +4,13 @@ import {
WorkspaceFormat
} from '@angular-devkit/core/src/workspace/core';
import { NxJson } from '@nrwl/workspace/src/core/shared-interfaces';
import { Architect, BuilderContext, Target } from '@angular-devkit/architect';
import {
TestingArchitectHost,
TestLogger
} from '@angular-devkit/architect/testing';
import { JsonObject } from '@angular-devkit/core';
import { ScheduleOptions } from '@angular-devkit/architect/src/api';
export function getFileContent(tree: Tree, path: string): string {
const fileEntry = tree.get(path);
@ -55,3 +62,78 @@ export function createEmptyWorkspace(tree: Tree): Tree {
);
return tree;
}
/**
* Mock context which makes testing builders easier
*/
export class MockBuilderContext implements BuilderContext {
id: 0;
builder: any = {};
analytics = null;
target: Target = {
project: null,
target: null
};
logger = new TestLogger('test');
get currentDirectory() {
return this.architectHost.currentDirectory;
}
get workspaceRoot() {
return this.architectHost.workspaceRoot;
}
constructor(
private architect: Architect,
private architectHost: TestingArchitectHost
) {}
async addBuilderFromPackage(path: string) {
return await this.architectHost.addBuilderFromPackage(path);
}
async addTarget(target: Target, builderName: string) {
return this.architectHost.addTarget(target, builderName);
}
getBuilderNameForTarget(target: Target) {
return this.architectHost.getBuilderNameForTarget(target);
}
scheduleTarget(
target: Target,
overrides?: JsonObject,
scheduleOptions?: ScheduleOptions
) {
return this.architect.scheduleTarget(target, overrides, scheduleOptions);
}
scheduleBuilder(
name: string,
overrides?: JsonObject,
scheduleOptions?: ScheduleOptions
) {
return this.architect.scheduleBuilder(name, overrides, scheduleOptions);
}
getTargetOptions(target: Target) {
return this.architectHost.getOptionsForTarget(target);
}
validateOptions<T extends JsonObject = JsonObject>(
options: JsonObject,
builderName: string
): Promise<T> {
return Promise.resolve<T>(options as T);
}
reportRunning() {}
reportStatus(status: string) {}
reportProgress(current: number, total?: number, status?: string) {}
addTeardown(teardown: () => Promise<void> | void) {}
}

View File

@ -1,4 +1,5 @@
export {
createEmptyWorkspace,
getFileContent
getFileContent,
MockBuilderContext
} from './src/utils/testing-utils';