generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 189
[generator] add recursive component discovery for GitHub repositories #909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShigrafS
wants to merge
11
commits into
meshery:master
Choose a base branch
from
ShigrafS:recursion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cfa3a36
feat(walker): implement recursive directory walking with max depth
ShigrafS b44792c
feat(github): support recursive component generation in git repo
ShigrafS 4d16898
feat(generators): add NewGeneratorWithOptions for recursion control
ShigrafS 0518bfd
test(generators): verify generator options passing
ShigrafS bff153f
fix(github): fix infinite recursion bug in git repo walker
ShigrafS 9445456
fix(walker): fix off-by-one error in max depth logic
ShigrafS 9997828
test(github): add functional recursive walk test
ShigrafS 3496689
feat(walker): implement file pattern filtering and fix depth logic
ShigrafS 1ead4cd
feat(generators): propagate file extensions to walker
ShigrafS 35beb67
test(github): add tests for file filtering and depth
ShigrafS 75cb5c9
Merge branch 'master' into recursion
ShigrafS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package generators | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/meshery/meshkit/generators/github" | ||
| ) | ||
|
|
||
| func TestNewGeneratorWithOptions(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| registrant string | ||
| url string | ||
| packageName string | ||
| opts GeneratorOptions | ||
| wantRec bool | ||
| wantDepth int | ||
| }{ | ||
| { | ||
| name: "Github Recursive", | ||
| registrant: "github", | ||
| url: "https://github.com/owner/repo", | ||
| packageName: "test", | ||
| opts: GeneratorOptions{ | ||
| Recursive: true, | ||
| MaxDepth: 5, | ||
| }, | ||
| wantRec: true, | ||
| wantDepth: 5, | ||
| }, | ||
| { | ||
| name: "Github Default", | ||
| registrant: "github", | ||
| url: "https://github.com/owner/repo", | ||
| packageName: "test", | ||
| opts: GeneratorOptions{ | ||
| Recursive: false, | ||
| MaxDepth: 0, | ||
| }, | ||
| wantRec: false, | ||
| wantDepth: 0, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| pm, err := NewGeneratorWithOptions(tt.registrant, tt.url, tt.packageName, tt.opts) | ||
| if err != nil { | ||
| t.Fatalf("NewGeneratorWithOptions() error = %v", err) | ||
| } | ||
|
|
||
| if ghpm, ok := pm.(github.GitHubPackageManager); ok { | ||
| if ghpm.Recursive != tt.wantRec { | ||
| t.Errorf("NewGeneratorWithOptions() Recursive = %v, want %v", ghpm.Recursive, tt.wantRec) | ||
| } | ||
| if ghpm.MaxDepth != tt.wantDepth { | ||
| t.Errorf("NewGeneratorWithOptions() MaxDepth = %v, want %v", ghpm.MaxDepth, tt.wantDepth) | ||
| } | ||
| } else { | ||
| t.Errorf("NewGeneratorWithOptions() returned unexpected type") | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/go-git/go-git/v5" | ||
| "github.com/go-git/go-git/v5/plumbing/object" | ||
| "github.com/meshery/meshkit/utils/walker" | ||
| ) | ||
|
|
||
| func TestRecursiveWalkFunctional(t *testing.T) { | ||
| tempDir, err := ioutil.TempDir("", "test-recursive-walk") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create temp dir: %v", err) | ||
| } | ||
| defer os.RemoveAll(tempDir) | ||
|
|
||
| r, err := git.PlainInit(tempDir, false) | ||
| if err != nil { | ||
| t.Fatalf("Failed to init git repo: %v", err) | ||
| } | ||
| w, err := r.Worktree() | ||
| if err != nil { | ||
| t.Fatalf("Failed to get worktree: %v", err) | ||
| } | ||
|
|
||
| createFile(t, tempDir, "root.yaml", "content") | ||
| createFile(t, tempDir, "root.json", "content") | ||
| createFile(t, tempDir, "level1/level1.yaml", "content") | ||
| createFile(t, tempDir, "level1/level1.txt", "content") | ||
| createFile(t, tempDir, "level1/level2/level2.yaml", "content") | ||
|
|
||
| _, err = w.Add(".") | ||
| if err != nil { | ||
| t.Fatalf("Failed to add files: %v", err) | ||
| } | ||
| _, err = w.Commit("Initial commit", &git.CommitOptions{ | ||
| Author: &object.Signature{ | ||
| Name: "Test", | ||
| Email: "test@example.com", | ||
| When: time.Now(), | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Failed to commit: %v", err) | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| recursive bool | ||
| maxDepth int | ||
| extensions []string | ||
| wantFiles []string | ||
| }{ | ||
| { | ||
| name: "Non-recursive (Root only, all types)", | ||
| recursive: false, | ||
| maxDepth: 0, | ||
| extensions: nil, | ||
| wantFiles: []string{"root.yaml", "root.json"}, | ||
| }, | ||
| { | ||
| name: "Recursive Unlimited (All types)", | ||
| recursive: true, | ||
| maxDepth: 0, | ||
| extensions: nil, | ||
| wantFiles: []string{"root.yaml", "root.json", "level1.yaml", "level1.txt", "level2.yaml"}, | ||
| }, | ||
| { | ||
| name: "Recursive MaxDepth 1 (All types)", | ||
| recursive: true, | ||
| maxDepth: 1, | ||
| extensions: nil, | ||
| wantFiles: []string{"root.yaml", "root.json", "level1.yaml", "level1.txt"}, | ||
| }, | ||
| { | ||
| name: "Recursive MaxDepth 2 (All types)", | ||
| recursive: true, | ||
| maxDepth: 2, | ||
| extensions: nil, | ||
| wantFiles: []string{"root.yaml", "root.json", "level1.yaml", "level1.txt", "level2.yaml"}, | ||
| }, | ||
| { | ||
| name: "Recursive Filtered (.yaml only)", | ||
| recursive: true, | ||
| maxDepth: 0, | ||
| extensions: []string{".yaml"}, | ||
| wantFiles: []string{"root.yaml", "level1.yaml", "level2.yaml"}, | ||
| }, | ||
| { | ||
| name: "Recursive Filtered (.yaml, .json)", | ||
| recursive: true, | ||
| maxDepth: 0, | ||
| extensions: []string{".yaml", ".json"}, | ||
| wantFiles: []string{"root.yaml", "root.json", "level1.yaml", "level2.yaml"}, | ||
| }, | ||
| { | ||
| name: "Recursive MaxDepth 1 Filtered (.yaml)", | ||
| recursive: true, | ||
| maxDepth: 1, | ||
| extensions: []string{".yaml"}, | ||
| wantFiles: []string{"root.yaml", "level1.yaml"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| repoPath := filepath.ToSlash(tempDir) | ||
| if !strings.HasPrefix(repoPath, "/") { | ||
| repoPath = "/" + repoPath | ||
| } | ||
| fileURL := "file://" + repoPath | ||
|
|
||
| walkerInst := walker.NewGit(). | ||
| Owner(""). | ||
| Repo(""). | ||
| BaseURL(fileURL). | ||
| Branch("master"). | ||
| Root(""). | ||
| MaxDepth(tt.maxDepth). | ||
| AllowedExtensions(tt.extensions) | ||
|
|
||
| var collectedFiles []string | ||
| walkerInst.RegisterFileInterceptor(func(f walker.File) error { | ||
| collectedFiles = append(collectedFiles, f.Name) | ||
| return nil | ||
| }) | ||
|
|
||
| if tt.recursive { | ||
| walkerInst.Root("/**") | ||
| } else { | ||
| walkerInst.Root("") | ||
| } | ||
|
|
||
| err := walkerInst.Walk() | ||
| if err != nil { | ||
| t.Fatalf("Walk failed: %v", err) | ||
| } | ||
|
|
||
| assertFilesEqual(t, collectedFiles, tt.wantFiles) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func assertFilesEqual(t *testing.T, got, want []string) { | ||
| gotMap := make(map[string]struct{}) | ||
| for _, f := range got { | ||
| gotMap[f] = struct{}{} | ||
| } | ||
| failed := false | ||
| for _, f := range want { | ||
| if _, ok := gotMap[f]; !ok { | ||
| t.Errorf("missing expected file: %s", f) | ||
| failed = true | ||
| } else { | ||
| delete(gotMap, f) | ||
| } | ||
| } | ||
| if len(gotMap) > 0 { | ||
| for f := range gotMap { | ||
| t.Errorf("unexpected file collected: %s", f) | ||
| failed = true | ||
| } | ||
| } | ||
| if failed { | ||
| t.Errorf("Got: %v", got) | ||
| t.Errorf("Want: %v", want) | ||
| } | ||
| } | ||
|
|
||
| func createFile(t *testing.T, base, path, content string) { | ||
| fullPath := filepath.Join(base, path) | ||
| err := os.MkdirAll(filepath.Dir(fullPath), 0755) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create dirs: %v", err) | ||
| } | ||
| err = ioutil.WriteFile(fullPath, []byte(content), 0644) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create file: %v", err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR description says recursive discovery is enabled by default, but
NewGenerator()callsNewGeneratorWithOptions(..., GeneratorOptions{}), which leavesRecursiveat its zero value (false) and therefore disables recursion unless callers explicitly opt in. Either update the defaults (and adjust tests/docs) or update the PR description to match the actual behavior/backward-compatibility plan.