Skip to content

Commit 831f28d

Browse files
e-kulikovclaude
andcommitted
test(segment): build bare layout fixtures from TestRootPath
TestEnabledInBareLayout and TestIsBareRepoResolvesPointer hardcoded "/repo" instead of following the platform-aware TestRootPath convention every other test in the file uses, which failed the windows-latest run. filepath.IsAbs is the real one, not the mocked GOOS, so on a Windows runner "/repo/.bare" has no volume name and IsAbs returns false. Checked against go1.26.5 internal/filepathlite: volumeNameLen("/repo/.bare") is 0, while "C:/repo/.bare" gives 2 and the remainder starts with a separator, so both asserted fields are absolute on either platform now. TestIsBareRepoResolvesPointer did not fail, but only by accident: "/repo/.bare" took the Windows disk-relative branch of resolveGitPath and landed on the same string, so its "absolute pointer" case never exercised the absolute branch it names. Its expected config path and the path the old filepath.Join produced are now derived from the pointer instead of written out, so they cannot drift apart per platform. Field order in the case struct follows fieldalignment, which CI runs separately from golangci-lint. Its -fix pass drops comments, so the field documentation is restored by hand. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 195dee5 commit 831f28d

1 file changed

Lines changed: 58 additions & 41 deletions

File tree

src/segments/git_test.go

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,15 @@ func TestResolveEmptyGitPath(t *testing.T) {
109109
}
110110

