forked from Nixius/authelia
1
0
Fork 0
ATLAS/docker/ss-atlas/internal/checks/lines_test.go

62 lines
1.3 KiB
Go

package checks
import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
)
const maxLines = 400
// TestNoFileExceedsMaxLines ensures no project file exceeds the line limit.
// This keeps the codebase modular and maintainable.
func TestNoFileExceedsMaxLines(t *testing.T) {
_, thisFile, _, _ := runtime.Caller(0)
root := filepath.Join(filepath.Dir(thisFile), "..", "..")
exts := map[string]bool{
".go": true, ".yml": true, ".yaml": true, ".html": true,
".sh": true, ".md": true,
}
var over []string
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
name := info.Name()
if name == "vendor" || name == "node_modules" || name == ".git" {
return filepath.SkipDir
}
return nil
}
if !exts[strings.ToLower(filepath.Ext(path))] {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return nil
}
lines := strings.Count(string(data), "\n")
if strings.HasSuffix(string(data), "\n") {
// last line has newline
} else if len(data) > 0 {
lines++
}
if lines > maxLines {
rel, _ := filepath.Rel(root, path)
over = append(over, rel+":"+strconv.Itoa(lines))
}
return nil
})
if len(over) > 0 {
t.Errorf("files exceed %d lines:\n%s", maxLines, strings.Join(over, "\n"))
}
}