aboutsummaryrefslogtreecommitdiff
path: root/ref/store/files/helpers_test.go
blob: c46cc9fcecc755762f1a77818d57fff8fcb7558c (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package files_test

import (
	"os"
	"slices"
	"strings"
	"testing"
	"time"

	"codeberg.org/lindenii/furgit/internal/testgit"
	objectid "codeberg.org/lindenii/furgit/object/id"
	"codeberg.org/lindenii/furgit/ref/store/files"
)

const testPackedRefsTimeout = time.Second

func openFilesStore(t *testing.T, testRepo *testgit.TestRepo, algo objectid.Algorithm) *files.Store {
	t.Helper()

	root := testRepo.OpenGitRoot(t)

	store, err := files.New(root, algo, testPackedRefsTimeout)
	if err != nil {
		t.Fatalf("files.New: %v", err)
	}

	return store
}

func openFilesStoreAt(t *testing.T, root *os.Root, algo objectid.Algorithm) *files.Store {
	t.Helper()

	store, err := files.New(root, algo, testPackedRefsTimeout)
	if err != nil {
		t.Fatalf("files.New: %v", err)
	}

	return store
}

func openGitRootUnder(t *testing.T, repoRoot *os.Root, worktreeName string) *os.Root {
	t.Helper()

	worktreeRoot, err := repoRoot.OpenRoot(worktreeName)
	if err != nil {
		t.Fatalf("OpenRoot(%q): %v", worktreeName, err)
	}

	t.Cleanup(func() {
		_ = worktreeRoot.Close()
	})

	info, err := worktreeRoot.Stat(".git")
	if err != nil {
		t.Fatalf("stat %q: %v", worktreeName+"/.git", err)
	}

	if info.IsDir() {
		gitRoot, err := worktreeRoot.OpenRoot(".git")
		if err != nil {
			t.Fatalf("OpenRoot(.git): %v", err)
		}

		t.Cleanup(func() {
			_ = gitRoot.Close()
		})

		return gitRoot
	}

	content, err := worktreeRoot.ReadFile(".git")
	if err != nil {
		t.Fatalf("read %q: %v", worktreeName+"/.git", err)
	}

	gitDir := strings.TrimSpace(strings.TrimPrefix(string(content), "gitdir:"))
	if gitDir == "" {
		t.Fatalf("%q does not contain a gitdir path", worktreeName+"/.git")
	}

	if strings.HasPrefix(gitDir, "/") {
		gitRoot, err := os.OpenRoot(gitDir)
		if err != nil {
			t.Fatalf("os.OpenRoot(%q): %v", gitDir, err)
		}

		t.Cleanup(func() {
			_ = gitRoot.Close()
		})

		return gitRoot
	}

	gitRoot, err := worktreeRoot.OpenRoot(gitDir)
	if err != nil {
		t.Fatalf("os.OpenRoot(%q): %v", gitDir, err)
	}

	t.Cleanup(func() {
		_ = gitRoot.Close()
	})

	return gitRoot
}

func assertListMatchesGitForEachRef(t *testing.T, gitOut string, store *files.Store) {
	t.Helper()

	listed, err := store.List("")
	if err != nil {
		t.Fatalf("List(\"\"): %v", err)
	}

	gotNames := make([]string, 0, len(listed))
	for _, got := range listed {
		if got.Name() == "HEAD" {
			continue
		}

		gotNames = append(gotNames, got.Name())
	}

	slices.Sort(gotNames)

	wantLines := strings.Split(strings.TrimSpace(gitOut), "\n")
	wantNames := make([]string, 0, len(wantLines))

	for _, line := range wantLines {
		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}

		wantNames = append(wantNames, line)
	}

	slices.Sort(wantNames)

	if !slices.Equal(gotNames, wantNames) {
		t.Fatalf("List names = %v, want %v", gotNames, wantNames)
	}
}

func forEachRefLines(output string) []string {
	if strings.TrimSpace(output) == "" {
		return nil
	}

	return strings.Split(strings.TrimSpace(output), "\n")
}