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

View File

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

View File

@ -16,7 +16,7 @@
import {SchematicsException, Tree} from '@angular-devkit/schematics'; import {SchematicsException, Tree} from '@angular-devkit/schematics';
import type {AngularJson} from './types.js'; import type {AngularJson, AngularProject} from './types.js';
export function getJsonFileAsObject( export function getJsonFileAsObject(
tree: Tree, tree: Tree,
@ -38,3 +38,18 @@ export function getObjectAsJson(object: Record<string, any>): string {
export function getAngularConfig(tree: Tree): AngularJson { export function getAngularConfig(tree: Tree): AngularJson {
return getJsonFileAsObject(tree, './angular.json') as 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 {getNgCommandName} from './files.js';
import { import {
getAngularConfig, getAngularConfig,
getApplicationProjects,
getJsonFileAsObject, getJsonFileAsObject,
getObjectAsJson, getObjectAsJson,
} from './json.js'; } from './json.js';
@ -162,9 +163,10 @@ export function updateAngularJsonScripts(
overwrite = true overwrite = true
): Tree { ): Tree {
const angularJson = getAngularConfig(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 = [ const e2eScript = [
{ {
name, name,

View File

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

View File

@ -2,7 +2,7 @@ import expect from 'expect';
import { import {
buildTestingTree, buildTestingTree,
getMultiProjectFile, getMultiApplicationFile,
setupHttpHooks, setupHttpHooks,
} from './utils.js'; } from './utils.js';
@ -20,7 +20,9 @@ describe('@puppeteer/ng-schematics: config', () => {
it('should create default file', async () => { it('should create default file', async () => {
const tree = await buildTestingTree('config', 'multi'); const tree = await buildTestingTree('config', 'multi');
expect(tree.files).toContain('/.puppeteerrc.mjs'); 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 { import {
buildTestingTree, buildTestingTree,
getMultiProjectFile, getMultiApplicationFile,
setupHttpHooks, setupHttpHooks,
} from './utils.js'; } from './utils.js';
@ -58,10 +58,10 @@ describe('@puppeteer/ng-schematics: e2e', () => {
name: 'myTest', name: 'myTest',
}); });
expect(tree.files).toContain( expect(tree.files).toContain(
getMultiProjectFile('e2e/tests/my-test.e2e.ts') getMultiApplicationFile('e2e/tests/my-test.e2e.ts')
); );
expect(tree.files).not.toContain( 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', testRunner: 'node',
}); });
expect(tree.files).not.toContain( expect(tree.files).not.toContain(
getMultiProjectFile('e2e/tests/my-test.e2e.ts') getMultiApplicationFile('e2e/tests/my-test.e2e.ts')
); );
expect(tree.files).toContain( 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, route,
}); });
expect(tree.files).toContain( expect(tree.files).toContain(
getMultiProjectFile('e2e/tests/my-test.e2e.ts') getMultiApplicationFile('e2e/tests/my-test.e2e.ts')
); );
expect( expect(
tree.readContent(getMultiProjectFile('e2e/tests/my-test.e2e.ts')) tree.readContent(getMultiApplicationFile('e2e/tests/my-test.e2e.ts'))
).toContain(`setupBrowserHooks('/${route}');`); ).toContain(`setupBrowserHooks('/${route}');`);
}); });
@ -99,10 +99,10 @@ describe('@puppeteer/ng-schematics: e2e', () => {
route, route,
}); });
expect(tree.files).toContain( expect(tree.files).toContain(
getMultiProjectFile('e2e/tests/my-test.e2e.ts') getMultiApplicationFile('e2e/tests/my-test.e2e.ts')
); );
expect( expect(
tree.readContent(getMultiProjectFile('e2e/tests/my-test.e2e.ts')) tree.readContent(getMultiApplicationFile('e2e/tests/my-test.e2e.ts'))
).toContain(`setupBrowserHooks('${route}');`); ).toContain(`setupBrowserHooks('${route}');`);
}); });
}); });

View File

