cmd/gitbayd/version_test.go
121 lines · 3970 bytes
5 symbols in this file
1package main
2
3import (
4 "bytes"
5 "log/slog"
6 "os/exec"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "gitbay.org/gitbay/internal/buildinfo"
12 "gitbay.org/gitbay/internal/config"
13 "gitbay.org/gitbay/internal/control"
14)
15
16func capture(t *testing.T, fn func()) string {
17 t.Helper()
18 var buf bytes.Buffer
19 prev := slog.Default()
20 slog.SetDefault(slog.New(slog.NewTextHandler(&buf, nil)))
21 defer slog.SetDefault(prev)
22 fn()
23 return buf.String()
24}
25
26func TestLogBuildWarnsOnAnUncommittedBuild(t *testing.T) {
27 prev := buildinfo.Commit
28 defer func() { buildinfo.Commit = prev }()
29
30 buildinfo.Commit = "abc123abc123"
31 if out := capture(t, logBuild); !strings.Contains(out, "level=INFO") {
32 t.Errorf("a committed build should log INFO:\n%s", out)
33 }
34
35 buildinfo.Commit = "abc123abc123-dirty"
36 out := capture(t, logBuild)
37 if !strings.Contains(out, "level=WARN") {
38 t.Errorf("a dirty build should log WARN:\n%s", out)
39 }
40 if !strings.Contains(out, "abc123abc123-dirty") {
41 t.Errorf("the warning should name the build:\n%s", out)
42 }
43}
44
45// sourceRepo builds a bare repository holding one commit on the default
46// branch, plus one commit off it, and returns the config pointing at it.
47func sourceRepo(t *testing.T) (cfg config.Config, onBranch, offBranch string) {
48 t.Helper()
49 root := t.TempDir()
50 dir := control.RepoDir(root, "krz", "gitbay")
51
52 work := filepath.Join(t.TempDir(), "work")
53 run := func(args ...string) string {
54 t.Helper()
55 cmd := exec.Command("git", args...)
56 cmd.Env = append(cmd.Environ(),
57 "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@e", "GIT_AUTHOR_DATE=2026-01-01T00:00:00Z",
58 "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@e", "GIT_COMMITTER_DATE=2026-01-01T00:00:00Z")
59 out, err := cmd.CombinedOutput()
60 if err != nil {
61 t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
62 }
63 return strings.TrimSpace(string(out))
64 }
65
66 run("init", "-q", "-b", "main", work)
67 run("-C", work, "commit", "-q", "--allow-empty", "-m", "on the branch")
68 onBranch = run("-C", work, "rev-parse", "HEAD")
69 run("-C", work, "checkout", "-q", "-b", "side")
70 run("-C", work, "commit", "-q", "--allow-empty", "-m", "off the branch")
71 offBranch = run("-C", work, "rev-parse", "HEAD")
72
73 run("clone", "-q", "--bare", work, dir)
74 run("-C", dir, "symbolic-ref", "HEAD", "refs/heads/main")
75
76 cfg = config.Config{Server: config.Server{Root: root, SourceRepo: "krz/gitbay"}}
77 return cfg, onBranch, offBranch
78}
79
80func TestWarnIfUnmerged(t *testing.T) {
81 prev := buildinfo.Commit
82 defer func() { buildinfo.Commit = prev }()
83
84 cfg, onBranch, offBranch := sourceRepo(t)
85
86 buildinfo.Commit = onBranch
87 if out := capture(t, func() { warnIfUnmerged(cfg) }); out != "" {
88 t.Errorf("a build on the default branch should be silent:\n%s", out)
89 }
90
91 buildinfo.Commit = offBranch
92 out := capture(t, func() { warnIfUnmerged(cfg) })
93 if !strings.Contains(out, "not on the source repository") {
94 t.Errorf("a build off the default branch should warn:\n%s", out)
95 }
96
97 // A commit the repository has never seen cannot be vouched for.
98 buildinfo.Commit = "0123456789abcdef0123456789abcdef01234567"
99 if out := capture(t, func() { warnIfUnmerged(cfg) }); !strings.Contains(out, "cannot check") {
100 t.Errorf("an unknown commit should warn:\n%s", out)
101 }
102}
103
104func TestWarnIfUnmergedIsSilentWithoutASourceRepo(t *testing.T) {
105 prev := buildinfo.Commit
106 defer func() { buildinfo.Commit = prev }()
107 buildinfo.Commit = "abc123abc123"
108
109 // The default for every instance that does not host its own source.
110 cfg := config.Config{Server: config.Server{Root: t.TempDir()}}
111 if out := capture(t, func() { warnIfUnmerged(cfg) }); out != "" {
112 t.Errorf("no source_repo should mean no check:\n%s", out)
113 }
114
115 // A dirty build has already been warned about by logBuild; do not warn twice.
116 cfg.Server.SourceRepo = "krz/gitbay"
117 buildinfo.Commit = "abc123abc123-dirty"
118 if out := capture(t, func() { warnIfUnmerged(cfg) }); out != "" {
119 t.Errorf("a dirty build is logBuild's to report, not this one's:\n%s", out)
120 }
121}