Files
pnpm/patching/apply-patch/test/applyPatchToDir.ts
Johan Quan Vo 7b1c189f2e feat!: remove deprecated patch options (#10505)
* refactor: remove allowNonAppliedPatches

* refactor: remove ignorePatchFailures

* refactor: remove `strict` field in groupPatchedDependencies

* test: update test failure in package patching

* test: fix

* docs: update changesets

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
2026-01-27 17:08:45 +01:00

73 lines
2.2 KiB
TypeScript

import fs from 'fs'
import path from 'path'
import { fixtures } from '@pnpm/test-fixtures'
import { tempDir } from '@pnpm/prepare'
import { jest } from '@jest/globals'
const f = 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()
f.copy('patch-target.txt', path.join(dir, 'patch-target.txt'))
return dir
}
describe('applyPatchToDir()', () => {
it('should succeed when patch is applicable', () => {
const patchFilePath = f.find('applicable.patch')
const successfullyPatched = f.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 = f.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(f.find('patch-target.txt'), 'utf-8'))
})
it('should fail on invalid patch', () => {
const patchFilePath = f.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')
})
})