Skip to content

Commit

Permalink
Output max warnings message to STDERR
Browse files Browse the repository at this point in the history
I think stylelint should output any error or warning messages to STDERR instead of STDOUT.
Because users should expect that *piping* is available.

For example, here is an example of using the `jq` command:

```console
$ stylelint --formatter=json --max-warnings=0 a.css | jq .

Max warnings exceeded: 1 found. 0 allowed

[
  {
    "source": "/tmp/a.css",
    "deprecations": [],
    "invalidOptionWarnings": [],
    "parseErrors": [],
    "errored": false,
    "warnings": [
      {
        "line": 1,
        "column": 2,
        "rule": "block-no-empty",
        "severity": "warning",
        "text": "Unexpected empty block (block-no-empty)"
      }
    ]
  }
]
```
  • Loading branch information
ybiquitous committed Apr 30, 2021
1 parent d034cc2 commit a99da80
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
27 changes: 27 additions & 0 deletions lib/__tests__/cli.test.js
Expand Up @@ -3,6 +3,7 @@
'use strict';

const path = require('path');
const stripAnsi = require('strip-ansi');

const cli = require('../cli');
const pkg = require('../../package.json');
Expand Down Expand Up @@ -296,4 +297,30 @@ describe('CLI', () => {
expect.stringContaining('Cannot find module'),
);
});

it('outputs a valid JSON even if max warnings exceeded', async () => {
await cli([
'--formatter=json',
'--max-warnings=0',
'--config',
replaceBackslashes(path.join(fixturesPath, 'default-severity-warning.json')),
replaceBackslashes(path.join(fixturesPath, 'empty-block.css')),
]);

expect(process.exitCode).toEqual(2);

expect(process.stdout.write).toHaveBeenCalledTimes(1);
const output = JSON.parse(process.stdout.write.mock.calls[0][0]);

expect(output).toBeInstanceOf(Array);
expect(output[0]).toHaveProperty(
'source',
replaceBackslashes(path.join(fixturesPath, 'empty-block.css')),
);

expect(process.stderr.write).toHaveBeenCalledTimes(1);
expect(stripAnsi(process.stderr.write.mock.calls[0][0])).toContain(
'Max warnings exceeded: 1 found. 0 allowed',
);
});
});
6 changes: 6 additions & 0 deletions lib/__tests__/fixtures/default-severity-warning.json
@@ -0,0 +1,6 @@
{
"defaultSeverity": "warning",
"rules": {
"block-no-empty": true
}
}
4 changes: 2 additions & 2 deletions lib/cli.js
Expand Up @@ -509,8 +509,8 @@ module.exports = (argv) => {
} else if (maxWarnings !== undefined && linted.maxWarningsExceeded) {
const foundWarnings = linted.maxWarningsExceeded.foundWarnings;

process.stdout.write(
`${chalk.red(`Max warnings exceeded: `)}${foundWarnings} found. ${chalk.dim(
process.stderr.write(
`${EOL}${chalk.red(`Max warnings exceeded: `)}${foundWarnings} found. ${chalk.dim(
`${maxWarnings} allowed${EOL}${EOL}`,
)}`,
);
Expand Down

0 comments on commit a99da80

Please sign in to comment.