diff --git a/.taprc b/.taprc new file mode 100644 index 0000000..381688a --- /dev/null +++ b/.taprc @@ -0,0 +1 @@ +check-coverage: false \ No newline at end of file diff --git a/package.json b/package.json index 228a2ac..e6fb258 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,11 @@ "description": "Automatically install pre-commit hooks for your npm modules.", "main": "index.js", "scripts": { - "coverage": "istanbul cover ./node_modules/.bin/_mocha -- test.js", "example-fail": "echo \"This is the example hook, I exit with 1\" && exit 1", "example-pass": "echo \"This is the example hook, I exit with 0\" && exit 0", "install": "node install.js", - "test": "mocha test.js", - "test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- test.js", + "unit": "tap test.js", + "test": "npm run unit", "uninstall": "node uninstall.js" }, "repository": { @@ -35,9 +34,7 @@ "which": "^2.0.2" }, "devDependencies": { - "assume": "^2.3.0", - "istanbul": "0.4.x", - "mocha": "^8.4.0", - "pre-commit": "git://github.com/observing/pre-commit.git" + "pre-commit": "git://github.com/observing/pre-commit.git", + "tap": "^15.0.9" } } diff --git a/test.js b/test.js index 829e17f..c50c518 100644 --- a/test.js +++ b/test.js @@ -1,257 +1,275 @@ -/* istanbul ignore next */ -describe('pre-commit', function () { - 'use strict'; +'use strict' +const t = require('tap') +const Hook = require('./') +const tty = require('tty') +const ttySupportColor = tty.isatty(process.stdout.fd) - var assume = require('assume') - , Hook = require('./'); +t.test('pre-commit', function (t) { + t.plan(5) - it('is exported as a function', function () { - assume(Hook).is.a('function'); - }); + t.test('is exported as a function', function (t) { + t.plan(1) + t.strictSame(typeof Hook, 'function') + }) - it('can be initialized without a `new` keyword', function () { - var hook = Hook(function () {}, { + t.test('can be initialized without a `new` keyword', function (t) { + t.plan(2) + + const hook = Hook(function () {}, { ignorestatus: true - }); + }) + + t.strictSame(hook instanceof Hook, true) + t.strictSame(typeof hook.parse, 'function') + }) - assume(hook).is.instanceOf(Hook); - assume(hook.parse).is.a('function'); - }); + t.test('#parse', function (t) { + t.plan(7) - describe('#parse', function () { - var hook; + let hook - beforeEach(function () { + t.beforeEach(function () { hook = new Hook(function () {}, { ignorestatus: true - }); - }); + }) + }) + + t.test('extracts configuration values from precommit.', function (t) { + t.plan(3) - it('extracts configuration values from precommit.', function () { hook.json = { 'precommit.silent': true - }; + } - assume(hook.silent).is.false(); + t.strictSame(hook.silent, false) - hook.parse(); + hook.parse() - assume(hook.config.silent).is.true(); - assume(hook.silent).is.true(); - }); + t.strictSame(hook.config.silent, true) + t.strictSame(hook.silent, true) + }) + + t.test('extracts configuration values from pre-commit.', function (t) { + t.plan(5) - it('extracts configuration values from pre-commit.', function () { hook.json = { 'pre-commit.silent': true, 'pre-commit.colors': false - }; + } + + t.strictSame(hook.silent, false) + t.strictSame(hook.colors, ttySupportColor) - assume(hook.silent).is.false(); - assume(hook.colors).is.true(); + hook.parse() - hook.parse(); + t.strictSame(hook.config.silent, true) + t.strictSame(hook.silent, true) + t.strictSame(hook.colors, false) + }) - assume(hook.config.silent).is.true(); - assume(hook.silent).is.true(); - assume(hook.colors).is.false(); - }); + t.test('normalizes the `pre-commit` to an array', function (t) { + t.plan(2) - it('normalizes the `pre-commit` to an array', function () { hook.json = { 'pre-commit': 'test, cows, moo' - }; + } + + hook.parse() - hook.parse(); + t.strictSame(hook.config.run.length, 3) + t.strictSame(hook.config.run, ['test', 'cows', 'moo']) + }) - assume(hook.config.run).is.length(3); - assume(hook.config.run).contains('test'); - assume(hook.config.run).contains('cows'); - assume(hook.config.run).contains('moo'); - }); + t.test('normalizes the `precommit` to an array', function (t) { + t.plan(2) - it('normalizes the `precommit` to an array', function () { hook.json = { - 'precommit': 'test, cows, moo' - }; + precommit: 'test, cows, moo' + } - hook.parse(); + hook.parse() - assume(hook.config.run).is.length(3); - assume(hook.config.run).contains('test'); - assume(hook.config.run).contains('cows'); - assume(hook.config.run).contains('moo'); - }); + t.strictSame(hook.config.run.length, 3) + t.strictSame(hook.config.run, ['test', 'cows', 'moo']) + }) + + t.test('allows `pre-commit` object based syntax', function (t) { + t.plan(4) - it('allows `pre-commit` object based syntax', function () { hook.json = { 'pre-commit': { run: 'test scripts go here', silent: true, colors: false } - }; + } + + hook.parse() - hook.parse(); + t.strictSame(hook.config.run.length, 4) + t.strictSame(hook.config.run, ['test', 'scripts', 'go', 'here']) + t.strictSame(hook.silent, true) + t.strictSame(hook.colors, false) + }) - assume(hook.config.run).is.length(4); - assume(hook.config.run).contains('test'); - assume(hook.config.run).contains('scripts'); - assume(hook.config.run).contains('go'); - assume(hook.config.run).contains('here'); - assume(hook.silent).is.true(); - assume(hook.colors).is.false(); - }); + t.test('defaults to `test` if nothing is specified', function (t) { + t.plan(2) - it('defaults to `test` if nothing is specified', function () { hook.json = { scripts: { test: 'mocha test.js' } - }; + } + + hook.parse() - hook.parse(); - assume(hook.config.run).deep.equals(['test']); - }); + t.strictSame(hook.config.run.length, 1) + t.strictSame(hook.config.run, ['test']) + }) + + t.test('ignores the default npm.script.test placeholder', function (t) { + t.plan(1) - it('ignores the default npm.script.test placeholder', function () { hook.json = { scripts: { test: 'echo "Error: no test specified" && exit 1' } - }; + } + + hook.parse() - hook.parse(); - assume(hook.config.run).has.length(0); - }); - }); + t.strictSame(typeof hook.config.run, 'undefined') + }) + }) - describe('#log', function () { - it('prefixes the logs with `pre-commit`', function (next) { - var hook = new Hook(function (code, lines) { - assume(code).equals(1); - assume(lines).is.a('array'); + t.test('#log', function (t) { + t.plan(6) - assume(lines[0]).includes('pre-commit'); - assume(lines[1]).includes('pre-commit'); - assume(lines[1]).includes('foo'); - assume(lines).has.length(3); + t.test('prefixes the logs with `pre-commit`', function (t) { + t.plan(9) + const hook = new Hook(function (code, lines) { + t.strictSame(code, 1) + t.strictSame(Array.isArray(lines), true) + + t.strictSame(lines[0], 'pre-commit: ') + t.strictSame(lines[1], 'pre-commit: foo') + t.strictSame(lines[2], 'pre-commit: ') + t.strictSame(lines.length, 3) // color prefix check lines.forEach(function (line) { - assume(line).contains('\u001b'); - }); + t.strictSame(line.includes('\u001b'), ttySupportColor) + }) + }, { ignorestatus: true }) - next(); - }, { ignorestatus: true }); + hook.config.silent = true + hook.log(['foo']) + }) - hook.config.silent = true; - hook.log(['foo']); - }); + t.test('allows for a custom error code', function (t) { + t.plan(1) - it('allows for a custom error code', function (next) { - var hook = new Hook(function (code, lines) { - assume(code).equals(0); + const hook = new Hook(function (code, lines) { + t.strictSame(code, 0) + }, { ignorestatus: true }) - next(); - }, { ignorestatus: true }); + hook.config.silent = true + hook.log(['foo'], 0) + }) - hook.config.silent = true; - hook.log(['foo'], 0); - }); + t.test('allows strings to be split \\n', function (t) { + t.plan(4) - it('allows strings to be split \\n', function (next) { - var hook = new Hook(function (code, lines) { - assume(code).equals(0); + const hook = new Hook(function (code, lines) { + t.strictSame(code, 0) - assume(lines).has.length(4); - assume(lines[1]).contains('foo'); - assume(lines[2]).contains('bar'); + t.strictSame(lines.length, 4) + t.strictSame(lines[1], 'pre-commit: foo') + t.strictSame(lines[2], 'pre-commit: bar') + }, { ignorestatus: true }) - next(); - }, { ignorestatus: true }); + hook.config.silent = true + hook.log('foo\nbar', 0) + }) - hook.config.silent = true; - hook.log('foo\nbar', 0); - }); + t.test('does not output colors when configured to do so', function (t) { + t.plan(5) - it('does not output colors when configured to do so', function (next) { - var hook = new Hook(function (code, lines) { - assume(code).equals(0); + const hook = new Hook(function (code, lines) { + t.strictSame(code, 0) lines.forEach(function (line) { - assume(line).does.not.contain('\u001b'); - }); - - next(); - }, { ignorestatus: true }); + t.strictSame(line.includes('\u001b'), false) + }) + }, { ignorestatus: true }) - hook.config.silent = true; - hook.config.colors = false; + hook.config.silent = true + hook.config.colors = false - hook.log('foo\nbar', 0); - }); + hook.log('foo\nbar', 0) + }) - it('output lines to stderr if error code 1', function (next) { - var err = console.error; - next = assume.plan(4, next); + t.test('output lines to stderr if error code 1', function (t) { + t.plan(4) - var hook = new Hook(function (code, lines) { - console.error = err; - next(); - }, { ignorestatus: true }); + const err = console.error + const hook = new Hook(function (code, lines) { + console.error = err + }, { ignorestatus: true }) console.error = function (line) { - assume(line).contains('pre-commit: '); - }; + t.strictSame(line.includes('pre-commit: '), true) + } - hook.config.colors = false; - hook.log('foo\nbar', 1); - }); + hook.config.colors = false + hook.log('foo\nbar', 1) + }) - it('output lines to stdout if error code 0', function (next) { - var log = console.log; - next = assume.plan(4, next); + t.test('output lines to stderr if error code 0', function (t) { + t.plan(4) - var hook = new Hook(function (code, lines) { - console.log = log; - next(); - }, { ignorestatus: true }); + const log = console.log + const hook = new Hook(function (code, lines) { + console.log = log + }, { ignorestatus: true }) console.log = function (line) { - assume(line).contains('pre-commit: '); - }; - - hook.config.colors = false; - hook.log('foo\nbar', 0); - }); - }); - - describe('#run', function () { - it('runs the specified scripts and exit with 0 on no error', function (next) { - var hook = new Hook(function (code, lines) { - assume(code).equals(0); - assume(lines).is.undefined(); - - next(); - }, { ignorestatus: true }); - - hook.config.run = ['example-pass']; - hook.run(); - }); - - it('runs the specified test and exits with 1 on error', function (next) { - var hook = new Hook(function (code, lines) { - assume(code).equals(1); - - assume(lines).is.a('array'); - assume(lines[1]).contains('`example-fail`'); - assume(lines[2]).contains('code (1)'); - - next(); - }, { ignorestatus: true }); - - hook.config.run = ['example-fail']; - hook.run(); - }); - }); -}); + t.strictSame(line.includes('pre-commit: '), true) + } + + hook.config.colors = false + hook.log('foo\nbar', 0) + }) + }) + + t.test('#run', function (t) { + t.plan(2) + + t.test('runs the specified scripts and exit with 0 on no error', function (t) { + t.plan(2) + + const hook = new Hook(function (code, lines) { + t.strictSame(code, 0) + t.strictSame(typeof lines, 'undefined') + }, { ignorestatus: true }) + + hook.config.run = ['example-pass'] + hook.run() + }) + + t.test('runs the specified test and exits with 1 on error', function (t) { + t.plan(4) + + const hook = new Hook(function (code, lines) { + t.strictSame(code, 1) + + t.strictSame(Array.isArray(lines), true) + t.strictSame(lines[1].includes('`example-fail`'), true) + t.strictSame(lines[2].includes('code (1)'), true) + }, { ignorestatus: true }) + + hook.config.run = ['example-fail'] + hook.run() + }) + }) +})