From 96733ea5f1333f2b83b27783a44f0af6beb24132 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 31 Aug 2023 14:58:45 +0000 Subject: [PATCH] test: update ssr test to use correct sha when testing `ng add @angular/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. --- .../e2e/tests/ssr/express-engine-csp-nonce.ts | 3 +- .../e2e/tests/ssr/express-engine-ngmodule.ts | 2 + .../tests/ssr/express-engine-standalone.ts | 6 +- tests/legacy-cli/e2e/utils/project.ts | 87 +++++++++---------- 4 files changed, 50 insertions(+), 48 deletions(-) diff --git a/tests/legacy-cli/e2e/tests/ssr/express-engine-csp-nonce.ts b/tests/legacy-cli/e2e/tests/ssr/express-engine-csp-nonce.ts index bf975deedf95..bcd99c360307 100644 --- a/tests/legacy-cli/e2e/tests/ssr/express-engine-csp-nonce.ts +++ b/tests/legacy-cli/e2e/tests/ssr/express-engine-csp-nonce.ts @@ -1,12 +1,13 @@ import { rimraf, writeMultipleFiles } from '../../utils/fs'; import { findFreePort } from '../../utils/network'; 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 writeMultipleFiles({ 'src/styles.css': `* { color: #000 }`, diff --git a/tests/legacy-cli/e2e/tests/ssr/express-engine-ngmodule.ts b/tests/legacy-cli/e2e/tests/ssr/express-engine-ngmodule.ts index c4d4ae759d67..54cad34caf4a 100644 --- a/tests/legacy-cli/e2e/tests/ssr/express-engine-ngmodule.ts +++ b/tests/legacy-cli/e2e/tests/ssr/express-engine-ngmodule.ts @@ -1,11 +1,13 @@ import { rimraf, writeMultipleFiles } from '../../utils/fs'; import { findFreePort } from '../../utils/network'; 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 writeMultipleFiles({ 'src/styles.css': `* { color: #000 }`, diff --git a/tests/legacy-cli/e2e/tests/ssr/express-engine-standalone.ts b/tests/legacy-cli/e2e/tests/ssr/express-engine-standalone.ts index 4c0c94f2e18d..0472c537f2aa 100644 --- a/tests/legacy-cli/e2e/tests/ssr/express-engine-standalone.ts +++ b/tests/legacy-cli/e2e/tests/ssr/express-engine-standalone.ts @@ -1,7 +1,7 @@ import { rimraf, writeMultipleFiles } from '../../utils/fs'; import { findFreePort } from '../../utils/network'; import { execAndWaitForOutputToMatch, killAllProcesses, ng } from '../../utils/process'; -import { useCIChrome, useCIDefaults } from '../../utils/project'; +import { prepareProjectForE2e, useCIChrome, useCIDefaults } from '../../utils/project'; export default async function () { // forcibly remove in case another test doesn't clean itself up @@ -12,10 +12,10 @@ export default async function () { 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 prepareProjectForE2e('test-project-two'); + await writeMultipleFiles({ 'projects/test-project-two/src/styles.css': `* { color: #000 }`, 'projects/test-project-two/src/main.ts': `import { bootstrapApplication } from '@angular/platform-browser'; diff --git a/tests/legacy-cli/e2e/utils/project.ts b/tests/legacy-cli/e2e/utils/project.ts index fb91da71b097..a76163d337d3 100644 --- a/tests/legacy-cli/e2e/utils/project.ts +++ b/tests/legacy-cli/e2e/utils/project.ts @@ -82,53 +82,52 @@ export function useBuiltPackagesVersions(): Promise { }); } -export function useSha() { +export async function useSha(): Promise { 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.match(/^@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 {