Skip to content

Commit

Permalink
fix: use test renderer during tests and when TERM=dumb
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Apr 25, 2020
1 parent efffa22 commit 16848d8
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 140 deletions.
12 changes: 12 additions & 0 deletions lib/getRenderer.js
Original file line number Diff line number Diff line change
@@ -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
9 changes: 1 addition & 8 deletions lib/runAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 0 additions & 4 deletions test/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
`;
Expand Down
23 changes: 23 additions & 0 deletions test/getRenderer.spec.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
2 changes: 1 addition & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down
2 changes: 1 addition & 1 deletion test/index2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('lintStaged', () => {
},
"dateFormat": false,
"exitOnError": false,
"renderer": "verbose",
"renderer": "test",
}
`)
})
Expand Down
156 changes: 78 additions & 78 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!"
`)
})

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."
`)
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 16848d8

Please sign in to comment.