|
| 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