Skip to content

Commit

Permalink
fix: fix regression where files like 'license-*' are not matched
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Jul 4, 2024
1 parent 0454c64 commit 1723c91
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/read-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ import fs from 'fs';
import _ from 'lodash';
import { fdir } from 'fdir';

const pathsMatch = (target) => {
const targetLower = target.toLowerCase();

return (p) => {
const pLower = p.toLowerCase();
return pLower === targetLower ||
pLower.slice(0, pLower.lastIndexOf('.')) === targetLower;
};
};
/**
* Find file and returns its content if file exists.
*
Expand Down Expand Up @@ -70,3 +61,30 @@ export function readFile(dir, names) {

return null;
}

/**
* Returns a predicate function that returns `true` if the given path matches the target path.
*
* @param {string} target Target path.
* @returns {function(*): boolean} Predicate function.
*/
function pathsMatch(target) {
const targetRegExp = generatePattern(target);
return (p) => (
targetRegExp.test(p)
);
}

const FILE_FORBIDDEN_CHARACTERS = ['#', '%', '&', '*', ':', '<', '>', '?', '/', path.sep, '{', '|', '}'];
const FILE_SUFFIX_PTN = `[^${FILE_FORBIDDEN_CHARACTERS.join('')}]`;

/**
* Generate filename pattern for the given input: the generated regexp will match any file
* starting with `input` (case insensitively).
*
* @param {string} input Input.
* @returns {RegExp} Generated pattern.
*/
function generatePattern(input) {
return new RegExp(`^${input}(${FILE_SUFFIX_PTN})*$`, 'i');
}
1 change: 1 addition & 0 deletions test/fixtures/fake-package-12/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LICENSE-MIT file
15 changes: 15 additions & 0 deletions test/fixtures/fake-package-12/package.json
Original file line number Diff line number Diff line change
@@ -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": "*"
}
}
25 changes: 25 additions & 0 deletions test/fixtures/fake-package-12/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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');
15 changes: 15 additions & 0 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,21 @@ describe('LicensePlugin', () => {

plugin.scanDependency(id);
});

it('should load pkg including license text from LICENSE-* file', () => {
const id = path.join(__dirname, 'fixtures', 'fake-package-12', 'src', 'index.js');

plugin.scanDependency(id);

expect(addDependency).toHaveBeenCalled();
expect(plugin._dependencies.size).toBe(1);
expect(plugin._dependencies.has('fake-package')).toBe(true);
expect(plugin._dependencies.get('fake-package')).toEqual({
...fakePackage,
self: false,
licenseText: 'LICENSE-MIT file',
});
});
});

describe('when adding dependencies', () => {
Expand Down

0 comments on commit 1723c91

Please sign in to comment.