Skip to content

Commit

Permalink
fix(patch): ignore No newline at end of file (#5321)
Browse files Browse the repository at this point in the history
close #5320
Co-authored-by: Zoltan Kochan <z@kochan.io>
  • Loading branch information
await-ovo committed Sep 15, 2022
1 parent 9b44d38 commit b93a21b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/blue-carrots-think.md
@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-patching": patch
"pnpm": patch
---

`pnpm patch` should work on files that don't have an end of line [#5320](https://github.com/pnpm/pnpm/issues/5320).
1 change: 1 addition & 0 deletions packages/plugin-commands-patching/src/patchCommit.ts
Expand Up @@ -98,4 +98,5 @@ async function diffFolders (folderA: string, folderB: string) {
.replace(new RegExp(`(a|b)${escapeStringRegexp(`/${normalizePath(folderBN)}/`)}`, 'g'), '$1/')
.replace(new RegExp(escapeStringRegexp(`${folderAN}/`), 'g'), '')
.replace(new RegExp(escapeStringRegexp(`${folderBN}/`), 'g'), '')
.replace(/\n\\ No newline at end of file$/, '')
}
77 changes: 77 additions & 0 deletions packages/plugin-commands-patching/test/patch.test.ts
Expand Up @@ -94,3 +94,80 @@ describe('patch and commit', () => {
.rejects.toThrow(`The target directory already exists: '${editDir}'`)
})
})

describe('patching should work when there is a no EOL in the patched file', () => {
let defaultPatchOption: patch.PatchCommandOptions

beforeEach(() => {
prepare({
dependencies: {
'safe-execa': '0.1.2',
},
})

const cacheDir = path.resolve('cache')
const storeDir = path.resolve('store')

defaultPatchOption = {
cacheDir,
dir: process.cwd(),
pnpmHomeDir: '',
rawConfig: {
registry: `http://localhost:${REGISTRY_MOCK_PORT}/`,
},
registries: { default: `http://localhost:${REGISTRY_MOCK_PORT}/` },
storeDir,
userConfig: {},
}
})
it('should work when adding content on a newline', async () => {
const output = await patch.handler(defaultPatchOption, ['safe-execa@0.1.2'])
const userPatchDir = output.substring(output.indexOf(':') + 1).trim()
const tempDir = os.tmpdir()

expect(userPatchDir).toContain(tempDir)
expect(fs.existsSync(userPatchDir)).toBe(true)
expect(fs.existsSync(path.join(userPatchDir, 'lib/index.js'))).toBe(true)

fs.appendFileSync(path.join(userPatchDir, 'lib/index.js'), '\n// test patching', 'utf8')

await patchCommit.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, [userPatchDir])

const { manifest } = await readProjectManifest(process.cwd())
expect(manifest.pnpm?.patchedDependencies).toStrictEqual({
'safe-execa@0.1.2': 'patches/safe-execa@0.1.2.patch',
})
const patchContent = fs.readFileSync('patches/safe-execa@0.1.2.patch', 'utf8')
expect(patchContent).toContain('diff --git')
expect(patchContent).toContain('// test patching')
expect(patchContent).not.toContain('No newline at end of file')
expect(fs.readFileSync('node_modules/safe-execa/lib/index.js', 'utf8')).toContain('// test patching')
})
it('should work fine when new content is appended', async () => {
const output = await patch.handler(defaultPatchOption, ['safe-execa@0.1.2'])
const userPatchDir = output.substring(output.indexOf(':') + 1).trim()
const tempDir = os.tmpdir()

expect(userPatchDir).toContain(tempDir)
expect(fs.existsSync(userPatchDir)).toBe(true)
expect(fs.existsSync(path.join(userPatchDir, 'lib/index.js'))).toBe(true)

fs.appendFileSync(path.join(userPatchDir, 'lib/index.js'), '// patch without newline', 'utf8')

await patchCommit.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, [userPatchDir])

const { manifest } = await readProjectManifest(process.cwd())
expect(manifest.pnpm?.patchedDependencies).toStrictEqual({
'safe-execa@0.1.2': 'patches/safe-execa@0.1.2.patch',
})
const patchContent = fs.readFileSync('patches/safe-execa@0.1.2.patch', 'utf8')
expect(patchContent).toContain('No newline at end of file')
expect(fs.readFileSync('node_modules/safe-execa/lib/index.js', 'utf8')).toContain('//# sourceMappingURL=index.js.map// patch without newline')
})
})

0 comments on commit b93a21b

Please sign in to comment.