From c3f24ff83ed19e9f5fb6b2fc38020a9d8af856fd Mon Sep 17 00:00:00 2001 From: Dino Chiesa Date: Tue, 27 Aug 2024 17:11:38 -0700 Subject: [PATCH] fix: cleaner error when formatter is missing. new test for same. --- lib/package/bundleLinter.js | 8 +++-- test/specs/testFormatters.js | 63 +++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/lib/package/bundleLinter.js b/lib/package/bundleLinter.js index 733deac5..a5dc4a6c 100644 --- a/lib/package/bundleLinter.js +++ b/lib/package/bundleLinter.js @@ -1,5 +1,5 @@ /* - Copyright 2019-2022 Google LLC + Copyright 2019-2022,2024 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -170,8 +170,10 @@ var getFormatter = function (format) { try { return require(formatterPath); } catch (ex) { - ex.message = `There was a problem loading formatter: ${formatterPath}\nError: ${ex.message}`; - throw ex; + throw new Error( + `There was a problem loading formatter: ${formatterPath}`, + { cause: ex } + ); } } else { return null; diff --git a/test/specs/testFormatters.js b/test/specs/testFormatters.js index 6b1c1a1a..3249cc95 100644 --- a/test/specs/testFormatters.js +++ b/test/specs/testFormatters.js @@ -1,5 +1,5 @@ /* - Copyright 2019-2021 Google LLC + Copyright 2019-2021,2024 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -17,10 +17,8 @@ /* jslint esversion:9 */ const assert = require("assert"), - debug = require("debug")("apigeelint:Formatters"), - //Bundle = require("../../lib/package/Bundle.js"), - //util = require("util"), - bl = require("../../lib/package/bundleLinter.js"); + debug = require("debug")("apigeelint:Formatters"), + bl = require("../../lib/package/bundleLinter.js"); const formatters = [ "checkstyle.js", @@ -38,18 +36,53 @@ const formatters = [ "pdf.js" ]; -describe("Formatters", function() { - configuration.source.path = './test/fixtures/resources/sample-proxy-with-issues/response-shaping/apiproxy'; - configuration.output = () => {}; // suppress output +function randomString(L) { + L = L || 18; + let s = ""; + do { + s += Math.random().toString(36).substring(2, 15); + } while (s.length < L); + return s.substring(0, L); +} + +describe("Formatters", function () { + let capturedOutput = null; + configuration.source.path = + "./test/fixtures/resources/sample-proxy-with-issues/response-shaping/apiproxy"; + configuration.output = (formatted) => { + capturedOutput = formatted; + }; debug("test configuration: " + JSON.stringify(configuration)); - formatters.forEach( formatter => { - it(`Linting with formatter ${formatter} should succeed`, function() { - configuration.formatter = formatter; - let bundle = bl.lint(configuration); - let report = bundle.getReport(); - assert.ok(report); - debug("formatted report: \n" + report); + formatters.forEach((formatter) => { + it(`Formatter ${formatter} should succeed`, function () { + try { + capturedOutput = null; + configuration.formatter = formatter; + const bundle = bl.lint(configuration); + const report = bundle.getReport(); + assert.ok(report); + assert.ok(capturedOutput); + //console.log(capturedOutput); + debug("formatted report: \n" + capturedOutput); + } catch (e) { + assert.fail("formatter implementation throws exception: " + e.stack); + } }); }); + + const nonExistingFormatterName = `${randomString(9)}.js`; + it(`Non-existing Formatter ${nonExistingFormatterName} should fail`, function () { + try { + configuration.formatter = nonExistingFormatterName; + const _bundle = bl.lint(configuration); + assert.fail("formatter implementation succeeds unexpectedly."); + } catch (e) { + assert.ok(e); + assert.equal( + e.message, + `There was a problem loading formatter: ./third_party/formatters/${nonExistingFormatterName}` + ); + } + }); });