Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade everything: move to jest, add eslint, add package-lock.json, add new features (respond with function) #21

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---

extends:
- airbnb-base

parser: babel-eslint

env:
jest: true

rules:
no-console: 0
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
npm-debug.log
test/tmp/*
!test/tmp/.keep
jest_0
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---

language: node_js
node_js:
- node
- 10
- 8
- 6
- 4
- 0.10
script:
- make test-ci
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# Paths
#

TEST_EXEC=node_modules/.bin/hydro

#
# All
#
Expand All @@ -22,7 +20,7 @@ install:
#

test:
@NODE_ENV=test $(TEST_EXEC)
npm test -s

#
# Clean
Expand Down
3 changes: 3 additions & 0 deletions __tests__/__snapshots__/no-command.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`nixt#run throws an error when no command is supplied 1`] = `"Please provide a command to run. Hint: \`nixt#run\`"`;
11 changes: 11 additions & 0 deletions __tests__/base.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { nfixt } = require('./utils');

describe('nixt#base', () => {
it('sets a base command', (done) => {
nfixt()
.base('node ')
.run('code-0.js')
.code(0)
.end(done);
});
});
30 changes: 30 additions & 0 deletions __tests__/code.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { nfixt } = require('./utils');

describe('nixt#code', () => {
it('can verify the exit code of a program', (done) => {
nfixt()
.run('node code-0.js')
.code(0)
.end(done);
});

it('returns an error when the exit code does not match the expected one', (done) => {
nfixt()
.run('node code-0.js')
.code(1)
.end((err) => {
expect(err.message).toBe('`node code-0.js`: Expected exit code: "1", actual: "0"');
done();
});
});

it('returns an error when the nonzero exit code does not match the expected one', (done) => {
nfixt()
.run('node code-1.js')
.code(0)
.end((err) => {
expect(err.message).toBe('`node code-1.js`: Expected exit code: "0", actual: "1"');
done();
});
});
});
13 changes: 13 additions & 0 deletions __tests__/colors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const nixt = require('..');
const { join } = require('path');

describe('nixt', () => {
it('can strip colors from stdout and stderr', (done) => {
nixt({ colors: false })
.cwd(join(__dirname, 'fixtures'))
.run('node colors.js')
.stdout('Stdout')
.stderr('Stderr')
.end(done);
});
});
12 changes: 12 additions & 0 deletions __tests__/env.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { nfixt } = require('./utils');

describe('nixt#env', () => {
it('sets environemt variables', (done) => {
nfixt()
.env('HELLO', 'true')
.env('BYE', 'true')
.run('node env.js')
.code(0)
.end(done);
});
});
33 changes: 33 additions & 0 deletions __tests__/exec.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fs = require('fs');
const { join } = require('path');
const { nfixt } = require('./utils');

const file = join(__dirname, 'tmp', 'writefile-test5');

describe('nixt#exec', () => {
it('runs the supplied command', (done) => {
nfixt()
.exec(`node ${join(__dirname, 'fixtures', 'create-file.js')} writefile-test5`)
.run('node writefile.js writefile-test5')
.stdout('File exists')
.unlink(file)
.end(done);
});

it('respects the current working directory', (done) => {
const root = join(__dirname, 'tmp');
const fixture = join(root, 'foo.tmp.js');

nfixt()
.cwd(root)
.exec('touch foo.tmp.js')
.run('')
.end((err) => {
// should.not.exist(err);
expect(err).toBeUndefined();
expect(fs.existsSync(fixture)).toBe(true);
fs.unlinkSync(fixture);
done();
});
});
});
47 changes: 47 additions & 0 deletions __tests__/exist.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const fs = require('fs');
const { join } = require('path');
const { nfixt } = require('./utils');

const file = join(__dirname, 'tmp', 'exists-file-test');
const dir = join(__dirname, 'tmp', 'exists-dir-test');

describe('nixt#exist', () => {
it('can verify that a file exists', (done) => {
nfixt()
.writeFile(file)
.run('node void.js')
.exist(file)
.unlink(file)
.end(done);
});

it('returns an error when the file does not exist', (done) => {
nfixt()
.run('node void.js')
.exist(file)
.end((err) => {
// (err !== null).should.eq(true);
expect(err).not.toBeNull();
done();
});
});

it('can verify that a directory exists', (done) => {
nfixt()
.mkdir(dir)
.run('node void.js')
.exist(dir)
.rmdir(dir)
.end(done);
});

it('returns an error when the directory does not exist', (done) => {
nfixt()
.run('node void.js')
.exist(dir)
.end((err) => {
expect(err).not.toBeNull();
done();
});
});
});
40 changes: 40 additions & 0 deletions __tests__/filters.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const fs = require('fs');
const { join } = require('path');
const { nfixt } = require('./utils');

const file1 = join(__dirname, 'tmp', 'file-1');
const file2 = join(__dirname, 'tmp', 'file-2');

describe('nixt filters', () => {
it('runs the filters in the expected order', (done) => {
let before = 0;
let after = 0;

nfixt()
.before(() => {
before++;
fs.writeFileSync(file1, '');
})
.before((next) => {
before++;
expect(fs.existsSync(file1)).toBe(true);
fs.writeFile(file2, '', next);
})
.after(() => {
after++;
fs.unlinkSync(file1);
fs.unlinkSync(file2);
})
.run('node filters.js')
.stdout('Files exist')
.end(() => {
expect(before).toBe(2);
expect(after).toBe(1);

expect(fs.existsSync(file1)).toBe(false);
expect(fs.existsSync(file2)).toBe(false);

done();
});
});
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions __tests__/fixtures/create-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const fs = require('fs');
const { join } = require('path');

const file = join(__dirname, '..', 'tmp', process.argv[2]);

fs.writeFileSync(file, '');
File renamed without changes.
11 changes: 11 additions & 0 deletions __tests__/fixtures/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs');
const { join } = require('path');

const file1 = join(__dirname, '..', 'tmp', 'file-1');
const file2 = join(__dirname, '..', 'tmp', 'file-2');

if (fs.existsSync(file1) && fs.existsSync(file2)) {
console.log('Files exist');
} else {
console.log('Files do not exist');
}
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions __tests__/fixtures/mkdir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const fs = require('fs');
const { join } = require('path');

const dir = join(__dirname, '..', 'tmp', process.argv[2]);

if (fs.existsSync(dir)) {
console.log('Directory exists');
}
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions __tests__/fixtures/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const prompt = require('prompt');

prompt.colors = false;
prompt.start();

prompt.get(['first', 'second'], (err, result) => {
console.log('Command-line input received:');
console.log(` first: ${result.first}`);
console.log(` second: ${result.second}`);
});
17 changes: 17 additions & 0 deletions __tests__/fixtures/rev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let data = '';

process.stdin.on('data', (chunk) => {
const lines = (data + chunk).split('\n');
data = lines.pop();
lines.forEach(printReversed);
});

process.stdin.on('end', () => {
if (data) {
printReversed(data);
}
});

function printReversed(line) {
console.log(line.split('').reverse().join(''));
}
2 changes: 2 additions & 0 deletions __tests__/fixtures/timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('output from spawned script');
setTimeout(() => {}, 10000);
File renamed without changes.
8 changes: 8 additions & 0 deletions __tests__/fixtures/writefile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const fs = require('fs');
const { join } = require('path');

const file = join(__dirname, '..', 'tmp', process.argv[2]);

if (fs.existsSync(file)) {
console.log('File exists');
}
38 changes: 38 additions & 0 deletions __tests__/match.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { join } = require('path');
const { nfixt } = require('./utils');

describe('nixt#match', () => {
it('can assert with strings', (done) => {
const file = join(__dirname, 'tmp', 'writefile-test1');

nfixt()
.writeFile(file, 'Hello')
.run('node void.js')
.match(file, 'Hello')
.unlink(file)
.end(done);
});

it('can assert with regular expressions', (done) => {
const file = join(__dirname, 'tmp', 'writefile-test2');
nfixt()
.writeFile(file, 'Hello')
.run('node void.js')
.match(file, /ello/)
.unlink(file)
.end(done);
});

it('returns an error when the contents does not match the desired string', (done) => {
const file = join(__dirname, 'tmp', 'writefile-test3');
nfixt()
.writeFile(file, 'Hello')
.run('node void.js')
.match(file, 'Bye')
.unlink(file)
.end((err) => {
expect(err).not.toBeNull();
done();
});
});
});
18 changes: 18 additions & 0 deletions __tests__/mkdir.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs');
const { join } = require('path');
const { nfixt } = require('./utils');

const dir = join(__dirname, 'tmp', 'mkdir-test');

describe('nixt#mkdir', () => {
it('creates a new directory', (done) => {
nfixt()
.mkdir(dir)
.run('node mkdir.js mkdir-test')
.stdout('Directory exists')
.after(() => {
fs.rmdirSync(dir);
})
.end(done);
});
});
Loading