This commit is contained in:
2026-07-31 06:52:09 +08:00
parent 532f9e09b5
commit 0d078e848c
6 changed files with 208 additions and 50 deletions
+24 -5
View File
@@ -285,15 +285,20 @@ describe('deleteLocalBranch', () => {
})
it('treats a missing local branch as already deleted', async () => {
execFile.mockImplementationOnce((_cmd, _args, _opts, cb) => {
cb(new Error('missing'), '', '')
})
// heads 不存在 + remote-tracking 也不存在
execFile
.mockImplementationOnce((_cmd, _args, _opts, cb) => {
cb(new Error('missing'), '', '')
})
.mockImplementationOnce((_cmd, _args, _opts, cb) => {
cb(new Error('missing remote'), '', '')
})
const { deleteLocalBranch } = await import('../../src/git/branchSync.js')
const result = await deleteLocalBranch('/repo', 'feature/test')
expect(result).toEqual({ ok: true, stdout: '', stderr: '' })
expect(execFile).toHaveBeenCalledOnce()
expect(execFile).toHaveBeenCalledTimes(2)
expect(execFile.mock.calls[0][1]).toEqual([
'-C',
'/repo',
@@ -302,22 +307,36 @@ describe('deleteLocalBranch', () => {
'--quiet',
'refs/heads/feature/test',
])
expect(execFile.mock.calls[1][1]).toEqual([
'-C',
'/repo',
'show-ref',
'--verify',
'--quiet',
'refs/remotes/origin/feature/test',
])
})
it('deletes an existing local branch with git args instead of shell interpolation', async () => {
execFile
// show-ref heads: exists
.mockImplementationOnce((_cmd, _args, _opts, cb) => {
cb(null, '', '')
})
// branch -D
.mockImplementationOnce((_cmd, _args, _opts, cb) => {
cb(null, 'deleted', '')
})
// show-ref remotes: missing
.mockImplementationOnce((_cmd, _args, _opts, cb) => {
cb(new Error('missing remote'), '', '')
})
const { deleteLocalBranch } = await import('../../src/git/branchSync.js')
const result = await deleteLocalBranch('/repo', 'feature/weird name;rm -rf')
expect(result).toEqual({ ok: true, stdout: 'deleted', stderr: '' })
expect(execFile).toHaveBeenCalledTimes(2)
expect(execFile).toHaveBeenCalledTimes(3)
expect(execFile.mock.calls[1][1]).toEqual([
'-C',
'/repo',