add test suite
This commit is contained in:
+21
-80
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"git.flo.fo/FLO/dirmd/internal/config"
|
||||
@@ -40,12 +41,17 @@ func BuildFullTree(absRoot string, cfg *config.Config) (*Node, error) {
|
||||
name := filepath.Base(path)
|
||||
|
||||
if d.IsDir() {
|
||||
if isIgnoredName(name, cfg.Ignores) {
|
||||
if strings.HasPrefix(name, ".") {
|
||||
node := addPath(root, rel, true)
|
||||
node.Annotation = "ignored (--ignore)"
|
||||
node.Annotation = "ignored (default)"
|
||||
return filepath.SkipDir
|
||||
}
|
||||
if cfg.OmitTest && isTestDir(rel) {
|
||||
if annotation, ok := ignoreReason(name, cfg.Ignores); ok {
|
||||
node := addPath(root, rel, true)
|
||||
node.Annotation = annotation
|
||||
return filepath.SkipDir
|
||||
}
|
||||
if cfg.OmitTest && filter.IsTestDir(rel) {
|
||||
node := addPath(root, rel, true)
|
||||
node.Annotation = "tests omitted (--omit-test)"
|
||||
return filepath.SkipDir
|
||||
@@ -70,105 +76,40 @@ func BuildFullTree(absRoot string, cfg *config.Config) (*Node, error) {
|
||||
return root, nil
|
||||
}
|
||||
|
||||
func isIgnoredName(name string, ignores []string) bool {
|
||||
if strings.HasPrefix(name, ".") {
|
||||
return true
|
||||
}
|
||||
func ignoreReason(name string, ignores []string) (string, bool) {
|
||||
for _, pattern := range ignores {
|
||||
if matched, _ := filepath.Match(pattern, name); matched {
|
||||
return true
|
||||
if slices.Contains(config.DefaultIgnores, pattern) {
|
||||
return "ignored (default)", true
|
||||
}
|
||||
return "ignored (--ignore)", true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isTestDir(rel string) bool {
|
||||
slashPath := filepath.ToSlash(rel)
|
||||
return strings.Contains(slashPath, "/tests/") || strings.HasPrefix(slashPath, "tests/")
|
||||
return "", false
|
||||
}
|
||||
|
||||
func fileAnnotation(rel, name string, cfg *config.Config) (string, bool) {
|
||||
if strings.HasPrefix(name, ".") {
|
||||
return "", false
|
||||
}
|
||||
for _, pattern := range cfg.Ignores {
|
||||
if matched, _ := filepath.Match(pattern, name); matched {
|
||||
return "ignored (--ignore)", true
|
||||
}
|
||||
if annotation, ok := ignoreReason(name, cfg.Ignores); ok {
|
||||
return annotation, true
|
||||
}
|
||||
if cfg.OmitTest && isTestFileName(rel, name) {
|
||||
if cfg.OmitTest && filter.IsTestFile(rel, name) {
|
||||
return "tests omitted (--omit-test)", true
|
||||
}
|
||||
if cfg.OmitMd && isNonReadmeMarkdown(name) {
|
||||
if cfg.OmitMd && filter.IsNonReadmeMarkdown(rel, name) {
|
||||
return "omitted (--omit-md)", true
|
||||
}
|
||||
if cfg.FrontendOnly && !isFrontendOrReadme(rel, name) {
|
||||
if cfg.FrontendOnly && !filter.IsFrontendOrReadme(rel, name) {
|
||||
return "omitted (--frontend-only)", true
|
||||
}
|
||||
if cfg.SkipFrontend && isFrontendExt(rel) {
|
||||
if cfg.SkipFrontend && filter.IsFrontendExt(rel) {
|
||||
return "omitted (--skip-frontend)", true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func isTestFileName(rel, name string) bool {
|
||||
ext := strings.ToLower(filepath.Ext(name))
|
||||
switch ext {
|
||||
case ".go":
|
||||
return strings.HasSuffix(strings.ToLower(name), "_test.go")
|
||||
case ".java":
|
||||
base := strings.TrimSuffix(name, filepath.Ext(name))
|
||||
if strings.HasSuffix(base, "Test") || strings.HasSuffix(base, "Tests") {
|
||||
return true
|
||||
}
|
||||
slashPath := filepath.ToSlash(rel)
|
||||
return strings.Contains(slashPath, "/src/test/")
|
||||
case ".py":
|
||||
lower := strings.ToLower(name)
|
||||
if strings.HasPrefix(lower, "test_") || strings.HasSuffix(lower, "_test.py") {
|
||||
return true
|
||||
}
|
||||
if strings.EqualFold(name, "conftest.py") {
|
||||
return true
|
||||
}
|
||||
return isTestDir(rel)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isNonReadmeMarkdown(name string) bool {
|
||||
ext := strings.ToLower(filepath.Ext(name))
|
||||
if ext != ".md" && ext != ".markdown" {
|
||||
return false
|
||||
}
|
||||
return !strings.EqualFold(name, "README.md")
|
||||
}
|
||||
|
||||
func isFrontendOrReadme(rel, name string) bool {
|
||||
if isFrontendExt(rel) {
|
||||
return true
|
||||
}
|
||||
return strings.EqualFold(name, "README.md")
|
||||
}
|
||||
|
||||
func isFrontendExt(rel string) bool {
|
||||
ext := strings.ToLower(filepath.Ext(rel))
|
||||
for _, fe := range frontendExts {
|
||||
if strings.EqualFold(ext, fe) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var frontendExts = []string{
|
||||
".html", ".htm", ".gohtml", ".tmpl",
|
||||
".css", ".scss", ".sass", ".less",
|
||||
".js", ".jsx", ".mjs", ".cjs",
|
||||
".ts", ".tsx",
|
||||
".vue", ".svelte",
|
||||
}
|
||||
|
||||
func RenderBreadcrumb(absRoot, relPath string) string {
|
||||
var sb strings.Builder
|
||||
rootName := filepath.Base(absRoot)
|
||||
|
||||
Reference in New Issue
Block a user