MCPcopy Index your code
hub / github.com/coder/coder / TestScanDeletedWorktreeGitdirEmitsRemoved

Function TestScanDeletedWorktreeGitdirEmitsRemoved

agent/agentgit/agentgit_test.go:549–600  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

547}
548
549func TestScanDeletedWorktreeGitdirEmitsRemoved(t *testing.T) {
550 t.Parallel()
551
552 // Set up a main repo that we'll use as the source for a worktree.
553 mainRepoDir := initTestRepo(t)
554
555 // Create a linked worktree using git CLI.
556 // Resolve symlinks and short (8.3) names on Windows so test
557 // expectations match the canonical paths returned by git.
558 wtBase := testutil.TempDirResolved(t)
559 worktreeDir := filepath.Join(wtBase, "wt")
560 gitCmd(t, mainRepoDir, "branch", "worktree-branch")
561 gitCmd(t, mainRepoDir, "worktree", "add", worktreeDir, "worktree-branch")
562
563 logger := slogtest.Make(t, nil)
564 h := agentgit.NewHandler(logger)
565
566 // Create a dirty file so the initial scan has something to report.
567 dirtyFile := filepath.Join(worktreeDir, "dirty.go")
568 require.NoError(t, os.WriteFile(dirtyFile, []byte("package dirty\n"), 0o600))
569
570 h.Subscribe([]string{dirtyFile})
571 ctx := context.Background()
572
573 // Initial scan should succeed.
574 msg1 := h.Scan(ctx)
575 require.NotNil(t, msg1)
576 require.Len(t, msg1.Repositories, 1)
577 require.False(t, msg1.Repositories[0].Removed)
578
579 // Now delete the target gitdir inside .git/worktrees/. The .git
580 // file in the worktree still exists, but it points to a directory
581 // that is gone.
582 gitdirPath := filepath.Join(mainRepoDir, ".git", "worktrees", filepath.Base(worktreeDir))
583 require.NoError(t, os.RemoveAll(gitdirPath))
584
585 // Verify the .git file still exists (this is the bug scenario).
586 _, err := os.Stat(filepath.Join(worktreeDir, ".git"))
587 require.NoError(t, err, ".git file should still exist")
588
589 // Next scan should detect the broken worktree and emit removal.
590 msg2 := h.Scan(ctx)
591 require.NotNil(t, msg2)
592 require.Len(t, msg2.Repositories, 1)
593 require.True(t, msg2.Repositories[0].Removed,
594 "worktree with deleted gitdir should be marked as removed")
595 require.Equal(t, worktreeDir, msg2.Repositories[0].RepoRoot)
596
597 // Repo should be evicted — subsequent scan returns nil.
598 msg3 := h.Scan(ctx)
599 require.Nil(t, msg3, "evicted worktree should not appear in subsequent scans")
600}
601
602func TestScanTransientErrorDoesNotRemoveRepo(t *testing.T) {
603 t.Parallel()

Callers

nothing calls this directly

Calls 10

SubscribeMethod · 0.95
ScanMethod · 0.95
TempDirResolvedFunction · 0.92
NewHandlerFunction · 0.92
initTestRepoFunction · 0.85
gitCmdFunction · 0.85
RemoveAllMethod · 0.80
WriteFileMethod · 0.65
LenMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected