Skip to content

Commit

Permalink
fix: follow verbosity rules for JSON and CSV (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Dec 24, 2020
1 parent cf29469 commit 9eb5590
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,25 @@ async function main() {
opts.linksToSkip = flags.skip.split(' ').filter(x => !!x);
}
const result = await checker.check(opts);
const filteredResults = result.links.filter(link => {
switch (link.state) {
case LinkState.OK:
return verbosity <= LogLevel.WARNING;
case LinkState.BROKEN:
if (verbosity > LogLevel.DEBUG) {
link.failureDetails = undefined;
}
return verbosity <= LogLevel.ERROR;
case LinkState.SKIPPED:
return verbosity <= LogLevel.INFO;
}
});
if (format === Format.JSON) {
result.links = filteredResults;
console.log(JSON.stringify(result, null, 2));
return;
} else if (format === Format.CSV) {
result.links = filteredResults;
const csv = await toCSV(result.links);
console.log(csv);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Logger {
}

error(message?: string) {
if (this.level <= LogLevel.ERROR) {
if (this.level <= LogLevel.ERROR && this.format === Format.TEXT) {
console.error(message);
}
}
Expand Down
21 changes: 18 additions & 3 deletions test/zcli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {describe, it} from 'mocha';
import * as execa from 'execa';
import {assert} from 'chai';
import {LinkResult, LinkState} from '../src/index';

describe('cli', () => {
before(async () => {
Expand Down Expand Up @@ -53,7 +54,6 @@ describe('cli', () => {
it('should provide CSV if asked nicely', async () => {
const res = await execa('npx', [
'linkinator',
'--markdown',
'--format',
'csv',
'test/fixtures/markdown/README.md',
Expand All @@ -64,12 +64,12 @@ describe('cli', () => {
it('should provide JSON if asked nicely', async () => {
const res = await execa('npx', [
'linkinator',
'--markdown',
'--format',
'json',
'test/fixtures/markdown/README.md',
]);
assert.match(res.stdout, /{/);
const output = JSON.parse(res.stdout);
assert.ok(output.links);
});

it('should not show links if --silent', async () => {
Expand All @@ -81,6 +81,21 @@ describe('cli', () => {
assert.notMatch(res.stdout, /\[/);
});

it('should not show 200 links if verbosity is ERROR with JSON', async () => {
const res = await execa('npx', [
'linkinator',
'--verbosity',
'ERROR',
'--format',
'JSON',
'test/fixtures/markdown/README.md',
]);
const links = JSON.parse(res.stdout).links as LinkResult[];
for (const link of links) {
assert.strictEqual(link.state, LinkState.BROKEN);
}
});

it('should accept a server-root', async () => {
const res = await execa('npx', [
'linkinator',
Expand Down

0 comments on commit 9eb5590

Please sign in to comment.