* refactor(ts): meet size and shape limits in 5 packages Refactor 5 TypeScript packages to satisfy strict size and shape lint limits (function length, nesting, cognitive complexity, local declarations): - `@pnpm/installing.linking.modules-cleaner` - `@pnpm/installing.linking.real-hoist` - `@pnpm/lockfile.utils` - `@pnpm/modules-mounter.daemon` - `@pnpm/releasing.exportable-manifest` Remove them from PENDING_SIZE_AND_SHAPE_REFACTOR in eslint.config.mjs. * refactor(ts): meet size and shape limits in 10 packages Refactor 10 TypeScript packages to satisfy strict size and shape lint limits (function length, nesting, cognitive complexity, local declarations): - `@pnpm/bins.cmd-shim` - `@pnpm/engine.runtime.commands` - `@pnpm/exec.lifecycle` - `@pnpm/fetching.git-fetcher` - `@pnpm/fetching.tarball-fetcher` - `@pnpm/fs.indexed-pkg-importer` - `@pnpm/hooks.pnpmfile` - `@pnpm/installing.context` - `@pnpm/resolving.git-resolver` - `@pnpm/store.commands` Graduate them from PENDING_SIZE_AND_SHAPE_REFACTOR in eslint.config.mjs. * fix(ts): address review feedback on shims and lockfile comments * test(cmd-shim): add regression test for isShimBasedirAnchorCurrent with absolute targets
351 lines
12 KiB
TypeScript
351 lines
12 KiB
TypeScript
/// <reference path="../../../__typings__/index.d.ts"/>
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
|
|
import { expect, test } from '@jest/globals'
|
|
import { PnpmError } from '@pnpm/error'
|
|
import {
|
|
makeNodePackageMapOption,
|
|
makeNodeRequireOption,
|
|
makeProjectNodePathOption,
|
|
runLifecycleHook,
|
|
runLifecycleHooksConcurrently,
|
|
runPostinstallHooks,
|
|
} from '@pnpm/exec.lifecycle'
|
|
import { tempDir } from '@pnpm/prepare'
|
|
import type { StoreController } from '@pnpm/store.controller-types'
|
|
import { fixtures } from '@pnpm/test-fixtures'
|
|
import { createTestIpcServer } from '@pnpm/test-ipc-server'
|
|
import type { ProjectRootDir } from '@pnpm/types'
|
|
import isWindows from 'is-windows'
|
|
|
|
const skipOnWindows = isWindows() ? test.skip : test
|
|
|
|
const testFixtures = fixtures(path.join(import.meta.dirname, 'fixtures'))
|
|
const rootModulesDir = path.join(import.meta.dirname, '..', 'node_modules')
|
|
|
|
skipOnWindows('makeProjectNodePathOption() puts the custom modules directory of a project with symlinked executables first on NODE_PATH', async () => {
|
|
const project = { modulesDir: '/project/vendor', rootDir: '/project' }
|
|
const opts = { preferSymlinkedExecutables: true, extraEnv: { NODE_PATH: '/project/vendor/.pnpm/node_modules' } }
|
|
|
|
expect(await makeProjectNodePathOption(project, opts)).toStrictEqual({
|
|
NODE_PATH: `/project/vendor${path.delimiter}/project/vendor/.pnpm/node_modules`,
|
|
})
|
|
expect(await makeProjectNodePathOption(project, { ...opts, extraEnv: { NODE_PATH: `/store/links/node_modules${path.delimiter}/project/vendor` } })).toStrictEqual({
|
|
NODE_PATH: `/project/vendor${path.delimiter}/store/links/node_modules`,
|
|
})
|
|
expect(await makeProjectNodePathOption(project, { ...opts, extendNodePath: false })).toStrictEqual({})
|
|
expect(await makeProjectNodePathOption(project, { ...opts, preferSymlinkedExecutables: false })).toStrictEqual({})
|
|
expect(await makeProjectNodePathOption({ ...project, modulesDir: '/project/node_modules' }, opts)).toStrictEqual({})
|
|
})
|
|
|
|
test('makeNodeRequireOption() preserves existing NODE_OPTIONS', () => {
|
|
expect(makeNodeRequireOption('/project/.pnp.cjs', {
|
|
NODE_OPTIONS: '--max-old-space-size=4096',
|
|
})).toStrictEqual({
|
|
NODE_OPTIONS: '--max-old-space-size=4096 --require=/project/.pnp.cjs',
|
|
})
|
|
})
|
|
|
|
test('makeNodeRequireOption() quotes and escapes module paths with backslashes or whitespace', () => {
|
|
expect(makeNodeRequireOption('C:\\project\\.pnp.cjs', {
|
|
NODE_OPTIONS: '',
|
|
})).toStrictEqual({
|
|
NODE_OPTIONS: '--require="C:\\\\project\\\\.pnp.cjs"',
|
|
})
|
|
expect(makeNodeRequireOption('/project with space/.pnp.cjs', {
|
|
NODE_OPTIONS: '',
|
|
})).toStrictEqual({
|
|
NODE_OPTIONS: '--require="/project with space/.pnp.cjs"',
|
|
})
|
|
})
|
|
|
|
test('makeNodeRequireOption() falls back to NODE_OPTIONS from process.env', () => {
|
|
const nodeOptions = process.env.NODE_OPTIONS
|
|
process.env.NODE_OPTIONS = '--trace-warnings'
|
|
try {
|
|
expect(makeNodeRequireOption('/project/.pnp.cjs', {})).toStrictEqual({
|
|
NODE_OPTIONS: '--trace-warnings --require=/project/.pnp.cjs',
|
|
})
|
|
} finally {
|
|
if (nodeOptions == null) {
|
|
delete process.env.NODE_OPTIONS
|
|
} else {
|
|
process.env.NODE_OPTIONS = nodeOptions
|
|
}
|
|
}
|
|
})
|
|
|
|
test('makeNodePackageMapOption() appends to NODE_OPTIONS', () => {
|
|
expect(makeNodePackageMapOption('/project/node_modules/.package-map.json', {
|
|
NODE_OPTIONS: '--max-old-space-size=4096',
|
|
})).toStrictEqual({
|
|
NODE_OPTIONS: '--max-old-space-size=4096 --experimental-package-map=/project/node_modules/.package-map.json',
|
|
})
|
|
})
|
|
|
|
test('makeNodePackageMapOption() quotes paths with whitespace', () => {
|
|
expect(makeNodePackageMapOption('/project with space/node_modules/.package-map.json', {
|
|
NODE_OPTIONS: '',
|
|
})).toStrictEqual({
|
|
NODE_OPTIONS: '--experimental-package-map="/project with space/node_modules/.package-map.json"',
|
|
})
|
|
})
|
|
|
|
test('makeNodePackageMapOption() quotes and escapes paths with backslashes or quotes', () => {
|
|
expect(makeNodePackageMapOption('C:\\project\\node_modules\\.package-map.json', {
|
|
NODE_OPTIONS: '',
|
|
})).toStrictEqual({
|
|
NODE_OPTIONS: '--experimental-package-map="C:\\\\project\\\\node_modules\\\\.package-map.json"',
|
|
})
|
|
expect(makeNodePackageMapOption('/quo"te/.package-map.json', {
|
|
NODE_OPTIONS: '',
|
|
})).toStrictEqual({
|
|
NODE_OPTIONS: '--experimental-package-map="/quo\\"te/.package-map.json"',
|
|
})
|
|
})
|
|
|
|
test('makeNodePackageMapOption() replaces existing package-map option', () => {
|
|
const nodeOptions = [
|
|
'--experimental-package-map=/old/node_modules/.package-map.json',
|
|
'--max-old-space-size=4096',
|
|
'--experimental-package-map="/old project/.package-map.json"',
|
|
'--experimental-package-map /other/.package-map.json',
|
|
'--inspect',
|
|
].join(' ')
|
|
|
|
expect(makeNodePackageMapOption('/new/node_modules/.package-map.json', {
|
|
NODE_OPTIONS: nodeOptions,
|
|
})).toStrictEqual({
|
|
NODE_OPTIONS: '--max-old-space-size=4096 --inspect --experimental-package-map=/new/node_modules/.package-map.json',
|
|
})
|
|
})
|
|
|
|
test('makeNodePackageMapOption() replaces an existing flag whose path contains an escaped quote', () => {
|
|
expect(makeNodePackageMapOption('/new/.package-map.json', {
|
|
NODE_OPTIONS: '--experimental-package-map="/quo\\"te/old.json" --inspect',
|
|
})).toStrictEqual({
|
|
NODE_OPTIONS: '--inspect --experimental-package-map=/new/.package-map.json',
|
|
})
|
|
})
|
|
|
|
test('runLifecycleHook()', async () => {
|
|
const pkgRoot = testFixtures.find('simple')
|
|
await using server = await createTestIpcServer(path.join(pkgRoot, 'test.sock'))
|
|
const { default: pkg } = await import(path.join(pkgRoot, 'package.json'))
|
|
await runLifecycleHook('postinstall', pkg, {
|
|
depPath: '/simple/1.0.0',
|
|
optional: false,
|
|
pkgRoot,
|
|
rootModulesDir,
|
|
unsafePerm: true,
|
|
})
|
|
|
|
expect(server.getLines()).toStrictEqual(['install'])
|
|
})
|
|
|
|
test('runLifecycleHook() escapes the args passed to the script', async () => {
|
|
const pkgRoot = testFixtures.find('escape-args')
|
|
const { default: pkg } = await import(path.join(pkgRoot, 'package.json'))
|
|
await runLifecycleHook('echo', pkg, {
|
|
depPath: '/escape-args/1.0.0',
|
|
pkgRoot,
|
|
rootModulesDir,
|
|
unsafePerm: true,
|
|
args: ['Revert "feature (#1)"'],
|
|
})
|
|
|
|
expect((await import(path.join(pkgRoot, 'output.json'))).default).toStrictEqual(['Revert "feature (#1)"'])
|
|
})
|
|
|
|
const argsThatCmdInterprets = [
|
|
'C:\\Program Files\\tool\\',
|
|
'%PATH%',
|
|
'a b',
|
|
'a"b',
|
|
'tab\there',
|
|
'^&|<>()!',
|
|
'',
|
|
'ends with a backslash\\',
|
|
]
|
|
|
|
test.each([
|
|
['a node script', 'node echo.js'],
|
|
['a node_modules/.bin shim', 'record-args'],
|
|
])('runLifecycleHook() passes the args unchanged to %s', async (_, script) => {
|
|
const pkgRoot = testFixtures.prepare('escape-args')
|
|
const binDir = path.join(pkgRoot, 'node_modules', '.bin')
|
|
fs.mkdirSync(binDir, { recursive: true })
|
|
fs.writeFileSync(path.join(binDir, 'record-args'), '#!/bin/sh\nexec node "$(dirname "$0")/../../echo.js" "$@"\n', { mode: 0o755 })
|
|
fs.writeFileSync(path.join(binDir, 'record-args.cmd'), '@node "%~dp0\\..\\..\\echo.js" %*\r\n')
|
|
const { default: pkg } = await import(path.join(pkgRoot, 'package.json'))
|
|
|
|
await runLifecycleHook('echo', { ...pkg, scripts: { echo: script } }, {
|
|
depPath: '/escape-args/1.0.0',
|
|
pkgRoot,
|
|
rootModulesDir,
|
|
unsafePerm: true,
|
|
args: argsThatCmdInterprets,
|
|
})
|
|
|
|
const recorded = JSON.parse(await fs.promises.readFile(path.join(pkgRoot, 'output.json'), 'utf8'))
|
|
expect(recorded).toStrictEqual(argsThatCmdInterprets)
|
|
})
|
|
|
|
test('runLifecycleHook() preserves literal arguments with the shell emulator', async () => {
|
|
const pkgRoot = testFixtures.find('escape-args')
|
|
const { default: pkg } = await import(path.join(pkgRoot, 'package.json'))
|
|
await fs.promises.rm(path.join(pkgRoot, 'output.json'), { force: true })
|
|
const args = [
|
|
'C:\\Program Files\\tool\\',
|
|
'',
|
|
'\'it\'\'s\'',
|
|
'a"b',
|
|
'$PNPM_QUOTING_TEST',
|
|
'$(echo expanded)',
|
|
'a;b',
|
|
'*',
|
|
'line\nbreak',
|
|
'中文',
|
|
]
|
|
await runLifecycleHook('echo', pkg, {
|
|
depPath: '/escape-args/1.0.0',
|
|
pkgRoot,
|
|
rootModulesDir,
|
|
unsafePerm: true,
|
|
shellEmulator: true,
|
|
extraEnv: { PNPM_QUOTING_TEST: 'expanded' },
|
|
args,
|
|
})
|
|
|
|
const recorded = JSON.parse(await fs.promises.readFile(path.join(pkgRoot, 'output.json'), 'utf8'))
|
|
expect(recorded).toStrictEqual(args)
|
|
})
|
|
|
|
test('runLifecycleHook() passes newline correctly', async () => {
|
|
const pkgRoot = testFixtures.find('escape-newline')
|
|
const { default: pkg } = await import(path.join(pkgRoot, 'package.json'))
|
|
await runLifecycleHook('echo', pkg, {
|
|
depPath: 'escape-newline@1.0.0',
|
|
pkgRoot,
|
|
rootModulesDir,
|
|
unsafePerm: true,
|
|
args: ['a\nb != \'A\\nB\''],
|
|
})
|
|
|
|
expect((await import(path.join(pkgRoot, 'output.json'))).default).toStrictEqual([
|
|
process.platform === 'win32' ? 'a\\nb != \'A\\nB\'' : 'a\nb != \'A\\nB\'',
|
|
])
|
|
})
|
|
|
|
test('runLifecycleHook() does not set npm_config env vars but preserves user-defined ones', async () => {
|
|
const pkgRoot = testFixtures.prepare('inspect-npm-config-env')
|
|
await using server = await createTestIpcServer(path.join(pkgRoot, 'test.sock'))
|
|
const { default: pkg } = await import(path.join(pkgRoot, 'package.json'))
|
|
const prevPlatformArch = process.env.npm_config_platform_arch
|
|
process.env.npm_config_platform_arch = 'x64'
|
|
try {
|
|
await runLifecycleHook('postinstall', pkg, {
|
|
depPath: '/inspect-npm-config-env/1.0.0',
|
|
pkgRoot,
|
|
rootModulesDir,
|
|
unsafePerm: true,
|
|
})
|
|
} finally {
|
|
if (prevPlatformArch === undefined) {
|
|
delete process.env.npm_config_platform_arch
|
|
} else {
|
|
process.env.npm_config_platform_arch = prevPlatformArch
|
|
}
|
|
}
|
|
|
|
expect(server.getLines()).toStrictEqual(['npm_config_platform_arch=x64'])
|
|
})
|
|
|
|
test('runPostinstallHooks()', async () => {
|
|
const pkgRoot = testFixtures.prepare('with-many-scripts')
|
|
await using server = await createTestIpcServer(path.join(pkgRoot, 'test.sock'))
|
|
await runPostinstallHooks({
|
|
depPath: '/with-many-scripts/1.0.0',
|
|
optional: false,
|
|
pkgRoot,
|
|
rootModulesDir,
|
|
unsafePerm: true,
|
|
})
|
|
|
|
expect(server.getLines()).toStrictEqual(['preinstall', 'install', 'postinstall'])
|
|
})
|
|
|
|
test('runLifecycleHook() should throw an error while missing script start or file server.js', async () => {
|
|
const pkgRoot = testFixtures.find('without-script-start-serverjs')
|
|
const { default: pkg } = await import(path.join(pkgRoot, 'package.json'))
|
|
await expect(
|
|
runLifecycleHook('start', pkg, {
|
|
depPath: '/without-script-start-serverjs/1.0.0',
|
|
optional: false,
|
|
pkgRoot,
|
|
rootModulesDir,
|
|
unsafePerm: true,
|
|
})
|
|
).rejects.toThrow(new PnpmError('NO_SCRIPT_OR_SERVER', 'Missing script start or file server.js'))
|
|
})
|
|
|
|
test('gypfile: false does not trigger node-gyp rebuild', async () => {
|
|
const ranAScript = await runPostinstallHooks({
|
|
depPath: '/gyp-with-gypfile-false/1.0.0',
|
|
optional: false,
|
|
pkgRoot: testFixtures.find('gyp-with-gypfile-false'),
|
|
rootModulesDir,
|
|
unsafePerm: true,
|
|
})
|
|
|
|
expect(ranAScript).toBe(false)
|
|
})
|
|
|
|
test('preinstall script does not trigger node-gyp rebuild', async () => {
|
|
const pkgRoot = testFixtures.find('gyp-with-preinstall')
|
|
await using server = await createTestIpcServer(path.join(pkgRoot, 'test.sock'))
|
|
await runPostinstallHooks({
|
|
depPath: '/gyp-with-preinstall/1.0.0',
|
|
optional: false,
|
|
pkgRoot,
|
|
rootModulesDir,
|
|
unsafePerm: true,
|
|
})
|
|
|
|
expect(server.getLines()).toStrictEqual(['preinstall'])
|
|
})
|
|
|
|
skipOnWindows('runLifecycleHooksConcurrently() should check binding.gyp', async () => {
|
|
const projectDir = tempDir(false)
|
|
|
|
fs.writeFileSync(path.join(projectDir, 'binding.gyp'), JSON.stringify({
|
|
targets: [
|
|
{
|
|
target_name: 'run_js_script',
|
|
actions: [
|
|
{
|
|
action_name: 'execute_postinstall',
|
|
inputs: [],
|
|
outputs: ['foo'],
|
|
action: ['node', '-e', 'require(\'fs\').writeFileSync(\'foo\', \'\', \'utf8\')'],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}), 'utf8')
|
|
|
|
await runLifecycleHooksConcurrently({
|
|
childConcurrency: 5,
|
|
importers: [{ buildIndex: 0, rootDir: projectDir as ProjectRootDir, modulesDir: '', manifest: {} }],
|
|
opts: {
|
|
storeController: {} as StoreController,
|
|
optional: false,
|
|
unsafePerm: true,
|
|
},
|
|
stages: ['install'],
|
|
})
|
|
|
|
expect(fs.existsSync(path.join(projectDir, 'foo'))).toBeTruthy()
|
|
})
|