Skip to content

Commit

Permalink
Allow tests to force AVA to behave like in CI or not
Browse files Browse the repository at this point in the history
Rather than messing with the third-party CI detection, allow the tests
to force it, so that they can be run more reliable in CIGTM.
  • Loading branch information
novemberborn committed Mar 21, 2020
1 parent 3195eb8 commit 52bbd5b
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const fs = require('fs');
const path = require('path');
const os = require('os');
const commonPathPrefix = require('common-path-prefix');
const isCi = require('is-ci');
const resolveCwd = require('resolve-cwd');
const debounce = require('lodash/debounce');
const arrify = require('arrify');
Expand All @@ -13,6 +12,7 @@ const Emittery = require('emittery');
const pMap = require('p-map');
const tempDir = require('temp-dir');
const globs = require('./globs');
const isCi = require('./is-ci');
const RunStatus = require('./run-status');
const fork = require('./fork');
const serializeError = require('./serialize-error');
Expand Down
2 changes: 1 addition & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const updateNotifier = require('update-notifier');
const figures = require('figures');
const arrify = require('arrify');
const yargs = require('yargs');
const isCi = require('is-ci');
const readPkg = require('read-pkg');
const isCi = require('./is-ci');
const loadConfig = require('./load-config');

function exit(message) {
Expand Down
5 changes: 5 additions & 0 deletions lib/is-ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const info = require('ci-info');

const {AVA_FORCE_CI} = process.env;

module.exports = AVA_FORCE_CI === 'not-ci' ? false : AVA_FORCE_CI === 'ci' || info.isCI;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"chalk": "^3.0.0",
"chokidar": "^3.3.1",
"chunkd": "^2.0.1",
"ci-info": "^2.0.0",
"ci-parallel-vars": "^1.0.0",
"clean-stack": "^2.2.0",
"clean-yaml-object": "^0.1.0",
Expand All @@ -83,7 +84,6 @@
"ignore-by-default": "^1.0.0",
"import-local": "^3.0.2",
"indent-string": "^4.0.0",
"is-ci": "^2.0.0",
"is-error": "^2.2.2",
"is-plain-object": "^3.0.0",
"is-promise": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion test/helper/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function execCli(args, opts, cb) {
// Inserting a shim here allows us to fake a TTY.
child = childProcess.spawn(process.execPath, ['--require', ttySimulator, cliPath].concat(args), {
cwd: dirname,
env: {CI: '1', ...env}, // Force CI to ensure the correct reporter is selected
env: {AVA_FORCE_CI: 'ci', ...env}, // Force CI to ensure the correct reporter is selected
// env,
stdio: [null, 'pipe', 'pipe']
});
Expand Down
2 changes: 1 addition & 1 deletion test/integration/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ test('use current working directory if `package.json` is not found', () => {

fs.writeFileSync(testFilePath, `const test = require(${JSON.stringify(avaPath)});\ntest('test', t => { t.pass(); });`);

return execa(process.execPath, [cliPath], {cwd, env: {CI: '1'}});
return execa(process.execPath, [cliPath], {cwd, env: {AVA_FORCE_CI: 'ci'}});
});
6 changes: 3 additions & 3 deletions test/integration/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ const {test} = require('tap');
const {execCli} = require('../helper/cli');

test('bails when using --watch while while debugging', t => {
execCli(['debug', '--watch', 'test.js'], {dirname: 'fixture/watcher', env: {CI: ''}}, (err, stdout, stderr) => {
execCli(['debug', '--watch', 'test.js'], {dirname: 'fixture/watcher', env: {AVA_FORCE_CI: 'not-ci'}}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'Watch mode is not available when debugging.');
t.end();
});
});

test('bails when debugging in CI', t => {
execCli(['debug', 'test.js'], {dirname: 'fixture/watcher', env: {CI: true}}, (err, stdout, stderr) => {
execCli(['debug', 'test.js'], {dirname: 'fixture/watcher', env: {AVA_FORCE_CI: 'ci'}}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'Debugging is not available in CI.');
t.end();
});
});

test('bails when --tap reporter is used while debugging', t => {
execCli(['debug', '--tap', 'test.js'], {dirname: 'fixture/watcher', env: {CI: ''}}, (err, stdout, stderr) => {
execCli(['debug', '--tap', 'test.js'], {dirname: 'fixture/watcher', env: {AVA_FORCE_CI: 'not-ci'}}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'The TAP reporter is not available when debugging.');
t.end();
Expand Down
2 changes: 1 addition & 1 deletion test/integration/parallel-runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test('correctly distributes the test files', t => {
execCli([], {
dirname: 'fixture/parallel-runs',
env: {
CI: '1',
AVA_FORCE_CI: 'ci',
CI_NODE_INDEX: String(i),
CI_NODE_TOTAL: '3'
}
Expand Down
16 changes: 5 additions & 11 deletions test/integration/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ const {test} = require('tap');
const tempy = require('tempy');
const {execCli} = require('../helper/cli');

const overrideCIChecks = {
CI: '',
TRAVIS: '',
CONTINUOUS_INTEGRATION: ''
};

for (const obj of [
{type: 'colocated', rel: '', dir: ''},
{type: '__tests__', rel: '__tests__-dir', dir: '__tests__/__snapshots__'},
Expand All @@ -30,7 +24,7 @@ for (const obj of [

const dirname = path.join('fixture/snapshots', obj.rel);
// Test should pass, and a snapshot gets written
execCli(['--update-snapshots', '--verbose'], {dirname, env: overrideCIChecks}, error => {
execCli(['--update-snapshots', '--verbose'], {dirname, env: {AVA_FORCE_CI: 'not-ci'}}, error => {
t.ifError(error);
t.true(fs.existsSync(snapPath));

Expand All @@ -56,7 +50,7 @@ test('one', t => {
})`;
fs.writeFileSync(path.join(cwd, 'test.js'), initial);

const run = () => execa(process.execPath, [cliPath, '--verbose', '--no-color'], {cwd, env: overrideCIChecks, reject: false});
const run = () => execa(process.execPath, [cliPath, '--verbose', '--no-color'], {cwd, env: {AVA_FORCE_CI: 'not-ci'}, reject: false});
return run().then(result => {
t.match(result.stdout, /1 test passed/);

Expand Down Expand Up @@ -167,7 +161,7 @@ test('snapshots infer their location and name from sourcemaps', t => {
t.true(fs.existsSync(relFilePath));
};

execCli(['--verbose'], {dirname: relativeFixtureDir, env: overrideCIChecks}, (error, stdout) => {
execCli(['--verbose'], {dirname: relativeFixtureDir, env: {AVA_FORCE_CI: 'not-ci'}}, (error, stdout) => {
t.ifError(error);
snapFixtureFilePaths.forEach(x => verifySnapFixtureFiles(x));
t.match(stdout, /6 tests passed/);
Expand Down Expand Up @@ -208,7 +202,7 @@ test('snapshots resolved location from "snapshotDir" in AVA config', t => {
t.true(fs.existsSync(relFilePath));
};

execCli(['--verbose'], {dirname: relativeFixtureDir, env: overrideCIChecks}, (error, stdout) => {
execCli(['--verbose'], {dirname: relativeFixtureDir, env: {AVA_FORCE_CI: 'not-ci'}}, (error, stdout) => {
t.ifError(error);
snapFixtureFilePaths.forEach(x => verifySnapFixtureFiles(x));
t.match(stdout, /6 tests passed/);
Expand Down Expand Up @@ -237,7 +231,7 @@ test('snapshots are indentical on different platforms', t => {
[reportPath, snapPath].forEach(fp => removeFile(fp));

// Test should pass, and a snapshot gets written
execCli(['--update-snapshots', '--verbose'], {dirname: fixtureDir, env: overrideCIChecks}, error => {
execCli(['--update-snapshots', '--verbose'], {dirname: fixtureDir, env: {AVA_FORCE_CI: 'not-ci'}}, error => {
t.ifError(error);
t.true(fs.existsSync(reportPath));
t.true(fs.existsSync(snapPath));
Expand Down
16 changes: 8 additions & 8 deletions test/integration/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const END_MESSAGE = 'Type `r` and press enter to rerun tests\nType `u` and press
test('watcher reruns test files upon change', t => {
let killed = false;

const child = execCli(['--verbose', '--watch', 'test.js'], {dirname: 'fixture/watcher', env: {CI: ''}}, err => {
const child = execCli(['--verbose', '--watch', 'test.js'], {dirname: 'fixture/watcher', env: {AVA_FORCE_CI: 'not-ci'}}, err => {
t.ok(killed);
t.ifError(err);
t.end();
Expand All @@ -35,7 +35,7 @@ test('watcher reruns test files upon change', t => {
test('watcher reruns test files when source dependencies change', t => {
let killed = false;

const child = execCli(['--verbose', '--watch', 'test-1.js', 'test-2.js'], {dirname: 'fixture/watcher/with-dependencies', env: {CI: ''}}, err => {
const child = execCli(['--verbose', '--watch', 'test-1.js', 'test-2.js'], {dirname: 'fixture/watcher/with-dependencies', env: {AVA_FORCE_CI: 'not-ci'}}, err => {
t.ok(killed);
t.ifError(err);
t.end();
Expand All @@ -59,7 +59,7 @@ test('watcher reruns test files when source dependencies change', t => {
test('watcher does not rerun test files when they write snapshot files', t => {
let killed = false;

const child = execCli(['--verbose', '--watch', '--update-snapshots', 'test.js'], {dirname: 'fixture/snapshots/watcher-rerun', env: {CI: ''}}, err => {
const child = execCli(['--verbose', '--watch', '--update-snapshots', 'test.js'], {dirname: 'fixture/snapshots/watcher-rerun', env: {AVA_FORCE_CI: 'not-ci'}}, err => {
t.ok(killed);
t.ifError(err);
t.end();
Expand All @@ -85,7 +85,7 @@ test('watcher does not rerun test files when they write snapshot files', t => {
test('watcher does not rerun test files when ignored files change', t => {
let killed = false;

const child = execCli(['--verbose', '--watch'], {dirname: 'fixture/watcher/ignored-files', env: {CI: ''}}, err => {
const child = execCli(['--verbose', '--watch'], {dirname: 'fixture/watcher/ignored-files', env: {AVA_FORCE_CI: 'not-ci'}}, err => {
t.ok(killed);
t.ifError(err);
t.end();
Expand All @@ -112,7 +112,7 @@ test('watcher does not rerun test files when ignored files change', t => {
test('watcher reruns test files when snapshot dependencies change', t => {
let killed = false;

const child = execCli(['--verbose', '--watch', '--update-snapshots', 'test.js'], {dirname: 'fixture/snapshots/watcher-rerun', env: {CI: ''}}, err => {
const child = execCli(['--verbose', '--watch', '--update-snapshots', 'test.js'], {dirname: 'fixture/snapshots/watcher-rerun', env: {AVA_FORCE_CI: 'not-ci'}}, err => {
t.ok(killed);
t.ifError(err);
t.end();
Expand Down Expand Up @@ -140,7 +140,7 @@ test('watcher reruns test files when snapshot dependencies change', t => {
test('`"tap": true` config is ignored when --watch is given', t => {
let killed = false;

const child = execCli(['--watch', '--verbose', 'test.js'], {dirname: 'fixture/watcher/tap-in-conf', env: {CI: ''}}, () => {
const child = execCli(['--watch', '--verbose', 'test.js'], {dirname: 'fixture/watcher/tap-in-conf', env: {AVA_FORCE_CI: 'not-ci'}}, () => {
t.ok(killed);
t.end();
});
Expand All @@ -160,15 +160,15 @@ test('`"tap": true` config is ignored when --watch is given', t => {
});

test('bails when --tap reporter is used while --watch is given', t => {
execCli(['--tap', '--watch', 'test.js'], {dirname: 'fixture/watcher', env: {CI: ''}}, (err, stdout, stderr) => {
execCli(['--tap', '--watch', 'test.js'], {dirname: 'fixture/watcher', env: {AVA_FORCE_CI: 'not-ci'}}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'The TAP reporter is not available when using watch mode.');
t.end();
});
});

test('bails when CI is used while --watch is given', t => {
execCli(['--watch', 'test.js'], {dirname: 'fixture/watcher', env: {CI: true}}, (err, stdout, stderr) => {
execCli(['--watch', 'test.js'], {dirname: 'fixture/watcher', env: {AVA_FORCE_CI: 'ci'}}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'Watch mode is not available in CI, as it prevents AVA from terminating.');
t.end();
Expand Down

0 comments on commit 52bbd5b

Please sign in to comment.