Skip to content

Commit

Permalink
test: add checks based on git diff in it tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fpaul-1A committed Sep 22, 2023
1 parent f5616e0 commit 1102f50
Show file tree
Hide file tree
Showing 36 changed files with 642 additions and 173 deletions.
1 change: 1 addition & 0 deletions .github/workflows/it-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jobs:
- name: Make sure that internal packages are not cached for whatever reason - npm
run: |
npm cache --cache=.cache/test-app/npm-cache ls | grep 127.0.0.1:4873 | xargs -d'\n' -r -n 1 npm cache --cache=.cache/test-app/npm-cache clean || true
npx --yes rimraf -g .cache/test-app/.angular/cache/*/angular-webpack
shell: bash
- uses: actions/download-artifact@v3
name: Download verdaccio storage prepared in the previous job
Expand Down
18 changes: 14 additions & 4 deletions packages/@o3r/analytics/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
addImportToAppModule,
getDefaultExecSyncOptions,
packageManagerAdd,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
prepareTestEnv,
setupLocalRegistry
} from '@o3r/test-helpers';
import { rm } from 'node:fs/promises';
import { join } from 'node:path';

const appName = 'test-app-analytics';
Expand All @@ -29,9 +30,14 @@ describe('new otter application with analytics', () => {
packageManagerExec('ng g @o3r/analytics:add-analytics --path="src/components/test-component/container/test-component-cont.component.ts"', execAppOptions);
addImportToAppModule(appFolderPath, 'TestComponentContModule', 'src/components/test-component');

const diff = getGitDiff(appFolderPath);
expect(diff.modified).toContain('package.json');
expect(diff.added).toContain('src/components/test-component/container/test-component-cont.analytics.ts');

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
afterAll(() => rm(execAppOptions.cwd, {recursive: true}));
});

describe('monorepo', () => {
Expand All @@ -41,9 +47,6 @@ describe('new otter application with analytics', () => {
execAppOptions.cwd = workspacePath;
});
test('should add analytics to existing application', () => {
// FIXME workaround for pnp
packageManagerAdd(`@o3r/analytics@${o3rVersion}`, {...execAppOptions, cwd: appFolderPath});

const projectName = '--project-name=test-app';
packageManagerExec(`ng add --skip-confirmation @o3r/analytics@${o3rVersion} ${projectName}`, execAppOptions);

Expand All @@ -54,8 +57,15 @@ describe('new otter application with analytics', () => {
);
addImportToAppModule(appFolderPath, 'TestComponentContModule', 'projects/test-app/src/components/test-component');

const diff = getGitDiff(execAppOptions.cwd as string);
expect(diff.all.some((file) => /projects[\\/]dont-modify-me/.test(file))).toBe(false);
expect(diff.modified).toContain('package.json');
expect(diff.modified).toContain('projects/test-app/package.json');
expect(diff.added).toContain('projects/test-app/src/components/test-component/container/test-component-cont.analytics.ts');

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
afterAll(() => rm(execAppOptions.cwd, {recursive: true}));
});
});
15 changes: 13 additions & 2 deletions packages/@o3r/analytics/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import type { Rule } from '@angular-devkit/schematics';
import * as fs from 'node:fs';
import * as path from 'node:path';
import type { NgAddSchematicsSchema } from './schema';

/**
* Add Otter analytics to an Angular Project
*
* @param options
*/
export function ngAdd(): Rule {
export function ngAdd(options: NgAddSchematicsSchema): Rule {
/* ng add rules */
return () => {};
return async (tree) => {
const { addDependenciesInPackageJson, getWorkspaceConfig } = await import('@o3r/schematics');
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }));
const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const workingDirectory = workspaceProject?.root;

return addDependenciesInPackageJson([packageJson.name!], {...options, workingDirectory, version: packageJson.version});
};
}
44 changes: 37 additions & 7 deletions packages/@o3r/apis-manager/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
prepareTestEnv,
setupLocalRegistry
} from '@o3r/test-helpers';
import { rm } from 'node:fs/promises';
import { join } from 'node:path';

const appName = 'test-app-apis-manager';
const o3rVersion = '999.0.0';
Expand All @@ -14,14 +17,41 @@ let appFolderPath: string;

