|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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 | +// tslint:disable:no-console |
| 9 | +// tslint:disable:no-implicit-dependencies |
| 10 | +import { logging } from '@angular-devkit/core'; |
| 11 | +import { execSync, spawnSync } from 'child_process'; |
| 12 | +import * as glob from 'glob'; |
| 13 | +import 'jasmine'; |
| 14 | +import { SpecReporter as JasmineSpecReporter, StacktraceOption } from 'jasmine-spec-reporter'; |
| 15 | +import { ParsedArgs } from 'minimist'; |
| 16 | +import { join, normalize, relative } from 'path'; |
| 17 | +import * as ts from 'typescript'; |
| 18 | +import { packages } from '../lib/packages'; |
| 19 | + |
| 20 | +const Jasmine = require('jasmine'); |
| 21 | + |
| 22 | +const knownFlakes = [ |
| 23 | + // Rebuild tests in test-large are flakey if not run as the first suite. |
| 24 | + // https://github.com/angular/angular-cli/pull/15204 |
| 25 | + 'packages/angular_devkit/build_angular/test/browser/rebuild_spec_large.ts', |
| 26 | + 'packages/angular_devkit/build_angular/test/browser/web-worker_spec_large.ts', |
| 27 | +]; |
| 28 | + |
| 29 | +const projectBaseDir = join(__dirname, '..'); |
| 30 | +require('source-map-support').install({ |
| 31 | + hookRequire: true, |
| 32 | +}); |
| 33 | + |
| 34 | +function _exec(command: string, args: string[], opts: { cwd?: string }, logger: logging.Logger) { |
| 35 | + const { status, error, stdout } = spawnSync(command, args, { |
| 36 | + stdio: ['ignore', 'pipe', 'inherit'], |
| 37 | + ...opts, |
| 38 | + }); |
| 39 | + |
| 40 | + if (status != 0) { |
| 41 | + logger.error(`Command failed: ${command} ${args.map(x => JSON.stringify(x)).join(', ')}`); |
| 42 | + throw error; |
| 43 | + } |
| 44 | + |
| 45 | + return stdout.toString('utf-8'); |
| 46 | +} |
| 47 | + |
| 48 | +// Create a Jasmine runner and configure it. |
| 49 | +const runner = new Jasmine({ projectBaseDir: projectBaseDir }); |
| 50 | + |
| 51 | +if (process.argv.indexOf('--spec-reporter') != -1) { |
| 52 | + runner.env.clearReporters(); |
| 53 | + runner.env.addReporter( |
| 54 | + new JasmineSpecReporter({ |
| 55 | + stacktrace: { |
| 56 | + // Filter all JavaScript files that appear after a TypeScript file (callers) from the stack |
| 57 | + // trace. |
| 58 | + filter: (x: string) => { |
| 59 | + return x.substr(0, x.indexOf('\n', x.indexOf('\n', x.lastIndexOf('.ts:')) + 1)); |
| 60 | + }, |
| 61 | + }, |
| 62 | + spec: { |
| 63 | + displayDuration: true, |
| 64 | + }, |
| 65 | + suite: { |
| 66 | + displayNumber: true, |
| 67 | + }, |
| 68 | + summary: { |
| 69 | + displayStacktrace: StacktraceOption.PRETTY, |
| 70 | + displayErrorMessages: true, |
| 71 | + displayDuration: true, |
| 72 | + }, |
| 73 | + }), |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +// Manually set exit code (needed with custom reporters) |
| 78 | +runner.onComplete((success: boolean) => { |
| 79 | + process.exitCode = success ? 0 : 1; |
| 80 | +}); |
| 81 | + |
| 82 | +glob |
| 83 | + .sync('packages/**/*.spec.ts') |
| 84 | + .filter(p => !/\/schematics\/.*\/(other-)?files\//.test(p)) |
| 85 | + .forEach(path => { |
| 86 | + console.error(`Invalid spec file name: ${path}. You're using the old convention.`); |
| 87 | + }); |
| 88 | + |
| 89 | +export default function(args: ParsedArgs, logger: logging.Logger) { |
| 90 | + const specGlob = '*_spec.ts'; |
| 91 | + const regex = args.glob ? args.glob : `packages/**/${specGlob}`; |
| 92 | + |
| 93 | + if (args.large) { |
| 94 | + if (args['ve']) { |
| 95 | + // tslint:disable-next-line:no-console |
| 96 | + console.warn('********* VE Enabled ***********'); |
| 97 | + } else { |
| 98 | + console.warn('********* Ivy Enabled ***********'); |
| 99 | + // CI is really flaky with NGCC |
| 100 | + // This is a working around test order and isolation issues. |
| 101 | + console.warn('********* Running ngcc ***********'); |
| 102 | + execSync('yarn ngcc', { stdio: 'inherit' }); |
| 103 | + } |
| 104 | + |
| 105 | + // Default timeout for large specs is 2.5 minutes. |
| 106 | + jasmine.DEFAULT_TIMEOUT_INTERVAL = 150000; |
| 107 | + } |
| 108 | + |
| 109 | + if (args.timeout && Number.parseInt(args.timeout) > 0) { |
| 110 | + jasmine.DEFAULT_TIMEOUT_INTERVAL = Number.parseInt(args.timeout); |
| 111 | + } |
| 112 | + |
| 113 | + // Run the tests. |
| 114 | + const allTests = glob.sync(regex).map(p => relative(projectBaseDir, p)); |
| 115 | + |
| 116 | + const tsConfigPath = join(__dirname, '../tsconfig.json'); |
| 117 | + const tsConfig = ts.readConfigFile(tsConfigPath, ts.sys.readFile); |
| 118 | + const pattern = |
| 119 | + '^(' + |
| 120 | + (tsConfig.config.exclude as string[]) |
| 121 | + .map( |
| 122 | + ex => |
| 123 | + '(' + |
| 124 | + ex |
| 125 | + .split(/[\/\\]/g) |
| 126 | + .map(f => |
| 127 | + f |
| 128 | + .replace(/[\-\[\]{}()+?.^$|]/g, '\\$&') |
| 129 | + .replace(/^\*\*/g, '(.+?)?') |
| 130 | + .replace(/\*/g, '[^/\\\\]*'), |
| 131 | + ) |
| 132 | + .join('[/\\\\]') + |
| 133 | + ')', |
| 134 | + ) |
| 135 | + .join('|') + |
| 136 | + ')($|/|\\\\)'; |
| 137 | + const excludeRe = new RegExp(pattern); |
| 138 | + let tests = allTests.filter(x => !excludeRe.test(x)); |
| 139 | + |
| 140 | + if (!args.full) { |
| 141 | + // Find the point where this branch merged with master. |
| 142 | + const branch = _exec('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {}, logger).trim(); |
| 143 | + const masterRevList = _exec('git', ['rev-list', 'master'], {}, logger) |
| 144 | + .trim() |
| 145 | + .split('\n'); |
| 146 | + const branchRevList = _exec('git', ['rev-list', branch], {}, logger) |
| 147 | + .trim() |
| 148 | + .split('\n'); |
| 149 | + const sha = branchRevList.find(s => masterRevList.includes(s)); |
| 150 | + |
| 151 | + if (sha) { |
| 152 | + const diffFiles = [ |
| 153 | + // Get diff between $SHA and HEAD. |
| 154 | + ..._exec('git', ['diff', sha, 'HEAD', '--name-only'], {}, logger) |
| 155 | + .trim() |
| 156 | + .split('\n'), |
| 157 | + // And add the current status to it (so it takes the non-committed changes). |
| 158 | + ..._exec('git', ['status', '--short', '--show-stash'], {}, logger) |
| 159 | + .split('\n') |
| 160 | + .map(x => x.slice(2).trim()), |
| 161 | + ] |
| 162 | + .map(x => normalize(x)) |
| 163 | + .filter(x => x !== '.' && x !== ''); // Empty paths will be normalized to dot. |
| 164 | + |
| 165 | + const diffPackages = new Set(); |
| 166 | + for (const pkgName of Object.keys(packages)) { |
| 167 | + const relativeRoot = relative(projectBaseDir, packages[pkgName].root); |
| 168 | + if (diffFiles.some(x => x.startsWith(relativeRoot))) { |
| 169 | + diffPackages.add(pkgName); |
| 170 | + // Add all reverse dependents too. |
| 171 | + packages[pkgName].reverseDependencies.forEach(d => diffPackages.add(d)); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Show the packages that we will test. |
| 176 | + logger.info(`Found ${diffPackages.size} packages:`); |
| 177 | + logger.info(JSON.stringify([...diffPackages], null, 2)); |
| 178 | + |
| 179 | + // Remove the tests from packages that haven't changed. |
| 180 | + tests = tests.filter(p => |
| 181 | + Object.keys(packages).some(name => { |
| 182 | + const relativeRoot = relative(projectBaseDir, packages[name].root); |
| 183 | + |
| 184 | + return p.startsWith(relativeRoot) && diffPackages.has(name); |
| 185 | + }), |
| 186 | + ); |
| 187 | + |
| 188 | + logger.info(`Found ${tests.length} spec files, out of ${allTests.length}.`); |
| 189 | + |
| 190 | + if (tests.length === 0) { |
| 191 | + logger.info('No test to run, exiting... You might want to rerun with "--full".'); |
| 192 | + process.exit('CI' in process.env ? 1 : 0); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + // Filter in/out flakes according to the --flakey flag. |
| 198 | + tests = tests.filter(test => !!args.flakey == knownFlakes.includes(test.replace(/[\/\\]/g, '/'))); |
| 199 | + |
| 200 | + if (args.shard !== undefined) { |
| 201 | + // Remove tests that are not part of this shard. |
| 202 | + const shardId = args['shard']; |
| 203 | + const nbShards = args['nb-shards'] || 2; |
| 204 | + tests = tests.filter((name, i) => i % nbShards == shardId); |
| 205 | + } |
| 206 | + |
| 207 | + return new Promise(resolve => { |
| 208 | + runner.onComplete((passed: boolean) => resolve(passed ? 0 : 1)); |
| 209 | + if (args.seed != undefined) { |
| 210 | + runner.seed(args.seed); |
| 211 | + } |
| 212 | + |
| 213 | + runner.execute(tests, args.filter); |
| 214 | + }); |
| 215 | +} |
0 commit comments