chore: ignore libraries (#10756)

This commit is contained in:
Nikolay Vitkov 2023-08-18 16:16:07 +02:00 committed by GitHub
parent 052745a9e7
commit 1376b77a7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 113 additions and 41 deletions

View File

@ -23,7 +23,7 @@ import {
} from '@angular-devkit/schematics';
import {addCommonFiles} from '../utils/files.js';
import {getAngularConfig} from '../utils/json.js';
import {getApplicationProjects} from '../utils/json.js';
import {
TestRunner,
SchematicsSpec,
@ -89,7 +89,7 @@ function addE2EFile(options: SchematicsSpec): Rule {
return async (tree: Tree, context: SchematicContext) => {
context.logger.debug('Adding Spec file.');
const {projects} = getAngularConfig(tree);
const projects = getApplicationProjects(tree);
const projectNames = Object.keys(projects) as [string, ...string[]];
const foundProject: [string, AngularProject | undefined] | undefined =
projectNames.length === 1

View File

@ -25,7 +25,7 @@ import {
getNgCommandName,
hasE2ETester,
} from '../utils/files.js';
import {getAngularConfig} from '../utils/json.js';
import {getApplicationProjects} from '../utils/json.js';
import {
addPackageJsonDependencies,
addPackageJsonScripts,
@ -80,7 +80,7 @@ function addDependencies(options: SchematicsOptions): Rule {
function updateScripts(): Rule {
return (tree: Tree, context: SchematicContext): Tree => {
context.logger.debug('Updating "package.json" scripts');
const {projects} = getAngularConfig(tree);
const projects = getApplicationProjects(tree);
const projectsKeys = Object.keys(projects);
if (projectsKeys.length === 1) {
@ -100,7 +100,7 @@ function updateScripts(): Rule {
function addCommonFiles(options: SchematicsOptions): Rule {
return (tree: Tree, context: SchematicContext) => {
context.logger.debug('Adding Puppeteer base files.');
const {projects} = getAngularConfig(tree);
const projects = getApplicationProjects(tree);
return addCommonFilesHelper(tree, context, projects, {
options: {
@ -115,7 +115,7 @@ function addCommonFiles(options: SchematicsOptions): Rule {
function addOtherFiles(options: SchematicsOptions): Rule {
return (tree: Tree, context: SchematicContext) => {
context.logger.debug('Adding Puppeteer additional files.');
const {projects} = getAngularConfig(tree);
const projects = getApplicationProjects(tree);
return addFrameworkFiles(tree, context, projects, {
options: {

View File

@ -16,7 +16,7 @@
import {SchematicsException, Tree} from '@angular-devkit/schematics';
import type {AngularJson} from './types.js';
import type {AngularJson, AngularProject} from './types.js';
export function getJsonFileAsObject(
tree: Tree,
@ -38,3 +38,18 @@ export function getObjectAsJson(object: Record<string, any>): string {
export function getAngularConfig(tree: Tree): AngularJson {
return getJsonFileAsObject(tree, './angular.json') as AngularJson;
}
export function getApplicationProjects(
tree: Tree
): Record<string, AngularProject> {
const {projects} = getAngularConfig(tree);
const applications: Record<string, AngularProject> = {};
for (const key in projects) {
const project = projects[key]!;
if (project.projectType === 'application') {
applications[key] = project;
}
}
return applications;
}

View File

@ -21,6 +21,7 @@ import {Tree} from '@angular-devkit/schematics';
import {getNgCommandName} from './files.js';
import {
getAngularConfig,
getApplicationProjects,
getJsonFileAsObject,
getObjectAsJson,
} from './json.js';
@ -162,9 +163,10 @@ export function updateAngularJsonScripts(
overwrite = true
): Tree {
const angularJson = getAngularConfig(tree);
const name = getNgCommandName(angularJson.projects);
const projects = getApplicationProjects(tree);
const name = getNgCommandName(projects);
Object.keys(angularJson['projects']).forEach(project => {
Object.keys(projects).forEach(project => {
const e2eScript = [
{
name,

View File

@ -33,6 +33,7 @@ export interface PuppeteerSchematicsConfig {
};
}
export interface AngularProject {
projectType: 'application' | 'library';
root: string;
architect: {
e2e?: PuppeteerSchematicsConfig;

View File

@ -2,7 +2,7 @@ import expect from 'expect';
import {
buildTestingTree,
getMultiProjectFile,
getMultiApplicationFile,
setupHttpHooks,
} from './utils.js';
@ -20,7 +20,9 @@ describe('@puppeteer/ng-schematics: config', () => {
it('should create default file', async () => {
const tree = await buildTestingTree('config', 'multi');
expect(tree.files).toContain('/.puppeteerrc.mjs');
expect(tree.files).not.toContain(getMultiProjectFile('.puppeteerrc.mjs'));
expect(tree.files).not.toContain(
getMultiApplicationFile('.puppeteerrc.mjs')
);
});
});
});

View File

@ -2,7 +2,7 @@ import expect from 'expect';
import {
buildTestingTree,
getMultiProjectFile,
getMultiApplicationFile,
setupHttpHooks,
} from './utils.js';
@ -58,10 +58,10 @@ describe('@puppeteer/ng-schematics: e2e', () => {
name: 'myTest',
});
expect(tree.files).toContain(
getMultiProjectFile('e2e/tests/my-test.e2e.ts')
getMultiApplicationFile('e2e/tests/my-test.e2e.ts')
);
expect(tree.files).not.toContain(
getMultiProjectFile('e2e/tests/my-test.test.ts')
getMultiApplicationFile('e2e/tests/my-test.test.ts')
);
});
@ -71,10 +71,10 @@ describe('@puppeteer/ng-schematics: e2e', () => {
testRunner: 'node',
});
expect(tree.files).not.toContain(
getMultiProjectFile('e2e/tests/my-test.e2e.ts')
getMultiApplicationFile('e2e/tests/my-test.e2e.ts')
);
expect(tree.files).toContain(
getMultiProjectFile('e2e/tests/my-test.test.ts')
getMultiApplicationFile('e2e/tests/my-test.test.ts')
);
});
@ -85,10 +85,10 @@ describe('@puppeteer/ng-schematics: e2e', () => {
route,
});
expect(tree.files).toContain(
getMultiProjectFile('e2e/tests/my-test.e2e.ts')
getMultiApplicationFile('e2e/tests/my-test.e2e.ts')
);
expect(
tree.readContent(getMultiProjectFile('e2e/tests/my-test.e2e.ts'))
tree.readContent(getMultiApplicationFile('e2e/tests/my-test.e2e.ts'))
).toContain(`setupBrowserHooks('/${route}');`);
});
@ -99,10 +99,10 @@ describe('@puppeteer/ng-schematics: e2e', () => {
route,
});
expect(tree.files).toContain(
getMultiProjectFile('e2e/tests/my-test.e2e.ts')
getMultiApplicationFile('e2e/tests/my-test.e2e.ts')
);
expect(
tree.readContent(getMultiProjectFile('e2e/tests/my-test.e2e.ts'))
tree.readContent(getMultiApplicationFile('e2e/tests/my-test.e2e.ts'))
).toContain(`setupBrowserHooks('${route}');`);
});
});

View File

@ -1,9 +1,11 @@
import expect from 'expect';
import {
MULTI_LIBRARY_OPTIONS,
buildTestingTree,
getAngularJsonScripts,
getMultiProjectFile,
getMultiApplicationFile,
getMultiLibraryFile,
getPackageJson,
runSchematic,
setupHttpHooks,
@ -99,15 +101,21 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
});
});
describe('Multi projects', () => {
describe('Multi projects Application', () => {
it('should create base files and update to "package.json"', async () => {
const tree = await buildTestingTree('ng-add', 'multi');
const {devDependencies, scripts} = getPackageJson(tree);
const {builder, configurations} = getAngularJsonScripts(tree);
expect(tree.files).toContain(getMultiProjectFile('e2e/tsconfig.json'));
expect(tree.files).toContain(getMultiProjectFile('e2e/tests/app.e2e.ts'));
expect(tree.files).toContain(getMultiProjectFile('e2e/tests/utils.ts'));
expect(tree.files).toContain(
getMultiApplicationFile('e2e/tsconfig.json')
);
expect(tree.files).toContain(
getMultiApplicationFile('e2e/tests/app.e2e.ts')
);
expect(tree.files).toContain(
getMultiApplicationFile('e2e/tests/utils.ts')
);
expect(devDependencies).toContain('puppeteer');
expect(scripts['e2e']).toBe('ng e2e');
expect(builder).toBe('@puppeteer/ng-schematics:puppeteer');
@ -130,7 +138,7 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
it('should not create Puppeteer config', async () => {
const {files} = await buildTestingTree('ng-add', 'multi');
expect(files).not.toContain(getMultiProjectFile('.puppeteerrc.cjs'));
expect(files).not.toContain(getMultiApplicationFile('.puppeteerrc.cjs'));
expect(files).not.toContain('/.puppeteerrc.cjs');
});
it('should create Jasmine files and update "package.json"', async () => {
@ -140,7 +148,7 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
const {devDependencies} = getPackageJson(tree);
const {options} = getAngularJsonScripts(tree);
expect(tree.files).toContain(getMultiProjectFile('e2e/jasmine.json'));
expect(tree.files).toContain(getMultiApplicationFile('e2e/jasmine.json'));
expect(devDependencies).toContain('jasmine');
expect(options['testRunner']).toBe('jasmine');
});
@ -151,7 +159,9 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
const {devDependencies} = getPackageJson(tree);
const {options} = getAngularJsonScripts(tree);
expect(tree.files).toContain(getMultiProjectFile('e2e/jest.config.js'));
expect(tree.files).toContain(
getMultiApplicationFile('e2e/jest.config.js')
);
expect(devDependencies).toContain('jest');
expect(devDependencies).toContain('@types/jest');
expect(options['testRunner']).toBe('jest');
@ -163,7 +173,7 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
const {devDependencies} = getPackageJson(tree);
const {options} = getAngularJsonScripts(tree);
expect(tree.files).toContain(getMultiProjectFile('e2e/.mocharc.js'));
expect(tree.files).toContain(getMultiApplicationFile('e2e/.mocharc.js'));
expect(devDependencies).toContain('mocha');
expect(devDependencies).toContain('@types/mocha');
expect(options['testRunner']).toBe('mocha');
@ -174,12 +184,12 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
});
const {options} = getAngularJsonScripts(tree);
expect(tree.files).toContain(getMultiProjectFile('e2e/.gitignore'));
expect(tree.files).toContain(getMultiApplicationFile('e2e/.gitignore'));
expect(tree.files).not.toContain(
getMultiProjectFile('e2e/tests/app.e2e.ts')
getMultiApplicationFile('e2e/tests/app.e2e.ts')
);
expect(tree.files).toContain(
getMultiProjectFile('e2e/tests/app.test.ts')
getMultiApplicationFile('e2e/tests/app.test.ts')
);
expect(options['testRunner']).toBe('node');
});
@ -190,4 +200,33 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
expect(options['port']).toBeUndefined();
});
});
describe('Multi projects Library', () => {
it('should create base files and update to "package.json"', async () => {
const tree = await buildTestingTree('ng-add', 'multi');
const config = getAngularJsonScripts(
tree,
true,
MULTI_LIBRARY_OPTIONS.name
);
expect(tree.files).not.toContain(
getMultiLibraryFile('e2e/tsconfig.json')
);
expect(tree.files).not.toContain(
getMultiLibraryFile('e2e/tests/app.e2e.ts')
);
expect(tree.files).not.toContain(
getMultiLibraryFile('e2e/tests/utils.ts')
);
expect(config).toBeUndefined();
});
it('should not create Puppeteer config', async () => {
const {files} = await buildTestingTree('ng-add', 'multi');
expect(files).not.toContain(getMultiLibraryFile('.puppeteerrc.cjs'));
expect(files).not.toContain('/.puppeteerrc.cjs');
});
});
});

View File

@ -14,10 +14,6 @@ const WORKSPACE_OPTIONS = {
version: '14.0.0',
};
const MULTI_APPLICATION_OPTIONS = {
name: 'sandbox',
};
const SINGLE_APPLICATION_OPTIONS = {
name: 'sandbox',
directory: '.',
@ -25,6 +21,14 @@ const SINGLE_APPLICATION_OPTIONS = {
version: '14.0.0',
};
const MULTI_APPLICATION_OPTIONS = {
name: SINGLE_APPLICATION_OPTIONS.name,
};
export const MULTI_LIBRARY_OPTIONS = {
name: 'components',
};
export function setupHttpHooks(): void {
// Stop outgoing Request for version fetching
before(() => {
@ -43,7 +47,8 @@ export function setupHttpHooks(): void {
export function getAngularJsonScripts(
tree: UnitTestTree,
isDefault = true
isDefault = true,
name = SINGLE_APPLICATION_OPTIONS.name
): {
builder: string;
configurations: Record<string, any>;
@ -51,9 +56,7 @@ export function getAngularJsonScripts(
} {
const angularJson = tree.readJson('angular.json') as any;
const e2eScript = isDefault ? 'e2e' : 'puppeteer';
return angularJson['projects']?.[SINGLE_APPLICATION_OPTIONS.name]?.[
'architect'
][e2eScript];
return angularJson['projects']?.[name]?.['architect'][e2eScript];
}
export function getPackageJson(tree: UnitTestTree): {
@ -69,9 +72,12 @@ export function getPackageJson(tree: UnitTestTree): {
};
}
export function getMultiProjectFile(file: string): string {
export function getMultiApplicationFile(file: string): string {
return `/${WORKSPACE_OPTIONS.newProjectRoot}/${MULTI_APPLICATION_OPTIONS.name}/${file}`;
}
export function getMultiLibraryFile(file: string): string {
return `/${WORKSPACE_OPTIONS.newProjectRoot}/${MULTI_LIBRARY_OPTIONS.name}/${file}`;
}
export async function buildTestingTree(
command: 'ng-add' | 'e2e' | 'config',
@ -109,6 +115,13 @@ export async function buildTestingTree(
MULTI_APPLICATION_OPTIONS,
workingTree
);
// Build dummy library
workingTree = await runner.runExternalSchematic(
'@schematics/angular',
'library',
MULTI_LIBRARY_OPTIONS,
workingTree
);
}
if (command !== 'ng-add') {