Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Code style tweaks
  • Loading branch information
sindresorhus committed Nov 16, 2017
1 parent cb1c3f7 commit efc3b31
Show file tree
Hide file tree
Showing 40 changed files with 149 additions and 39 deletions.
11 changes: 11 additions & 0 deletions api.js
Expand Up @@ -55,6 +55,7 @@ class Api extends EventEmitter {
this.options = Object.assign({match: []}, options);
this.options.require = resolveModules(this.options.require);
}

_runFile(file, runStatus, execArgv) {
const hash = this.precompiler.precompileFile(file);
const precompiled = Object.assign({}, this._precompiledHelpers);
Expand All @@ -71,17 +72,20 @@ class Api extends EventEmitter {

return emitter;
}

run(files, options) {
return new AvaFiles({cwd: this.options.resolveTestsFrom, files})
.findTestFiles()
.then(files => this._run(files, options));
}

_onTimeout(runStatus) {
const timeout = ms(this.options.timeout);
const err = new AvaError(`Exited because no new tests completed within the last ${timeout}ms of inactivity`);
this._handleError(runStatus, err);
runStatus.emit('timeout');
}

_setupTimeout(runStatus) {
const timeout = ms(this.options.timeout);

Expand All @@ -92,9 +96,11 @@ class Api extends EventEmitter {
runStatus._restartTimer();
runStatus.on('test', runStatus._restartTimer);
}

_cancelTimeout(runStatus) {
runStatus._restartTimer.cancel();
}

_setupPrecompiler(files) {
const isCacheEnabled = this.options.cacheEnabled !== false;
let cacheDir = uniqueTempDir();
Expand All @@ -121,6 +127,7 @@ class Api extends EventEmitter {
});
});
}

_precompileHelpers() {
this._precompiledHelpers = {};

Expand All @@ -136,6 +143,7 @@ class Api extends EventEmitter {
this._precompiledHelpers[file] = hash;
});
}

_run(files, options) {
options = options || {};

Expand Down Expand Up @@ -175,6 +183,7 @@ class Api extends EventEmitter {
return this._runWithPool(files, runStatus, concurrency);
});
}

_computeForkExecArgs(files) {
const execArgv = this.options.testOnlyExecArgv || process.execArgv;
let debugArgIndex = -1;
Expand Down Expand Up @@ -220,12 +229,14 @@ class Api extends EventEmitter {
return forkExecArgv;
});
}

_handleError(runStatus, err) {
runStatus.handleExceptions({
exception: err,
file: err.file ? path.relative(process.cwd(), err.file) : undefined
});
}

