From 16848d83f3ec2cf67406755ec0aaa931ffb6787e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20J=C3=A4ppinen?= Date: Fri, 24 Apr 2020 18:18:35 +0300 Subject: [PATCH] fix: use test renderer during tests and when TERM=dumb --- lib/getRenderer.js | 12 ++ lib/runAll.js | 9 +- package.json | 2 +- test/__snapshots__/index.spec.js.snap | 4 - test/getRenderer.spec.js | 23 ++++ test/index.spec.js | 2 +- test/index2.spec.js | 2 +- test/integration.test.js | 156 +++++++++++++------------- test/runAll.spec.js | 86 +++++++------- yarn.lock | 8 +- 10 files changed, 164 insertions(+), 140 deletions(-) create mode 100644 lib/getRenderer.js create mode 100644 test/getRenderer.spec.js diff --git a/lib/getRenderer.js b/lib/getRenderer.js new file mode 100644 index 000000000..563e53406 --- /dev/null +++ b/lib/getRenderer.js @@ -0,0 +1,12 @@ +'use strict' + +const getRenderer = ({ debug, quiet }, env = process.env) => { + if (quiet) return 'silent' + // Better support for dumb terminals: https://en.wikipedia.org/wiki/Computer_terminal#Dumb_terminals + const isDumbTerminal = env.TERM === 'dumb' + if (isDumbTerminal || env.NODE_ENV === 'test') return 'test' + if (debug) return 'verbose' + return 'update' +} + +module.exports = getRenderer diff --git a/lib/runAll.js b/lib/runAll.js index d891ccccc..219ae1484 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -8,6 +8,7 @@ const chunkFiles = require('./chunkFiles') const debugLog = require('debug')('lint-staged:run') const execGit = require('./execGit') const generateTasks = require('./generateTasks') +const getRenderer = require('./getRenderer') const getStagedFiles = require('./getStagedFiles') const GitWorkflow = require('./gitWorkflow') const makeCmdTasks = require('./makeCmdTasks') @@ -35,14 +36,6 @@ const { GitRepoError, GetStagedFilesError, GitError } = require('./symbols') const createError = (ctx) => Object.assign(new Error('lint-staged failed'), { ctx }) -const getRenderer = ({ debug, quiet }) => { - if (quiet) return 'silent' - // Better support for dumb terminals: https://en.wikipedia.org/wiki/Computer_terminal#Dumb_terminals - const isDumbTerminal = process.env.TERM === 'dumb' - if (debug || isDumbTerminal) return 'verbose' - return 'update' -} - /** * Executes all tasks and either resolves or rejects the promise * diff --git a/package.json b/package.json index 683c7acc2..abf8be794 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "debug": "^4.1.1", "dedent": "^0.7.0", "execa": "^4.0.0", - "listr2": "1.3.7", + "listr2": "1.3.8", "log-symbols": "^3.0.0", "micromatch": "^4.0.2", "normalize-path": "^3.0.0", diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index f1934f91b..3046e8dbf 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -78,10 +78,6 @@ See https://github.com/okonet/lint-staged#configuration." exports[`lintStaged should use config object 1`] = ` " -LOG Running lint-staged with the following config: -LOG { - '*': 'node -e \\"process.exit(1)\\"' -} ERROR × Failed to get staged files!" `; diff --git a/test/getRenderer.spec.js b/test/getRenderer.spec.js new file mode 100644 index 000000000..2f2aac70a --- /dev/null +++ b/test/getRenderer.spec.js @@ -0,0 +1,23 @@ +import getRenderer from '../lib/getRenderer' + +describe('getRenderer', () => { + it('should return silent when quiet', () => { + expect(getRenderer({ quiet: true }, {})).toEqual('silent') + }) + + it('should return test when NODE_ENV=test', () => { + expect(getRenderer({}, { NODE_ENV: 'test' })).toEqual('test') + }) + + it('should return test when TERM=dumb', () => { + expect(getRenderer({}, { TERM: 'dumb' })).toEqual('test') + }) + + it('should return verbose when debug', () => { + expect(getRenderer({ debug: true }, {})).toEqual('verbose') + }) + + it('should return update by default', () => { + expect(getRenderer({}, {})).toEqual('update') + }) +}) diff --git a/test/index.spec.js b/test/index.spec.js index b1e3cee12..a13455f36 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -135,7 +135,7 @@ describe('lintStaged', () => { '*': 'node -e "process.exit(1)"' } expect.assertions(1) - await lintStaged({ config, debug: true, quiet: true }, logger) + await lintStaged({ config, quiet: true }, logger) expect(logger.printHistory()).toMatchSnapshot() }) diff --git a/test/index2.spec.js b/test/index2.spec.js index 06951df85..cdab934d5 100644 --- a/test/index2.spec.js +++ b/test/index2.spec.js @@ -56,7 +56,7 @@ describe('lintStaged', () => { }, "dateFormat": false, "exitOnError": false, - "renderer": "verbose", + "renderer": "test", } `) }) diff --git a/test/integration.test.js b/test/integration.test.js index 1211d64fa..46961685a 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -242,22 +242,22 @@ describe('lint-staged', () => { await gitCommit({ config: { '*.js': 'prettier --list-different' } }) expect(console.printHistory()).toMatchInlineSnapshot(` " - INFO ❯ Preparing... - LOG ✔ Preparing... - INFO ❯ Hiding unstaged changes to partially staged files... - LOG ✔ Hiding unstaged changes to partially staged files... - INFO ❯ Running tasks... - INFO ❯ Running tasks for *.js - INFO ❯ prettier --list-different - LOG ✔ prettier --list-different - LOG ✔ Running tasks for *.js - LOG ✔ Running tasks... - INFO ❯ Applying modifications... - LOG ✔ Applying modifications... - INFO ❯ Restoring unstaged changes to partially staged files... - LOG ✔ Restoring unstaged changes to partially staged files... - INFO ❯ Cleaning up... - LOG ✔ Cleaning up..." + LOG [STARTED] Preparing... + LOG [SUCCESS] Preparing... + LOG [STARTED] Hiding unstaged changes to partially staged files... + LOG [SUCCESS] Hiding unstaged changes to partially staged files... + LOG [STARTED] Running tasks... + LOG [STARTED] Running tasks for *.js + LOG [STARTED] prettier --list-different + LOG [SUCCESS] prettier --list-different + LOG [SUCCESS] Running tasks for *.js + LOG [SUCCESS] Running tasks... + LOG [STARTED] Applying modifications... + LOG [SUCCESS] Applying modifications... + LOG [STARTED] Restoring unstaged changes to partially staged files... + LOG [SUCCESS] Restoring unstaged changes to partially staged files... + LOG [STARTED] Cleaning up... + LOG [SUCCESS] Cleaning up..." `) // Nothing is wrong, so a new commit is created and file is pretty @@ -737,18 +737,18 @@ describe('lint-staged', () => { expect(console.printHistory()).toMatchInlineSnapshot(` " - INFO ❯ Preparing... - LOG ✔ Preparing... - INFO ❯ Running tasks... - INFO ❯ Running tasks for *.js - INFO ❯ [Function] git ... - LOG ✔ [Function] git ... - LOG ✔ Running tasks for *.js - LOG ✔ Running tasks... - INFO ❯ Applying modifications... - LOG ✔ Applying modifications... - INFO ❯ Cleaning up... - ERROR ✖ lint-staged automatic backup is missing!" + LOG [STARTED] Preparing... + LOG [SUCCESS] Preparing... + LOG [STARTED] Running tasks... + LOG [STARTED] Running tasks for *.js + LOG [STARTED] [Function] git ... + LOG [SUCCESS] [Function] git ... + LOG [SUCCESS] Running tasks for *.js + LOG [SUCCESS] Running tasks... + LOG [STARTED] Applying modifications... + LOG [SUCCESS] Applying modifications... + LOG [STARTED] Cleaning up... + ERROR [FAILED] lint-staged automatic backup is missing!" `) }) @@ -775,22 +775,22 @@ describe('lint-staged', () => { " WARN ‼ Some of your tasks use \`git add\` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index. - INFO ❯ Preparing... - LOG ✔ Preparing... - INFO ❯ Running tasks... - INFO ❯ Running tasks for *.js - INFO ❯ prettier --write - LOG ✔ prettier --write - INFO ❯ git add - LOG ✔ git add - LOG ✔ Running tasks for *.js - LOG ✔ Running tasks... - INFO ❯ Applying modifications... - ERROR ✖ Prevented an empty git commit! - INFO ❯ Reverting to original state because of errors... - LOG ✔ Reverting to original state because of errors... - INFO ❯ Cleaning up... - LOG ✔ Cleaning up... + LOG [STARTED] Preparing... + LOG [SUCCESS] Preparing... + LOG [STARTED] Running tasks... + LOG [STARTED] Running tasks for *.js + LOG [STARTED] prettier --write + LOG [SUCCESS] prettier --write + LOG [STARTED] git add + LOG [SUCCESS] git add + LOG [SUCCESS] Running tasks for *.js + LOG [SUCCESS] Running tasks... + LOG [STARTED] Applying modifications... + ERROR [FAILED] Prevented an empty git commit! + LOG [STARTED] Reverting to original state because of errors... + LOG [SUCCESS] Reverting to original state because of errors... + LOG [STARTED] Cleaning up... + LOG [SUCCESS] Cleaning up... WARN ‼ lint-staged prevented an empty git commit. Use the --allow-empty option to continue, or check your task configuration @@ -923,16 +923,16 @@ describe('lint-staged', () => { " WARN ‼ Skipping backup because \`--no-stash\` was used. - INFO ❯ Preparing... - LOG ✔ Preparing... - INFO ❯ Running tasks... - INFO ❯ Running tasks for *.js - INFO ❯ prettier --write - LOG ✔ prettier --write - LOG ✔ Running tasks for *.js - LOG ✔ Running tasks... - INFO ❯ Applying modifications... - LOG ✔ Applying modifications..." + LOG [STARTED] Preparing... + LOG [SUCCESS] Preparing... + LOG [STARTED] Running tasks... + LOG [STARTED] Running tasks for *.js + LOG [STARTED] prettier --write + LOG [SUCCESS] prettier --write + LOG [SUCCESS] Running tasks for *.js + LOG [SUCCESS] Running tasks... + LOG [STARTED] Applying modifications... + LOG [SUCCESS] Applying modifications..." `) // Nothing is wrong, so a new commit is created @@ -964,20 +964,20 @@ describe('lint-staged', () => { " WARN ‼ Skipping backup because \`--no-stash\` was used. - INFO ❯ Preparing... - LOG ✔ Preparing... - INFO ❯ Hiding unstaged changes to partially staged files... - LOG ✔ Hiding unstaged changes to partially staged files... - INFO ❯ Running tasks... - INFO ❯ Running tasks for *.js - INFO ❯ [Function] prettier ... - LOG ✔ [Function] prettier ... - LOG ✔ Running tasks for *.js - LOG ✔ Running tasks... - INFO ❯ Applying modifications... - LOG ✔ Applying modifications... - INFO ❯ Restoring unstaged changes to partially staged files... - ERROR ✖ Unstaged changes could not be restored due to a merge conflict! + LOG [STARTED] Preparing... + LOG [SUCCESS] Preparing... + LOG [STARTED] Hiding unstaged changes to partially staged files... + LOG [SUCCESS] Hiding unstaged changes to partially staged files... + LOG [STARTED] Running tasks... + LOG [STARTED] Running tasks for *.js + LOG [STARTED] [Function] prettier ... + LOG [SUCCESS] [Function] prettier ... + LOG [SUCCESS] Running tasks for *.js + LOG [SUCCESS] Running tasks... + LOG [STARTED] Applying modifications... + LOG [SUCCESS] Applying modifications... + LOG [STARTED] Restoring unstaged changes to partially staged files... + ERROR [FAILED] Unstaged changes could not be restored due to a merge conflict! ERROR × lint-staged failed due to a git error." `) @@ -1060,16 +1060,16 @@ describe('lintStaged', () => { " WARN ‼ Skipping backup because there’s no initial commit yet. - INFO ❯ Preparing... - LOG ✔ Preparing... - INFO ❯ Running tasks... - INFO ❯ Running tasks for *.js - INFO ❯ prettier --list-different - LOG ✔ prettier --list-different - LOG ✔ Running tasks for *.js - LOG ✔ Running tasks... - INFO ❯ Applying modifications... - LOG ✔ Applying modifications..." + LOG [STARTED] Preparing... + LOG [SUCCESS] Preparing... + LOG [STARTED] Running tasks... + LOG [STARTED] Running tasks for *.js + LOG [STARTED] prettier --list-different + LOG [SUCCESS] prettier --list-different + LOG [SUCCESS] Running tasks for *.js + LOG [SUCCESS] Running tasks... + LOG [STARTED] Applying modifications... + LOG [SUCCESS] Applying modifications..." `) // Nothing is wrong, so the initial commit is created diff --git a/test/runAll.spec.js b/test/runAll.spec.js index 5c9996592..5f8fbc9b5 100644 --- a/test/runAll.spec.js +++ b/test/runAll.spec.js @@ -69,18 +69,18 @@ describe('runAll', () => { await runAll({ config: { '*.js': ['echo "sample"'] } }) expect(console.printHistory()).toMatchInlineSnapshot(` " - INFO ❯ Preparing... - LOG ✔ Preparing... - INFO ❯ Running tasks... - INFO ❯ Running tasks for *.js - INFO ❯ echo \\"sample\\" - LOG ✔ echo \\"sample\\" - LOG ✔ Running tasks for *.js - LOG ✔ Running tasks... - INFO ❯ Applying modifications... - LOG ✔ Applying modifications... - INFO ❯ Cleaning up... - LOG ✔ Cleaning up..." + LOG [STARTED] Preparing... + LOG [SUCCESS] Preparing... + LOG [STARTED] Running tasks... + LOG [STARTED] Running tasks for *.js + LOG [STARTED] echo \\"sample\\" + LOG [SUCCESS] echo \\"sample\\" + LOG [SUCCESS] Running tasks for *.js + LOG [SUCCESS] Running tasks... + LOG [STARTED] Applying modifications... + LOG [SUCCESS] Applying modifications... + LOG [STARTED] Cleaning up... + LOG [SUCCESS] Cleaning up..." `) }) @@ -101,11 +101,11 @@ describe('runAll', () => { expect(console.printHistory()).toMatchInlineSnapshot(` " - INFO ❯ Preparing... - ERROR ✖ test - INFO ❯ Running tasks... - INFO ❯ Applying modifications... - INFO ❯ Cleaning up..." + LOG [STARTED] Preparing... + ERROR [FAILED] test + LOG [STARTED] Running tasks... + LOG [STARTED] Applying modifications... + LOG [STARTED] Cleaning up..." `) }) @@ -128,19 +128,19 @@ describe('runAll', () => { expect(console.printHistory()).toMatchInlineSnapshot(` " - INFO ❯ Preparing... - LOG ✔ Preparing... - INFO ❯ Running tasks... - INFO ❯ Running tasks for *.js - INFO ❯ echo \\"sample\\" - ERROR ✖ echo \\"sample\\" [1] - ERROR ✖ echo \\"sample\\" [1] - LOG ✔ Running tasks... - INFO ❯ Applying modifications... - INFO ❯ Reverting to original state because of errors... - LOG ✔ Reverting to original state because of errors... - INFO ❯ Cleaning up... - LOG ✔ Cleaning up..." + LOG [STARTED] Preparing... + LOG [SUCCESS] Preparing... + LOG [STARTED] Running tasks... + LOG [STARTED] Running tasks for *.js + LOG [STARTED] echo \\"sample\\" + ERROR [FAILED] echo \\"sample\\" [1] + ERROR [FAILED] echo \\"sample\\" [1] + LOG [SUCCESS] Running tasks... + LOG [STARTED] Applying modifications... + LOG [STARTED] Reverting to original state because of errors... + LOG [SUCCESS] Reverting to original state because of errors... + LOG [STARTED] Cleaning up... + LOG [SUCCESS] Cleaning up..." `) }) @@ -165,19 +165,19 @@ describe('runAll', () => { expect(console.printHistory()).toMatchInlineSnapshot(` " - INFO ❯ Preparing... - LOG ✔ Preparing... - INFO ❯ Running tasks... - INFO ❯ Running tasks for *.js - INFO ❯ echo \\"sample\\" - ERROR ✖ echo \\"sample\\" [SIGINT] - ERROR ✖ echo \\"sample\\" [SIGINT] - LOG ✔ Running tasks... - INFO ❯ Applying modifications... - INFO ❯ Reverting to original state because of errors... - LOG ✔ Reverting to original state because of errors... - INFO ❯ Cleaning up... - LOG ✔ Cleaning up..." + LOG [STARTED] Preparing... + LOG [SUCCESS] Preparing... + LOG [STARTED] Running tasks... + LOG [STARTED] Running tasks for *.js + LOG [STARTED] echo \\"sample\\" + ERROR [FAILED] echo \\"sample\\" [SIGINT] + ERROR [FAILED] echo \\"sample\\" [SIGINT] + LOG [SUCCESS] Running tasks... + LOG [STARTED] Applying modifications... + LOG [STARTED] Reverting to original state because of errors... + LOG [SUCCESS] Reverting to original state because of errors... + LOG [STARTED] Cleaning up... + LOG [SUCCESS] Cleaning up..." `) }) }) diff --git a/yarn.lock b/yarn.lock index 98ca91473..a8e897b7f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3993,10 +3993,10 @@ lines-and-columns@^1.1.6: resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00" integrity sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA= -listr2@1.3.7: - version "1.3.7" - resolved "https://registry.yarnpkg.com/listr2/-/listr2-1.3.7.tgz#37a9971e077f9bc9490b3462ce19f2cd25cc0d3e" - integrity sha512-yeSjMMLwW7TQqUh0a4kW9uBF6K+vtOEFYzJdhgMWS0dYFBtT3O6gT8ng9f29P8tXKN281gKyGDR1P9OLXf3FsQ== +listr2@1.3.8: + version "1.3.8" + resolved "https://registry.yarnpkg.com/listr2/-/listr2-1.3.8.tgz#30924d79de1e936d8c40af54b6465cb814a9c828" + integrity sha512-iRDRVTgSDz44tBeBBg/35TQz4W+EZBWsDUq7hPpqeUHm7yLPNll0rkwW3lIX9cPAK7l+x95mGWLpxjqxftNfZA== dependencies: "@samverschueren/stream-to-observable" "^0.3.0" chalk "^3.0.0"