Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use stdout and stderr to display text #5935

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions packages/cspell-json-reporter/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,194 @@ exports[`getReporter > saves additional data 1`] = `
}"
`;

exports[`getReporter > saves json to console { outFile: 'stderr' } 1`] = `
"{
"issues": [
{
"text": "fulll",
"offset": 13,
"line": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
},
"row": 1,
"col": 14,
"uri": "text.txt",
"context": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
}
}
],
"info": [
{
"message": "some warnings",
"msgType": "Warning"
}
],
"debug": [],
"error": [
{
"message": "something went wrong",
"error": {}
}
],
"progress": [],
"result": {
"files": 1,
"filesWithIssues": [
"text.txt"
],
"issues": 2,
"errors": 1,
"cachedFiles": 0
}
}"
`;

exports[`getReporter > saves json to console { outFile: 'stderr' } 2`] = `""`;

exports[`getReporter > saves json to console { outFile: 'stdout' } 1`] = `""`;

exports[`getReporter > saves json to console { outFile: 'stdout' } 2`] = `
"{
"issues": [
{
"text": "fulll",
"offset": 13,
"line": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
},
"row": 1,
"col": 14,
"uri": "text.txt",
"context": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
}
}
],
"info": [
{
"message": "some warnings",
"msgType": "Warning"
}
],
"debug": [],
"error": [
{
"message": "something went wrong",
"error": {}
}
],
"progress": [],
"result": {
"files": 1,
"filesWithIssues": [
"text.txt"
],
"issues": 2,
"errors": 1,
"cachedFiles": 0
}
}"
`;

exports[`getReporter > saves json to console { outFile: undefined } 1`] = `""`;

exports[`getReporter > saves json to console { outFile: undefined } 2`] = `
"{
"issues": [
{
"text": "fulll",
"offset": 13,
"line": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
},
"row": 1,
"col": 14,
"uri": "text.txt",
"context": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
}
}
],
"info": [
{
"message": "some warnings",
"msgType": "Warning"
}
],
"debug": [],
"error": [
{
"message": "something went wrong",
"error": {}
}
],
"progress": [],
"result": {
"files": 1,
"filesWithIssues": [
"text.txt"
],
"issues": 2,
"errors": 1,
"cachedFiles": 0
}
}"
`;

exports[`getReporter > saves json to console undefined 1`] = `""`;

exports[`getReporter > saves json to console undefined 2`] = `
"{
"issues": [
{
"text": "fulll",
"offset": 13,
"line": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
},
"row": 1,
"col": 14,
"uri": "text.txt",
"context": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
}
}
],
"info": [
{
"message": "some warnings",
"msgType": "Warning"
}
],
"debug": [],
"error": [
{
"message": "something went wrong",
"error": {}
}
],
"progress": [],
"result": {
"files": 1,
"filesWithIssues": [
"text.txt"
],
"issues": 2,
"errors": 1,
"cachedFiles": 0
}
}"
`;

exports[`getReporter > saves json to file 1`] = `
"{
"issues": [
Expand Down
19 changes: 19 additions & 0 deletions packages/cspell-json-reporter/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ describe('getReporter', () => {
expect(joinCalls(stdout.mock.calls)).toMatchSnapshot();
});

test.each`
settings
${undefined}
${{ outFile: undefined }}
${{ outFile: 'stdout' }}
${{ outFile: 'stderr' }}
`('saves json to console $settings', async ({ settings }) => {
const console = {
log: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
};

const reporter = getReporter(settings, { console });
await runReporter(reporter);
expect(joinCalls(console.error.mock.calls)).toMatchSnapshot();
expect(joinCalls(console.log.mock.calls)).toMatchSnapshot();
});

test('saves additional data', async () => {
const reporter = getReporter({ outFile: 'out.json', verbose: true, debug: true, progress: true });
await runReporter(reporter);
Expand Down
11 changes: 10 additions & 1 deletion packages/cspell-json-reporter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ function mkdirp(p: string) {

const noopReporter = () => undefined;

type ReporterConsole = Pick<Console, 'log' | 'warn' | 'error'>;

export interface CSpellJSONReporterConfiguration extends ReporterConfiguration {
console?: ReporterConsole;
}

const STDOUT = 'stdout';
const STDERR = 'stderr';

type Data = Omit<CSpellJSONReporterOutput, 'result'>;

const _console = console;

export function getReporter(
settings: unknown | CSpellJSONReporterSettings,
cliOptions?: ReporterConfiguration,
cliOptions?: CSpellJSONReporterConfiguration,
): Required<CSpellReporter> {
const useSettings = normalizeSettings(settings);
const reportData: Data = { issues: [], info: [], debug: [], error: [], progress: [] };
const console = cliOptions?.console ?? _console;
return {
issue: (issue) => {
reportData.issues.push(issue);
Expand Down
5 changes: 4 additions & 1 deletion packages/cspell/bin.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/env node
import { format } from 'node:util';

import { CommanderError, program } from 'commander';

import * as app from './dist/esm/app.mjs';

app.run(program, process.argv).catch((e) => {
if (!(e instanceof CommanderError) && !(e instanceof app.CheckFailed)) {
console.log(e);
const msg = format(e) + '\n';
process.stdout.write(msg);
// It is possible an explicit exit code was set, use it if it was.
process.exitCode = process.exitCode || 1;
}
Expand Down
Loading