summaryrefslogtreecommitdiff
path: root/internal/git/git_test.go
blob: fa1f8f71d41deed65e73d04fe812ffe6432511ea (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package git_test

import (
	"context"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"

	"codeberg.org/snonux/foostore/internal/git"
)

// initRepo creates a temporary git repository with a minimal config so that
// git commit works without requiring the user's global identity to be set.
func initRepo(t *testing.T) string {
	t.Helper()
	dir := t.TempDir()

	for _, args := range [][]string{
		{"git", "init", dir},
		{"git", "-C", dir, "config", "user.email", "test@geheim.test"},
		{"git", "-C", dir, "config", "user.name", "Geheim Test"},
	} {
		out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
		if err != nil {
			t.Fatalf("setup %v: %v\n%s", args, err, out)
		}
	}

	return dir
}

// writeFile creates a file with the given content inside dir.
func writeFile(t *testing.T, dir, name, content string) string {
	t.Helper()
	path := filepath.Join(dir, name)
	if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
		t.Fatalf("writeFile %s: %v", path, err)
	}
	return path
}

// gitOutput runs a raw git command in dir and returns trimmed stdout+stderr.
func gitOutput(t *testing.T, dir string, args ...string) string {
	t.Helper()
	args = append([]string{"-C", dir}, args...)
	out, err := exec.Command("git", args...).CombinedOutput()
	if err != nil {
		t.Fatalf("git %v: %v\n%s", args, err, out)
	}
	return strings.TrimSpace(string(out))
}

// commitAll stages everything in dir and creates a commit so subsequent
// operations have a baseline history to work against.
func commitAll(t *testing.T, dir, msg string) {
	t.Helper()
	for _, args := range [][]string{
		{"git", "-C", dir, "add", "."},
		{"git", "-C", dir, "commit", "-m", msg},
	} {
		out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
		if err != nil {
			t.Fatalf("commitAll %v: %v\n%s", args, err, out)
		}
	}
}

// TestAdd verifies that Add stages a file so git status reports it as a new file.
func TestAdd(t *testing.T) {
	dir := initRepo(t)
	g := git.New(dir)
	ctx := context.Background()

	path := writeFile(t, dir, "secret.age", "encrypted data")

	if err := g.Add(ctx, path); err != nil {
		t.Fatalf("Add: %v", err)
	}

	status := gitOutput(t, dir, "status", "--short")
	if !strings.Contains(status, "A  secret.age") {
		t.Errorf("expected 'A  secret.age' in status, got: %q", status)
	}
}

// TestRemove verifies that Remove stages a file deletion (git rm) so the file
// disappears from the index after a committed baseline.
func TestRemove(t *testing.T) {
	dir := initRepo(t)
	g := git.New(dir)
	ctx := context.Background()

	path := writeFile(t, dir, "secret.age", "encrypted data")
	commitAll(t, dir, "initial commit")

	if err := g.Remove(ctx, path); err != nil {
		t.Fatalf("Remove: %v", err)
	}

	status := gitOutput(t, dir, "status", "--short")
	if !strings.Contains(status, "D  secret.age") {
		t.Errorf("expected 'D  secret.age' in status, got: %q", status)
	}
}

// TestCommit verifies that Commit records changes with the hardcoded message.
func TestCommit(t *testing.T) {
	dir := initRepo(t)
	g := git.New(dir)
	ctx := context.Background()

	writeFile(t, dir, "secret.age", "encrypted data")
	// Stage the file so there is something to commit.
	if out, err := exec.Command("git", "-C", dir, "add", ".").CombinedOutput(); err != nil {
		t.Fatalf("git add: %v\n%s", err, out)
	}

	if err := g.Commit(ctx); err != nil {
		t.Fatalf("Commit: %v", err)
	}

	log := gitOutput(t, dir, "log", "--oneline", "-1")
	const want = "Changing stuff, not telling what in commit history"
	if !strings.Contains(log, want) {
		t.Errorf("expected commit message %q in log, got: %q", want, log)
	}
}

// TestStatus verifies that Status runs without error on a clean repository.
func TestStatus(t *testing.T) {
	dir := initRepo(t)
	g := git.New(dir)
	ctx := context.Background()

	if err := g.Status(ctx); err != nil {
		t.Fatalf("Status: %v", err)
	}
}

