This repository was archived by the owner on Feb 17, 2026. It is now read-only.
generated from kubrickcode/Template-Repository
-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add test file detector for automatic discovery #20
Merged
Merged
Changes from all commits
Commits
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
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,216 @@ | ||
| package parser | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/bmatcuk/doublestar/v4" | ||
| ) | ||
|
|
||
| const ( | ||
| DefaultMaxFileSize = 10 * 1024 * 1024 // 10MB | ||
|
|
||
| jsTestInfix = ".test." | ||
| jsSpecInfix = ".spec." | ||
| jsTestsDir = "/__tests__/" | ||
| jsTestsDirPrefix = "__tests__/" | ||
| ) | ||
|
|
||
| var DefaultSkipPatterns = []string{ | ||
| "node_modules", | ||
| ".git", | ||
| "vendor", | ||
| "dist", | ||
| ".next", | ||
| "__pycache__", | ||
| "coverage", | ||
| ".cache", | ||
| } | ||
|
|
||
| var ErrInvalidRootPath = errors.New("detector: root path does not exist or is not accessible") | ||
|
|
||
| // DetectionResult contains detected test files and any errors encountered during traversal. | ||
| type DetectionResult struct { | ||
| Errors []error | ||
| Files []string | ||
| } | ||
|
|
||
| type DetectorOptions struct { | ||
| SkipPatterns []string | ||
| Patterns []string | ||
| MaxFileSize int64 | ||
| } | ||
kubrickcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| type DetectorOption func(*DetectorOptions) | ||
|
|
||
| func WithSkipPatterns(patterns []string) DetectorOption { | ||
| return func(o *DetectorOptions) { | ||
| o.SkipPatterns = patterns | ||
| } | ||
| } | ||
|
|
||
| func WithPatterns(patterns []string) DetectorOption { | ||
| return func(o *DetectorOptions) { | ||
| o.Patterns = patterns | ||
| } | ||
| } | ||
|
|
||
| func WithMaxFileSize(size int64) DetectorOption { | ||
| return func(o *DetectorOptions) { | ||
| o.MaxFileSize = size | ||
| } | ||
| } | ||
|
|
||
| func DetectTestFiles(ctx context.Context, rootPath string, opts ...DetectorOption) (*DetectionResult, error) { | ||
| options := &DetectorOptions{ | ||
| SkipPatterns: DefaultSkipPatterns, | ||
| Patterns: nil, | ||
| MaxFileSize: DefaultMaxFileSize, | ||
| } | ||
|
|
||
| for _, opt := range opts { | ||
| opt(options) | ||
| } | ||
|
|
||
| rootInfo, err := os.Stat(rootPath) | ||
| if err != nil { | ||
| return nil, ErrInvalidRootPath | ||
| } | ||
| if !rootInfo.IsDir() { | ||
| return nil, ErrInvalidRootPath | ||
| } | ||
|
|
||
| skipSet := buildSkipSet(options.SkipPatterns) | ||
|
|
||
| result := &DetectionResult{ | ||
| Files: []string{}, | ||
| Errors: []error{}, | ||
| } | ||
|
|
||
| err = filepath.WalkDir(rootPath, func(path string, d fs.DirEntry, walkErr error) error { | ||
| if err := ctx.Err(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if walkErr != nil { | ||
| result.Errors = append(result.Errors, fmt.Errorf("access error at %s: %w", path, walkErr)) | ||
| return nil | ||
| } | ||
|
|
||
| if d.IsDir() { | ||
| if shouldSkipDir(path, rootPath, skipSet) { | ||
| return filepath.SkipDir | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| if !isTestFileCandidate(path) { | ||
| return nil | ||
| } | ||
|
|
||
| if len(options.Patterns) > 0 { | ||
| if !matchesAnyPattern(path, rootPath, options.Patterns) { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| if options.MaxFileSize > 0 { | ||
| info, err := d.Info() | ||
| if err != nil { | ||
| result.Errors = append(result.Errors, fmt.Errorf("failed to get file info for %s: %w", path, err)) | ||
| return nil | ||
| } | ||
| if info.Size() > options.MaxFileSize { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| result.Files = append(result.Files, path) | ||
| return nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return result, err | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| func buildSkipSet(patterns []string) map[string]bool { | ||
kubrickcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| skipSet := make(map[string]bool, len(patterns)) | ||
| for _, p := range patterns { | ||
| skipSet[p] = true | ||
| } | ||
| return skipSet | ||
| } | ||
|
|
||
| func shouldSkipDir(path, rootPath string, skipSet map[string]bool) bool { | ||
| if path == rootPath { | ||
| return false | ||
| } | ||
|
|
||
| base := filepath.Base(path) | ||
| return skipSet[base] | ||
| } | ||
|
|
||
| func isTestFileCandidate(path string) bool { | ||
| ext := strings.ToLower(filepath.Ext(path)) | ||
|
|
||
| switch ext { | ||
| case ".ts", ".tsx", ".js", ".jsx": | ||
| return isJSTestFile(path) | ||
| case ".go": | ||
| return isGoTestFile(path) | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func isGoTestFile(path string) bool { | ||
| base := filepath.Base(path) | ||
| return strings.HasSuffix(base, "_test.go") | ||
| } | ||
|
|
||
| func isJSTestFile(path string) bool { | ||
| base := filepath.Base(path) | ||
| lowerBase := strings.ToLower(base) | ||
|
|
||
| // *.test.*, *.spec.* | ||
| if strings.Contains(lowerBase, jsTestInfix) || strings.Contains(lowerBase, jsSpecInfix) { | ||
| return true | ||
| } | ||
|
|
||
| // __tests__ directory | ||
| normalizedPath := filepath.ToSlash(path) | ||
| if strings.Contains(normalizedPath, jsTestsDir) || strings.HasPrefix(normalizedPath, jsTestsDirPrefix) { | ||
| return true | ||
| } | ||
kubrickcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return false | ||
| } | ||
|
|
||
| func matchesAnyPattern(path, rootPath string, patterns []string) bool { | ||
| relPath, err := filepath.Rel(rootPath, path) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| relPath = filepath.ToSlash(relPath) | ||
|
|
||
| for _, pattern := range patterns { | ||
| matched, err := doublestar.Match(pattern, relPath) | ||
| if err != nil { | ||
| // Invalid pattern syntax - skip this pattern | ||
| continue | ||
| } | ||
kubrickcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if matched { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
kubrickcode marked this conversation as resolved.
Show resolved
Hide resolved
kubrickcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.