| 1 | package gitrepo |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/access" |
| 12 | ) |
| 13 | |
| 14 | func 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 | |
| 31 | func 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 | |
| 81 | func 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 | |
| 87 | func TestInitWritesHook(t *testing.T) { |
| 88 | if _, err := os.Stat("/usr/bin/git"); err != nil { |
| 89 | t.Skip("git ontbreekt") |
| 90 | } |
| 91 | dir := filepath.Join(t.TempDir(), "demo.git") |
| 92 | if err := Init("git", dir, "main", "/usr/bin/bluegit"); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | body, err := os.ReadFile(filepath.Join(dir, "hooks", "pre-receive")) |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | if !strings.Contains(string(body), "hook pre-receive") || !strings.Contains(string(body), "/usr/bin/bluegit") { |
| 100 | t.Fatalf("hook: %s", body) |
| 101 | } |
| 102 | } |