|
| 1 | +package gitops |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/Bharath-code/git-scope/internal/model" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSummaryString(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + sum Summary |
| 16 | + want string |
| 17 | + }{ |
| 18 | + {"all ok", Summary{Succeeded: 8}, "✓ 8 fetched"}, |
| 19 | + {"with skips", Summary{Succeeded: 8, Skipped: 2}, "✓ 8 fetched · 2 skipped (no remote)"}, |
| 20 | + {"with failures", Summary{Succeeded: 8, Skipped: 2, Failed: 1}, "✓ 8 fetched · 2 skipped (no remote) · 1 failed"}, |
| 21 | + {"none", Summary{}, "✓ 0 fetched"}, |
| 22 | + } |
| 23 | + for _, tt := range tests { |
| 24 | + t.Run(tt.name, func(t *testing.T) { |
| 25 | + if got := tt.sum.String(); got != tt.want { |
| 26 | + t.Errorf("String() = %q, want %q", got, tt.want) |
| 27 | + } |
| 28 | + }) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestSummarize(t *testing.T) { |
| 33 | + results := []ActionResult{ |
| 34 | + {Status: StatusSuccess}, |
| 35 | + {Status: StatusSuccess}, |
| 36 | + {Status: StatusSkipped}, |
| 37 | + {Status: StatusFailed}, |
| 38 | + } |
| 39 | + s := summarize(results) |
| 40 | + if s.Succeeded != 2 || s.Skipped != 1 || s.Failed != 1 { |
| 41 | + t.Errorf("summarize = %+v, want 2/1/1", s) |
| 42 | + } |
| 43 | + if len(s.Results) != 4 { |
| 44 | + t.Errorf("Results length = %d, want 4", len(s.Results)) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestFirstLine(t *testing.T) { |
| 49 | + if got := firstLine("one\ntwo"); got != "one" { |
| 50 | + t.Errorf("firstLine multi = %q, want one", got) |
| 51 | + } |
| 52 | + if got := firstLine("only"); got != "only" { |
| 53 | + t.Errorf("firstLine single = %q, want only", got) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// --- Integration tests against real git repos --- |
| 58 | + |
| 59 | +func gitRun(t *testing.T, dir string, args ...string) { |
| 60 | + t.Helper() |
| 61 | + cmd := exec.Command("git", args...) |
| 62 | + cmd.Dir = dir |
| 63 | + cmd.Env = append(os.Environ(), |
| 64 | + "GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.com", |
| 65 | + "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.com", |
| 66 | + ) |
| 67 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 68 | + t.Fatalf("git %v: %v\n%s", args, err, out) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func requireGit(t *testing.T) { |
| 73 | + t.Helper() |
| 74 | + if _, err := exec.LookPath("git"); err != nil { |
| 75 | + t.Skip("git not available") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// makeCloneWithRemote creates a bare "origin" plus a working clone of it, |
| 80 | +// returning the path to the working clone (which has a configured remote). |
| 81 | +func makeCloneWithRemote(t *testing.T) string { |
| 82 | + t.Helper() |
| 83 | + base := t.TempDir() |
| 84 | + |
| 85 | + origin := filepath.Join(base, "origin.git") |
| 86 | + gitRun(t, base, "init", "--bare", origin) |
| 87 | + |
| 88 | + seed := filepath.Join(base, "seed") |
| 89 | + if err := os.MkdirAll(seed, 0755); err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + gitRun(t, seed, "init", "-b", "main") |
| 93 | + if err := os.WriteFile(filepath.Join(seed, "f.txt"), []byte("x\n"), 0644); err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + gitRun(t, seed, "add", "f.txt") |
| 97 | + gitRun(t, seed, "commit", "-m", "init") |
| 98 | + gitRun(t, seed, "remote", "add", "origin", origin) |
| 99 | + gitRun(t, seed, "push", "origin", "main") |
| 100 | + |
| 101 | + clone := filepath.Join(base, "clone") |
| 102 | + gitRun(t, base, "clone", origin, clone) |
| 103 | + return clone |
| 104 | +} |
| 105 | + |
| 106 | +func makeRepoNoRemote(t *testing.T) string { |
| 107 | + t.Helper() |
| 108 | + dir := t.TempDir() |
| 109 | + gitRun(t, dir, "init", "-b", "main") |
| 110 | + if err := os.WriteFile(filepath.Join(dir, "f.txt"), []byte("y\n"), 0644); err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + gitRun(t, dir, "add", "f.txt") |
| 114 | + gitRun(t, dir, "commit", "-m", "init") |
| 115 | + return dir |
| 116 | +} |
| 117 | + |
| 118 | +func TestHasRemote(t *testing.T) { |
| 119 | + requireGit(t) |
| 120 | + if !hasRemote(makeCloneWithRemote(t)) { |
| 121 | + t.Error("clone with origin should report a remote") |
| 122 | + } |
| 123 | + if hasRemote(makeRepoNoRemote(t)) { |
| 124 | + t.Error("local-only repo should report no remote") |
| 125 | + } |
| 126 | + if hasRemote(t.TempDir()) { |
| 127 | + t.Error("non-git dir should report no remote") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestFetchAll_MixedRepos(t *testing.T) { |
| 132 | + requireGit(t) |
| 133 | + |
| 134 | + withRemote := makeCloneWithRemote(t) |
| 135 | + noRemote := makeRepoNoRemote(t) |
| 136 | + |
| 137 | + repos := []model.Repo{ |
| 138 | + {Name: "cloned", Path: withRemote}, |
| 139 | + {Name: "local-only", Path: noRemote}, |
| 140 | + } |
| 141 | + |
| 142 | + sum := FetchAll(repos) |
| 143 | + |
| 144 | + if sum.Succeeded != 1 { |
| 145 | + t.Errorf("Succeeded = %d, want 1", sum.Succeeded) |
| 146 | + } |
| 147 | + if sum.Skipped != 1 { |
| 148 | + t.Errorf("Skipped = %d, want 1", sum.Skipped) |
| 149 | + } |
| 150 | + if sum.Failed != 0 { |
| 151 | + t.Errorf("Failed = %d, want 0", sum.Failed) |
| 152 | + } |
| 153 | + |
| 154 | + byName := map[string]ActionResult{} |
| 155 | + for _, r := range sum.Results { |
| 156 | + byName[r.Repo] = r |
| 157 | + } |
| 158 | + if byName["cloned"].Status != StatusSuccess { |
| 159 | + t.Errorf("cloned status = %v, want success", byName["cloned"].Status) |
| 160 | + } |
| 161 | + if byName["local-only"].Status != StatusSkipped { |
| 162 | + t.Errorf("local-only status = %v, want skipped", byName["local-only"].Status) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func TestFetchAll_Empty(t *testing.T) { |
| 167 | + sum := FetchAll(nil) |
| 168 | + if sum.Succeeded != 0 || sum.Failed != 0 || sum.Skipped != 0 { |
| 169 | + t.Errorf("empty FetchAll should be all zero, got %+v", sum) |
| 170 | + } |
| 171 | +} |
0 commit comments