Skip to content

Commit

Permalink
feat: Make license check configuration optional for analyze command. (#…
Browse files Browse the repository at this point in the history
…718)

Without a license check configuration the most strict analysis of the
license is performed.
  • Loading branch information
yeldiRium committed Sep 16, 2021
1 parent 8397a84 commit 122b81d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 15 deletions.
8 changes: 5 additions & 3 deletions lib/steps/analyze/checkLicenseExpression.ts
Expand Up @@ -9,11 +9,13 @@ const checkLicenseExpression = async function ({
}: {
applicationRoot: string;
}): Promise<Result<undefined, errors.LicenseNotFound | errors.LicenseNotSupported | errors.LicenseDeprecated>> {
const licenseCheckConfiguration = (await getLicenseCheckConfiguration({ absoluteDirectory: applicationRoot })).unwrapOrThrow();
const licenseCheckConfigurationResult = await getLicenseCheckConfiguration({ absoluteDirectory: applicationRoot });
const licenseCheckConfiguration = licenseCheckConfigurationResult.hasValue() ? licenseCheckConfigurationResult.value : undefined;

const licenseResult = await getLicense({ absoluteDirectory: applicationRoot, licenseCheckConfiguration });

if (licenseResult.hasError()) {
if (licenseResult.error.code === errors.LicenseNotSupported.code && licenseCheckConfiguration.allowUnsupportedLicenseForThisPackage) {
if (licenseResult.error.code === errors.LicenseNotSupported.code && licenseCheckConfiguration?.allowUnsupportedLicenseForThisPackage) {
return value();
}

Expand All @@ -25,7 +27,7 @@ const checkLicenseExpression = async function ({

const license = licenseResult.unwrapOrThrow();

if (deprecatedSpdxLicenseIds.includes(license) && !licenseCheckConfiguration.allowDeprecatedLicenseForThisPackage) {
if (deprecatedSpdxLicenseIds.includes(license) && !licenseCheckConfiguration?.allowDeprecatedLicenseForThisPackage) {
return error(new errors.LicenseDeprecated({
message: `${license} is deprecated, please consider using an updated version of this license, or disable this check by setting 'allowDeprecatedLicenseForThisPackage' in licenseCheck.json.`
}));
Expand Down
14 changes: 8 additions & 6 deletions lib/steps/license/getLicense.ts
Expand Up @@ -8,7 +8,7 @@ import * as errors from '../../errors';

const getLicense = async function ({ absoluteDirectory, licenseCheckConfiguration }: {
absoluteDirectory: string;
licenseCheckConfiguration: LicenseCheckConfiguration;
licenseCheckConfiguration?: LicenseCheckConfiguration;
}): Promise<Result<string, errors.LicenseNotFound | errors.LicenseNotSupported>> {
const packageJsonResult = await getPackageJson({ absoluteDirectory });

Expand Down Expand Up @@ -58,11 +58,13 @@ const getLicense = async function ({ absoluteDirectory, licenseCheckConfiguratio

let knownPackageLicense: string | undefined;

knownPackageLicense = getMatchingLicense({
licensesMap: licenseCheckConfiguration.knownPackageLicenses ?? {},
packageName,
packageVersion
});
if (licenseCheckConfiguration) {
knownPackageLicense = getMatchingLicense({
licensesMap: licenseCheckConfiguration.knownPackageLicenses ?? {},
packageName,
packageVersion
});
}
if (!knownPackageLicense) {
knownPackageLicense = getMatchingLicense({
licensesMap: packageLicenses,
Expand Down
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions test/integration/analyzeTests.ts
Expand Up @@ -358,4 +358,27 @@ suite('analyze', function (): void {
assert.that(roboterResult.unwrapOrThrow().exitCode).is.equalTo(0);
}
);

testWithFixture(
'validates license strictly if no license check configuration exists.',
[ 'analyze', 'without-license-check-configuration' ],
async (fixture): Promise<void> => {
const roboterResult = await runCommand('npx roboter analyze', {
cwd: fixture.absoluteTestDirectory,
silent: true
});

if (roboterResult.hasValue()) {
throw new Error(`The command should have failed, but didn't.`);
}

const { error } = roboterResult;

assert.that(error.exitCode).is.equalTo(1);
assert.that(stripAnsi(error.stdout)).is.containing(stripIndent`
The given license is not supported, please check your spelling
`);
assert.that(stripAnsi(error.stderr)).is.containing('The given license is not a valid SPDX expression.');
}
);
});
@@ -0,0 +1,4 @@
'use strict';

// eslint-disable-next-line no-unused-vars
const x = 42;
@@ -0,0 +1,16 @@
{
"name": "test-package",
"version": "0.0.1",
"description": "a test-package.",
"contributors": [],
"private": false,
"main": "",
"types": "",
"engines": {},
"dependencies": {},
"devDependencies": {},
"scripts": {},
"repository": {},
"keywords": [],
"license": "BLNKLGHTS-1.33.7"
}

0 comments on commit 122b81d

Please sign in to comment.