Skip to content

Commit

Permalink
fix: avoid generating error report when disableErrorReport is true (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
inigomarquinez committed Jul 20, 2023
1 parent cd53a8d commit 88c1abe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const {
} = require('./utils');

const check = (license) => {
if (!license) throw new Error('Error: You must provide a license to check.');

// @TODO Remove after issue has been solved
if (isLicenseError(license)) {
throw new Error(
Expand All @@ -25,6 +27,8 @@ const check = (license) => {
const scan = async (options) => {
const { failOn } = options;

if (!failOn) throw new Error('Error: You must provide a list of licenses to fail on in the "failOn" option.');

checkLicenseError(failOn); // @TODO Remove after issue has been solved
checkSPDXCompliance(failOn);
const bannedLicenses = generateSPDXExpression(failOn);
Expand All @@ -38,7 +42,7 @@ const scan = async (options) => {
}

if (forbiddenPackages.length) {
reporter.writeErrorReportFile(options.errorReportFileName, forbiddenPackages);
if (!options.disableErrorReport) reporter.writeErrorReportFile(options.errorReportFileName, forbiddenPackages);
throw new Error(formatForbiddenLicenseError(forbiddenPackages));
}

Expand Down
40 changes: 37 additions & 3 deletions test/scan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ describe('scan command', () => {
describe('command execution output', () => {
it('should call the checker passing the path to the working directory', async () => {
const options = {
start: '/path/to/cwd'
start: '/path/to/cwd',
failOn: ['MIT']
};

const packages = {
Expand All @@ -67,7 +68,8 @@ describe('scan command', () => {

it('should print a message if any of the packages\' licenses are not SPDX compliant', async () => {
const options = {
start: '/path/to/cwd'
start: '/path/to/cwd',
failOn: ['MIT']
};

const packages = {
Expand Down Expand Up @@ -251,12 +253,42 @@ describe('scan command', () => {
);
}
});

it('should not call the reporter to generate the error report file if the option "disableErrorReport" is enabled', async () => {
const options = {
start: '/path/to/cwd',
failOn: ['GPL-1.0+'],
disableErrorReport: true
};

const packages = {
'package-1': {
licenses: 'GPL-1.0',
repository: 'https://git.com/repo/repo',
path: '/path/to/package',
licenseFile: '/path/to/package/LICENSE'
}
};

parsePackages.mockResolvedValueOnce(packages);

let error;
try {
await scan(options);
} catch (err) {
error = err;
} finally {
expect(error.message).toBe('Found 1 packages with licenses defined by the --failOn flag:\n > 1 packages with license GPL-1.0');
expect(writeErrorReportFile).not.toHaveBeenCalled();
}
});
});

describe('licenses report file', () => {
it('should not call the reporter to generate the report file if the option "disableReport" is supplied', async () => {
const options = {
start: '/path/to/cwd',
failOn: ['MIT'],
disableReport: true
};

Expand All @@ -278,7 +310,8 @@ describe('scan command', () => {

it('should call the reporter to generate the report file', async () => {
const options = {
start: '/path/to/cwd'
start: '/path/to/cwd',
failOn: ['MIT']
};

const packages = {
Expand All @@ -300,6 +333,7 @@ describe('scan command', () => {
it('should call the reporter with the right arguments', async () => {
const options = {
start: '/path/to/cwd',
failOn: ['MIT'],
outputFileName: 'outputFileName',
customHeader: 'customHeader'
};
Expand Down

0 comments on commit 88c1abe

Please sign in to comment.