111111
func TestEnabledInWorktree(t *testing.T) {
112+
// Field order below is what fieldalignment wants, so the groups a reader would expect
113+
// together are not adjacent. The comments travel with the fields instead.
112114
cases := []struct {
113-
Pointer string
114-
RawGitFile string
115-
// For a worktree topology, the discovered parent is the worktree root and
116-
// must match the metadata back-reference.
117-
DiscoveredGitFile string
115+
// nil reserves directory-role assertions for a later decision.
116+
ExpectedWorkingFolder *string
117+
ExpectedRootFolder *string
118+
ExpectedRealFolder *string
119+
// For a worktree topology, the discovered parent is the worktree root and must
120+
// match the metadata back-reference. See DiscoveredGitFile below.
118121
DiscoveredParent string
119122
MetadataAddon string
120123
MetadataContent string
@@ -124,12 +127,16 @@ func TestEnabledInWorktree(t *testing.T) {
124127
// from a --separate-git-dir target, which has no core.worktree at all.
125128
TargetConfig string
126129
Case string
127-
// nil reserves directory-role assertions for a later decision.
128-
ExpectedWorkingFolder *string
129-
ExpectedRealFolder *string
130-
ExpectedRootFolder *string
131-
ExpectedIsWorkTree bool
132-
ExpectedEnabled bool
130+
Pointer string
131+
// DiscoveredGitFile is the .git file HasParentFilePath found, whose parent is
132+
// DiscoveredParent above.
133+
DiscoveredGitFile string
134+
// RawGitFile overrides the whole .git file body. Use it when the test is about
135+
// the file's syntax rather than the pointer it carries; leave it empty and the
136+
// loop writes "gitdir: <Pointer>".
137+
RawGitFile string
138+
ExpectedIsWorkTree bool
139+
ExpectedEnabled bool
133140
}{
134141
{
135142
Case: "worktree",
@@ -489,9 +496,14 @@ func TestEnabledInBareLayout(t *testing.T) {
489496
}
490497

491498
for _, tc := range cases {
499+
// Fixtures are built from TestRootPath so the paths are absolute on the platform
500+
// the test binary runs on. filepath.IsAbs below is the real one, not the mocked
501+
// GOOS, so a literal "/repo" would not be absolute on Windows.
502+
root := TestRootPath + "repo"
503+
492504
fileInfo := &runtime.FileInfo{
493-
Path: "/repo/.git",
494-
ParentFolder: "/repo",
505+
Path: root + "/.git",
506+
ParentFolder: root,
495507
}
496508

497509
env := new(mock.Environment)
@@ -504,13 +516,13 @@ func TestEnabledInBareLayout(t *testing.T) {
504516
env.On("Home").Return(poshHome)
505517
env.On("Getenv", poshGitEnv).Return("")
506518
env.On("DirMatchesOneOf", testify_.Anything, testify_.Anything).Return(false)
507-
env.On("FileContent", "/repo/.git").Return("gitdir: ./.bare")
508-
env.On("HasFilesInDir", "/repo/.bare", "HEAD").Return(true)
509-
env.On("FileContent", "/repo/.bare/config").Return("[core]\n\tbare = true")
510-
env.On("FileContent", "/repo//HEAD").Return("")
511-
env.MockGitCommand("/repo/", "1234567890abcdef1234567890abcdef12345678", "rev-parse", "HEAD")
512-
env.MockGitCommand("/repo/", "", "describe", "--tags", "--exact-match")
513-
env.MockGitCommand("/repo/", "", "remote")
519+
env.On("FileContent", root+"/.git").Return("gitdir: ./.bare")
520+
env.On("HasFilesInDir", root+"/.bare", "HEAD").Return(true)
521+
env.On("FileContent", root+"/.bare/config").Return("[core]\n\tbare = true")
522+
env.On("FileContent", root+"//HEAD").Return("")
523+
env.MockGitCommand(root+"/", "1234567890abcdef1234567890abcdef12345678", "rev-parse", "HEAD")
524+
env.MockGitCommand(root+"/", "", "describe", "--tags", "--exact-match")
525+
env.MockGitCommand(root+"/", "", "remote")
514526

515527
props := options.Map{}
516528
if tc.FetchBareInfo {
@@ -528,67 +540,72 @@ func TestEnabledInBareLayout(t *testing.T) {
528540
}
529541

530542
func TestIsBareRepoResolvesPointer(t *testing.T) {
543+
// Fixtures are built from TestRootPath so that "absolute pointer" really is absolute
544+
// on the platform the test binary runs on. A literal "/repo/.bare" is only
545+
// disk-relative on Windows, which would exercise a different resolveGitPath branch
546+
// than the one these cases are about.
547+
root := TestRootPath + "repo"
548+
531549
cases := []struct {
532550
Case string
533551
Pointer string
534-
ExpectedConfig string
535552
ExpectedProbedDir string
536553
ExpectedIsBare bool
537-
OldConfig string
538554
}{
539555
{
540556
Case: "relative pointer",
541557
Pointer: "./.bare",
542-
ExpectedConfig: "/repo/.bare/config",
543-
ExpectedProbedDir: "/repo/.bare",
558+
ExpectedProbedDir: root + "/.bare",
544559
ExpectedIsBare: true,
545560
},
546561
{
547562
Case: "absolute pointer",
548-
Pointer: "/repo/.bare",
549-
ExpectedConfig: "/repo/.bare/config",
550-
ExpectedProbedDir: "/repo/.bare",
563+
Pointer: root + "/.bare",
564+
ExpectedProbedDir: root + "/.bare",
551565
ExpectedIsBare: true,
552-
OldConfig: "/repo/repo/.bare/config",
553566
},
554567
{
555568
Case: "absolute pointer to a non-bare git dir",
556-
Pointer: "/elsewhere/gitdir",
557-
ExpectedConfig: "/elsewhere/gitdir/config",
558-
ExpectedProbedDir: "/elsewhere/gitdir",
569+
Pointer: TestRootPath + "elsewhere/gitdir",
570+
ExpectedProbedDir: TestRootPath + "elsewhere/gitdir",
559571
ExpectedIsBare: false,
560-
OldConfig: "/repo/elsewhere/gitdir/config",
561572
},
562573
}
563574

564575
for _, tc := range cases {
565576
fileInfo := &runtime.FileInfo{
566-
Path: "/repo/.git",
567-
ParentFolder: "/repo",
577+
Path: root + "/.git",
578+
ParentFolder: root,
568579
}
569580

581+
expectedConfig := tc.ExpectedProbedDir + "/config"
582+
// What the old filepath.Join produced: the same path for a relative pointer, and
583+
// the pointer concatenated onto the parent for an absolute one. Derived rather
584+
// than written out so it stays correct under Windows path semantics too.
585+
oldConfig := filepath.Join(fileInfo.ParentFolder, tc.Pointer) + "/config"
586+
570587
env := new(mock.Environment)
571588
env.On("InWSLSharedDrive").Return(false)
572589
env.On("HasCommand", "git").Return(true)
573590
env.On("GOOS").Return("")
574591
env.On("HasParentFilePath", ".git", true).Return(fileInfo, nil)
575-
env.On("FileContent", "/repo/.git").Return(fmt.Sprintf("gitdir: %s", tc.Pointer))
576-
env.On("FileContent", tc.ExpectedConfig).Return(fmt.Sprintf("[core]\n\tbare = %t", tc.ExpectedIsBare))
592+
env.On("FileContent", root+"/.git").Return(fmt.Sprintf("gitdir: %s", tc.Pointer))
593+
env.On("FileContent", expectedConfig).Return(fmt.Sprintf("[core]\n\tbare = %t", tc.ExpectedIsBare))
577594
env.On("HasFilesInDir", tc.ExpectedProbedDir, "HEAD").Return(true)
578595

579-
if tc.OldConfig != "" {
580-
env.On("FileContent", tc.OldConfig).Return("")
596+
if oldConfig != expectedConfig {
597+
env.On("FileContent", oldConfig).Return("")
581598
}
582599

583600
g := &Git{}
584601
g.Init(options.Map{FetchBareInfo: true}, env)
585602

586603
assert.True(t, g.shouldDisplay(), tc.Case)
587604
assert.Equal(t, tc.ExpectedIsBare, g.IsBare, tc.Case)
588-
env.AssertCalled(t, "FileContent", tc.ExpectedConfig)
605+
env.AssertCalled(t, "FileContent", expectedConfig)
589606

590-
if tc.OldConfig != "" {
591-
env.AssertNotCalled(t, "FileContent", tc.OldConfig)
607+
if oldConfig != expectedConfig {
608+
env.AssertNotCalled(t, "FileContent", oldConfig)
592609
}
593610
}
594611
}

0 commit comments

Comments
 (0)