// TestReset verifies that Reset discards uncommitted working-tree changes.
func TestReset(t *testing.T) {
	dir := initRepo(t)
	g := git.New(dir)
	ctx := context.Background()

	path := writeFile(t, dir, "secret.age", "original")
	commitAll(t, dir, "initial commit")

	// Overwrite the file so there is a dirty working tree.
	if err := os.WriteFile(path, []byte("modified"), 0o600); err != nil {
		t.Fatalf("overwrite: %v", err)
	}

	if err := g.Reset(ctx); err != nil {
		t.Fatalf("Reset: %v", err)
	}

	got, err := os.ReadFile(path)
	if err != nil {
		t.Fatalf("ReadFile after Reset: %v", err)
	}
	if string(got) != "original" {
		t.Errorf("expected file content 'original' after reset, got: %q", got)
	}
}

// TestAdd_nonexistent_file verifies that Add returns an error when the target
// file does not exist, because git add will fail.
func TestAdd_nonexistent_file(t *testing.T) {
	dir := initRepo(t)
	g := git.New(dir)
	ctx := context.Background()

	err := g.Add(ctx, filepath.Join(dir, "does-not-exist.age"))
	if err == nil {
		t.Fatal("expected error when adding a nonexistent file, got nil")
	}
}

// TestCommit_nothing_to_commit verifies that Commit returns an error (exit 1
// from git) when there is nothing staged, rather than panicking or crashing.
// Callers are expected to guard against this case, but the package must not hide
// the error entirely.
func TestCommit_nothing_to_commit(t *testing.T) {
	dir := initRepo(t)
	g := git.New(dir)
	ctx := context.Background()

	// Create and commit a file so HEAD exists; index is now clean.
	writeFile(t, dir, "secret.age", "data")
	commitAll(t, dir, "initial commit")

	// Nothing staged — git commit should exit non-zero.
	err := g.Commit(ctx)
	if err == nil {
		t.Fatal("expected error when committing with nothing to commit, got nil")
	}
}

// TestRemove_nonexistent_file verifies that Remove returns an error when the
// target is not tracked by git, because git rm exits non-zero in that case.
func TestRemove_nonexistent_file(t *testing.T) {
	dir := initRepo(t)
	g := git.New(dir)
	ctx := context.Background()

	err := g.Remove(ctx, filepath.Join(dir, "ghost.age"))
	if err == nil {
		t.Fatal("expected error when removing a non-tracked file, got nil")
	}
}

// TestSync verifies the pull-push-status loop using two local repos so no
// real network is needed. A bare repo acts as the remote; a working repo
// with an initial commit pushes to it, then Sync pulls and pushes again.
func TestSync(t *testing.T) {
	ctx := context.Background()

	runcmd := func(args ...string) {
		t.Helper()
		out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
		if err != nil {
			t.Fatalf("%v: %v\n%s", args, err, out)
		}
	}

	// Create a working directory with an initial commit on master.
	workDir := t.TempDir()
	runcmd("git", "init", "--initial-branch=master", workDir)
	runcmd("git", "-C", workDir, "config", "user.email", "test@geheim.test")
	runcmd("git", "-C", workDir, "config", "user.name", "Geheim Test")
	path := filepath.Join(workDir, "init.txt")
	if err := os.WriteFile(path, []byte("init"), 0o600); err != nil {
		t.Fatalf("WriteFile: %v", err)
	}
	runcmd("git", "-C", workDir, "add", ".")
	runcmd("git", "-C", workDir, "commit", "-m", "init")

	// Create a bare repo and push the initial commit into it so master exists.
	bareDir := t.TempDir()
	runcmd("git", "init", "--bare", "--initial-branch=master", bareDir)
	runcmd("git", "-C", workDir, "remote", "add", "localremote", bareDir)
	runcmd("git", "-C", workDir, "push", "localremote", "master")

	g := git.New(workDir)
	if err := g.Sync(ctx, []string{"localremote"}); err != nil {
		t.Fatalf("Sync: %v", err)
	}
}

// TestSync_bad_remote verifies that Sync returns an error when a configured
// remote does not exist, rather than silently succeeding.
func TestSync_bad_remote(t *testing.T) {
	dir := initRepo(t)
	// Create an initial commit so the repo has a valid HEAD.
	writeFile(t, dir, "init.txt", "init")
	commitAll(t, dir, "init")

	g := git.New(dir)
	err := g.Sync(context.Background(), []string{"nonexistent-remote"})
	if err == nil {
		t.Fatal("expected error when syncing with a nonexistent remote, got nil")
	}
}