Skip to content

Commit 0589791

Browse files
committed
test: setup ng-update v13 integration test with Bazel
Sets up the `ng update` v13 integration test to run with Bazel. The test will run `ng update` for the CDK and Material and will then compare the migrated files against some expectation files. like for custom theme file where the tilde should be removed.
1 parent bf685ac commit 0589791

File tree

5 files changed

+173
-1
lines changed

5 files changed

+173
-1
lines changed

.bazelignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
node_modules
2+
3+
integration/ng-update-v13/.angular
4+
integration/ng-update-v13/node_modules

integration/ng-update-v13/BUILD.bazel

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
load("@bazel_skylib//lib:dicts.bzl", "dicts")
2+
load("//tools:integration.bzl", "CLI_PROJECT_MAPPINGS")
3+
load("//tools:defaults.bzl", "node_integration_test")
4+
5+
npmPackageMappings = dicts.add(
6+
CLI_PROJECT_MAPPINGS,
7+
{
8+
"//src/cdk:npm_package_archive": "@angular/cdk",
9+
"//src/material:npm_package_archive": "@angular/material",
10+
},
11+
)
12+
13+
node_integration_test(
14+
name = "test",
15+
srcs = glob(["**/*"]),
16+
commands = [
17+
# Note: We use a cache folder within the integration test as otherwise
18+
# the NPM package mapped archive would be cached in the system.
19+
# See: https://github.com/yarnpkg/yarn/issues/2165.
20+
# TODO(devversion): determine if a solution/workaround could live in the test runner.
21+
"yarn install --cache-folder .yarn_cache_folder/",
22+
"yarn test",
23+
],
24+
npm_packages = npmPackageMappings,
25+
)
26+
27+
node_integration_test(
28+
name = "test_node12",
29+
srcs = glob(["**/*"]),
30+
commands = [
31+
# Note: We use a cache folder within the integration test as otherwise
32+
# the NPM package mapped archive would be cached in the system.
33+
# See: https://github.com/yarnpkg/yarn/issues/2165.
34+
# TODO(devversion): determine if a solution/workaround could live in the test runner.
35+
"yarn install --cache-folder .yarn_cache_folder/",
36+
"yarn test",
37+
],
38+
npm_packages = npmPackageMappings,
39+
tool_mappings = {
40+
"@node12_host//:yarn_bin": "yarn",
41+
"@node12_host//:node_bin": "node",
42+
},
43+
)

