Skip to content

Commit

Permalink
fix: improve license file reading logic (#5740)
Browse files Browse the repository at this point in the history
* fix: improve license file reading logic

If the file index is of the appropriate type so we can leverage the contents of `PackageFileInfo`-type we try
to lookup the file mode to and pass it to the `readLicenseFileFromCafs`-function so it can be correctly
passed to the underlying `getFilePathByModeInCafs`-function responsible for fetching the file from
the PNPM store

* test: add integration test for the problematic `svgicons2svgfont`

Added a test to verify that the LICENSE files of the `svgicons2svgfont@5.0.2`-package
and similar packages can be read without errors

* refactor: license

* docs: add changesets

Co-authored-by: Weyert de Boer <weyert@innerfuse.biz>
Co-authored-by: Zoltan Kochan <z@kochan.io>
  • Loading branch information
3 people committed Dec 9, 2022
1 parent ef134a0 commit 5464e1d
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/wet-snakes-sit.md
@@ -0,0 +1,7 @@
---
"@pnpm/license-scanner": patch
"@pnpm/plugin-commands-licenses": patch
"pnpm": patch
---

`pnpm license list` should not fail if a license file is an executable [#5740](https://github.com/pnpm/pnpm/pull/5740).
9 changes: 3 additions & 6 deletions reviewing/license-scanner/src/getPkgInfo.ts
Expand Up @@ -128,10 +128,7 @@ async function parseLicense (
if (pkg.files.local) {
licenseContents = await readFile(licensePackageFileInfo as string)
} else {
licenseContents = await readLicenseFileFromCafs(
opts.cafsDir,
(licensePackageFileInfo as PackageFileInfo).integrity
)
licenseContents = await readLicenseFileFromCafs(opts.cafsDir, licensePackageFileInfo as PackageFileInfo)
}

return {
Expand All @@ -150,8 +147,8 @@ async function parseLicense (
* @param opts the options for reading file
* @returns Promise<Buffer>
*/
async function readLicenseFileFromCafs (cafsDir: string, fileIntegrity: string) {
const fileName = getFilePathByModeInCafs(cafsDir, fileIntegrity, 0)
async function readLicenseFileFromCafs (cafsDir: string, { integrity, mode }: PackageFileInfo) {
const fileName = getFilePathByModeInCafs(cafsDir, integrity, mode)
const fileContents = await readFile(fileName)
return fileContents
}
Expand Down
Expand Up @@ -6,6 +6,57 @@ exports[`pnpm licenses: output as json: found-license-types 1`] = `
]
`;

exports[`pnpm licenses: should correctly read LICENSE file with executable file mode: show-packages-details 1`] = `
"┌──────────────────────────────┬─────────┬───────────────────────────────────────────────────────┐
│ Package │ License │ Details │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ inherits │ ISC │ https://github.com/isaacs/inherits#readme │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ sax │ ISC │ Isaac Z. Schlueter │
│ │ │ https://github.com/isaacs/sax-js#readme │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ commander │ MIT │ TJ Holowaychuk │
│ │ │ https://github.com/tj/commander.js#readme │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ core-util-is │ MIT │ Isaac Z. Schlueter │
│ │ │ https://github.com/isaacs/core-util-is#readme │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ isarray │ MIT │ Julian Gruber │
│ │ │ https://github.com/juliangruber/isarray │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ neatequal │ MIT │ Nicolas Froidure │
│ │ │ https://github.com/nfroidure/neatequal │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ process-nextick-args │ MIT │ https://github.com/calvinmetcalf/process-nextick-args │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ readable-stream │ MIT │ https://github.com/nodejs/readable-stream#readme │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ safe-buffer │ MIT │ Feross Aboukhadijeh │
│ │ │ https://github.com/feross/safe-buffer │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ string_decoder │ MIT │ https://github.com/nodejs/string_decoder │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ string.fromcodepoint │ MIT │ Mathias Bynens │
│ │ │ http://mths.be/fromcodepoint │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ string.prototype.codepointat │ MIT │ Mathias Bynens │
│ │ │ https://mths.be/codepointat │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ svg-pathdata │ MIT │ Nicolas Froidure │
│ │ │ https://github.com/nfroidure/SVGPathData#readme │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ svgicons2svgfont │ MIT │ Nicolas Froidure │
│ │ │ https://github.com/nfroidure/svgicons2svgfont │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ util-deprecate │ MIT │ Nathan Rajlich │
│ │ │ https://github.com/TooTallNate/util-deprecate │
├──────────────────────────────┼─────────┼───────────────────────────────────────────────────────┤
│ varstream │ Unknown │ Nicolas Froidure │
│ │ │ https://github.com/nfroidure/VarStream#readme │
└──────────────────────────────┴─────────┴───────────────────────────────────────────────────────┘
"
`;

exports[`pnpm licenses: show details: show-packages-details 1`] = `
"┌─────────────┬─────────┬─────────────────────────────────────────────┐
│ Package │ License │ Details │
Expand Down
@@ -0,0 +1,15 @@
{
"name": "pnpm-licenses-bug001",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"svgicons2svgfont": "5.0.2"
}
}

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

27 changes: 27 additions & 0 deletions reviewing/plugin-commands-licenses/test/index.ts
Expand Up @@ -134,3 +134,30 @@ test('pnpm licenses: fails when lockfile is missing', async () => {
'"No pnpm-lock.yaml found: Cannot check a project without a lockfile"'
)
})

test('pnpm licenses: should correctly read LICENSE file with executable file mode', async () => {
const workspaceDir = path.resolve('./test/fixtures/file-mode-test')

const tmp = tempy.directory()
const storeDir = path.join(tmp, 'store')
await install.handler({
...DEFAULT_OPTS,
dir: workspaceDir,
pnpmHomeDir: '',
storeDir,
})

// Attempt to run the licenses command now
const { output, exitCode } = await licenses.handler({
...LICENSES_OPTIONS,
dir: workspaceDir,
pnpmHomeDir: '',
long: true,
// we need to prefix it with v3 otherwise licenses tool can't find anything
// in the content-addressable directory
storeDir: path.resolve(storeDir, 'v3'),
}, ['list'])

expect(exitCode).toBe(0)
expect(stripAnsi(output)).toMatchSnapshot('show-packages-details')
})

0 comments on commit 5464e1d

Please sign in to comment.