Skip to content

Commit

Permalink
feat(builder): add maxWarnings option (#351)
Browse files Browse the repository at this point in the history
Co-authored-by: James Henry <james@henry.sc>
  • Loading branch information
Lehoczky and JamesHenry committed Mar 8, 2021
1 parent 75c406f commit 5dc2dc3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async function run(
const projectName = context.target?.project || '<???>';
const printInfo = options.format && !options.silent;
const reportOnlyErrors = options.quiet;
const maxWarnings = options.maxWarnings;

context.reportStatus(`Linting ${JSON.stringify(projectName)}...`);
if (printInfo) {
Expand Down Expand Up @@ -94,8 +95,15 @@ async function run(
context.logger.info('All files pass linting.\n');
}

const tooManyWarnings = maxWarnings >= 0 && totalWarnings > maxWarnings;
if (tooManyWarnings && printInfo) {
context.logger.error(
`Found ${totalWarnings} warnings, which exceeds your configured limit (${options.maxWarnings}). Either increase your maxWarnings limit or fix some of the lint warnings.`,
);
}

return {
success: options.force || totalErrors === 0,
success: options.force || (totalErrors === 0 && !tooManyWarnings),
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/builder/src/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface Schema extends JsonObject {
lintFilePatterns: string[];
force: boolean;
quiet: boolean;
maxWarnings: number;
silent: boolean;
fix: boolean;
cache: boolean;
Expand Down
5 changes: 5 additions & 0 deletions packages/builder/src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
"description": "Report errors only.",
"default": false
},
"maxWarnings": {
"type": "integer",
"description": "Number of warnings to trigger nonzero exit code.",
"default": -1
},
"silent": {
"type": "boolean",
"description": "Hide output text.",
Expand Down
65 changes: 65 additions & 0 deletions packages/builder/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function createValidRunBuilderOptions(
format: 'stylish',
force: false,
quiet: false,
maxWarnings: -1,
silent: false,
ignorePath: null,
...additionalOptions,
Expand Down Expand Up @@ -148,6 +149,7 @@ describe('Linter Builder', () => {
format: 'stylish',
force: false,
silent: false,
maxWarnings: -1,
ignorePath: null,
}),
);
Expand All @@ -162,6 +164,7 @@ describe('Linter Builder', () => {
format: 'stylish',
force: false,
silent: false,
maxWarnings: -1,
ignorePath: null,
});
});
Expand Down Expand Up @@ -331,6 +334,7 @@ describe('Linter Builder', () => {
eslintConfig: './.eslintrc',
lintFilePatterns: ['includedFile1'],
format: 'json',
maxWarnings: 1,
silent: true,
}),
);
Expand Down Expand Up @@ -511,4 +515,65 @@ describe('Linter Builder', () => {
);
expect(output.success).toBeFalsy();
});

it('should be a failure if the the amount of warnings exceeds the maxWarnings option', async () => {
mockReports = [
{
errorCount: 0,
warningCount: 4,
results: [],
messages: [],
usedDeprecatedRules: [],
},
{
errorCount: 0,
warningCount: 6,
results: [],
messages: [],
usedDeprecatedRules: [],
},
];
setupMocks();
const output = await runBuilder(
createValidRunBuilderOptions({
eslintConfig: './.eslintrc',
lintFilePatterns: ['includedFile1'],
format: 'json',
maxWarnings: 5,
}),
);
expect(output.success).toBeFalsy();
});

it('should log too many warnings even if quiet flag was passed', async () => {
mockReports = [
{
errorCount: 0,
warningCount: 1,
results: [],
messages: [],
usedDeprecatedRules: [],
},
];
setupMocks();
await runBuilder(
createValidRunBuilderOptions({
eslintConfig: './.eslintrc',
lintFilePatterns: ['includedFile1'],
format: 'json',
quiet: true,
maxWarnings: 0,
}),
);
const flattenedCalls = loggerSpy.mock.calls.reduce((logs, call) => {
return [...logs, call[0]];
}, []);
expect(flattenedCalls).toContainEqual(
expect.objectContaining({
message: expect.stringContaining(
'Found 1 warnings, which exceeds your configured limit (0).',
),
}),
);
});
});

0 comments on commit 5dc2dc3

Please sign in to comment.