Skip to content

Commit

Permalink
Merge fb4386a into c111c9d
Browse files Browse the repository at this point in the history
  • Loading branch information
ldgit committed Jan 25, 2020
2 parents c111c9d + fb4386a commit 549d9a5
Show file tree
Hide file tree
Showing 18 changed files with 1,741 additions and 2,577 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: node_js
node_js:
- "8"
- "10"
- "11"
- "12"
script:
- npm run test-all
after_success: npm run coverage
Empty file modified index.js
100755 → 100644
Empty file.
4,061 changes: 1,582 additions & 2,479 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@
},
"devDependencies": {
"chai": "^4.2.0",
"coveralls": "^3.0.2",
"eslint": "^5.12.1",
"coveralls": "^3.0.3",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.15.0",
"lolex": "^3.0.0",
"mocha": "^5.2.0",
"nyc": "^13.1.0"
"eslint-plugin-import": "^2.17.2",
"lolex": "^4.0.1",
"mocha": "^6.1.4",
"nyc": "^14.1.1",
"touch": "^3.1.0"
},
"dependencies": {
"chalk": "^2.4.2",
"chokidar": "^2.0.4",
"commander": "^2.19.0",
"chokidar": "^3.0.0",
"commander": "^2.20.0",
"cross-spawn": "^6.0.5",
"date-fns": "^1.30.1",
"glob": "^7.1.3",
"glob": "^7.1.4",
"lodash": "^4.17.11"
},
"optionalDependencies": {
Expand All @@ -49,7 +51,8 @@
"coverage/**",
"test/**",
"**/node_modules/**",
"*.config.js"
"*.config.js",
".eslintrc.js"
]
},
"scripts": {
Expand Down
20 changes: 13 additions & 7 deletions src/command-runner.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
const { spawnSync } = require('child_process');
const formatDate = require('date-fns/format');
const crossSpawn = require('cross-spawn');
const { consolePrinter, format } = require('../src/printer');

function configureRunCommands(spawn, printer, stdin) {
return runCommands.bind(null, spawn, printer, stdin);
function configureRunCommands(spawn, printer, stdin, platform) {
return runCommands.bind(null, spawn, printer, stdin, platform);
}

function runCommands(spawn, printer, stdin, commands) {
stdin.setRawMode(false);
function runCommands(spawn, printer, stdin, platform, commands) {
if (platform !== 'win32') {
stdin.setRawMode(false);
}

commands.forEach((command) => {
printer.info(`[${getCurrentTime()}] ${command.command} ${command.args.join(' ')}`);
spawn(command.command, command.args, { stdio: 'inherit' });
});
stdin.setRawMode(true);

if (platform !== 'win32') {
stdin.setRawMode(true);
}

printer.message(`\nPress ${format.red('l')} to list available commands\n`);
}
Expand All @@ -23,5 +29,5 @@ function getCurrentTime() {

module.exports = {
configureRunCommands,
runCommands: configureRunCommands(spawnSync, consolePrinter, process.stdin),
runCommands: configureRunCommands(crossSpawn.sync, consolePrinter, process.stdin, process.platform),
};
7 changes: 5 additions & 2 deletions src/test-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ function findTestsFor(environments, filePath) {
}

function getPossibleTestPath(filePath, environment) {
if (filePath.startsWith(environment.testDir)) {
return filePath;
const normalizedFilePath = path.normalize(filePath).replace(/\\/g, '/');
const normalizedTestDir = path.normalize(environment.testDir).replace(/\\/g, '/');

if (normalizedFilePath.startsWith(normalizedTestDir)) {
return path.join(normalizedFilePath);
}

return [
Expand Down
11 changes: 7 additions & 4 deletions src/watchlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ function configureCompileWatchlist(printer, environments) {
locationsToWatch.push(sourceFilePath);
}

locationsToWatch.push(filepath);
locationsToWatch.push(path.join(filepath));
});
});

return removeDuplicates(locationsToWatch);
}

function getSourcePathFromTestPath(testPath, environment) {
return path.join('./', testPath
.replace(path.normalize(environment.testDir), `/${environment.sourceDir}`)
.replace(`${environment.testNameSuffix}.${environment.extension}`, `.${environment.extension}`));
return path.join(
'./',
path.normalize(testPath)
.replace(path.normalize(environment.testDir), path.normalize(`/${environment.sourceDir}`))
.replace(`${environment.testNameSuffix}.${environment.extension}`, `.${environment.extension}`),
);
}

function removeDuplicates(array) {
Expand Down
10 changes: 6 additions & 4 deletions test/command-line-options.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const assert = require('assert');
const { expect } = require('chai');
const path = require('path');
const getCommandLineOptions = require('../src/command-line-options');
const { version } = require('./../package.json');

Expand All @@ -11,25 +13,25 @@ describe('command line options', () => {

it('should return default configuration file if no config file specified', () => {
const commandLineOptions = getCommandLineOptions(processArgv);
assert.equal(commandLineOptions.config, `${process.cwd()}/argus.config.js`);
assert.equal(commandLineOptions.config, path.join(process.cwd(), 'argus.config.js'));
});

it('should get configuration file from command line', () => {
processArgv.push('-c', 'custom.argus.config.js');
const commandLineOptions = getCommandLineOptions(processArgv);
assert.equal(commandLineOptions.config, `${process.cwd()}/custom.argus.config.js`);
assert.equal(commandLineOptions.config, path.join(process.cwd(), 'custom.argus.config.js'));
});

it('should normalize given absolute config path', () => {
processArgv.push('-c', '/some///weird//path/to/argus.config.js');
const commandLineOptions = getCommandLineOptions(processArgv);
assert.equal(commandLineOptions.config, '/some/weird/path/to/argus.config.js');
expect(commandLineOptions.config).to.equal(path.resolve(path.join('/some/weird/path/to/argus.config.js')));
});

it('should normalize given relative config path', () => {
processArgv.push('-c', './weird//path///argus.config.js');
const commandLineOptions = getCommandLineOptions(processArgv);
assert.equal(commandLineOptions.config, `${process.cwd()}/weird/path/argus.config.js`);
expect(commandLineOptions.config).to.contain(path.join(process.cwd(), '/weird/path/argus.config.js'));
});

it('should use same version as package json', () => {
Expand Down
25 changes: 17 additions & 8 deletions test/command-runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { spawnSync } = require('child_process');
const { configureRunCommands } = require('../src/command-runner');
const { createPrinterSpy, format } = require('../src/printer');
const { StdinMock } = require('./helpers/mockStdio');
const wait = require('./helpers/wait');

describe('command-runner synchronous implementation', () => {
it('smoke test', () => {
Expand All @@ -28,7 +29,7 @@ describe('command-runner', () => {
spawnSpyData = [];
spawnSpyWasCalled = false;
printerSpy = createPrinterSpy();
runCommands = configureRunCommands(spawnSpy, printerSpy, mockStdin);
runCommands = configureRunCommands(spawnSpy, printerSpy, mockStdin, 'linux');
});

it('should run given commands', () => {
Expand All @@ -49,24 +50,24 @@ describe('command-runner', () => {
assert.strictEqual(spawnSpyWasCalled, false);
});

context('when running command', () => {
context('time sensitive stuff', () => {
let clock;

beforeEach(() => {
clock = lolex.install({ now: new Date(2017, 7, 1, 18, 5, 5) });
});

afterEach(() => {
clock.uninstall();
});
afterEach(() => clock.uninstall());

it('should print info message', () => {
it('when running command should print info message', () => {
runCommands([{ command: 'echo', args: ['one'] }, { command: 'phpunit', args: ['-c', 'phpunit.xml'] }]);
const printedMessages = printerSpy.getPrintedMessages();
assert.deepStrictEqual(printedMessages[0], { text: '[2017-08-01 18:05:05] echo one', type: 'info' });
assert.deepStrictEqual(printedMessages[1], { text: '[2017-08-01 18:05:05] phpunit -c phpunit.xml', type: 'info' });
});
});

context('when running command', () => {
it('should print info on how to list all commands', () => {
runCommands([{ command: 'echo', args: ['one'] }, { command: 'echo', args: ['two'] }]);

Expand All @@ -77,8 +78,16 @@ describe('command-runner', () => {
);
});

it('should temporarily disable stdin raw mode so user can terminate the process during command execution', () => {
// We monkeypatch stdin.setRawMode() method to also log events to spawnSpyData array
it('should not temporarily disable stdin raw mode on windows OS because this leads to weird behaviour (unable to rerun, list commands, etc.)', () => {
runCommands = configureRunCommands(spawnSpy, printerSpy, mockStdin, 'win32');
runCommands([{ command: 'npm', args: ['t'] }]);
return wait(10).then(() => assert.strictEqual(mockStdin.rawModeCalled, false));
});

it('should temporarily disable stdin raw mode on non-windows OS so user can terminate the process during command execution', () => {
runCommands = configureRunCommands(spawnSpy, printerSpy, mockStdin, 'linux');
// We monkeypatch stdin.setRawMode() method to also log events to spawnSpyData array. We do this so we can assert
// that setRawMode was called exactly before and after spawn function was called.
const originalSetRawMode = mockStdin.setRawMode.bind(mockStdin);
mockStdin.setRawMode = (isRaw) => {
spawnSpyData.push(`raw mode set to ${isRaw}`);
Expand Down
24 changes: 12 additions & 12 deletions test/configuration-reader.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const path = require('path');
const { expect } = require('chai');
const readConfiguration = require('../src/configuration-reader');

Expand All @@ -11,7 +11,7 @@ describe('readConfiguration', () => {

it('should exit with error message if given configuration file does not exist', () => {
const readConfigurationFromNonExistingFile = () => readConfiguration('some/file/that/does/not/exist.js');
expect(readConfigurationFromNonExistingFile).to.throw(TypeError, /some\/file\/that\/does\/not\/exist.js/);
expect(readConfigurationFromNonExistingFile).to.throw(TypeError, path.join('some/file/that/does/not/exist.js'));
});

context('if given a configuration file', () => {
Expand All @@ -22,7 +22,7 @@ describe('readConfiguration', () => {
});

it('should import and return it', () => {
assert.deepEqual(readConfiguration('test/fixtures/configuration-reader.test/valid.config.js').environments, [
expect(readConfiguration('test/fixtures/configuration-reader.test/valid.config.js').environments).to.deep.equal([
{
extension: 'js',
testNameSuffix: '.test',
Expand All @@ -35,27 +35,27 @@ describe('readConfiguration', () => {

it('should lowercase each environment extension automatically', () => {
const config = readConfiguration('test/fixtures/configuration-reader.test/technically.valid.config.js');
assert.equal(config.environments[0].extension, 'php');
assert.equal(config.environments[1].extension, 'js');
expect(config.environments[0].extension).to.equal('php');
expect(config.environments[1].extension).to.equal('js');
});

it('should assign empty array to arguments property for testRunnerCommand if missing', () => {
const config = readConfiguration('test/fixtures/configuration-reader.test/no-test-command-arguments.config.js');
assert.deepStrictEqual(config.environments[0].testRunnerCommand.arguments, []);
assert.deepStrictEqual(config.environments[1].testRunnerCommand.arguments, ['t', '--']);
assert.deepStrictEqual(config.environments[2].testRunnerCommand.arguments, []);
expect(config.environments[0].testRunnerCommand.arguments).to.deep.equal([]);
expect(config.environments[1].testRunnerCommand.arguments).to.deep.equal(['t', '--']);
expect(config.environments[2].testRunnerCommand.arguments).to.deep.equal([]);
});

it('should use empty string for sourceDir if source is current directory', () => {
const config = readConfiguration('test/fixtures/configuration-reader.test/technically.valid.config.js');
assert.equal(config.environments[0].sourceDir, '');
assert.equal(config.environments[1].sourceDir, '');
assert.equal(config.environments[2].sourceDir, '');
expect(config.environments[0].sourceDir).to.equal('');
expect(config.environments[1].sourceDir).to.equal('');
expect(config.environments[2].sourceDir).to.equal('');
});

it('should trim source directory when reading it', () => {
const config = readConfiguration('test/fixtures/configuration-reader.test/technically.valid.config.js');
assert.equal(config.environments[3].sourceDir, 'src');
expect(config.environments[3].sourceDir).to.equal('src');
});
});
});
2 changes: 1 addition & 1 deletion test/file-watcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('watcher', function watcherTest() {
callbackWasCalled = true;
});

wait(30).then(() => assert.strictEqual(callbackWasCalled, false));
return wait(30).then(() => assert.strictEqual(callbackWasCalled, false));
});

// Watchlist (input for watchFiles() function) also does this when it is compiled, but in this case it doesn't hurt
Expand Down
2 changes: 2 additions & 0 deletions test/helpers/mockStdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class StdinMock extends Readable {
constructor(options) {
super(options);
this.rawMode = false;
this.rawModeCalled = false;
this.encoding = 'not utf 8';
}

/* eslint-disable-next-line class-methods-use-this */
_read() {}

setRawMode(isRaw) {
this.rawModeCalled = true;
this.rawMode = isRaw;
}

Expand Down
5 changes: 3 additions & 2 deletions test/helpers/touch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { spawnSync } = require('child_process');
const path = require('path');
const touch = require('touch');

spawnSync('touch', [process.argv[2]]);
touch(path.join(process.argv[2]));
3 changes: 3 additions & 0 deletions test/helpers/wait.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const wait = miliseconds => new Promise(resolve => setTimeout(resolve, miliseconds));

module.exports = wait;
Loading

0 comments on commit 549d9a5

Please sign in to comment.