Skip to content

Commit

Permalink
test: update ssr test to use correct sha when testing `ng add @angula…
Browse files Browse the repository at this point in the history
…r/ssr`

Before this change when running snapshot test, the schematic would add `@angular/platform-server` with the same version of `@angular/core`. This causing a problem when testing snapshots as the version is not a semver specifier but rather the path to the Github build repo.

With this change, we ensure that the correct SHA is used for platform-server when running snapshot tests.
  • Loading branch information
alan-agius4 committed Aug 31, 2023
1 parent 3958466 commit c7a6c3e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 47 deletions.
6 changes: 5 additions & 1 deletion tests/legacy-cli/e2e/tests/ssr/express-engine-csp-nonce.ts
@@ -1,13 +1,17 @@
import { rimraf, writeMultipleFiles } from '../../utils/fs';
import { findFreePort } from '../../utils/network';
import { installWorkspacePackages } from '../../utils/packages';
import { execAndWaitForOutputToMatch, killAllProcesses, ng } from '../../utils/process';
import { useSha } from '../../utils/project';

export default async function () {
// forcibly remove in case another test doesn't clean itself up
await rimraf('node_modules/@angular/ssr');

await ng('add', '@angular/ssr', '--skip-confirmation');

await useSha();
await installWorkspacePackages();

await writeMultipleFiles({
'src/styles.css': `* { color: #000 }`,
'src/main.ts': `
Expand Down
5 changes: 5 additions & 0 deletions tests/legacy-cli/e2e/tests/ssr/express-engine-ngmodule.ts
@@ -1,12 +1,17 @@
import { rimraf, writeMultipleFiles } from '../../utils/fs';
import { findFreePort } from '../../utils/network';
import { installWorkspacePackages } from '../../utils/packages';
import { execAndWaitForOutputToMatch, killAllProcesses, ng } from '../../utils/process';
import { useSha } from '../../utils/project';

export default async function () {
// forcibly remove in case another test doesn't clean itself up
await rimraf('node_modules/@angular/ssr');
await ng('add', '@angular/ssr', '--skip-confirmation');

await useSha();
await installWorkspacePackages();

await writeMultipleFiles({
'src/styles.css': `* { color: #000 }`,
'src/main.ts': `
Expand Down
8 changes: 6 additions & 2 deletions tests/legacy-cli/e2e/tests/ssr/express-engine-standalone.ts
@@ -1,21 +1,25 @@
import { rimraf, writeMultipleFiles } from '../../utils/fs';
import { findFreePort } from '../../utils/network';
import { installWorkspacePackages } from '../../utils/packages';
import { execAndWaitForOutputToMatch, killAllProcesses, ng } from '../../utils/process';
import { useCIChrome, useCIDefaults } from '../../utils/project';
import { useCIChrome, useCIDefaults, useSha } from '../../utils/project';

export default async function () {
// forcibly remove in case another test doesn't clean itself up
await rimraf('node_modules/@angular/ssr');

await ng('generate', 'app', 'test-project-two', '--standalone', '--skip-install');

await ng('generate', 'e2e', '--related-app-name=test-project-two');

// Setup testing to use CI Chrome.
await useCIChrome('test-project-two', 'projects/test-project-two/e2e/');
await useCIDefaults('test-project-two');

await ng('add', '@angular/ssr', '--skip-confirmation', '--project=test-project-two');

await useSha();
await installWorkspacePackages();

await writeMultipleFiles({
'projects/test-project-two/src/styles.css': `* { color: #000 }`,
'projects/test-project-two/src/main.ts': `import { bootstrapApplication } from '@angular/platform-browser';
Expand Down
87 changes: 43 additions & 44 deletions tests/legacy-cli/e2e/utils/project.ts
Expand Up @@ -82,53 +82,52 @@ export function useBuiltPackagesVersions(): Promise<void> {
});
}

export function useSha() {
export async function useSha(): Promise<void> {
const argv = getGlobalVariable('argv');
if (argv['ng-snapshots'] || argv['ng-tag']) {
// We need more than the sha here, version is also needed. Examples of latest tags:
// 7.0.0-beta.4+dd2a650
// 6.1.6+4a8d56a
const label = argv['ng-tag'] ? argv['ng-tag'] : '';
const ngSnapshotVersions = require('../ng-snapshot/package.json');
return updateJsonFile('package.json', (json) => {
// Install over the project with snapshot builds.
function replaceDependencies(key: string) {
const missingSnapshots: string[] = [];
Object.keys(json[key] || {})
.filter((name) => name.match(/^@angular\//))
.forEach((name) => {
const pkgName = name.split(/\//)[1];
if (pkgName == 'cli') {
return;
}
if (label) {
json[key][`@angular/${pkgName}`] = `github:angular/${pkgName}-builds${label}`;
} else {
const replacement = ngSnapshotVersions.dependencies[`@angular/${pkgName}`];
if (!replacement) {
missingSnapshots.push(`missing @angular/${pkgName}`);
}
json[key][`@angular/${pkgName}`] = replacement;
if (!argv['ng-snapshots'] && !argv['ng-tag']) {
return;
}

// We need more than the sha here, version is also needed. Examples of latest tags:
// 7.0.0-beta.4+dd2a650
// 6.1.6+4a8d56a
const label = argv['ng-tag'] || '';
const ngSnapshotVersions = require('../ng-snapshot/package.json');

return updateJsonFile('package.json', (json) => {
// Install over the project with snapshot builds.
function replaceDependencies(key: string) {
const missingSnapshots: string[] = [];
Object.keys(json[key] || {})
.filter((name) => name.startsWith('@angular/'))
.forEach((name) => {
const pkgName = name.split(/\//)[1];
if (pkgName === 'cli' || pkgName === 'ssr') {
return;
}

if (label) {
json[key][`@angular/${pkgName}`] = `github:angular/${pkgName}-builds${label}`;
} else {
const replacement = ngSnapshotVersions.dependencies[`@angular/${pkgName}`];
if (!replacement) {
missingSnapshots.push(`missing @angular/${pkgName}`);
}
});
if (missingSnapshots.length > 0) {
throw new Error(
'e2e test with --ng-snapshots requires all angular packages be ' +
'listed in tests/legacy-cli/e2e/ng-snapshot/package.json.\nErrors:\n' +
missingSnapshots.join('\n '),
);
}
json[key][`@angular/${pkgName}`] = replacement;
}
});
if (missingSnapshots.length > 0) {
throw new Error(
'e2e test with --ng-snapshots requires all angular packages be ' +
'listed in tests/legacy-cli/e2e/ng-snapshot/package.json.\nErrors:\n' +
missingSnapshots.join('\n '),
);
}
try {
replaceDependencies('dependencies');
replaceDependencies('devDependencies');
} catch (e) {
return Promise.reject(e);
}
});
} else {
return Promise.resolve();
}
}

replaceDependencies('dependencies');
replaceDependencies('devDependencies');
});
}

export function useCIDefaults(projectName = 'test-project'): Promise<void> {
Expand Down

0 comments on commit c7a6c3e

Please sign in to comment.