_runWithPool(files, runStatus, concurrency) {
const tests = [];
let execArgvList;
Expand Down
2 changes: 1 addition & 1 deletion bench/concurrent/alternating-sync-async.js
@@ -1,4 +1,4 @@
import test from '../../';
import test from '../..';

for (let i = 0; i < 10000; i++) {
if (i % 2) {
Expand Down
2 changes: 1 addition & 1 deletion bench/concurrent/async-immediate.js
@@ -1,4 +1,4 @@
import test from '../../';
import test from '../..';

for (let i = 0; i < 10000; i++) {
test(`test${i}`, () => new Promise(resolve => setImmediate(resolve)));
Expand Down
2 changes: 1 addition & 1 deletion bench/concurrent/async-timeout.js
@@ -1,4 +1,4 @@
import test from '../../';
import test from '../..';

for (let i = 0; i < 10000; i++) {
test(`test${i}`, () => new Promise(resolve => setTimeout(resolve, 0)));
Expand Down
2 changes: 1 addition & 1 deletion bench/concurrent/sync.js
@@ -1,4 +1,4 @@
import test from '../../';
import test from '../..';

for (let i = 0; i < 10000; i++) {
test(`test${i}`, () => {});
Expand Down
2 changes: 1 addition & 1 deletion bench/other/failures.js
@@ -1,4 +1,4 @@
import test from '../../';
import test from '../..';

for (let i = 0; i < 1000; i++) {
test.serial(`test${i}`, t => {
Expand Down
2 changes: 1 addition & 1 deletion bench/serial/alternating-sync-async.js
@@ -1,4 +1,4 @@
import test from '../../';
import test from '../..';

for (let i = 0; i < 10000; i++) {
if (i % 2) {
Expand Down
2 changes: 1 addition & 1 deletion bench/serial/async-immediate.js
@@ -1,4 +1,4 @@
import test from '../../';
import test from '../..';

for (let i = 0; i < 10000; i++) {
test.serial(`test${i}`, () => new Promise(resolve => setImmediate(resolve)));
Expand Down
2 changes: 1 addition & 1 deletion bench/serial/async-timeout.js
@@ -1,4 +1,4 @@
import test from '../../';
import test from '../..';

for (let i = 0; i < 3000; i++) {
test.serial(`test${i}`, () => new Promise(resolve => setTimeout(resolve, 0)));
Expand Down
2 changes: 1 addition & 1 deletion bench/serial/sync.js
@@ -1,4 +1,4 @@
import test from '../../';
import test from '../..';

for (let i = 0; i < 10000; i++) {
test.serial(`test${i}`, () => {});
Expand Down
5 changes: 5 additions & 0 deletions lib/ava-files.js
Expand Up @@ -125,6 +125,7 @@ class AvaFiles {

autoBind(this);
}

findTestFiles() {
return handlePaths(this.files, this.excludePatterns, {
cwd: this.cwd,
Expand All @@ -134,6 +135,7 @@ class AvaFiles {
symlinks: Object.create(null)
});
}

findTestHelpers() {
return handlePaths(defaultHelperPatterns(), ['!**/node_modules/**'], {
cwd: this.cwd,
Expand All @@ -144,6 +146,7 @@ class AvaFiles {
symlinks: Object.create(null)
});
}

isSource(filePath) {
let mixedPatterns = [];
const defaultIgnorePatterns = getDefaultIgnorePatterns();
Expand Down Expand Up @@ -195,6 +198,7 @@ class AvaFiles {

return false;
}

isTest(filePath) {
const excludePatterns = this.excludePatterns;
const initialPatterns = this.files.concat(excludePatterns);
Expand Down Expand Up @@ -241,6 +245,7 @@ class AvaFiles {
// excludePatterns into account. This mimicks the behavior in api.js
return multimatch(matchable(filePath), recursivePatterns.concat(excludePatterns)).length === 1;
}

getChokidarPatterns() {
let paths = [];
let ignored = [];
Expand Down
2 changes: 1 addition & 1 deletion lib/beautify-stack.js
Expand Up @@ -8,7 +8,7 @@ let ignoreStackLines = [];

const avaInternals = /\/ava\/(?:lib\/)?[\w-]+\.js:\d+:\d+\)?$/;
const avaDependencies = /\/node_modules\/(?:bluebird|empower-core|(?:ava\/node_modules\/)?(?:babel-runtime|core-js))\//;
const stackFrameLine = /^.+( \(.+:[0-9]+:[0-9]+\)|:[0-9]+:[0-9]+)$/;
const stackFrameLine = /^.+( \(.+:\d+:\d+\)|:\d+:\d+)$/;

if (!debug.enabled) {
ignoreStackLines = StackUtils.nodeInternals();
Expand Down
5 changes: 5 additions & 0 deletions lib/caching-precompiler.js
Expand Up @@ -33,6 +33,7 @@ class CachingPrecompiler {
this.fileHashes = {};
this.transform = this._createTransform();
}

precompileFile(filePath) {
if (!this.fileHashes[filePath]) {
const source = stripBomBuf(fs.readFileSync(filePath));
Expand All @@ -41,11 +42,13 @@ class CachingPrecompiler {

return this.fileHashes[filePath];
}

// Conditionally called by caching-transform when precompiling is required
_init() {
this.babel = require('babel-core');
return this._transform;
}

_transform(code, filePath, hash) {
code = code.toString();

Expand Down Expand Up @@ -79,6 +82,7 @@ class CachingPrecompiler {

return `${result.code}\n${comment}`;
}

_createTransform() {
const salt = packageHash.sync([
require.resolve('../package.json'),
Expand All @@ -93,6 +97,7 @@ class CachingPrecompiler {
ext: '.js'
});
}

_generateHash(code, filePath, salt) {
const hash = md5Hex([code, filePath, salt]);
this.fileHashes[filePath] = hash;
Expand Down
11 changes: 11 additions & 0 deletions lib/logger.js
Expand Up @@ -6,44 +6,51 @@ class Logger {
this.reporter = reporter;
autoBind(this);
}

start(runStatus) {
if (!this.reporter.start) {
return;
}

this.write(this.reporter.start(runStatus), runStatus);
}

reset(runStatus) {
if (!this.reporter.reset) {
return;
}

this.write(this.reporter.reset(runStatus), runStatus);
}

test(test, runStatus) {
this.write(this.reporter.test(test, runStatus), runStatus);
}

unhandledError(err, runStatus) {
if (!this.reporter.unhandledError) {
return;
}

this.write(this.reporter.unhandledError(err, runStatus), runStatus);
}

finish(runStatus) {
if (!this.reporter.finish) {
return;
}

this.write(this.reporter.finish(runStatus), runStatus);
}

section() {
if (!this.reporter.section) {
return;
}

this.write(this.reporter.section());
}

clear() {
if (!this.reporter.clear) {
return false;
Expand All @@ -52,27 +59,31 @@ class Logger {
this.write(this.reporter.clear());
return true;
}

write(str, runStatus) {
if (typeof str === 'undefined') {
return;
}

this.reporter.write(str, runStatus);
}

stdout(data, runStatus) {
if (!this.reporter.stdout) {
return;
}

this.reporter.stdout(data, runStatus);
}

stderr(data, runStatus) {
if (!this.reporter.stderr) {
return;
}

this.reporter.stderr(data, runStatus);
}

exit(code) {
process.exit(code); // eslint-disable-line unicorn/no-process-exit
}
Expand Down
4 changes: 3 additions & 1 deletion lib/reporters/improper-usage-messages.js
Expand Up @@ -15,7 +15,9 @@ exports.forError = error => {
Visit the following URL for more details:
${chalk.blue.underline('https://github.com/avajs/ava#throwsfunctionpromise-error-message')}`;
} else if (assertion === 'snapshot') {
}

if (assertion === 'snapshot') {
const name = error.improperUsage.name;
const snapPath = error.improperUsage.snapPath;

Expand Down

0 comments on commit efc3b31

Please sign in to comment.