Skip to content

Commit

Permalink
feat: include notice file in third party output
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Mar 9, 2024
1 parent c6c059f commit ea5997c
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 20 deletions.
12 changes: 11 additions & 1 deletion src/dependency.js
Expand Up @@ -46,6 +46,7 @@ export class Dependency {
this.private = pkg.private || false;
this.license = pkg.license || null;
this.licenseText = pkg.licenseText || null;
this.noticeText = pkg.noticeText || null;

// Parse the author field to get an object.
this.author = pkg.author ? new Person(pkg.author) : null;
Expand Down Expand Up @@ -105,8 +106,17 @@ export class Dependency {
lines.push('===');
lines.push('');
lines.push(this.licenseText);
lines.push('');
}

if (this.noticeText) {
lines.push('Notice:');
lines.push('===');
lines.push('');
lines.push(this.noticeText);
lines.push('');
}

return lines.join(EOL);
return lines.join(EOL).trim();
}
}
5 changes: 5 additions & 0 deletions src/index.d.ts
Expand Up @@ -168,6 +168,11 @@ export interface Dependency {
*/
readonly licenseText: string | null;

/**
* Full notice file text.
*/
readonly noticeText: string | null;

/**
* Author information.
*/
Expand Down
8 changes: 7 additions & 1 deletion src/license-plugin.js
Expand Up @@ -194,13 +194,19 @@ class LicensePlugin {
// We found it!
pkg = pkgJson;

// Read license file, if it exists.
// Read license & notice files, if it exists.
const cwd = this._cwd || process.cwd();

const licenseText = readFile(dir, cwd, ['license', 'licence']);
if (licenseText) {
pkg.licenseText = licenseText;
}

const noticeText = readFile(dir, cwd, 'notice');
if (noticeText) {
pkg.noticeText = noticeText;
}

// Add the new dependency to the set of third-party dependencies.
this.addDependency(pkg);

Expand Down
45 changes: 28 additions & 17 deletions test/base.spec.js
Expand Up @@ -26,6 +26,32 @@ import _ from 'lodash';
import {Dependency} from '../src/dependency.js';
import {Person} from '../src/person.js';

/**
* Normalize input dependency object, so it can be safely compared with another
* normalized dependency object.
*
* @param {Dependency|object} input Input.
* @returns {object} Normalized output.
*/
function normalizeDependencyObject(input) {
const output = _.toPlainObject(input);

if (output.author) {
output.author = _.toPlainObject(output.author);
}
if (output.contributors) {
output.contributors = output.contributors.map((o) => _.toPlainObject(o));
}
if (output.licenseText) {
output.licenseText = output.licenseText.trim();
}
if (output.noticeText) {
output.noticeText = output.noticeText.trim();
}

return output;
}

beforeEach(() => {
jasmine.addCustomEqualityTester((first, second) => {
if ((first instanceof Person) || (second instanceof Person)) {
Expand All @@ -37,23 +63,8 @@ beforeEach(() => {

jasmine.addCustomEqualityTester((first, second) => {
if ((first instanceof Dependency) || (second instanceof Dependency)) {
const o1 = _.toPlainObject(first);
const o2 = _.toPlainObject(second);

if (o1.author) {
o1.author = _.toPlainObject(o1.author);
}
if (o1.contributors) {
o1.contributors = o1.contributors.map((o) => _.toPlainObject(o));
}

if (o2.author) {
o2.author = _.toPlainObject(o2.author);
}
if (o2.contributors) {
o2.contributors = o2.contributors.map((o) => _.toPlainObject(o));
}

const o1 = normalizeDependencyObject(first);
const o2 = normalizeDependencyObject(second);
return _.isEqual(o1, o2);
}
});
Expand Down
33 changes: 33 additions & 0 deletions test/dependency.spec.js
Expand Up @@ -59,6 +59,7 @@ describe('Dependency', () => {
version: '1.0.0',
license: 'MIT',
licenseText: null,
noticeText: null,
description: 'Desc',
private: false,
homepage: 'https://github.com/mjeanroy',
Expand Down Expand Up @@ -345,4 +346,36 @@ describe('Dependency', () => {
`The MIT License (MIT) -- Copyright (c) Mickael Jeanroy`,
]));
});

it('should format dependency with notice text', () => {
const pkg = {
name: 'foo',
version: '1.0.0',
license: 'MIT',
licenseText: 'The MIT License (MIT) -- Copyright (c) Mickael Jeanroy',
noticeText: 'Software libraries under third_party',
description: 'Desc',
homepage: 'https://github.com/mjeanroy',
};

const dependency = new Dependency(pkg);

expect(dependency.text()).toEqual(join([
`Name: ${pkg.name}`,
`Version: ${pkg.version}`,
`License: ${pkg.license}`,
`Private: false`,
`Description: ${pkg.description}`,
`Homepage: ${pkg.homepage}`,
`License Copyright:`,
`===`,
``,
`The MIT License (MIT) -- Copyright (c) Mickael Jeanroy`,
``,
`Notice:`,
`===`,
``,
`Software libraries under third_party`,
]));
});
});
25 changes: 25 additions & 0 deletions test/fixtures/fake-package-11/index.js
@@ -0,0 +1,25 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Mickael Jeanroy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

console.log('fake-package');
1 change: 1 addition & 0 deletions test/fixtures/fake-package-11/license.md
@@ -0,0 +1 @@
license.md file
1 change: 1 addition & 0 deletions test/fixtures/fake-package-11/notice.md
@@ -0,0 +1 @@
notice.md file
15 changes: 15 additions & 0 deletions test/fixtures/fake-package-11/package.json
@@ -0,0 +1,15 @@
{
"name": "fake-package",
"version": "1.0.0",
"description": "Fake package used in unit tests",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Mickael Jeanroy <mickael.jeanroy@gmail.com>",
"license": "MIT",
"private": true,
"dependencies": {
"lodash": "*"
}
}
23 changes: 22 additions & 1 deletion test/license-plugin.spec.js
Expand Up @@ -28,7 +28,6 @@ import tmp from 'tmp';
import moment from 'moment';
import {licensePlugin} from '../src/license-plugin.js';
import {join} from './utils/join.js';
import {pkg} from '../scripts/config';

describe('LicensePlugin', () => {
let tmpDir;
Expand Down Expand Up @@ -110,6 +109,7 @@ describe('LicensePlugin', () => {
description: 'Fake package used in unit tests',
license: 'MIT',
licenseText: null,
noticeText: null,
private: true,
homepage: null,
repository: null,
Expand Down Expand Up @@ -190,6 +190,7 @@ describe('LicensePlugin', () => {
private: true,
license: 'MIT',
licenseText: null,
noticeText: null,
author: null,
contributors: [],
},
Expand Down Expand Up @@ -238,6 +239,21 @@ describe('LicensePlugin', () => {
});
});

it('should load pkg and find notice file', () => {
const id = path.join(__dirname, 'fixtures', 'fake-package-11', 'src', 'index.js');

plugin.scanDependency(id);

expect(addDependency).toHaveBeenCalled();
expect(plugin._dependencies).toEqual({
'fake-package': {
...fakePackage,
licenseText: 'license.md file',
noticeText: 'notice.md file',
},
});
});

it('should load pkg including license text from license.md file ignoring case of license file', () => {
const id = path.join(__dirname, 'fixtures', 'fake-package-8', 'src', 'index.js');

Expand Down Expand Up @@ -415,6 +431,7 @@ describe('LicensePlugin', () => {
description: 'Fake Description',
license: 'MIT',
licenseText: null,
noticeText: null,
homepage: 'https://www.google.fr',
private: true,
maintainers: [],
Expand Down Expand Up @@ -1205,6 +1222,7 @@ describe('LicensePlugin', () => {
description: 'Foo Package',
license: 'MIT',
licenseText: null,
noticeText: null,
private: false,
homepage: null,
repository: null,
Expand Down Expand Up @@ -1242,6 +1260,7 @@ describe('LicensePlugin', () => {
description: 'Foo Package',
license: 'MIT',
licenseText: null,
noticeText: null,
private: false,
homepage: null,
repository: null,
Expand Down Expand Up @@ -1281,6 +1300,7 @@ describe('LicensePlugin', () => {
description: 'Foo Package',
license: 'MIT',
licenseText: null,
noticeText: null,
private: false,
maintainers: [],
contributors: [],
Expand All @@ -1298,6 +1318,7 @@ describe('LicensePlugin', () => {
description: 'Bar Package',
license: 'Apache 2.0',
licenseText: null,
noticeText: null,
maintainers: [],
contributors: [],
author: null,
Expand Down

0 comments on commit ea5997c

Please sign in to comment.