integration/ng-update-v13/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"ng": "ng",
66
"start": "ng serve",
77
"build": "ng build",
8-
"watch": "ng build --watch --configuration development"
8+
"watch": "ng build --watch --configuration development",
9+
"test": "node ./test-migrations.js"
910
},
1011
"private": true,
1112
"dependencies": {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
// Custom Theming for Angular Material
3+
// For more information: https://material.angular.io/guide/theming
4+
@use '@angular/material' as mat;
5+
// Plus imports for other components in your app.
6+
7+
// Include the common styles for Angular Material. We include this here so that you only
8+
// have to load a single css file for Angular Material in your app.
9+
// Be sure that you only ever include this mixin once!
10+
@include mat.core();
11+
12+
// Define the palettes for your theme using the Material Design palettes available in palette.scss
13+
// (imported above). For each palette, you can optionally specify a default, lighter, and darker
14+
// hue. Available color palettes: https://material.io/design/color/
15+
$ng-update-primary: mat.define-palette(mat.$indigo-palette);
16+
$ng-update-accent: mat.define-palette(mat.$pink-palette, A200, A100, A400);
17+
18+
// The warn palette is optional (defaults to red).
19+
$ng-update-warn: mat.define-palette(mat.$red-palette);
20+
21+
// Create the theme object. A theme consists of configurations for individual
22+
// theming systems such as "color" or "typography".
23+
$ng-update-theme: mat.define-light-theme((
24+
color: (
25+
primary: $ng-update-primary,
26+
accent: $ng-update-accent,
27+
warn: $ng-update-warn,
28+
)
29+
));
30+
31+
// Include theme styles for core and each component used in your app.
32+
// Alternatively, you can import and @include the theme mixins for each component
33+
// that you are using.
34+
@include mat.all-component-themes($ng-update-theme);
35+
36+
/* You can add global styles to this file, and also import other style files */
37+
38+
html, body { height: 100%; }
39+
body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
const path = require('path');
10+
const fs = require('fs');
11+
const child_process = require('child_process');
12+
const glob = require('glob');
13+
const chalk = require('chalk');
14+
const diff = require('diff');
15+
16+
const projectDir = __dirname;
17+
const cliBinPath = path.join(projectDir, 'node_modules/@angular/cli/bin/ng');
18+
19+
const expectationFiles = glob.sync('**/*.expected', {cwd: projectDir});
20+
const fromVersion = '12.0.0';
21+
const toVersion = '13.0.0';
22+
23+
runUpdateCommand('@angular/cdk');
24+
runUpdateCommand('@angular/material');
25+
26+
let testsPassing = true;
27+
28+
// Check if each expectation file matches the actual file in the CLI project.
29+
expectationFiles.forEach(relativeFilePath => {
30+
const actualFilePath = relativeFilePath.replace(/\.(ts|scss)\.expected$/, '.$1');
31+
const expectedContent = fs.readFileSync(path.join(projectDir, relativeFilePath), 'utf8');
32+
const actualContent = fs.readFileSync(path.join(projectDir, actualFilePath), 'utf8');
33+
34+
if (expectedContent === actualContent) {
35+
console.log(chalk.green(`✓ File "${actualFilePath}" matches the expected output.`));
36+
} else {
37+
testsPassing = false;
38+
console.error(chalk.red(`✘ File "${actualFilePath}" does not match the expected output.`));
39+
console.log(chalk.yellow('--------------------------------------------'));
40+
printColoredPatch(actualFilePath, actualContent, expectedContent);
41+
console.log(chalk.yellow('--------------------------------------------\n'));
42+
}
43+
});
44+
45+
process.exit(testsPassing ? 0 : 1);
46+
47+
/** Runs the `ng update` command for the given package. */
48+
function runUpdateCommand(packageName) {
49+
// Note that we need to specify "--allow-dirty" as the repository will become dirty
50+
// if dependencies for the integration test are installed (i.e. modified lock files)
51+
const updateCommandArgs = ['--migrate-only', '--from', fromVersion,
52+
'--to', toVersion, '--allow-dirty'];
53+
54+
// Print out the command that is used to run the migrations for easier debugging.
55+
console.error(`Running "ng update ${updateCommandArgs.join(' ')}":`);
56+
console.error(chalk.yellow(`------------------------------------------`));
57+
58+
const updateProcess = child_process.spawnSync(
59+
'node', [cliBinPath, 'update', packageName, ...updateCommandArgs], {stdio: 'inherit', cwd: projectDir});
60+
61+
console.error(chalk.yellow(`------------------------------------------\n`));
62+
63+
if (updateProcess.status !== 0) {
64+
console.error(chalk.red('✘ Running "ng update" failed. See output above.'));
65+
process.exit(1);
66+
}
67+
}
68+
69+
/** Compares the two strings and prints out a colored diff to stdout. */
70+
function printColoredPatch(actualFilePath, actualContent, expectedContent) {
71+
const patchLines =
72+
diff.createPatch(
73+
actualFilePath, expectedContent, actualContent, 'Expected content', 'Actual content')
74+
.split(/\r?\n/);
75+
// Walk through each line of the patch and print it. We omit the first two lines
76+
// as these are the patch header and not relevant to the test.
77+
for (let line of patchLines.slice(2)) {
78+
if (line.startsWith('+')) {
79+
console.log(chalk.green(line));
80+
} else if (line.startsWith('-')) {
81+
console.log(chalk.red(line));
82+
} else {
83+
console.log(chalk.grey(line));
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)