@ -1,9 +1,11 @@
import expect from 'expect'; import expect from 'expect';
import { import {
MULTI_LIBRARY_OPTIONS,
buildTestingTree, buildTestingTree,
getAngularJsonScripts, getAngularJsonScripts,
getMultiProjectFile, getMultiApplicationFile,
getMultiLibraryFile,
getPackageJson, getPackageJson,
runSchematic, runSchematic,
setupHttpHooks, 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 () => { it('should create base files and update to "package.json"', async () => {
const tree = await buildTestingTree('ng-add', 'multi'); const tree = await buildTestingTree('ng-add', 'multi');
const {devDependencies, scripts} = getPackageJson(tree); const {devDependencies, scripts} = getPackageJson(tree);
const {builder, configurations} = getAngularJsonScripts(tree); const {builder, configurations} = getAngularJsonScripts(tree);
expect(tree.files).toContain(getMultiProjectFile('e2e/tsconfig.json')); expect(tree.files).toContain(
expect(tree.files).toContain(getMultiProjectFile('e2e/tests/app.e2e.ts')); getMultiApplicationFile('e2e/tsconfig.json')
expect(tree.files).toContain(getMultiProjectFile('e2e/tests/utils.ts')); );
expect(tree.files).toContain(
getMultiApplicationFile('e2e/tests/app.e2e.ts')
);
expect(tree.files).toContain(
getMultiApplicationFile('e2e/tests/utils.ts')
);
expect(devDependencies).toContain('puppeteer'); expect(devDependencies).toContain('puppeteer');
expect(scripts['e2e']).toBe('ng e2e'); expect(scripts['e2e']).toBe('ng e2e');
expect(builder).toBe('@puppeteer/ng-schematics:puppeteer'); expect(builder).toBe('@puppeteer/ng-schematics:puppeteer');
@ -130,7 +138,7 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
it('should not create Puppeteer config', async () => { it('should not create Puppeteer config', async () => {
const {files} = await buildTestingTree('ng-add', 'multi'); 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'); expect(files).not.toContain('/.puppeteerrc.cjs');
}); });
it('should create Jasmine files and update "package.json"', async () => { 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 {devDependencies} = getPackageJson(tree);
const {options} = getAngularJsonScripts(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(devDependencies).toContain('jasmine');
expect(options['testRunner']).toBe('jasmine'); expect(options['testRunner']).toBe('jasmine');
}); });
@ -151,7 +159,9 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
const {devDependencies} = getPackageJson(tree); const {devDependencies} = getPackageJson(tree);
const {options} = getAngularJsonScripts(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('jest');
expect(devDependencies).toContain('@types/jest'); expect(devDependencies).toContain('@types/jest');
expect(options['testRunner']).toBe('jest'); expect(options['testRunner']).toBe('jest');
@ -163,7 +173,7 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
const {devDependencies} = getPackageJson(tree); const {devDependencies} = getPackageJson(tree);
const {options} = getAngularJsonScripts(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('mocha');
expect(devDependencies).toContain('@types/mocha'); expect(devDependencies).toContain('@types/mocha');
expect(options['testRunner']).toBe('mocha'); expect(options['testRunner']).toBe('mocha');
@ -174,12 +184,12 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
}); });
const {options} = getAngularJsonScripts(tree); const {options} = getAngularJsonScripts(tree);
expect(tree.files).toContain(getMultiProjectFile('e2e/.gitignore')); expect(tree.files).toContain(getMultiApplicationFile('e2e/.gitignore'));
expect(tree.files).not.toContain( expect(tree.files).not.toContain(
getMultiProjectFile('e2e/tests/app.e2e.ts') getMultiApplicationFile('e2e/tests/app.e2e.ts')
); );
expect(tree.files).toContain( expect(tree.files).toContain(
getMultiProjectFile('e2e/tests/app.test.ts') getMultiApplicationFile('e2e/tests/app.test.ts')
); );
expect(options['testRunner']).toBe('node'); expect(options['testRunner']).toBe('node');
}); });
@ -190,4 +200,33 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
expect(options['port']).toBeUndefined(); 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', version: '14.0.0',
}; };
const MULTI_APPLICATION_OPTIONS = {
name: 'sandbox',
};
const SINGLE_APPLICATION_OPTIONS = { const SINGLE_APPLICATION_OPTIONS = {
name: 'sandbox', name: 'sandbox',
directory: '.', directory: '.',
@ -25,6 +21,14 @@ const SINGLE_APPLICATION_OPTIONS = {
version: '14.0.0', version: '14.0.0',
}; };
const MULTI_APPLICATION_OPTIONS = {
name: SINGLE_APPLICATION_OPTIONS.name,
};
export const MULTI_LIBRARY_OPTIONS = {
name: 'components',
};
export function setupHttpHooks(): void { export function setupHttpHooks(): void {
// Stop outgoing Request for version fetching // Stop outgoing Request for version fetching
before(() => { before(() => {
@ -43,7 +47,8 @@ export function setupHttpHooks(): void {
export function getAngularJsonScripts( export function getAngularJsonScripts(
tree: UnitTestTree, tree: UnitTestTree,
isDefault = true isDefault = true,
name = SINGLE_APPLICATION_OPTIONS.name
): { ): {
builder: string; builder: string;
configurations: Record<string, any>; configurations: Record<string, any>;
@ -51,9 +56,7 @@ export function getAngularJsonScripts(
} { } {
const angularJson = tree.readJson('angular.json') as any; const angularJson = tree.readJson('angular.json') as any;
const e2eScript = isDefault ? 'e2e' : 'puppeteer'; const e2eScript = isDefault ? 'e2e' : 'puppeteer';
return angularJson['projects']?.[SINGLE_APPLICATION_OPTIONS.name]?.[ return angularJson['projects']?.[name]?.['architect'][e2eScript];
'architect'
][e2eScript];
} }
export function getPackageJson(tree: UnitTestTree): { 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}`; 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( export async function buildTestingTree(
command: 'ng-add' | 'e2e' | 'config', command: 'ng-add' | 'e2e' | 'config',
@ -109,6 +115,13 @@ export async function buildTestingTree(
MULTI_APPLICATION_OPTIONS, MULTI_APPLICATION_OPTIONS,
workingTree workingTree
); );
// Build dummy library
workingTree = await runner.runExternalSchematic(
'@schematics/angular',
'library',
MULTI_LIBRARY_OPTIONS,
workingTree
);
} }
if (command !== 'ng-add') { if (command !== 'ng-add') {