Add TypeScript ports of the perfectionist rules that the Rust workspace enforces through dylint.toml to @pnpm/eslint-config, with the same limits, and fix every finding that needs no refactor. The size and shape rules (function length, cognitive complexity, nesting, local bindings, file length) are enforced for packages that already meet them. The packages that do not are listed in PENDING_SIZE_AND_SHAPE_REFACTOR in eslint.config.mjs and are refactored in follow-up changes. The custom rules mirror perfectionist's semantics rather than reusing sonarjs or max-lines-per-function, so that one limit means the same thing in both codebases: a closure is a level of the body that contains it, a nested function declaration is measured on its own, and a test-framework callback plays the role of a #[test] function. Bugs found and fixed along the way: - a registry error body cut at exactly 64 KiB lost its truncation marker - removing a dependency could leave dangling .bin shims - constructor/__proto__ names crashed the projects graph, were skipped by the injected-deps syncer, and were dropped from writeWantedLockfile's returned lockfile The CI chunk runner learns to run the node:test suite of the ESLint config package.
89 lines
2.8 KiB
TypeScript
89 lines
2.8 KiB
TypeScript
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
|
|
import { beforeEach, describe, expect, it, jest } from '@jest/globals'
|
|
import { tempDir } from '@pnpm/prepare'
|
|
import { fixtures } from '@pnpm/test-fixtures'
|
|
|
|
const testFixtures = fixtures(import.meta.dirname)
|
|
|
|
const originalModule = await import('@pnpm/logger')
|
|
jest.unstable_mockModule('@pnpm/logger', () => {
|
|
return {
|
|
...originalModule,
|
|
globalWarn: jest.fn(),
|
|
}
|
|
})
|
|
|
|
const { globalWarn } = await import('@pnpm/logger')
|
|
const { applyPatchToDir } = await import('@pnpm/patching.apply-patch')
|
|
|
|
beforeEach(() => {
|
|
jest.mocked(globalWarn).mockClear()
|
|
})
|
|
|
|
function prepareDirToPatch () {
|
|
const dir = tempDir()
|
|
testFixtures.copy('patch-target.txt', path.join(dir, 'patch-target.txt'))
|
|
return dir
|
|
}
|
|
|
|
describe('applyPatchToDir()', () => {
|
|
it('should succeed when patch is applicable', () => {
|
|
const patchFilePath = testFixtures.find('applicable.patch')
|
|
const successfullyPatched = testFixtures.find('successfully-patched.txt')
|
|
const patchedDir = prepareDirToPatch()
|
|
expect(
|
|
applyPatchToDir({
|
|
patchFilePath,
|
|
patchedDir,
|
|
})
|
|
).toBe(true)
|
|
const patchTarget = path.join(patchedDir, 'patch-target.txt')
|
|
expect(fs.readFileSync(patchTarget, 'utf-8')).toBe(fs.readFileSync(successfullyPatched, 'utf-8'))
|
|
})
|
|
it('should fail when patch fails to apply', () => {
|
|
const patchFilePath = testFixtures.find('non-applicable.patch')
|
|
const patchedDir = prepareDirToPatch()
|
|
expect(() => {
|
|
applyPatchToDir({
|
|
patchFilePath,
|
|
patchedDir,
|
|
})
|
|
}).toThrow(`Could not apply patch ${patchFilePath} to ${patchedDir}`)
|
|
expect(fs.readFileSync(path.join(patchedDir, 'patch-target.txt'), 'utf-8')).toBe(fs.readFileSync(testFixtures.find('patch-target.txt'), 'utf-8'))
|
|
})
|
|
it('should fail on invalid patch', () => {
|
|
const patchFilePath = testFixtures.find('invalid.patch')
|
|
expect(() => {
|
|
applyPatchToDir({
|
|
patchFilePath,
|
|
patchedDir: tempDir(),
|
|
})
|
|
}).toThrow(`Applying patch "${patchFilePath}" failed: hunk header integrity check failed`)
|
|
})
|
|
it('should fail if the patch file is not found', () => {
|
|
expect(() => {
|
|
applyPatchToDir({
|
|
patchFilePath: 'does-not-exist.patch',
|
|
patchedDir: tempDir(),
|
|
})
|
|
}).toThrow('Patch file not found')
|
|
})
|
|
it('should reject a patch whose paths escape the patched directory', () => {
|
|
const patchFilePath = testFixtures.find('path-traversal.patch')
|
|
const patchedDir = tempDir()
|
|
const sentinel = path.join('/tmp', 'pnpm-patch-traversal-pwned')
|
|
try {
|
|
fs.unlinkSync(sentinel)
|
|
} catch {}
|
|
expect(() => {
|
|
applyPatchToDir({
|
|
patchFilePath,
|
|
patchedDir,
|
|
})
|
|
}).toThrow(/patch path escapes target dir/)
|
|
expect(fs.existsSync(sentinel)).toBe(false)
|
|
})
|
|
})
|