From 13ab5b0caf3dc8916ce4de80203e54104e144a6f Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Tue, 2 Jul 2024 11:04:08 -0400 Subject: [PATCH] fix: JSON stringify failure details for CSV (#613) --- src/cli.ts | 7 ++++--- test/fixtures/localbroke/README.md | 3 +++ test/test.cli.ts | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/localbroke/README.md diff --git a/src/cli.ts b/src/cli.ts index ea6f839..cc69ea7 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -174,10 +174,11 @@ async function main() { if (format === Format.CSV) { const showIt = shouldShowResult(link, verbosity); if (showIt) { + const failureDetails = link.failureDetails + ? JSON.stringify(link.failureDetails, null, 2) + : ''; console.log( - `"${link.url}",${link.status},${link.state},"${link.parent || ''}","${ - link.failureDetails?.toString() || '' - }"`, + `"${link.url}",${link.status},${link.state},"${link.parent || ''}","${failureDetails}"`, ); } } diff --git a/test/fixtures/localbroke/README.md b/test/fixtures/localbroke/README.md new file mode 100644 index 0000000..e3590a5 --- /dev/null +++ b/test/fixtures/localbroke/README.md @@ -0,0 +1,3 @@ +# Locally broken + +This is a [link](/test) to a page that doesn't exist. diff --git a/test/test.cli.ts b/test/test.cli.ts index 99b43ce..cc1e05a 100644 --- a/test/test.cli.ts +++ b/test/test.cli.ts @@ -99,6 +99,20 @@ describe('cli', function () { assert.match(response.stdout, /README.md",200,OK,/); }); + it('should serialize errors with CSV and verbose output', async () => { + const response = await execa(node, [ + linkinator, + '--format', + 'csv', + '--verbosity', + 'DEBUG', + 'test/fixtures/localbroke/README.md', + ]); + // This is a very lazy way of trying to recognize the JSON we expect + // in CSV without using a CSV parser. + assert.match(response.stdout, /statusText": "Not Found"/); + }); + it('should provide JSON if asked nicely', async () => { const response = await execa(node, [ linkinator,