BlueGit

timfa bluegit

Files

bluegit / internal / gitrepo / repo_test.go

repo_test.go

feat: activiteitsheatmap op dashboard, groep en repository
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 4 weeks ago
Failed d5433bc7 History
repo_test.go 4.5 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"}, &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"}, &buf); err != nil {
24 t.Fatal(err)
25 }
26 if err := EvaluatePush(access.RoleMaintainer, nil, strings.NewReader("")); err == nil {
27 t.Fatal("lege push")
28 }
29}
30
31func TestTouchAndStats(t *testing.T) {
32 if _, err := os.Stat("/usr/bin/git"); err != nil {
33 t.Skip("git ontbreekt")
34 }
35 ctx := context.Background()
36 dir := filepath.Join(t.TempDir(), "demo.git")
37 if err := Init("git", dir, "master", "/bin/true"); err != nil {
38 t.Fatal(err)
39 }
40 work := t.TempDir()
41 runGit := func(args ...string) {
42 t.Helper()
43 cmd := execCommand(t, work, args...)
44 if cmd != nil {
45 t.Fatal(cmd)
46 }
47 }
48 runGit("init", "-b", "master")
49 runGit("config", "user.email", "tim@example.com")
50 runGit("config", "user.name", "tim")
51 if err := os.WriteFile(filepath.Join(work, "README.md"), []byte("# Hallo\n"), 0o644); err != nil {
52 t.Fatal(err)
53 }
54 if err := os.MkdirAll(filepath.Join(work, "src"), 0o755); err != nil {
55 t.Fatal(err)
56 }
57 if err := os.WriteFile(filepath.Join(work, "src", "main.go"), []byte("package main\n"), 0o644); err != nil {
58 t.Fatal(err)
59 }
60 runGit("add", ".")
61 runGit("commit", "-m", "eerste")
62 if _, err := run(ctx, "git", nil, "-C", work, "remote", "add", "origin", dir); err != nil {
63 t.Fatal(err)
64 }
65 if _, err := run(ctx, "git", nil, "-C", work, "push", "origin", "master"); err != nil {
66 t.Fatal(err)
67 }
68 touch, err := Touch(ctx, "git", dir, "master")
69 if err != nil {
70 t.Fatal(err)
71 }
72 if touch["README.md"].Subject != "eerste" || touch["src"].Subject != "eerste" || touch["src/main.go"].Subject != "eerste" {
73 t.Fatalf("%v", touch)
74 }
75 commits, branches, tags, err := Stats(ctx, "git", dir, "master")
76 if err != nil || commits != 1 || branches != 1 || tags != 0 {
77 t.Fatalf("stats %d %d %d %v", commits, branches, tags, err)
78 }
79}
80
81func execCommand(t *testing.T, dir string, args ...string) error {
82 t.Helper()
83 _, err := run(context.Background(), "git", nil, append([]string{"-C", dir}, args...)...)
84 return err
85}
86
87func TestCommitsSince(t *testing.T) {
88 if _, err := os.Stat("/usr/bin/git"); err != nil {
89 t.Skip("git ontbreekt")
90 }
91 ctx := context.Background()
92 dir := filepath.Join(t.TempDir(), "demo.git")
93 if err := Init("git", dir, "master", "/bin/true"); err != nil {
94 t.Fatal(err)
95 }
96 work := t.TempDir()
97 runGit := func(args ...string) {
98 t.Helper()
99 if err := execCommand(t, work, args...); err != nil {
100 t.Fatal(err)
101 }
102 }
103 runGit("init", "-b", "master")
104 runGit("config", "user.email", "tim@users.git.timfalken.com")
105 runGit("config", "user.name", "tim")
106 if err := os.WriteFile(filepath.Join(work, "README.md"), []byte("a\n"), 0o644); err != nil {
107 t.Fatal(err)
108 }
109 runGit("add", "README.md")
110 env := append(os.Environ(), "GIT_AUTHOR_DATE=2026-05-05T12:00:00", "GIT_COMMITTER_DATE=2026-05-05T12:00:00")
111 if _, err := run(ctx, "git", env, "-C", work, "commit", "-m", "eerste"); err != nil {
112 t.Fatal(err)
113 }
114 if _, err := run(ctx, "git", nil, "-C", work, "remote", "add", "origin", dir); err != nil {
115 t.Fatal(err)
116 }
117 if _, err := run(ctx, "git", nil, "-C", work, "push", "origin", "master"); err != nil {
118 t.Fatal(err)
119 }
120 list, err := CommitsSince(ctx, "git", dir, "", "master")
121 if err != nil || len(list) != 1 || list[0].Email != "tim@users.git.timfalken.com" || list[0].Unix == 0 {
122 t.Fatalf("%v %v", list, err)
123 }
124 again, err := CommitsSince(ctx, "git", dir, list[0].SHA, "master")
125 if err != nil || len(again) != 0 {
126 t.Fatalf("opnieuw %v %v", again, err)
127 }
128}
129
130func TestInitWritesHook(t *testing.T) {
131 if _, err := os.Stat("/usr/bin/git"); err != nil {
132 t.Skip("git ontbreekt")
133 }
134 dir := filepath.Join(t.TempDir(), "demo.git")
135 if err := Init("git", dir, "main", "/usr/bin/bluegit"); err != nil {
136 t.Fatal(err)
137 }
138 body, err := os.ReadFile(filepath.Join(dir, "hooks", "pre-receive"))
139 if err != nil {
140 t.Fatal(err)
141 }
142 if !strings.Contains(string(body), "hook pre-receive") || !strings.Contains(string(body), "/usr/bin/bluegit") {
143 t.Fatalf("hook: %s", body)
144 }
145}