Skip with notice functionality

This commit is contained in:
Mariusz Nowak 2019-06-05 09:40:11 +02:00
parent 2bc1b2925c
commit 194a12e799
No known key found for this signature in database
GPG Key ID: B1FBDA8A182B03F2
2 changed files with 43 additions and 0 deletions

View File

@ -11,7 +11,9 @@ const os = require('os');
const Spec = require('mocha/lib/reporters/spec');
const Runner = require('mocha/lib/runner');
const { ensureDirSync, removeSync } = require('fs-extra');
const chalk = require('chalk');
const { tmpDirCommonPath } = require('../tests/utils/fs');
const { skippedWithNotice } = require('../tests/utils/misc');
// Ensure faster tests propagation
// It's to expose errors otherwise hidden by race conditions
@ -83,6 +85,29 @@ module.exports = class ServerlessSpec extends Spec {
});
runner.on('end', () => {
// Output eventual skip notices
if (skippedWithNotice.length) {
const resolveTestName = test => {
const names = [test.title];
let parent = test.parent;
while (parent) {
if (parent.title) names.push(parent.title);
parent = parent.parent;
}
return `${chalk.cyan(names.reverse().join(': '))} (in: ${chalk.grey(
test.file.slice(process.cwd().length + 1)
)})`;
};
process.stdout.write(
' Notice: Some tests were skipped due to following environment issues:' +
`\n\n - ${skippedWithNotice
.map(
meta => `${resolveTestName(meta.context.test)}\n\n ${chalk.red(meta.reason)}\n`
)
.join('\n - ')}\n\n`
);
}
// Cleanup temporary homedir
try {
removeSync(tmpDirCommonPath);

View File

@ -4,6 +4,7 @@ const path = require('path');
const fse = require('fs-extra');
const BbPromise = require('bluebird');
const { execSync } = require('child_process');
const chalk = require('chalk');
const { replaceTextInFile } = require('../fs');
const logger = console;
@ -102,6 +103,20 @@ function persistentRequest(...args) {
});
}
const skippedWithNotice = [];
function skipWithNotice(context, reason) {
if (process.env.CI) return; // Do not tolerate skips in CI environment
skippedWithNotice.push({ context, reason });
process.stdout.write(chalk.yellow(`\n Skipped due to: ${chalk.red(reason)}\n\n`));
context.skip();
}
function skipOnWindowsDisabledSymlinks(error, context) {
if (error.code !== 'EPERM' || process.platform !== 'win32') return;
skipWithNotice(context, 'Missing admin rights to create symlinks');
}
module.exports = {
logger,
region,
@ -115,4 +130,7 @@ module.exports = {
createTestService,
getFunctionLogs,
persistentRequest,
skippedWithNotice,
skipWithNotice,
skipOnWindowsDisabledSymlinks,
};