BlueGit

timfa bluegit

Files

bluegit / internal / gitrepo / repo_test.go

repo_test.go

feat: voeg een tagspagina per repository toe
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
2ec9a856 History
repo_test.go 4.7 KiB Download Raw
1package gitrepo
2
3import (
4 "bytes"
5 "context"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "gitlab.com/Timfa/bluegit/internal/access"
12)
13
14func TestEvaluatePush(t *testing.T) {
15 var buf bytes.Buffer
16 buf.WriteString("0000000000000000000000000000000000000000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa refs/heads/main\n")
17 err := EvaluatePush(access.RoleDeveloper, []string{"main"}, nil, &buf)
18 if err == nil {
19 t.Fatal("ontwikkelaar mag main niet")
20 }
21 buf.Reset()
22 buf.WriteString("0000000000000000000000000000000000000000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa refs/heads/feature\n")
23 if err := EvaluatePush(access.RoleDeveloper, []string{"main"}, nil, &buf); err != nil {
24 t.Fatal(err)
25 }
26 if err := EvaluatePush(access.RoleMaintainer, nil, nil, strings.NewReader("")); err == nil {
27 t.Fatal("lege push")
28 }
29 buf.Reset()
30 buf.WriteString("0000000000000000000000000000000000000000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa refs/tags/v1\n")
31 if err := EvaluatePush(access.RoleDeveloper, nil, []string{"v*"}, &buf); err == nil {
32 t.Fatal("ontwikkelaar mag v1 niet")
33 }
34}
35
36func TestTouchAndStats(t *testing.T) {
37 if _, err := os.Stat("/usr/bin/git"); err != nil {
38 t.Skip("git ontbreekt")
39 }
40 ctx := context.Background()
41 dir := filepath.Join(t.TempDir(), "demo.git")
42 if err := Init("git", dir, "master", "/bin/true"); err != nil {
43 t.Fatal(err)
44 }
45 work := t.TempDir()
46 runGit := func(args ...string) {
47 t.Helper()
48 cmd := execCommand(t, work, args...)
49 if cmd != nil {
50 t.Fatal(cmd)
51 }
52 }
53 runGit("init", "-b", "master")
54 runGit("config", "user.email", "tim@example.com")
55 runGit("config", "user.name", "tim")
56 if err := os.WriteFile(filepath.Join(work, "README.md"), []byte("# Hallo\n"), 0o644); err != nil {
57 t.Fatal(err)
58 }
59 if err := os.MkdirAll(filepath.Join(work, "src"), 0o755); err != nil {
60 t.Fatal(err)
61 }
62 if err := os.WriteFile(filepath.Join(work, "src", "main.go"), []byte("package main\n"), 0o644); err != nil {
63 t.Fatal(err)
64 }
65 runGit("add", ".")
66 runGit("commit", "-m", "eerste")
67 if _, err := run(ctx, "git", nil, "-C", work, "remote", "add", "origin", dir); err != nil {
68 t.Fatal(err)
69 }
70 if _, err := run(ctx, "git", nil, "-C", work, "push", "origin", "master"); err != nil {
71 t.Fatal(err)
72 }
73 touch, err := Touch(ctx, "git", dir, "master")
74 if err != nil {
75 t.Fatal(err)
76 }
77 if touch["README.md"].Subject != "eerste" || touch["src"].Subject != "eerste" || touch["src/main.go"].Subject != "eerste" {
78 t.Fatalf("%v", touch)
79 }
80 commits, branches, tags, err := Stats(ctx, "git", dir, "master")
81 if err != nil || commits != 1 || branches != 1 || tags != 0 {
82 t.Fatalf("stats %d %d %d %v", commits, branches, tags, err)
83 }
84}
85
86func execCommand(t *testing.T, dir string, args ...string) error {
87 t.Helper()
88 _, err := run(context.Background(), "git", nil, append([]string{"-C", dir}, args...)...)
89 return err
90}
91
92func TestCommitsSince(t *testing.T) {
93 if _, err := os.Stat("/usr/bin/git"); err != nil {
94 t.Skip("git ontbreekt")
95 }
96 ctx := context.Background()
97 dir := filepath.Join(t.TempDir(), "demo.git")
98 if err := Init("git", dir, "master", "/bin/true"); err != nil {
99 t.Fatal(err)
100 }
101 work := t.TempDir()
102 runGit := func(args ...string) {
103 t.Helper()
104 if err := execCommand(t, work, args...); err != nil {
105 t.Fatal(err)
106 }
107 }
108 runGit("init", "-b", "master")
109 runGit("config", "user.email", "tim@users.git.timfalken.com")
110 runGit("config", "user.name", "tim")
111 if err := os.WriteFile(filepath.Join(work, "README.md"), []byte("a\n"), 0o644); err != nil {
112 t.Fatal(err)
113 }
114 runGit("add", "README.md")
115 env := append(os.Environ(), "GIT_AUTHOR_DATE=2026-05-05T12:00:00", "GIT_COMMITTER_DATE=2026-05-05T12:00:00")
116 if _, err := run(ctx, "git", env, "-C", work, "commit", "-m", "eerste"); err != nil {
117 t.Fatal(err)
118 }
119 if _, err := run(ctx, "git", nil, "-C", work, "remote", "add", "origin", dir); err != nil {
120 t.Fatal(err)
121 }
122 if _, err := run(ctx, "git", nil, "-C", work, "push", "origin", "master"); err != nil {
123 t.Fatal(err)
124 }
125 list, err := CommitsSince(ctx, "git", dir, "", "master")
126 if err != nil || len(list) != 1 || list[0].Email != "tim@users.git.timfalken.com" || list[0].Unix == 0 {
127 t.Fatalf("%v %v", list, err)
128 }
129 again, err := CommitsSince(ctx, "git", dir, list[0].SHA, "master")
130 if err != nil || len(again) != 0 {
131 t.Fatalf("opnieuw %v %v", again, err)
132 }
133}
134
135func TestInitWritesHook(t *testing.T) {
136 if _, err := os.Stat("/usr/bin/git"); err != nil {
137 t.Skip("git ontbreekt")
138 }
139 dir := filepath.Join(t.TempDir(), "demo.git")
140 if err := Init("git", dir, "main", "/usr/bin/bluegit"); err != nil {
141 t.Fatal(err)
142 }
143 body, err := os.ReadFile(filepath.Join(dir, "hooks", "pre-receive"))
144 if err != nil {
145 t.Fatal(err)
146 }
147 if !strings.Contains(string(body), "hook pre-receive") || !strings.Contains(string(body), "/usr/bin/bluegit") {
148 t.Fatalf("hook: %s", body)
149 }
150}