describe('new otter application with apis-manager', () => {
setupLocalRegistry();
beforeAll(async () => {
appFolderPath = await prepareTestEnv(appName, 'angular-with-o3r-core');
execAppOptions.cwd = appFolderPath;
describe('standalone', () => {
beforeAll(async () => {
appFolderPath = await prepareTestEnv(appName, 'angular-with-o3r-core');
execAppOptions.cwd = appFolderPath;
});
test('should add apis-manager to existing application', () => {
packageManagerExec(`ng add --skip-confirmation @o3r/apis-manager@${o3rVersion}`, execAppOptions);

const diff = getGitDiff(appFolderPath);
expect(diff.modified).toContain('package.json');

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
afterAll(() => rm(execAppOptions.cwd, {recursive: true}));
});
test('should add apis-manager to existing application', () => {
packageManagerExec(`ng add --skip-confirmation @o3r/apis-manager@${o3rVersion}`, execAppOptions);

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
describe('monorepo', () => {
beforeAll(async () => {
const workspacePath = await prepareTestEnv(`${appName}-monorepo`, 'angular-monorepo-with-o3r-core');
appFolderPath = join(workspacePath, 'projects', 'test-app');
execAppOptions.cwd = workspacePath;
});
test('should add apis-manager to existing application', () => {
const projectName = '--project-name=test-app';
packageManagerExec(`ng add --skip-confirmation @o3r/apis-manager@${o3rVersion} ${projectName}`, execAppOptions);

const diff = getGitDiff(execAppOptions.cwd as string);
expect(diff.all.some((file) => /projects[\\/]dont-modify-me/.test(file))).toBe(false);
expect(diff.modified).toContain('package.json');
expect(diff.modified).toContain('projects/test-app/package.json');

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
afterAll(() => rm(execAppOptions.cwd, {recursive: true}));
});
});
8 changes: 6 additions & 2 deletions packages/@o3r/apis-manager/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { chain, noop, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';

import * as fs from 'node:fs';
import * as path from 'node:path';
import { NgAddSchematicsSchema } from './schema';

Expand All @@ -10,9 +11,11 @@ import { NgAddSchematicsSchema } from './schema';
export function ngAdd(options: NgAddSchematicsSchema): Rule {
return async (tree: Tree, context: SchematicContext) => {
try {
const { ngAddPackages, getO3rPeerDeps, applyEsLintFix, getWorkspaceConfig, getProjectNewDependenciesType } = await import('@o3r/schematics');
const { addDependenciesInPackageJson, ngAddPackages, getO3rPeerDeps, applyEsLintFix, getWorkspaceConfig, getProjectNewDependenciesType } = await import('@o3r/schematics');
const { updateApiDependencies } = await import('../helpers/update-api-deps');
const depsInfo = getO3rPeerDeps(path.resolve(__dirname, '..', '..', 'package.json'));
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }));
const depsInfo = getO3rPeerDeps(packageJsonPath);
const rulesToExecute: Rule[] = [];
const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const workingDirectory = workspaceProject?.root;
Expand All @@ -22,6 +25,7 @@ export function ngAdd(options: NgAddSchematicsSchema): Rule {
}

return () => chain([
addDependenciesInPackageJson([packageJson.name!], {...options, workingDirectory, version: packageJson.version}),
...rulesToExecute,
options.skipLinter ? noop : applyEsLintFix(),
ngAddPackages(depsInfo.o3rPeerDeps, {
Expand Down
8 changes: 6 additions & 2 deletions packages/@o3r/application/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { chain } from '@angular-devkit/schematics';
import * as fs from 'node:fs';
import * as path from 'node:path';
import type { NgAddSchematicsSchema } from './schema';

Expand All @@ -13,11 +14,13 @@ export function ngAdd(options: NgAddSchematicsSchema): Rule {
return async (tree: Tree, context: SchematicContext) => {
try {
const {
addImportToModuleFile, getAppModuleFilePath, getWorkspaceConfig, insertImportToModuleFile, ngAddPackages, getO3rPeerDeps, getProjectNewDependenciesType
addDependenciesInPackageJson, addImportToModuleFile, getAppModuleFilePath, getWorkspaceConfig, insertImportToModuleFile, ngAddPackages, getO3rPeerDeps, getProjectNewDependenciesType
} = await import('@o3r/schematics');
const {getDecoratorMetadata, isImported} = await import('@schematics/angular/utility/ast-utils');
const ts = await import('typescript');
const depsInfo = getO3rPeerDeps(path.resolve(__dirname, '..', '..', 'package.json'));
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }));
const depsInfo = getO3rPeerDeps(packageJsonPath);

const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const workingDirectory = workspaceProject?.root;
Expand Down Expand Up @@ -71,6 +74,7 @@ export function ngAdd(options: NgAddSchematicsSchema): Rule {
const dependencyType = getProjectNewDependenciesType(workspaceProject);

return () => chain([
addDependenciesInPackageJson([packageJson.name!], {...options, workingDirectory, version: packageJson.version}),
ngAddPackages(depsInfo.o3rPeerDeps, {
skipConfirmation: true,
version: depsInfo.packageVersion,
Expand Down
44 changes: 37 additions & 7 deletions packages/@o3r/components/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
prepareTestEnv,
setupLocalRegistry
} from '@o3r/test-helpers';
import { rm } from 'node:fs/promises';
import { join } from 'node:path';

const appName = 'test-app-apis-components';
const o3rVersion = '999.0.0';
Expand All @@ -14,14 +17,41 @@ let appFolderPath: string;

describe('new otter application with components', () => {
setupLocalRegistry();
beforeAll(async () => {
appFolderPath = await prepareTestEnv(appName, 'angular-with-o3r-core');
execAppOptions.cwd = appFolderPath;
describe('standalone', () => {
beforeAll(async () => {
appFolderPath = await prepareTestEnv(appName, 'angular-with-o3r-core');
execAppOptions.cwd = appFolderPath;
});
test('should add components to existing application', () => {
packageManagerExec(`ng add --skip-confirmation @o3r/components@${o3rVersion} --enable-metadata-extract`, execAppOptions);

const diff = getGitDiff(appFolderPath);
expect(diff.modified).toContain('package.json');

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
afterAll(() => rm(execAppOptions.cwd, {recursive: true}));
});
test('should add components to existing application', () => {
packageManagerExec(`ng add --skip-confirmation @o3r/components@${o3rVersion} --enable-metadata-extract`, execAppOptions);

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
describe('monorepo', () => {
beforeAll(async () => {
const workspacePath = await prepareTestEnv(`${appName}-monorepo`, 'angular-monorepo-with-o3r-core');
appFolderPath = join(workspacePath, 'projects', 'test-app');
execAppOptions.cwd = workspacePath;
});
test('should add components to existing application', () => {
const projectName = '--project-name=test-app';
packageManagerExec(`ng add --skip-confirmation @o3r/components@${o3rVersion} --enable-metadata-extract ${projectName}`, execAppOptions);

const diff = getGitDiff(execAppOptions.cwd as string);
expect(diff.all.some((file) => /projects[\\/]dont-modify-me/.test(file))).toBe(false);
expect(diff.modified).toContain('package.json');
expect(diff.modified).toContain('projects/test-app/package.json');

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
afterAll(() => rm(execAppOptions.cwd, {recursive: true}));
});
});
2 changes: 2 additions & 0 deletions packages/@o3r/components/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function ngAdd(options: NgAddSchematicsSchema): Rule {
return async (tree: Tree, context: SchematicContext) => {
try {
const {
addDependenciesInPackageJson,
getDefaultOptionsForSchematic,
getO3rPeerDeps,
getProjectNewDependenciesType,
Expand All @@ -36,6 +37,7 @@ export function ngAdd(options: NgAddSchematicsSchema): Rule {
const workingDirectory = workspaceProject?.root || '.';
const dependencyType = getProjectNewDependenciesType(workspaceProject);
const rule = chain([
addDependenciesInPackageJson([packageJson.name!], {...options, workingDirectory, version: packageJson.version}),
removePackages(['@otter/components']),
ngAddPackages(depsInfo.o3rPeerDeps, {
skipConfirmation: true,
Expand Down
56 changes: 46 additions & 10 deletions packages/@o3r/configuration/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
addImportToAppModule,
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
prepareTestEnv,
setupLocalRegistry
} from '@o3r/test-helpers';
import { rm } from 'node:fs/promises';
import { join } from 'node:path';

const appName = 'test-app-configuration';
const o3rVersion = '999.0.0';
Expand All @@ -15,18 +18,51 @@ let appFolderPath: string;

describe('new otter application with configuration', () => {
setupLocalRegistry();
beforeAll(async () => {
appFolderPath = await prepareTestEnv(appName, 'angular-with-o3r-core');
execAppOptions.cwd = appFolderPath;
describe('standalone', () => {
beforeAll(async () => {
appFolderPath = await prepareTestEnv(appName, 'angular-with-o3r-core');
execAppOptions.cwd = appFolderPath;
});
test('should add configuration to existing application', () => {
packageManagerExec(`ng add --skip-confirmation @o3r/configuration@${o3rVersion}`, execAppOptions);

packageManagerExec('ng g @o3r/core:component test-component --use-otter-config=false', execAppOptions);
packageManagerExec('ng g @o3r/configuration:add-config --path="src/components/test-component/container/test-component-cont.component.ts"', execAppOptions);
addImportToAppModule(appFolderPath, 'TestComponentContModule', 'src/components/test-component');

const diff = getGitDiff(appFolderPath);
expect(diff.modified).toContain('package.json');
expect(diff.added).toContain('src/components/test-component/container/test-component-cont.config.ts');

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
afterAll(() => rm(execAppOptions.cwd, {recursive: true}));
});
test('should add configuration to existing application', () => {
packageManagerExec(`ng add --skip-confirmation @o3r/configuration@${o3rVersion}`, execAppOptions);

packageManagerExec('ng g @o3r/core:component test-component --use-otter-config=false', execAppOptions);
packageManagerExec('ng g @o3r/configuration:add-config --path="src/components/test-component/container/test-component-cont.component.ts"', execAppOptions);
addImportToAppModule(appFolderPath, 'TestComponentContModule', 'src/components/test-component');
describe('monorepo', () => {
beforeAll(async () => {
const workspacePath = await prepareTestEnv(`${appName}-monorepo`, 'angular-monorepo-with-o3r-core');
appFolderPath = join(workspacePath, 'projects', 'test-app');
execAppOptions.cwd = workspacePath;
});
test('should add configuration to existing application', () => {
const projectName = '--project-name=test-app';
packageManagerExec(`ng add --skip-confirmation @o3r/configuration@${o3rVersion} ${projectName}`, execAppOptions);

packageManagerExec(`ng g @o3r/core:component test-component --use-otter-config=false ${projectName}`, execAppOptions);
packageManagerExec('ng g @o3r/configuration:add-config --path="projects/test-app/src/components/test-component/container/test-component-cont.component.ts"', execAppOptions);
addImportToAppModule(appFolderPath, 'TestComponentContModule', 'projects/test-app/src/components/test-component');

const diff = getGitDiff(execAppOptions.cwd as string);
expect(diff.all.some((file) => /projects[\\/]dont-modify-me/.test(file))).toBe(false);
expect(diff.modified).toContain('package.json');
expect(diff.modified).toContain('projects/test-app/package.json');
expect(diff.added).toContain('projects/test-app/src/components/test-component/container/test-component-cont.config.ts');

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
afterAll(() => rm(execAppOptions.cwd, {recursive: true}));
});
});
2 changes: 2 additions & 0 deletions packages/@o3r/configuration/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function ngAdd(options: NgAddSchematicsSchema) {
return async (tree: Tree, context: SchematicContext) => {
try {
const {
addDependenciesInPackageJson,
ngAddPackages,
getProjectNewDependenciesType,
getWorkspaceConfig,
Expand All @@ -28,6 +29,7 @@ export function ngAdd(options: NgAddSchematicsSchema) {
context.logger.info(`The package ${depsInfo.packageName as string} comes with a debug mechanism`);
context.logger.info('Get more information on the following page: https://github.com/AmadeusITGroup/otter/tree/main/docs/configuration/OVERVIEW.md#Runtime-debugging');
return chain([
addDependenciesInPackageJson(['@o3r/configuration'], {...options, workingDirectory, version: packageJson.version}),
registerPackageCollectionSchematics(packageJson),
setupSchematicsDefaultParams({
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down
Loading

0 comments on commit 1102f50

Please sign in to comment.