From 419ae6506bc9278ab260783319fadbb74b82cab7 Mon Sep 17 00:00:00 2001 From: "Anantachai Saothong (Manta)" Date: Mon, 12 Feb 2024 17:08:17 +0700 Subject: [PATCH] feat: support --bail and --silent for "test" command --- index.js | 11 ++++++++-- index.test.js | 2 ++ package-lock.json | 17 ++++++++++++++ package.json | 1 + test.js | 7 ++++-- test.test.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 7fe8273..d60cb53 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const fs = require('fs') const path = require('path') const test = require('./test') +const { parseArguments } = require('@thisismanta/pessimist') const rulePath = findRulePath(__dirname) if (!rulePath) { @@ -19,13 +20,19 @@ module.exports.meta.name = name module.exports.meta.version = version module.exports.rules = module.exports.rules || {} -if (process.argv.includes('test')) { +const { bail, silent, ...args } = parseArguments(process.argv.slice(2), { + bail: false, + silent: false, +}) + +if (args[0] === 'test') { if (Object.keys(module.exports.rules).length === 0) { throw new Error('Could not find any rules.') } const errorCount = test(module.exports.rules, { - log: console.log, + bail, + log: silent ? () => { } : console.log, err: console.error, }) if (errorCount > 0) { diff --git a/index.test.js b/index.test.js index b53f738..9ea2d7f 100644 --- a/index.test.js +++ b/index.test.js @@ -2,6 +2,8 @@ const fs = require('fs') const path = require('path') const { jest, afterEach, it, expect } = require('@jest/globals') +jest.mock('@thisismanta/pessimist', () => ({ parseArguments: () => ({}) })) + afterEach(() => { jest.resetModules() diff --git a/package-lock.json b/package-lock.json index a381418..79eea64 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "3.2.1", "license": "MIT", "dependencies": { + "@thisismanta/pessimist": "^1.2.0", "chalk": "^4.0.0" }, "bin": { @@ -1372,6 +1373,17 @@ "@sinonjs/commons": "^3.0.0" } }, + "node_modules/@thisismanta/pessimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@thisismanta/pessimist/-/pessimist-1.2.0.tgz", + "integrity": "sha512-rm8/zjNMuO9hPYhEMavVIIxmvawJJB8mthvbVXd74XUW7V/SbgmtDBQjICbCWKjluvA+gh+cqi7dv85/jexknA==", + "dependencies": { + "lodash": "^4.17.21" + }, + "engines": { + "node": ">=16.0.0" + } + }, "node_modules/@thisismanta/semantic-version": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/@thisismanta/semantic-version/-/semantic-version-8.0.0.tgz", @@ -3785,6 +3797,11 @@ "node": ">=8" } }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" + }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", diff --git a/package.json b/package.json index b566a31..e66e956 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "lefthook": "^1.6.1" }, "dependencies": { + "@thisismanta/pessimist": "^1.2.0", "chalk": "^4.0.0" } } diff --git a/test.js b/test.js index 7f449e6..18471f8 100644 --- a/test.js +++ b/test.js @@ -34,7 +34,7 @@ function only(item) { */ module.exports = function test( rules, - { log, err } = { log: console.log, err: console.error } + { bail, log, err } = { bail: false, log: console.log, err: console.error } ) { // See https://eslint.org/docs/latest/integrate/nodejs-api#ruletester const tester = new RuleTester() @@ -88,7 +88,10 @@ module.exports = function test( err(offset(getPrettyCode(testCase.code), chalk.bgRed)) err('') err(offset(error.message, chalk.red)) - return 1 + + if (bail) { + return 1 + } } } else if (totalItems.length === runningItems.length) { diff --git a/test.test.js b/test.test.js index 9ba532c..53ff9f2 100644 --- a/test.test.js +++ b/test.test.js @@ -79,6 +79,62 @@ it('returns non-zero errors, given any failing test case', () => { const err = jest.fn() const errorCount = test(rules, { log, err }) + expect(errorCount).toBe(2) + expect(log.mock.calls.join('\n')).toMatchInlineSnapshot(` +" + PASS 0 + FAIL 2" +`) + expect(err.mock.calls.join('\n')).toMatchInlineSnapshot(` +"🔴 foo + + void(0) + + Should have no errors but had 1: [ + { + ruleId: 'foo', + severity: 1, + message: 'bar', + line: 1, + column: 1, + nodeType: 'Program', + endLine: 1, + endColumn: 8 + } + ] (1 strictEqual 0) + + + + Should have 1 error but had 0: [] (0 strictEqual 1)" +`) +}) + +it('returns at most one error, given bailing out', () => { + const rules = { + foo: { + create(context) { + return { + Program(node) { + if (node.body.length > 0) { + context.report({ + node, + message: 'bar' + }) + } + } + } + }, + tests: { + valid: [{ code: 'void(0)' }], + invalid: [{ code: '', errors: [{ message: 'bar' }] }], + } + } + } + + const log = jest.fn() + const err = jest.fn() + const errorCount = test(rules, { bail: true, log, err }) + expect(errorCount).toBe(1) expect(log.mock.calls.join('\n')).toMatchInlineSnapshot(`""`) expect(err.mock.calls.join('\n')).toMatchInlineSnapshot(`