|
| 1 | +#!/usr/bin/env bash |
| 2 | +# End-to-end test with a fresh Angular app. |
| 3 | +# |
| 4 | +# Usage: test-angular-app.sh <ng-new-command> <package-tgz> <angular-version-range> |
| 5 | +# ng-new-command: command that runs `ng new`, e.g. "npx @angular/cli@22" |
| 6 | +# package-tgz: packed @angular-schule/flat-prerender |
| 7 | +# angular-version-range: version range for all @angular/* packages, e.g. "^22.0.0" or "~22.0.0" |
| 8 | +# |
| 9 | +# Checks: ng add refuses outputMode "server", ng add + ng build write flat files |
| 10 | +# (also for a second locale), and a route "index" fails with a clear message. |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +NG_NEW="$1" |
| 14 | +TGZ="$2" |
| 15 | +RANGE="$3" |
| 16 | +APP=test-app |
| 17 | + |
| 18 | +export NG_CLI_ANALYTICS=false |
| 19 | + |
| 20 | +$NG_NEW new "$APP" --defaults --ssr --skip-git --skip-install |
| 21 | +cd "$APP" |
| 22 | + |
| 23 | +# Pin all @angular/* packages to the requested range |
| 24 | +node -e ' |
| 25 | + const fs = require("fs"); |
| 26 | + const pkg = JSON.parse(fs.readFileSync("package.json", "utf8")); |
| 27 | + for (const deps of [pkg.dependencies, pkg.devDependencies]) { |
| 28 | + for (const name of Object.keys(deps ?? {})) { |
| 29 | + if (name.startsWith("@angular/")) deps[name] = process.argv[1]; |
| 30 | + } |
| 31 | + } |
| 32 | + fs.writeFileSync("package.json", JSON.stringify(pkg, null, 2)); |
| 33 | +' "$RANGE" |
| 34 | +npm install --no-audit --no-fund |
| 35 | +npm install --save-dev --no-audit --no-fund "$TGZ" |
| 36 | +NG=./node_modules/.bin/ng |
| 37 | +echo "@angular/build $(node -p 'require("@angular/build/package.json").version')" |
| 38 | + |
| 39 | +set_build_option() { |
| 40 | + node -e ' |
| 41 | + const fs = require("fs"); |
| 42 | + const a = JSON.parse(fs.readFileSync("angular.json", "utf8")); |
| 43 | + a.projects["'"$APP"'"].architect.build.options[process.argv[1]] = JSON.parse(process.argv[2]); |
| 44 | + fs.writeFileSync("angular.json", JSON.stringify(a, null, 2)); |
| 45 | + ' "$1" "$2" |
| 46 | +} |
| 47 | + |
| 48 | +expect_failure() { |
| 49 | + local expected="$1" |
| 50 | + shift |
| 51 | + if "$@" > output.log 2>&1; then |
| 52 | + cat output.log |
| 53 | + echo "Expected '$*' to fail" |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + if ! grep -q "$expected" output.log; then |
| 57 | + cat output.log |
| 58 | + echo "Expected output to contain: $expected" |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + echo "Failed as expected: $expected" |
| 62 | +} |
| 63 | + |
| 64 | +# ng new --ssr uses outputMode "server", which flat output refuses |
| 65 | +expect_failure "ships an Angular SSR server" $NG add @angular-schule/flat-prerender --skip-confirmation |
| 66 | + |
| 67 | +set_build_option outputMode '"static"' |
| 68 | +$NG add @angular-schule/flat-prerender --skip-confirmation |
| 69 | + |
| 70 | +cat > src/app/app.routes.ts <<'EOF' |
| 71 | +import { Routes } from '@angular/router'; |
| 72 | +import { App } from './app'; |
| 73 | +
|
| 74 | +export const routes: Routes = [ |
| 75 | + { path: '', component: App }, |
| 76 | + { path: 'about', component: App }, |
| 77 | + { path: 'blog', component: App }, |
| 78 | + { path: 'blog/article', component: App } |
| 79 | +]; |
| 80 | +EOF |
| 81 | + |
| 82 | +$NG build |
| 83 | +(cd "dist/$APP/browser" && find . -name '*.html' | sort) |
| 84 | +for file in index.html about.html blog.html blog/article.html; do |
| 85 | + test -f "dist/$APP/browser/$file" || { echo "Missing $file"; exit 1; } |
| 86 | +done |
| 87 | +for dir in about blog/article; do |
| 88 | + test ! -e "dist/$APP/browser/$dir/index.html" || { echo "Unexpected $dir/index.html"; exit 1; } |
| 89 | +done |
| 90 | + |
| 91 | +# Second locale: each locale keeps its start page as index.html |
| 92 | +npm install --save-dev --no-audit --no-fund "@angular/localize@$RANGE" |
| 93 | +$NG add @angular/localize --skip-confirmation |
| 94 | +mkdir -p src/locale |
| 95 | +cat > src/locale/messages.de.xlf <<'EOF' |
| 96 | +<?xml version="1.0" encoding="UTF-8" ?> |
| 97 | +<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> |
| 98 | + <file source-language="en-US" target-language="de" datatype="plaintext" original="ng2.template"> |
| 99 | + <body></body> |
| 100 | + </file> |
| 101 | +</xliff> |
| 102 | +EOF |
| 103 | +node -e ' |
| 104 | + const fs = require("fs"); |
| 105 | + const a = JSON.parse(fs.readFileSync("angular.json", "utf8")); |
| 106 | + const project = a.projects["'"$APP"'"]; |
| 107 | + project.i18n = { sourceLocale: "en-US", locales: { de: { translation: "src/locale/messages.de.xlf" } } }; |
| 108 | + project.architect.build.options.localize = true; |
| 109 | + fs.writeFileSync("angular.json", JSON.stringify(a, null, 2)); |
| 110 | +' |
| 111 | +$NG build |
| 112 | +(cd "dist/$APP/browser" && find . -name '*.html' | sort) |
| 113 | +for locale in en-US de; do |
| 114 | + for file in index.html about.html blog.html blog/article.html; do |
| 115 | + test -f "dist/$APP/browser/$locale/$file" || { echo "Missing $locale/$file"; exit 1; } |
| 116 | + done |
| 117 | + test ! -e "dist/$APP/browser/$locale/about/index.html" || { echo "Unexpected $locale/about/index.html"; exit 1; } |
| 118 | +done |
| 119 | + |
| 120 | +# A route "index" would overwrite the start page |
| 121 | +node -e ' |
| 122 | + const fs = require("fs"); |
| 123 | + const file = "src/app/app.routes.ts"; |
| 124 | + fs.writeFileSync(file, fs.readFileSync(file, "utf8").replace("];", ",\n { path: \"index\", component: App }\n];")); |
| 125 | +' |
| 126 | +expect_failure "Route '/index' cannot be prerendered" $NG build |
| 127 | + |
| 128 | +echo "Angular $RANGE: flat prerender output successful" |
0 commit comments