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 Feb 2, 2024
1 parent f703e90 commit c6c2af3
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 27 deletions.
12 changes: 11 additions & 1 deletion packages/@o3r/analytics/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
addImportToAppModule,
getDefaultExecSyncOptions,
packageManagerAdd,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand Down Expand Up @@ -29,6 +29,10 @@ describe('new otter application with analytics', () => {
packageManagerExec('ng g @o3r/analytics:add-analytics --path="src/components/test-component/test-component.component.ts"', execAppOptions);
addImportToAppModule(appFolderPath, 'TestComponentModule', 'src/components/test-component');

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

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
Expand All @@ -54,6 +58,12 @@ describe('new otter application with analytics', () => {
);
addImportToAppModule(appFolderPath, 'TestComponentModule', '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/test-component.analytics.ts');

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
Expand Down
4 changes: 4 additions & 0 deletions packages/@o3r/apis-manager/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand All @@ -21,6 +22,9 @@ describe('new otter application with apis-manager', () => {
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();
});
Expand Down
4 changes: 4 additions & 0 deletions packages/@o3r/components/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand All @@ -21,6 +22,9 @@ describe('new otter application with components', () => {
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();
});
Expand Down
5 changes: 5 additions & 0 deletions packages/@o3r/configuration/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addImportToAppModule,
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand All @@ -26,6 +27,10 @@ describe('new otter application with configuration', () => {
packageManagerExec('ng g @o3r/configuration:add-config --path="src/components/test-component/test-component.component.ts"', execAppOptions);
addImportToAppModule(appFolderPath, 'TestComponentModule', 'src/components/test-component');

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

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
Expand Down
39 changes: 35 additions & 4 deletions packages/@o3r/core/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addImportToAppModule,
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand Down Expand Up @@ -80,11 +81,23 @@ describe('new otter application', () => {
packageManagerExec('ng g @schematics/angular:component test-ng-component', execAppOptions);
packageManagerExec('ng g @o3r/core:convert-component --path="src/app/test-ng-component/test-ng-component.component.ts"', execAppOptions);

packageManagerExec('ng g @o3r/testing:playwright-scenario --name=test-scenario', execAppOptions);
packageManagerExec('ng g @o3r/testing:playwright-sanity --name=test-sanity', execAppOptions);

const diff = getGitDiff(execAppOptions.cwd as string);

// Expect created files inside `test-app` project
expect(diff.added.filter((file) => /e2e-playwright/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /src[\\/]app/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /src[\\/]components/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /src[\\/]environments/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /src[\\/]services/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /src[\\/]store/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /src[\\/]styling/.test(file)).length).toBeGreaterThan(0);

expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();

// should pass the e2e tests
packageManagerExec('ng g @o3r/testing:playwright-scenario --name=test-scenario', execAppOptions);
packageManagerExec('ng g @o3r/testing:playwright-sanity --name=test-sanity', execAppOptions);
spawn(`npx http-server -p ${devServerPort} ./dist/browser`, [], {
...execAppOptions,
shell: true,
Expand Down Expand Up @@ -203,11 +216,29 @@ describe('new otter application', () => {
execAppOptions
);

packageManagerExec(`ng g @o3r/testing:playwright-scenario --name=test-scenario ${projectName}`, execAppOptions);
packageManagerExec(`ng g @o3r/testing:playwright-sanity --name=test-sanity ${projectName}`, execAppOptions);

const diff = getGitDiff(execAppOptions.cwd as string);

// Expect no file modified inside 'dont-modify-me' project
expect(diff.all.filter((file) => /projects[\\/]dont-modify-me/.test(file)).length).toBe(0);

// Expect no file created outside 'test-app' project
expect(diff.added.filter((file) => !/projects[\\/]test-app/.test(file)).length).toBe(0);

// Expect created files inside `test-app` project
expect(diff.added.filter((file) => /projects[\\/]test-app[\\/]e2e-playwright/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /projects[\\/]test-app[\\/]src[\\/]app/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /projects[\\/]test-app[\\/]src[\\/]components/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /projects[\\/]test-app[\\/]src[\\/]environments/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /projects[\\/]test-app[\\/]src[\\/]services/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /projects[\\/]test-app[\\/]src[\\/]store/.test(file)).length).toBeGreaterThan(0);
expect(diff.added.filter((file) => /projects[\\/]test-app[\\/]src[\\/]styling/.test(file)).length).toBeGreaterThan(0);

expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();

// should pass the e2e tests
packageManagerExec(`ng g @o3r/testing:playwright-scenario --name=test-scenario ${projectName}`, execAppOptions);
packageManagerExec(`ng g @o3r/testing:playwright-sanity --name=test-sanity ${projectName}`, execAppOptions);
spawn(`npx http-server -p ${devServerPort} ./projects/test-app/dist/browser`, [], {
...execAppOptions,
shell: true,
Expand Down
4 changes: 4 additions & 0 deletions packages/@o3r/extractors/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand All @@ -21,6 +22,9 @@ describe('new otter application with extractors', () => {
test('should add extractors to existing application', () => {
packageManagerExec(`ng add --skip-confirmation @o3r/extractors@${o3rVersion}`, execAppOptions);

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

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
Expand Down
6 changes: 6 additions & 0 deletions packages/@o3r/localization/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addImportToAppModule,
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand Down Expand Up @@ -28,6 +29,11 @@ describe('new otter application with localization', () => {
execAppOptions);
addImportToAppModule(appFolderPath, 'TestComponentModule', 'src/components/test-component');

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

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
Expand Down
4 changes: 4 additions & 0 deletions packages/@o3r/rules-engine/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addImportToAppModule,
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand All @@ -26,6 +27,9 @@ describe('new otter application with rules-engine', () => {
packageManagerExec('ng g @o3r/rules-engine:rules-engine-to-component --path=src/components/test-component/test-component.component.ts', execAppOptions);
addImportToAppModule(appFolderPath, 'TestComponentModule', 'src/components/test-component');

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

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
Expand Down
5 changes: 5 additions & 0 deletions packages/@o3r/styling/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addImportToAppModule,
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand All @@ -26,6 +27,10 @@ describe('new otter application with styling', () => {
packageManagerExec('ng g @o3r/styling:add-theming --path="src/components/test-component/test-component.style.scss"', execAppOptions);
addImportToAppModule(appFolderPath, 'TestComponentModule', 'src/components/test-component');

const diff = getGitDiff(execAppOptions.cwd as string);
expect(diff.modified).toContain('package.json');
expect(diff.added).toContain('src/components/test-component/test-component.style.theme.scss');

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
Expand Down
21 changes: 2 additions & 19 deletions packages/@o3r/test-helpers/src/prepare-test-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { minVersion } from 'semver';
import { createTestEnvironmentAngular } from './test-environments/create-test-environment-angular';
import { createTestEnvironmentAngularWithO3rCore } from './test-environments/create-test-environment-angular-with-o3r-core';
import { createTestEnvironmentBlank } from './test-environments/create-test-environment-blank';
import {createWithLock, packageManagerInstall, setPackagerManagerConfig} from './utilities';
import { createWithLock, packageManagerInstall, setPackagerManagerConfig, setupGit } from './utilities';

/**
* 'blank' only create yarn/npm config
Expand Down Expand Up @@ -134,24 +134,7 @@ export async function prepareTestEnv(folderName: string, type: PrepareTestEnvTyp
}

// Setup git and initial commit to easily make checks on the diff inside the tests
try {
const authorName = 'otter it tests';
const authorEmail = '[email protected]';
execSync('git init -b master && git add -A && git commit -m "initial commit"', {
cwd: appFolderPath,
env: {
/* eslint-disable @typescript-eslint/naming-convention, camelcase */
GIT_AUTHOR_NAME: authorName,
GIT_COMMITTER_NAME: authorName,
GIT_AUTHOR_EMAIL: authorEmail,
GIT_COMMITTER_EMAIL: authorEmail
/* eslint-enable @typescript-eslint/naming-convention, camelcase */
}
});
} catch (err) {
// eslint-disable-next-line no-console
console.error('Unable to setup git for the test-app', err);
}
setupGit(appFolderPath);

return appFolderPath;
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ export async function createTestEnvironmentAngularWithO3rCore(inputOptions: Part
}
const o3rVersion = '999.0.0';
if (options.generateMonorepo) {
packageManagerExec(`ng add --skip-confirmation @o3r/core@${o3rVersion}`, execAppOptions);
packageManagerExec(`ng add --skip-confirmation @o3r/core@${o3rVersion} --skip-git`, execAppOptions);
// FIXME: workaround for yarn pnp (same issue with node_modules but the runner won't complain if package is present in root instead of project)
packageManagerAdd(`@o3r/core@${o3rVersion}`, {...execAppOptions, cwd: path.join(appFolderPath, 'projects', 'test-app')});
packageManagerExec(`ng add --skip-confirmation @o3r/core@${o3rVersion} --project-name=test-app`, execAppOptions);
packageManagerExec(`ng add --skip-confirmation @o3r/core@${o3rVersion} --project-name=test-app --skip-git`, execAppOptions);
} else {
packageManagerExec(`ng add --skip-confirmation @o3r/core@${o3rVersion}`, execAppOptions);
packageManagerExec(`ng add --skip-confirmation @o3r/core@${o3rVersion} --skip-git`, execAppOptions);
}

packageManagerInstall(execAppOptions);
Expand Down
44 changes: 44 additions & 0 deletions packages/@o3r/test-helpers/src/utilities/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { execFileSync, ExecFileSyncOptionsWithStringEncoding, execSync } from 'node:child_process';

/**
* Setup git and initial commit
* @param workingDirectory
*/
export function setupGit(workingDirectory?: string) {
const authorName = 'otter it tests';
const authorEmail = '[email protected]';
execSync('git init -b master && git add -A && git commit -m "initial commit" && git tag -a after-init -m "after-init"', {
cwd: workingDirectory,
env: {
/* eslint-disable @typescript-eslint/naming-convention, camelcase */
GIT_AUTHOR_NAME: authorName,
GIT_COMMITTER_NAME: authorName,
GIT_AUTHOR_EMAIL: authorEmail,
GIT_COMMITTER_EMAIL: authorEmail
/* eslint-enable @typescript-eslint/naming-convention, camelcase */
}
});
}

/**
* Get all files added, modified or deleted based on baseBranch param
* @param workingDirectory
* @param baseBranch
*/
export function getGitDiff(workingDirectory?: string, baseBranch = 'after-init') {
const execOptions: ExecFileSyncOptionsWithStringEncoding = {stdio: ['pipe', 'pipe', 'ignore'], encoding: 'utf8', cwd: workingDirectory};
const untrackedFiles = execFileSync('git', ['ls-files', '--others', '--exclude-standard'], execOptions).split('\n');
const trackedFiles = execFileSync('git', ['diff', '--name-status', baseBranch], execOptions).split('\n');
const indexedFiles = execFileSync('git', ['diff', '--cached', '--name-status', baseBranch], execOptions).split('\n');
const extractFile = (line: string) => line.replace(/^[ADM]\s*/, '');
const cleanList = (list: string[]) => [...new Set(list.filter((file) => !!file))];
const added = cleanList([...untrackedFiles, ...[...trackedFiles, ...indexedFiles].filter((line) => /^A/.test(line)).map(extractFile)]);
const modified = cleanList([...trackedFiles, ...indexedFiles].filter((line) => /^M/.test(line)).map(extractFile));
const deleted = cleanList([...trackedFiles, ...indexedFiles].filter((line) => /^D/.test(line)).map(extractFile));
return {
added,
modified,
deleted,
all: cleanList([...added, ...modified, ...deleted])
};
}
1 change: 1 addition & 0 deletions packages/@o3r/test-helpers/src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './angular';
export * from './create-with-lock';
export * from './exec';
export * from './git';
export * from './locker';
export * from './package-manager';
export * from './verdaccio';
Expand Down
5 changes: 5 additions & 0 deletions packages/@o3r/testing/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addImportToAppModule,
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand All @@ -27,6 +28,10 @@ describe('new otter application with testing', () => {
packageManagerExec('ng g @o3r/testing:add-fixture --path="src/components/test-component/presenter/test-component-pres.component.ts"', execAppOptions);
addImportToAppModule(appFolderPath, 'TestComponentContModule', 'src/components/test-component');

const diff = getGitDiff(execAppOptions.cwd as string);
expect(diff.added).toContain('src/components/test-component/container/test-component-cont.fixture.ts');
expect(diff.added).toContain('src/components/test-component/presenter/test-component-pres.fixture.ts');

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRun('build', execAppOptions)).not.toThrow();
});
Expand Down

0 comments on commit c6c2af3

Please sign in to comment.