|
| 1 | +package sdk_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/chainguard-dev/terraform-infra-common/modules/github-bots/sdk" |
| 9 | + "github.com/go-git/go-billy/v5/osfs" |
| 10 | + "github.com/go-git/go-git/v5" |
| 11 | + "github.com/go-git/go-git/v5/plumbing" |
| 12 | + "github.com/go-git/go-git/v5/plumbing/object" |
| 13 | + "github.com/go-git/go-git/v5/storage/memory" |
| 14 | + "github.com/google/go-cmp/cmp" |
| 15 | +) |
| 16 | + |
| 17 | +func TestGithubClient_GetChangedFiles(t *testing.T) { |
| 18 | + ctx := context.Background() |
| 19 | + client := sdk.NewGitHubClient(ctx, "org", "repo", "bot") |
| 20 | + |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + patches []gpatch |
| 24 | + expected map[string]struct{} |
| 25 | + wantErr bool |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "Added file", |
| 29 | + patches: []gpatch{ |
| 30 | + {action: "add", path: "new.txt"}, |
| 31 | + }, |
| 32 | + expected: map[string]struct{}{ |
| 33 | + "new.txt": {}, |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "Modified file", |
| 38 | + patches: []gpatch{ |
| 39 | + {action: "add", path: "existing.txt"}, |
| 40 | + {action: "modify", path: "existing.txt"}, |
| 41 | + }, |
| 42 | + expected: map[string]struct{}{ |
| 43 | + "existing.txt": {}, |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Renamed file", |
| 48 | + patches: []gpatch{ |
| 49 | + {action: "add", path: "old.txt"}, |
| 50 | + {action: "rename", path: "old.txt", newPath: "new.txt"}, |
| 51 | + }, |
| 52 | + expected: map[string]struct{}{ |
| 53 | + "new.txt": {}, |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Deleted file", |
| 58 | + patches: []gpatch{ |
| 59 | + {action: "add", path: "to_delete.txt"}, |
| 60 | + {action: "delete", path: "initial.txt"}, |
| 61 | + }, |
| 62 | + expected: map[string]struct{}{ |
| 63 | + "to_delete.txt": {}, |
| 64 | + "initial.txt": {}, |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "Multiple changes", |
| 69 | + patches: []gpatch{ |
| 70 | + {action: "add", path: "new.txt"}, |
| 71 | + {action: "add", path: "to_modify.txt"}, |
| 72 | + {action: "modify", path: "to_modify.txt"}, |
| 73 | + {action: "add", path: "to_rename.txt"}, |
| 74 | + {action: "rename", path: "to_rename.txt", newPath: "renamed.txt"}, |
| 75 | + {action: "add", path: "to_delete.txt"}, |
| 76 | + {action: "delete", path: "to_delete.txt"}, |
| 77 | + }, |
| 78 | + expected: map[string]struct{}{ |
| 79 | + "new.txt": {}, |
| 80 | + "to_modify.txt": {}, |
| 81 | + "renamed.txt": {}, |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tt := range tests { |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + repo := setupTestRepo(t, tt.patches) |
| 89 | + |
| 90 | + got, err := client.GetChangedFiles(ctx, repo, "feature", "main") |
| 91 | + if (err != nil) != tt.wantErr { |
| 92 | + t.Errorf("GetChangedFiles() error = %v, wantErr %v", err, tt.wantErr) |
| 93 | + return |
| 94 | + } |
| 95 | + if err != nil { |
| 96 | + t.Logf("Error details: %+v", err) |
| 97 | + } |
| 98 | + if diff := cmp.Diff(got, tt.expected); diff != "" { |
| 99 | + t.Errorf("GetChangedFiles() mismatch (-want +got):\n%s", diff) |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +type gpatch struct { |
| 106 | + action string |
| 107 | + path string |
| 108 | + newPath string |
| 109 | +} |
| 110 | + |
| 111 | +func setupTestRepo(t *testing.T, patches []gpatch) *git.Repository { |
| 112 | + t.Helper() |
| 113 | + |
| 114 | + dir := t.TempDir() |
| 115 | + fs := osfs.New(dir) |
| 116 | + repo, err := git.Init(memory.NewStorage(), fs) |
| 117 | + if err != nil { |
| 118 | + t.Fatalf("failed to init git repo: %v", err) |
| 119 | + } |
| 120 | + |
| 121 | + w, err := repo.Worktree() |
| 122 | + if err != nil { |
| 123 | + t.Fatalf("failed to get worktree: %v", err) |
| 124 | + } |
| 125 | + |
| 126 | + // Create and commit an initial file on main |
| 127 | + if err := writeFile(w, "initial.txt", "initial content"); err != nil { |
| 128 | + t.Fatalf("failed to create initial file: %v", err) |
| 129 | + } |
| 130 | + if _, err := w.Add("initial.txt"); err != nil { |
| 131 | + t.Fatalf("failed to add initial file: %v", err) |
| 132 | + } |
| 133 | + if _, err := w.Commit("Initial commit", &git.CommitOptions{ |
| 134 | + Author: &object. Signature{ Name: "Test Author", Email: "[email protected]"}, |
| 135 | + }); err != nil { |
| 136 | + t.Fatalf("failed to commit initial file: %v", err) |
| 137 | + } |
| 138 | + |
| 139 | + // Create main branch |
| 140 | + mainRef := plumbing.NewBranchReferenceName("main") |
| 141 | + headRef, err := repo.Head() |
| 142 | + if err != nil { |
| 143 | + t.Fatalf("failed to get HEAD reference: %v", err) |
| 144 | + } |
| 145 | + if err := repo.Storer.SetReference(plumbing.NewHashReference(mainRef, headRef.Hash())); err != nil { |
| 146 | + t.Fatalf("failed to set main branch reference: %v", err) |
| 147 | + } |
| 148 | + |
| 149 | + // Create and checkout feature branch |
| 150 | + if err := w.Checkout(&git.CheckoutOptions{ |
| 151 | + Create: true, |
| 152 | + Branch: plumbing.NewBranchReferenceName("feature"), |
| 153 | + }); err != nil { |
| 154 | + t.Fatalf("failed to checkout feature branch: %v", err) |
| 155 | + } |
| 156 | + |
| 157 | + // Apply patches |
| 158 | + for _, p := range patches { |
| 159 | + if err := applyPatch(w, p); err != nil { |
| 160 | + t.Fatalf("failed to apply patch: %v", err) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + // Commit changes |
| 165 | + if _, err := w.Add("."); err != nil { |
| 166 | + t.Fatalf("failed to add changes: %v", err) |
| 167 | + } |
| 168 | + if _, err := w.Commit("Test changes", &git.CommitOptions{ |
| 169 | + Author: &object. Signature{ Name: "Test Author", Email: "[email protected]"}, |
| 170 | + }); err != nil { |
| 171 | + t.Fatalf("failed to commit changes: %v", err) |
| 172 | + } |
| 173 | + |
| 174 | + return repo |
| 175 | +} |
| 176 | + |
| 177 | +func applyPatch(w *git.Worktree, p gpatch) error { |
| 178 | + switch p.action { |
| 179 | + case "add": |
| 180 | + return writeFile(w, p.path, "content") |
| 181 | + case "modify": |
| 182 | + return writeFile(w, p.path, "modified content") |
| 183 | + case "rename": |
| 184 | + if err := w.Filesystem.Rename(p.path, p.newPath); err != nil { |
| 185 | + return fmt.Errorf("failed to rename file: %w", err) |
| 186 | + } |
| 187 | + case "delete": |
| 188 | + if err := w.Filesystem.Remove(p.path); err != nil { |
| 189 | + return fmt.Errorf("failed to delete file: %w", err) |
| 190 | + } |
| 191 | + } |
| 192 | + return nil |
| 193 | +} |
| 194 | + |
| 195 | +func writeFile(w *git.Worktree, path, content string) error { |
| 196 | + f, err := w.Filesystem.Create(path) |
| 197 | + if err != nil { |
| 198 | + return fmt.Errorf("failed to create file %s: %w", path, err) |
| 199 | + } |
| 200 | + defer f.Close() |
| 201 | + if _, err := f.Write([]byte(content)); err != nil { |
| 202 | + return fmt.Errorf("failed to write to file %s: %w", path, err) |
| 203 | + } |
| 204 | + return nil |
| 205 | +} |
0 commit comments