2017-08-22 15:31:15 -04:00
|
|
|
const path = require('canonical-path');
|
2017-04-13 17:35:13 -04:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const argv = require('yargs').argv;
|
|
|
|
const globby = require('globby');
|
|
|
|
const xSpawn = require('cross-spawn');
|
|
|
|
const treeKill = require('tree-kill');
|
2017-10-06 09:48:37 -04:00
|
|
|
const shelljs = require('shelljs');
|
|
|
|
|
|
|
|
shelljs.set('-e');
|
2017-04-13 17:35:13 -04:00
|
|
|
|
|
|
|
const AIO_PATH = path.join(__dirname, '../../');
|
|
|
|
const SHARED_PATH = path.join(__dirname, '/shared');
|
|
|
|
const EXAMPLES_PATH = path.join(AIO_PATH, './content/examples/');
|
|
|
|
const PROTRACTOR_CONFIG_FILENAME = path.join(__dirname, './shared/protractor.config.js');
|
2017-08-22 15:31:15 -04:00
|
|
|
const SJS_SPEC_FILENAME = 'e2e-spec.ts';
|
|
|
|
const CLI_SPEC_FILENAME = 'e2e/app.e2e-spec.ts';
|
2017-04-13 17:35:13 -04:00
|
|
|
const EXAMPLE_CONFIG_FILENAME = 'example-config.json';
|
2017-08-22 15:31:15 -04:00
|
|
|
const IGNORED_EXAMPLES = [ // temporary ignores
|
|
|
|
'quickstart',
|
|
|
|
'http',
|
|
|
|
'setup',
|
|
|
|
'webpack',
|
|
|
|
'upgrade-p'
|
2017-04-13 17:35:13 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run Protractor End-to-End Tests for Doc Samples
|
|
|
|
*
|
|
|
|
* Flags
|
|
|
|
* --filter to filter/select _example app subdir names
|
|
|
|
* e.g. --filter=foo // all example apps with 'foo' in their folder names.
|
|
|
|
*
|
2017-07-27 03:31:45 -04:00
|
|
|
* --setup run yarn install, copy boilerplate and update webdriver
|
2017-04-13 17:35:13 -04:00
|
|
|
* e.g. --setup
|
2017-07-27 03:31:45 -04:00
|
|
|
*
|
2017-07-20 16:19:01 -04:00
|
|
|
* --local to use the locally built Angular packages, rather than versions from npm
|
|
|
|
* Must be used in conjunction with --setup as this is when the packages are copied.
|
|
|
|
* e.g. --setup --local
|
|
|
|
*
|
2017-07-27 03:31:45 -04:00
|
|
|
* --shard to shard the specs into groups to allow you to run them in parallel
|
|
|
|
* e.g. --shard=0/2 // the even specs: 0, 2, 4, etc
|
|
|
|
* e.g. --shard=1/2 // the odd specs: 1, 3, 5, etc
|
|
|
|
* e.g. --shard=1/3 // the second of every three specs: 1, 4, 7, etc
|
2017-04-13 17:35:13 -04:00
|
|
|
*/
|
|
|
|
function runE2e() {
|
|
|
|
if (argv.setup) {
|
|
|
|
// Run setup.
|
2017-10-06 09:48:37 -04:00
|
|
|
console.log('runE2e: setup boilerplate');
|
|
|
|
const installPackagesCommand = `example-use-${argv.local ? 'local' : 'npm'}`;
|
|
|
|
const addBoilerplateCommand = 'boilerplate:add';
|
|
|
|
shelljs.exec(`yarn ${installPackagesCommand}`, { cwd: AIO_PATH });
|
|
|
|
shelljs.exec(`yarn ${addBoilerplateCommand}`, { cwd: AIO_PATH });
|
2017-09-28 12:29:04 -04:00
|
|
|
}
|
2017-04-13 17:35:13 -04:00
|
|
|
|
|
|
|
const outputFile = path.join(AIO_PATH, './protractor-results.txt');
|
|
|
|
|
2017-10-06 09:48:37 -04:00
|
|
|
return Promise.resolve()
|
2017-07-27 03:31:45 -04:00
|
|
|
.then(() => findAndRunE2eTests(argv.filter, outputFile, argv.shard))
|
2017-04-13 17:35:13 -04:00
|
|
|
.then((status) => {
|
|
|
|
reportStatus(status, outputFile);
|
|
|
|
if (status.failed.length > 0) {
|
|
|
|
return Promise.reject('Some test suites failed');
|
|
|
|
}
|
|
|
|
}).catch(function (e) {
|
|
|
|
console.log(e);
|
|
|
|
process.exitCode = 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finds all of the *e2e-spec.tests under the examples folder along with the corresponding apps
|
|
|
|
// that they should run under. Then run each app/spec collection sequentially.
|
2017-07-27 03:31:45 -04:00
|
|
|
function findAndRunE2eTests(filter, outputFile, shard) {
|
|
|
|
|
|
|
|
const shardParts = shard ? shard.split('/') : [0,1];
|
|
|
|
const shardModulo = parseInt(shardParts[0], 10);
|
|
|
|
const shardDivider = parseInt(shardParts[1], 10);
|
|
|
|
|
2017-04-13 17:35:13 -04:00
|
|
|
// create an output file with header.
|
|
|
|
const startTime = new Date().getTime();
|
|
|
|
let header = `Doc Sample Protractor Results on ${new Date().toLocaleString()}\n`;
|
|
|
|
header += ` Filter: ${filter ? filter : 'All tests'}\n\n`;
|
|
|
|
fs.writeFileSync(outputFile, header);
|
|
|
|
|
|
|
|
// Run the tests sequentially.
|
|
|
|
const status = { passed: [], failed: [] };
|
2017-08-22 15:31:15 -04:00
|
|
|
return getE2eSpecs(EXAMPLES_PATH, filter)
|
|
|
|
.then(e2eSpecPaths => {
|
|
|
|
Object.keys(e2eSpecPaths).forEach(key => {
|
|
|
|
const value = e2eSpecPaths[key];
|
2017-09-28 12:29:04 -04:00
|
|
|
e2eSpecPaths[key] = value.filter((p, index) => index % shardDivider === shardModulo);
|
2017-08-22 15:31:15 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return e2eSpecPaths.systemjs.reduce((promise, specPath) => {
|
|
|
|
return promise.then(() => {
|
|
|
|
const examplePath = path.dirname(specPath);
|
|
|
|
return runE2eTestsSystemJS(examplePath, outputFile).then(ok => {
|
|
|
|
const arr = ok ? status.passed : status.failed;
|
|
|
|
arr.push(examplePath);
|
|
|
|
});
|
2017-04-13 17:35:13 -04:00
|
|
|
});
|
2017-08-22 15:31:15 -04:00
|
|
|
}, Promise.resolve())
|
|
|
|
.then(() => {
|
|
|
|
return e2eSpecPaths.cli.reduce((promise, specPath) => {
|
|
|
|
return promise.then(() => {
|
|
|
|
return runE2eTestsCLI(specPath, outputFile).then(ok => {
|
|
|
|
const arr = ok ? status.passed : status.failed;
|
|
|
|
arr.push(specPath);
|
|
|
|
});
|
|
|
|
});
|
2017-09-28 12:29:04 -04:00
|
|
|
}, Promise.resolve());
|
2017-04-13 17:35:13 -04:00
|
|
|
});
|
2017-09-28 12:29:04 -04:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
const stopTime = new Date().getTime();
|
|
|
|
status.elapsedTime = (stopTime - startTime) / 1000;
|
|
|
|
return status;
|
|
|
|
});
|
2017-04-13 17:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the example in appDir; then run protractor with the specified
|
|
|
|
// fileName; then shut down the example.
|
|
|
|
// All protractor output is appended to the outputFile.
|
2017-08-22 15:31:15 -04:00
|
|
|
// SystemJS version
|
|
|
|
function runE2eTestsSystemJS(appDir, outputFile) {
|
2017-04-13 17:35:13 -04:00
|
|
|
const config = loadExampleConfig(appDir);
|
|
|
|
|
|
|
|
const appBuildSpawnInfo = spawnExt('yarn', [config.build], { cwd: appDir });
|
2017-09-28 12:29:04 -04:00
|
|
|
const appRunSpawnInfo = spawnExt('yarn', [config.run, '-s'], { cwd: appDir }, true);
|
2017-04-13 17:35:13 -04:00
|
|
|
|
2017-08-22 15:31:15 -04:00
|
|
|
let run = runProtractorSystemJS(appBuildSpawnInfo.promise, appDir, appRunSpawnInfo, outputFile);
|
2017-04-13 17:35:13 -04:00
|
|
|
|
|
|
|
if (fs.existsSync(appDir + '/aot/index.html')) {
|
|
|
|
run = run.then((ok) => ok && runProtractorAoT(appDir, outputFile));
|
|
|
|
}
|
|
|
|
return run;
|
|
|
|
}
|
|
|
|
|
2017-08-22 15:31:15 -04:00
|
|
|
function runProtractorSystemJS(prepPromise, appDir, appRunSpawnInfo, outputFile) {
|
|
|
|
const specFilename = path.resolve(`${appDir}/${SJS_SPEC_FILENAME}`);
|
2017-04-13 17:35:13 -04:00
|
|
|
return prepPromise
|
|
|
|
.catch(function () {
|
|
|
|
const emsg = `Application at ${appDir} failed to transpile.\n\n`;
|
|
|
|
console.log(emsg);
|
|
|
|
fs.appendFileSync(outputFile, emsg);
|
|
|
|
return Promise.reject(emsg);
|
|
|
|
})
|
2017-09-28 12:29:04 -04:00
|
|
|
.then(function () {
|
2017-04-13 17:35:13 -04:00
|
|
|
let transpileError = false;
|
|
|
|
|
|
|
|
// Start protractor.
|
|
|
|
|
2017-09-28 12:29:04 -04:00
|
|
|
const spawnInfo = spawnExt('yarn', ['protractor',
|
2017-04-13 17:35:13 -04:00
|
|
|
PROTRACTOR_CONFIG_FILENAME,
|
|
|
|
`--specs=${specFilename}`,
|
|
|
|
'--params.appDir=' + appDir,
|
|
|
|
'--params.outputFile=' + outputFile
|
|
|
|
], { cwd: SHARED_PATH });
|
|
|
|
|
|
|
|
spawnInfo.proc.stderr.on('data', function (data) {
|
|
|
|
transpileError = transpileError || /npm ERR! Exit status 100/.test(data.toString());
|
|
|
|
});
|
2017-09-28 12:29:04 -04:00
|
|
|
return spawnInfo.promise.catch(function () {
|
2017-04-13 17:35:13 -04:00
|
|
|
if (transpileError) {
|
|
|
|
const emsg = `${specFilename} failed to transpile.\n\n`;
|
|
|
|
console.log(emsg);
|
|
|
|
fs.appendFileSync(outputFile, emsg);
|
|
|
|
}
|
2017-09-28 12:29:04 -04:00
|
|
|
return Promise.reject();
|
2017-04-13 17:35:13 -04:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(
|
2017-08-22 15:31:15 -04:00
|
|
|
function () { return finish(appRunSpawnInfo.proc.pid, true); },
|
|
|
|
function () { return finish(appRunSpawnInfo.proc.pid, false); }
|
2017-09-28 12:29:04 -04:00
|
|
|
);
|
2017-08-22 15:31:15 -04:00
|
|
|
}
|
2017-04-13 17:35:13 -04:00
|
|
|
|
2017-08-22 15:31:15 -04:00
|
|
|
function finish(spawnProcId, ok) {
|
|
|
|
// Ugh... proc.kill does not work properly on windows with child processes.
|
|
|
|
// appRun.proc.kill();
|
|
|
|
treeKill(spawnProcId);
|
|
|
|
return ok;
|
2017-04-13 17:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run e2e tests over the AOT build for projects that examples it.
|
|
|
|
function runProtractorAoT(appDir, outputFile) {
|
|
|
|
fs.appendFileSync(outputFile, '++ AoT version ++\n');
|
|
|
|
const aotBuildSpawnInfo = spawnExt('yarn', ['build:aot'], { cwd: appDir });
|
|
|
|
let promise = aotBuildSpawnInfo.promise;
|
|
|
|
|
|
|
|
const copyFileCmd = 'copy-dist-files.js';
|
|
|
|
if (fs.existsSync(appDir + '/' + copyFileCmd)) {
|
|
|
|
promise = promise.then(() => spawnExt('node', [copyFileCmd], { cwd: appDir }).promise);
|
|
|
|
}
|
|
|
|
const aotRunSpawnInfo = spawnExt('yarn', ['serve:aot'], { cwd: appDir }, true);
|
2017-08-22 15:31:15 -04:00
|
|
|
return runProtractorSystemJS(promise, appDir, aotRunSpawnInfo, outputFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the example in appDir; then run protractor with the specified
|
|
|
|
// fileName; then shut down the example.
|
|
|
|
// All protractor output is appended to the outputFile.
|
|
|
|
// CLI version
|
|
|
|
function runE2eTestsCLI(appDir, outputFile) {
|
2017-12-11 09:27:29 -05:00
|
|
|
// `--preserve-symlinks` is needed due the symlinked `node_modules/` in each example.
|
|
|
|
// `--no-webdriver-update` is needed to preserve the ChromeDriver version already installed.
|
|
|
|
const args = ['e2e', '--preserve-symlinks', '--no-webdriver-update'];
|
|
|
|
const e2eSpawn = spawnExt('yarn', args, { cwd: appDir });
|
2017-08-22 15:31:15 -04:00
|
|
|
return e2eSpawn.promise.then(
|
|
|
|
function () {
|
|
|
|
fs.appendFileSync(outputFile, `Passed: ${appDir}\n\n`);
|
|
|
|
return finish(e2eSpawn.proc.pid, true);
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
fs.appendFileSync(outputFile, `Failed: ${appDir}\n\n`);
|
|
|
|
return finish(e2eSpawn.proc.pid, false);
|
|
|
|
}
|
|
|
|
);
|
2017-04-13 17:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Report final status.
|
|
|
|
function reportStatus(status, outputFile) {
|
|
|
|
let log = [''];
|
|
|
|
log.push('Suites passed:');
|
|
|
|
status.passed.forEach(function (val) {
|
|
|
|
log.push(' ' + val);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (status.failed.length == 0) {
|
|
|
|
log.push('All tests passed');
|
|
|
|
} else {
|
|
|
|
log.push('Suites failed:');
|
|
|
|
status.failed.forEach(function (val) {
|
|
|
|
log.push(' ' + val);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
log.push('\nElapsed time: ' + status.elapsedTime + ' seconds');
|
|
|
|
log = log.join('\n');
|
|
|
|
console.log(log);
|
|
|
|
fs.appendFileSync(outputFile, log);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns both a promise and the spawned process so that it can be killed if needed.
|
|
|
|
function spawnExt(command, args, options, ignoreClose = false) {
|
|
|
|
let proc;
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
2017-09-28 12:29:04 -04:00
|
|
|
let descr = command + ' ' + args.join(' ');
|
2017-04-13 17:35:13 -04:00
|
|
|
console.log('running: ' + descr);
|
|
|
|
try {
|
|
|
|
proc = xSpawn.spawn(command, args, options);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
reject(e);
|
|
|
|
return { proc: null, promise };
|
|
|
|
}
|
|
|
|
proc.stdout.on('data', function (data) {
|
|
|
|
process.stdout.write(data.toString());
|
|
|
|
});
|
|
|
|
proc.stderr.on('data', function (data) {
|
|
|
|
process.stdout.write(data.toString());
|
|
|
|
});
|
|
|
|
proc.on('close', function (returnCode) {
|
|
|
|
console.log(`completed: ${descr} \n`);
|
|
|
|
// Many tasks (e.g., tsc) complete but are actually errors;
|
|
|
|
// Confirm return code is zero.
|
|
|
|
returnCode === 0 || ignoreClose ? resolve(0) : reject(returnCode);
|
|
|
|
});
|
|
|
|
proc.on('error', function (data) {
|
|
|
|
console.log(`completed with error: ${descr} \n`);
|
|
|
|
console.log(data.toString());
|
|
|
|
reject(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return { proc, promise };
|
|
|
|
}
|
|
|
|
|
2017-08-22 15:31:15 -04:00
|
|
|
function getE2eSpecs(basePath, filter) {
|
|
|
|
let specs = {};
|
|
|
|
|
|
|
|
return getE2eSpecsFor(basePath, SJS_SPEC_FILENAME, filter).then(sjsPaths => {
|
|
|
|
specs.systemjs = sjsPaths;
|
|
|
|
}).then(() => {
|
|
|
|
return getE2eSpecsFor(basePath, CLI_SPEC_FILENAME, filter).then(cliPaths => {
|
|
|
|
return cliPaths.map(p => {
|
|
|
|
return p.replace(`${CLI_SPEC_FILENAME}`, '');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).then(cliPaths => {
|
|
|
|
specs.cli = cliPaths;
|
|
|
|
}).then(() => specs);
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:35:13 -04:00
|
|
|
// Find all e2e specs in a given example folder.
|
2017-08-22 15:31:15 -04:00
|
|
|
function getE2eSpecsFor(basePath, specFile, filter) {
|
2017-04-13 17:35:13 -04:00
|
|
|
// Only get spec file at the example root.
|
2017-08-22 15:31:15 -04:00
|
|
|
const e2eSpecGlob = `${filter ? `*${filter}*` : '*'}/${specFile}`;
|
2017-04-13 17:35:13 -04:00
|
|
|
return globby(e2eSpecGlob, { cwd: basePath, nodir: true })
|
|
|
|
.then(paths => paths
|
2017-08-22 15:31:15 -04:00
|
|
|
.filter(file => !IGNORED_EXAMPLES.some(ignored => file.startsWith(ignored)))
|
2017-04-13 17:35:13 -04:00
|
|
|
.map(file => path.join(basePath, file))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-22 15:31:15 -04:00
|
|
|
// Load configuration for an example. Used for SystemJS
|
2017-04-13 17:35:13 -04:00
|
|
|
function loadExampleConfig(exampleFolder) {
|
|
|
|
// Default config.
|
|
|
|
let config = {
|
|
|
|
build: 'build',
|
|
|
|
run: 'serve:e2e'
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const exampleConfig = fs.readJsonSync(`${exampleFolder}/${EXAMPLE_CONFIG_FILENAME}`);
|
|
|
|
Object.assign(config, exampleConfig);
|
|
|
|
} catch (e) { }
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
runE2e();
|