Skip to content

Commit 97202bd

Browse files
committed
Fix tests by mocking rg
1 parent 1a7fa65 commit 97202bd

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/services/checkpoints/__tests__/ShadowCheckpointService.test.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { EventEmitter } from "events"
88
import { simpleGit, SimpleGit } from "simple-git"
99

1010
import { fileExistsAtPath } from "../../../utils/fs"
11+
import * as fileSearch from "../../../services/search/file-search"
1112

1213
import { RepoPerTaskCheckpointService } from "../RepoPerTaskCheckpointService"
1314

@@ -418,28 +419,49 @@ describe.each([[RepoPerTaskCheckpointService, "RepoPerTaskCheckpointService"]])(
418419
expect(await fileExistsAtPath(nestedGitDir)).toBe(true)
419420
expect(await fileExistsAtPath(nestedGitDisabledDir)).toBe(false)
420421

421-
// Create a spy on fs.rename to track when it's called.
422422
const renameSpy = jest.spyOn(fs, "rename")
423423

424-
// Initialize the shadow git service.
425-
const service = new klass(taskId, shadowDir, workspaceDir, () => {})
424+
jest.spyOn(fileSearch, "executeRipgrep").mockImplementation(({ args }) => {
425+
const searchPattern = args[4]
426+
427+
if (searchPattern.includes(".git/HEAD")) {
428+
return Promise.resolve([
429+
{
430+
path: path.relative(workspaceDir, nestedGitDir),
431+
type: "folder",
432+
label: ".git",
433+
},
434+
])
435+
} else {
436+
return Promise.resolve([])
437+
}
438+
})
426439

427-
// Initialize the shadow git repo.
440+
const service = new klass(taskId, shadowDir, workspaceDir, () => {})
428441
await service.initShadowGit()
429442

430443
// Verify rename was called with correct paths.
431-
expect(renameSpy.mock.calls).toHaveLength(2)
444+
expect(renameSpy.mock.calls).toHaveLength(1)
432445
expect(renameSpy.mock.calls[0][0]).toBe(nestedGitDir)
433446
expect(renameSpy.mock.calls[0][1]).toBe(nestedGitDisabledDir)
434-
expect(renameSpy.mock.calls[1][0]).toBe(nestedGitDisabledDir)
435-
expect(renameSpy.mock.calls[1][1]).toBe(nestedGitDir)
447+
448+
jest.spyOn(require("../../../utils/fs"), "fileExistsAtPath").mockImplementation((path) => {
449+
if (path === nestedGitDir) {
450+
return Promise.resolve(true)
451+
} else if (path === nestedGitDisabledDir) {
452+
return Promise.resolve(false)
453+
}
454+
455+
return Promise.resolve(false)
456+
})
436457

437458
// Verify the nested git directory is back to normal after initialization.
438459
expect(await fileExistsAtPath(nestedGitDir)).toBe(true)
439460
expect(await fileExistsAtPath(nestedGitDisabledDir)).toBe(false)
440461

441462
// Clean up.
442463
renameSpy.mockRestore()
464+
jest.restoreAllMocks()
443465
await fs.rm(shadowDir, { recursive: true, force: true })
444466
await fs.rm(workspaceDir, { recursive: true, force: true })
445467
})

0 commit comments

Comments
 (0)