|
1 | 1 | package github |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "net/url" |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
5 | 8 | "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/go-git/go-git/v5" |
| 12 | + "github.com/go-git/go-git/v5/plumbing/object" |
| 13 | + "github.com/meshery/meshkit/utils/walker" |
6 | 14 | ) |
7 | 15 |
|
8 | | -func TestRecursiveWalk(t *testing.T) { |
9 | | - gr := GitRepo{ |
10 | | - URL: &url.URL{Path: "/owner/repo/branch/root"}, |
11 | | - Recursive: true, |
12 | | - MaxDepth: 2, |
13 | | - PackageName: "test-package", |
| 16 | +func TestRecursiveWalkFunctional(t *testing.T) { |
| 17 | + tempDir, err := ioutil.TempDir("", "test-recursive-walk") |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 20 | + } |
| 21 | + defer os.RemoveAll(tempDir) |
| 22 | + |
| 23 | + r, err := git.PlainInit(tempDir, false) |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("Failed to init git repo: %v", err) |
14 | 26 | } |
| 27 | + w, err := r.Worktree() |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("Failed to get worktree: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + createFile(t, tempDir, "root.yaml", "content") |
| 33 | + createFile(t, tempDir, "level1/level1.yaml", "content") |
| 34 | + createFile(t, tempDir, "level1/level2/level2.yaml", "content") |
15 | 35 |
|
16 | | - if !gr.Recursive { |
17 | | - t.Error("GitRepo.Recursive should be true") |
| 36 | + _, err = w.Add(".") |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("Failed to add files: %v", err) |
18 | 39 | } |
19 | | - if gr.MaxDepth != 2 { |
20 | | - t.Errorf("GitRepo.MaxDepth should be 2, got %d", gr.MaxDepth) |
| 40 | + _, err = w.Commit("Initial commit", &git.CommitOptions{ |
| 41 | + Author: &object.Signature{ |
| 42 | + Name: "Test", |
| 43 | + Email: "test@example.com", |
| 44 | + When: time.Now(), |
| 45 | + }, |
| 46 | + }) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("Failed to commit: %v", err) |
21 | 49 | } |
22 | 50 |
|
23 | | - ghpm := GitHubPackageManager{ |
24 | | - PackageName: "test", |
25 | | - SourceURL: "https://github.com/owner/repo", |
26 | | - Recursive: true, |
27 | | - MaxDepth: 3, |
| 51 | + tests := []struct { |
| 52 | + name string |
| 53 | + recursive bool |
| 54 | + maxDepth int |
| 55 | + wantFiles []string |
| 56 | + }{ |
| 57 | + { |
| 58 | + name: "Non-recursive (Root only)", |
| 59 | + recursive: false, |
| 60 | + maxDepth: 0, |
| 61 | + wantFiles: []string{"root.yaml"}, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "Recursive Unlimited", |
| 65 | + recursive: true, |
| 66 | + maxDepth: 0, |
| 67 | + wantFiles: []string{"root.yaml", "level1.yaml", "level2.yaml"}, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "Recursive MaxDepth 1", |
| 71 | + recursive: true, |
| 72 | + maxDepth: 1, |
| 73 | + wantFiles: []string{"root.yaml", "level1.yaml"}, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "Recursive MaxDepth 2", |
| 77 | + recursive: true, |
| 78 | + maxDepth: 2, |
| 79 | + wantFiles: []string{"root.yaml", "level1.yaml", "level2.yaml"}, |
| 80 | + }, |
28 | 81 | } |
29 | 82 |
|
30 | | - if !ghpm.Recursive { |
31 | | - t.Error("GitHubPackageManager.Recursive should be true") |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + repoPath := filepath.ToSlash(tempDir) |
| 86 | + if !strings.HasPrefix(repoPath, "/") { |
| 87 | + repoPath = "/" + repoPath |
| 88 | + } |
| 89 | + fileURL := "file://" + repoPath |
| 90 | + |
| 91 | + walkerInst := walker.NewGit(). |
| 92 | + Owner(""). |
| 93 | + Repo(""). |
| 94 | + BaseURL(fileURL). |
| 95 | + Branch("master"). |
| 96 | + Root(""). |
| 97 | + MaxDepth(tt.maxDepth) |
| 98 | + |
| 99 | + var collectedFiles []string |
| 100 | + walkerInst.RegisterFileInterceptor(func(f walker.File) error { |
| 101 | + collectedFiles = append(collectedFiles, f.Name) |
| 102 | + return nil |
| 103 | + }) |
| 104 | + |
| 105 | + if tt.recursive { |
| 106 | + walkerInst.Root("/**") |
| 107 | + } else { |
| 108 | + walkerInst.Root("") |
| 109 | + } |
| 110 | + |
| 111 | + err := walkerInst.Walk() |
| 112 | + if err != nil { |
| 113 | + t.Fatalf("Walk failed: %v", err) |
| 114 | + } |
| 115 | + |
| 116 | + assertFilesEqual(t, collectedFiles, tt.wantFiles) |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func assertFilesEqual(t *testing.T, got, want []string) { |
| 122 | + gotMap := make(map[string]struct{}) |
| 123 | + for _, f := range got { |
| 124 | + gotMap[f] = struct{}{} |
| 125 | + } |
| 126 | + for _, f := range want { |
| 127 | + if _, ok := gotMap[f]; !ok { |
| 128 | + t.Errorf("missing expected file: %s", f) |
| 129 | + } else { |
| 130 | + delete(gotMap, f) |
| 131 | + } |
| 132 | + } |
| 133 | + if len(gotMap) > 0 { |
| 134 | + for f := range gotMap { |
| 135 | + t.Errorf("unexpected file collected: %s", f) |
| 136 | + } |
| 137 | + } |
| 138 | + if len(got) != len(want) { |
| 139 | + t.Errorf("got %d files, want %d", len(got), len(want)) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func createFile(t *testing.T, base, path, content string) { |
| 144 | + fullPath := filepath.Join(base, path) |
| 145 | + err := os.MkdirAll(filepath.Dir(fullPath), 0755) |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("Failed to create dirs: %v", err) |
32 | 148 | } |
33 | | - if ghpm.MaxDepth != 3 { |
34 | | - t.Errorf("GitHubPackageManager.MaxDepth should be 3, got %d", ghpm.MaxDepth) |
| 149 | + err = ioutil.WriteFile(fullPath, []byte(content), 0644) |
| 150 | + if err != nil { |
| 151 | + t.Fatalf("Failed to create file: %v", err) |
35 | 152 | } |
36 | 153 | } |
0 commit comments