Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve license file reading logic #5740

Merged
merged 6 commits into from Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
})