From 194a12e799e13d0741e5c893876bbfc656d23f9e Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Wed, 5 Jun 2019 09:40:11 +0200 Subject: [PATCH] Skip with notice functionality --- tests/mocha-reporter.js | 25 +++++++++++++++++++++++++ tests/utils/misc/index.js | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/tests/mocha-reporter.js b/tests/mocha-reporter.js index 6a04db9e7..e9ecf9445 100644 --- a/tests/mocha-reporter.js +++ b/tests/mocha-reporter.js @@ -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); diff --git a/tests/utils/misc/index.js b/tests/utils/misc/index.js index 4dd68fc7d..2d928ce7b 100644 --- a/tests/utils/misc/index.js +++ b/tests/utils/misc/index.js @@ -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, };