Skip to content

Commit

Permalink
fix: cleaner error when formatter is missing. new test for same.
Browse files Browse the repository at this point in the history
  • Loading branch information
DinoChiesa committed Aug 28, 2024
1 parent c0dd01e commit c3f24ff
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
8 changes: 5 additions & 3 deletions lib/package/bundleLinter.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down
63 changes: 48 additions & 15 deletions test/specs/testFormatters.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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",
Expand All @@ -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}`
);
}
});
});

0 comments on commit c3f24ff

Please sign in to comment.