Skip to content

Commit 444c8fc

Browse files
committed
test(github): add functional recursive walk test
Signed-off-by: ShigrafS <shigrafsalik@proton.me>
1 parent aa6fba2 commit 444c8fc

File tree

1 file changed

+150
-41
lines changed

1 file changed

+150
-41
lines changed
Lines changed: 150 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,171 @@
11
package github
22

33
import (
4-
"net/url"
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
"strings"
58
"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"
614
)
715

8-
func TestRecursiveWalk(t *testing.T) {
9-
// Ideally we would mock the network calls or setup a local git repo.
10-
// However, given the environment limitations and existing tests using real network,
11-
// we will try to make a test that relies on the walker logic directly or uses a stable public repo.
12-
// But since testing walker directly is unit testing, let's try to verify GitRepo logic using a mock-like approach if possible,
13-
// or just rely on the existing pattern.
16+
func TestRecursiveWalkFunctional(t *testing.T) {
17+
// 1. Create a temporary local git repo
18+
tempDir, err := ioutil.TempDir("", "test-recursive-walk")
19+
if err != nil {
20+
t.Fatalf("Failed to create temp dir: %v", err)
21+
}
22+
defer os.RemoveAll(tempDir)
1423

15-
// Let's create a test that uses the walker directly first to verify local directory walking if possible,
16-
// but the Git walker is specifically for git repos.
24+
// Initialize git repo
25+
r, err := git.PlainInit(tempDir, false)
26+
if err != nil {
27+
t.Fatalf("Failed to init git repo: %v", err)
28+
}
29+
w, err := r.Worktree()
30+
if err != nil {
31+
t.Fatalf("Failed to get worktree: %v", err)
32+
}
1733

18-
// Verify that the fields are correctly set on the walker.
34+
// 2. Commit a file structure
35+
// root/
36+
// root.yaml
37+
// level1/
38+
// level1.yaml
39+
// level2/
40+
// level2.yaml
1941

20-
gr := GitRepo{
21-
URL: &url.URL{Path: "/owner/repo/branch/root"},
22-
Recursive: true,
23-
MaxDepth: 2,
24-
PackageName: "test-package",
25-
}
42+
createFile(t, tempDir, "root.yaml", "content")
43+
createFile(t, tempDir, "level1/level1.yaml", "content")
44+
createFile(t, tempDir, "level1/level2/level2.yaml", "content")
2645

27-
if !gr.Recursive {
28-
t.Error("GitRepo.Recursive should be true")
46+
_, err = w.Add(".")
47+
if err != nil {
48+
t.Fatalf("Failed to add files: %v", err)
2949
}
30-
if gr.MaxDepth != 2 {
31-
t.Errorf("GitRepo.MaxDepth should be 2, got %d", gr.MaxDepth)
50+
// Use a fixed time for commit to be deterministic if needed, but not strictly required here.
51+
_, err = w.Commit("Initial commit", &git.CommitOptions{
52+
Author: &object.Signature{
53+
Name: "Test",
54+
Email: "test@example.com",
55+
When: time.Now(),
56+
},
57+
})
58+
if err != nil {
59+
t.Fatalf("Failed to commit: %v", err)
3260
}
3361

34-
// Since we cannot easily mock the internal walker without refactoring using interfaces,
35-
// we will rely on integration/manual verification via code structure.
36-
// Wait, we CAN test the logic in `clonewalk` if we can intercept the git clone.
37-
// But `git.PlainClone` is hardcoded.
62+
// 3. Test Cases
63+
tests := []struct {
64+
name string
65+
recursive bool
66+
maxDepth int
67+
wantFiles []string
68+
}{
69+
{
70+
name: "Non-recursive (Root only)",
71+
recursive: false,
72+
maxDepth: 0,
73+
wantFiles: []string{"root.yaml"},
74+
},
75+
{
76+
name: "Recursive Unlimited",
77+
recursive: true,
78+
maxDepth: 0,
79+
wantFiles: []string{"root.yaml", "level1.yaml", "level2.yaml"},
80+
},
81+
{
82+
name: "Recursive MaxDepth 1",
83+
recursive: true,
84+
maxDepth: 1,
85+
wantFiles: []string{"root.yaml", "level1.yaml"},
86+
},
87+
{
88+
name: "Recursive MaxDepth 2",
89+
recursive: true,
90+
maxDepth: 2,
91+
wantFiles: []string{"root.yaml", "level1.yaml", "level2.yaml"},
92+
},
93+
}
94+
95+
for _, tt := range tests {
96+
t.Run(tt.name, func(t *testing.T) {
97+
// Construct file URL for local repo
98+
// Convert tempDir to file URI (e.g., file:///C:/...)
99+
// ToSlash is important for Windows
100+
repoPath := filepath.ToSlash(tempDir)
101+
if !strings.HasPrefix(repoPath, "/") {
102+
repoPath = "/" + repoPath
103+
}
104+
fileURL := "file://" + repoPath
105+
106+
walkerInst := walker.NewGit().
107+
Owner("").
108+
Repo("").
109+
BaseURL(fileURL).
110+
Branch("master").
111+
Root("").
112+
MaxDepth(tt.maxDepth)
113+
114+
var collectedFiles []string
115+
walkerInst.RegisterFileInterceptor(func(f walker.File) error {
116+
collectedFiles = append(collectedFiles, f.Name)
117+
return nil
118+
})
38119

39-
// Instead, let's add a test for `GitHubPackageManager` ensuring options are passed.
120+
// Adjust root to trigger recursion internal flag
121+
if tt.recursive {
122+
walkerInst.Root("/**")
123+
} else {
124+
walkerInst.Root("")
125+
}
40126

41-
ghpm := GitHubPackageManager{
42-
PackageName: "test",
43-
SourceURL: "https://github.com/owner/repo",
44-
Recursive: true,
45-
MaxDepth: 3,
127+
// Run Walk
128+
err := walkerInst.Walk()
129+
if err != nil {
130+
// We expect this to work for local repos. Fail if it doesn't.
131+
t.Fatalf("Walk failed: %v", err)
132+
}
133+
134+
assertFilesEqual(t, collectedFiles, tt.wantFiles)
135+
})
46136
}
137+
}
47138

48-
if !ghpm.Recursive {
49-
t.Error("GitHubPackageManager.Recursive should be true")
139+
func assertFilesEqual(t *testing.T, got, want []string) {
140+
gotMap := make(map[string]struct{})
141+
for _, f := range got {
142+
gotMap[f] = struct{}{}
50143
}
51-
if ghpm.MaxDepth != 3 {
52-
t.Errorf("GitHubPackageManager.MaxDepth should be 3, got %d", ghpm.MaxDepth)
144+
for _, f := range want {
145+
if _, ok := gotMap[f]; !ok {
146+
t.Errorf("missing expected file: %s", f)
147+
} else {
148+
delete(gotMap, f)
149+
}
53150
}
151+
if len(gotMap) > 0 {
152+
for f := range gotMap {
153+
t.Errorf("unexpected file collected: %s", f)
154+
}
155+
}
156+
if len(got) != len(want) {
157+
t.Errorf("got %d files, want %d", len(got), len(want))
158+
}
159+
}
54160

55-
// Verify internally somehow? No easy way without exposure.
56-
57-
// Let's rely on the fact that I modified the code correctly and maybe add a small test
58-
// that uses a real small repo if I can find one, similar to existing tests.
59-
60-
// Existing tests use: https://github.com/GoogleCloudPlatform/k8s-config-connector
61-
// Let's try to simple test struct passing.
161+
func createFile(t *testing.T, base, path, content string) {
162+
fullPath := filepath.Join(base, path)
163+
err := os.MkdirAll(filepath.Dir(fullPath), 0755)
164+
if err != nil {
165+
t.Fatalf("Failed to create dirs: %v", err)
166+
}
167+
err = ioutil.WriteFile(fullPath, []byte(content), 0644)
168+
if err != nil {
169+
t.Fatalf("Failed to create file: %v", err)
170+
}
62171
}

0 commit comments

Comments
 (0)