Skip to content

Commit

Permalink
fix(stdout): switch diagnostic output to stderr (#42)
Browse files Browse the repository at this point in the history
* Switch diagnostic output to stderr

Separate the content output and the diagnostic output.
Allows users to redirect the output separately from the diagnostic output.

* test: update tests to account for stderr

* chore(deps): add missing nps dep

Closes #36
  • Loading branch information
denis-sokolov authored and Kent C. Dodds committed Apr 14, 2017
1 parent 7490c60 commit 81f6542
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 38 deletions.
45 changes: 26 additions & 19 deletions cli-test/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,29 @@ test(`prettier-eslint ${writeCommand}`, async () => {
// we have to recreate and delete the files every time
const example1Path = path.resolve(__dirname, '../fixtures/example1.js')
const example2Path = path.resolve(__dirname, '../fixtures/example2.js')
const example1 = `const { example1 } = baz.bar`
const example2 = `function example2(thing){return thing;};;;;;;;;;`
await Promise.all([
pWriteFile(example1Path, example1),
pWriteFile(example2Path, example2),
])
const stdout = await runPrettierESLintCLI(writeCommand)
expect(stdout).toMatchSnapshot(`stdout: prettier-eslint ${writeCommand}`)
const [example1Result, example2Result] = await Promise.all([
pReadFile(example1Path, 'utf8'),
pReadFile(example2Path, 'utf8'),
])
expect({example1Result, example2Result}).toMatchSnapshot(
`file contents: prettier-eslint ${writeCommand}`,
)
await Promise.all([pUnlink(example1Path), pUnlink(example2Path)])
try {
const example1 = `const { example1 } = baz.bar`
const example2 = `function example2(thing){return thing;};;;;;;;;;`
await Promise.all([
pWriteFile(example1Path, example1),
pWriteFile(example2Path, example2),
])
const stdout = await runPrettierESLintCLI(writeCommand)
expect(stdout).toMatchSnapshot(`stdout: prettier-eslint ${writeCommand}`)
const [example1Result, example2Result] = await Promise.all([
pReadFile(example1Path, 'utf8'),
pReadFile(example2Path, 'utf8'),
])
expect({example1Result, example2Result}).toMatchSnapshot(
`file contents: prettier-eslint ${writeCommand}`,
)
} finally {
try {
await Promise.all([pUnlink(example1Path), pUnlink(example2Path)])
} catch (error) {
// ignore error
}
}
})

function testOutput(command) {
Expand Down Expand Up @@ -135,10 +142,10 @@ function runPrettierESLintCLI(args = '', stdin = '') {
})

child.on('close', () => {
if (stderr) {
reject(relativeizePath(stderr))
if (!stderr || stderr.includes('success')) {
resolve(relativeizePath(stdout || stderr))
} else {
resolve(relativeizePath(stdout))
reject(relativeizePath(stderr))
}
})
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"eslint-config-kentcdodds": "^12.2.1",
"husky": "^0.13.3",
"jest-cli": "^19.0.2",
"nps": "^5.0.5",
"nps-utils": "^1.2.0",
"opt-cli": "^1.5.1",
"pify": "^2.3.0",
Expand Down
6 changes: 3 additions & 3 deletions src/format-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function formatFilesFromGlobs(

function onComplete() {
if (successes.length) {
console.log(
console.error(
messages.success({
success: chalk.green('success'),
count: successes.length,
Expand All @@ -136,7 +136,7 @@ function formatFilesFromGlobs(
}
if (failures.length) {
process.exitCode = 1
console.log(
console.error(
messages.failure({
failure: chalk.red('failure'),
count: failures.length,
Expand All @@ -145,7 +145,7 @@ function formatFilesFromGlobs(
)
}
if (unchanged.length) {
console.log(
console.error(
messages.unchanged({
unchanged: chalk.gray('unchanged'),
count: unchanged.length,
Expand Down
24 changes: 12 additions & 12 deletions src/format-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ test('sanity test', async () => {
expect(fsMock.readFile).toHaveBeenCalledTimes(6)
expect(formatMock).toHaveBeenCalledTimes(6)
expect(fsMock.writeFile).toHaveBeenCalledTimes(0)
expect(console.log).toHaveBeenCalledTimes(7)
expect(console.log).toHaveBeenCalledTimes(6)
expect(console.error).toHaveBeenCalledTimes(1)
const mockOutput = expect.stringMatching(/MOCK_OUTPUT.*index.js/)
const successOutput = expect.stringMatching(/success.*6.*files/)
expect(console.log).toHaveBeenCalledWith(mockOutput)
expect(console.log).toHaveBeenCalledWith(successOutput)
expect(console.error).toHaveBeenCalledWith(successOutput)
})

test('glob call inclues an ignore of node_modules', async () => {
Expand Down Expand Up @@ -80,11 +81,11 @@ test('handles file errors gracefully', async () => {
const globs = ['files-with-syntax-errors/*.js', 'src/**/1*.js']
await formatFiles({_: globs, write: true})
expect(fsMock.writeFile).toHaveBeenCalledTimes(4)
expect(console.log).toHaveBeenCalledTimes(2)
expect(console.error).toHaveBeenCalledTimes(4)
const successOutput = expect.stringMatching(/success.*4.*files/)
const failureOutput = expect.stringMatching(/failure.*2.*files/)
expect(console.log).toHaveBeenCalledWith(successOutput)
expect(console.log).toHaveBeenCalledWith(failureOutput)
expect(console.error).toHaveBeenCalledWith(successOutput)
expect(console.error).toHaveBeenCalledWith(failureOutput)
})

test('does not print success if there were no successful files', async () => {
Expand All @@ -106,15 +107,14 @@ test('logs errors to the console if something goes wrong', async () => {
const globs = ['eslint-config-error/*.js', 'src/**/2*.js']
await formatFiles({_: globs, write: true})
expect(fsMock.writeFile).toHaveBeenCalledTimes(4)
expect(console.log).toHaveBeenCalledTimes(2)
expect(console.error).toHaveBeenCalledTimes(4)
const successOutput = expect.stringMatching(/success.*4.*files/)
const failureOutput = expect.stringMatching(/failure.*2.*files/)
expect(console.log).toHaveBeenCalledWith(successOutput)
expect(console.log).toHaveBeenCalledWith(failureOutput)
expect(console.error).toHaveBeenCalledWith(successOutput)
expect(console.error).toHaveBeenCalledWith(failureOutput)
const errorPrefix = expect.stringMatching(/prettier-eslint-cli.*ERROR/)
const cliError = expect.stringContaining('eslint-config-error')
const errorOutput = expect.stringContaining('Some weird eslint config error')
expect(console.error).toHaveBeenCalledTimes(2)
expect(console.error).toHaveBeenCalledWith(
errorPrefix,
cliError,
Expand All @@ -141,7 +141,7 @@ test('wont save file if contents did not change', async () => {
expect(fsMock.readFile).toHaveBeenCalledTimes(3)
expect(fsMock.writeFile).toHaveBeenCalledTimes(0)
const unchangedOutput = expect.stringMatching(/3.*?files.*?unchanged/)
expect(console.log).toHaveBeenCalledWith(unchangedOutput)
expect(console.error).toHaveBeenCalledWith(unchangedOutput)
})

test('allows you to specify an ignore glob', async () => {
Expand Down Expand Up @@ -171,7 +171,7 @@ test('wont modify a file if it is eslint ignored', async () => {
expect.any(Function),
)
const ignoredOutput = expect.stringMatching(/success.*1.*file/)
expect(console.log).toHaveBeenCalledWith(ignoredOutput)
expect(console.error).toHaveBeenCalledWith(ignoredOutput)
})

test('will modify a file if it is eslint ignored with noIgnore', async () => {
Expand All @@ -183,7 +183,7 @@ test('will modify a file if it is eslint ignored with noIgnore', async () => {
expect(fsMock.readFile).toHaveBeenCalledTimes(4)
expect(fsMock.writeFile).toHaveBeenCalledTimes(4)
const ignoredOutput = expect.stringMatching(/success.*4.*files/)
expect(console.log).toHaveBeenCalledWith(ignoredOutput)
expect(console.error).toHaveBeenCalledWith(ignoredOutput)
})

test('will not blow up if an .eslintignore cannot be found', async () => {
Expand Down
61 changes: 57 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,7 @@ event-emitter@~0.3.4:
d "~0.1.1"
es5-ext "~0.10.7"

event-stream@^3.3.0:
event-stream@^3.3.0, event-stream@~3.3.0:
version "3.3.4"
resolved "https://registry.yarnpkg.com/event-stream/-/event-stream-3.3.4.tgz#4ab4c9a0f5a54db9338b4c34d86bfce8f4b35571"
dependencies:
Expand Down Expand Up @@ -2894,6 +2894,10 @@ is-number@^2.0.2, is-number@^2.1.0:
dependencies:
kind-of "^3.0.2"

is-object@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/is-object/-/is-object-1.0.1.tgz#8952688c5ec2ffd6b03ecc85e769e02903083470"

is-path-cwd@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-path-cwd/-/is-path-cwd-1.0.0.tgz#d225ec23132e89edd38fda767472e62e65f1106d"
Expand Down Expand Up @@ -3549,7 +3553,7 @@ lodash@^3.6.0:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

lodash@^4.0.0, lodash@^4.11.2, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.2.0, lodash@^4.3.0, lodash@^4.5.1:
lodash@^4.0.0, lodash@^4.11.2, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0, lodash@^4.5.1:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

Expand Down Expand Up @@ -3604,7 +3608,7 @@ [email protected]:
dependencies:
tmpl "1.0.x"

[email protected]:
[email protected], manage-path@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/manage-path/-/manage-path-2.0.0.tgz#f4cf8457b926eeee2a83b173501414bc76eb9597"

Expand Down Expand Up @@ -3962,6 +3966,23 @@ nps-utils@^1.2.0:
opn-cli "^3.1.0"
rimraf "^2.6.1"

nps@^5.0.5:
version "5.0.5"
resolved "https://registry.yarnpkg.com/nps/-/nps-5.0.5.tgz#52fee585d650c8aa093936e78d78d036899f2ea8"
dependencies:
arrify "^1.0.1"
chalk "^1.1.3"
common-tags "^1.4.0"
find-up "^2.1.0"
js-yaml "^3.7.0"
lodash "^4.17.4"
manage-path "^2.0.0"
prefix-matches "^0.0.9"
readline-sync "^1.4.6"
spawn-command-with-kill "^1.0.0"
type-detect "^4.0.0"
yargs "^6.6.0"

number-is-nan@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d"
Expand Down Expand Up @@ -4240,6 +4261,13 @@ pluralize@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-1.2.1.tgz#d1a21483fd22bb41e58a12fa3421823140897c45"

prefix-matches@^0.0.9:
version "0.0.9"
resolved "https://registry.yarnpkg.com/prefix-matches/-/prefix-matches-0.0.9.tgz#5887019e2b9566b38917f7529b6ce0fc2c72d68c"
dependencies:
is-object "^1.0.1"
starts-with "^1.0.2"

prelude-ls@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
Expand Down Expand Up @@ -4307,6 +4335,12 @@ prr@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a"

ps-tree@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/ps-tree/-/ps-tree-1.1.0.tgz#b421b24140d6203f1ed3c76996b4427b08e8c014"
dependencies:
event-stream "~3.3.0"

pseudomap@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"
Expand Down Expand Up @@ -4424,6 +4458,10 @@ readdirp@^2.0.0:
readable-stream "^2.0.2"
set-immediate-shim "^1.0.1"

readline-sync@^1.4.6:
version "1.4.7"
resolved "https://registry.yarnpkg.com/readline-sync/-/readline-sync-1.4.7.tgz#001bfdd4c06110c3c084c63bf7c6a56022213f30"

readline2@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/readline2/-/readline2-1.0.1.tgz#41059608ffc154757b715d9989d199ffbf372e35"
Expand Down Expand Up @@ -4834,6 +4872,13 @@ source-map@~0.2.0:
dependencies:
amdefine ">=0.0.4"

spawn-command-with-kill@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/spawn-command-with-kill/-/spawn-command-with-kill-1.0.0.tgz#803ad79f2f56e44dd926183768aac2faec7d0ce6"
dependencies:
ps-tree "^1.1.0"
spawn-command "^0.0.2-1"

[email protected]:
version "0.0.2-1"
resolved "https://registry.yarnpkg.com/spawn-command/-/spawn-command-0.0.2-1.tgz#62f5e9466981c1b796dc5929937e11c9c6921bd0"
Expand Down Expand Up @@ -4888,6 +4933,10 @@ sshpk@^1.7.0:
jsbn "~0.1.0"
tweetnacl "~0.14.0"

starts-with@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/starts-with/-/starts-with-1.0.2.tgz#16793a729d89d4cf3d4fb2eda2f908ae357f196f"

stealthy-require@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.0.0.tgz#1a8ed8fc19a8b56268f76f5a1a3e3832b0c26200"
Expand Down Expand Up @@ -5178,6 +5227,10 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

type-detect@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.0.tgz#62053883542a321f2f7b25746dc696478b18ff6b"

typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
Expand Down Expand Up @@ -5475,7 +5528,7 @@ yargs@^4.7.0:
y18n "^3.2.1"
yargs-parser "^2.4.1"

yargs@^6.0.0, yargs@^6.3.0:
yargs@^6.0.0, yargs@^6.3.0, yargs@^6.6.0:
version "6.6.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-6.6.0.tgz#782ec21ef403345f830a808ca3d513af56065208"
dependencies:
Expand Down

0 comments on commit 81f6542

Please sign in to comment.