fix(formatters) fix invalid json when no results found (#2912)#2913
fix(formatters) fix invalid json when no results found (#2912)#2913Pentusha wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes spectral lint --format json producing invalid JSON when there are no findings by preventing the “No results…” informational message from being appended to JSON output on stdout.
Changes:
- Suppress the “No results…” stdout informational message when JSON output format is selected.
- Add CLI tests covering the no-results behavior for
jsonvsstylishformats.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/cli/src/commands/lint.ts | Prevents the “No results…” message from being written to stdout when JSON formatting is requested. |
| packages/cli/src/commands/tests/lint.test.ts | Adds regression tests ensuring stdout messaging differs between json and stylish formats when there are no results. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (linterResult.results.length > 0) { | ||
| process.exit(severeEnoughToFail(linterResult.results, failSeverity) ? 1 : 0); | ||
| } else if (config.quiet !== true) { | ||
| } else if (config.quiet !== true && !format.includes(OutputFormat.JSON)) { |
| it('does not write informational message to stdout when format is json and no results found', async () => { | ||
| (lint as jest.Mock).mockReset(); | ||
| (lint as jest.Mock).mockResolvedValueOnce({ results: [], resolvedRuleset: {} }); | ||
| (formatOutput as jest.Mock).mockReturnValueOnce('[]'); | ||
| (writeOutput as jest.Mock).mockResolvedValueOnce(undefined); | ||
|
|
||
| await run(`lint -f json ./__fixtures__/empty-oas2-document.json`); | ||
| expect(process.stdout.write).not.toHaveBeenCalledWith(expect.stringContaining('No results')); | ||
| }); | ||
|
|
||
| it('writes informational message to stdout when format is stylish and no results found', async () => { | ||
| (lint as jest.Mock).mockReset(); | ||
| (lint as jest.Mock).mockResolvedValueOnce({ results: [], resolvedRuleset: {} }); | ||
| (formatOutput as jest.Mock).mockReturnValueOnce(''); | ||
| (writeOutput as jest.Mock).mockResolvedValueOnce(undefined); | ||
|
|
slegarraga
left a comment
There was a problem hiding this comment.
Clean fix — JSON stdout should stay machine-parseable. The stylish-vs-json test split in lint.test.ts covers the regression well. LGTM from my side.
slegarraga
left a comment
There was a problem hiding this comment.
JSON stdout fix looks correct; tests for stylish vs json paths are the right guard. Thanks.
slegarraga
left a comment
There was a problem hiding this comment.
LGTM — JSON output must be machine-parseable; skipping the human "No results" line when -f json is the right fix.
Tests cover both json (no stdout noise) and stylish (message preserved). Small, focused change for #2912.
Fixes #2912.
Checklist
Does this PR introduce a breaking change?