add test suite
This commit is contained in:
+21
-10
@@ -46,15 +46,15 @@ func (f *Filter) ShouldSkipFile(path, name string, info os.FileInfo) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
if f.OmitMd && isNonReadmeMarkdown(path, name) {
|
||||
if f.OmitMd && IsNonReadmeMarkdown(path, name) {
|
||||
return true
|
||||
}
|
||||
|
||||
if f.FrontendOnly && !isFrontendOrReadme(path, name) {
|
||||
if f.FrontendOnly && !IsFrontendOrReadme(path, name) {
|
||||
return true
|
||||
}
|
||||
|
||||
if f.OmitTest && isTestFile(path, name) {
|
||||
if f.OmitTest && IsTestFile(path, name) {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -68,25 +68,37 @@ func (f *Filter) ShouldSkipFile(path, name string, info os.FileInfo) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func isNonReadmeMarkdown(path, name string) bool {
|
||||
func IsNonReadmeMarkdown(path, name string) bool {
|
||||
ext := strings.ToLower(filepath.Ext(path))
|
||||
if ext != ".md" && ext != ".markdown" {
|
||||
return false
|
||||
}
|
||||
return !strings.EqualFold(name, "README.md")
|
||||
return !strings.EqualFold(name, "README.md") && !strings.EqualFold(name, "README.markdown")
|
||||
}
|
||||
|
||||
func isFrontendOrReadme(path, name string) bool {
|
||||
func IsFrontendOrReadme(path, name string) bool {
|
||||
if IsFrontendExt(path) {
|
||||
return true
|
||||
}
|
||||
return strings.EqualFold(name, "README.md")
|
||||
}
|
||||
|
||||
func IsFrontendExt(path string) bool {
|
||||
ext := strings.ToLower(filepath.Ext(path))
|
||||
for _, fe := range config.FrontendExts {
|
||||
if strings.EqualFold(ext, fe) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return strings.EqualFold(name, "README.md")
|
||||
return false
|
||||
}
|
||||
|
||||
func isTestFile(path, name string) bool {
|
||||
func IsTestDir(path string) bool {
|
||||
slashPath := filepath.ToSlash(path)
|
||||
return strings.Contains(slashPath, "/tests/") || strings.HasPrefix(slashPath, "tests/")
|
||||
}
|
||||
|
||||
func IsTestFile(path, name string) bool {
|
||||
ext := strings.ToLower(filepath.Ext(path))
|
||||
|
||||
switch ext {
|
||||
@@ -107,8 +119,7 @@ func isTestFile(path, name string) bool {
|
||||
if strings.EqualFold(name, "conftest.py") {
|
||||
return true
|
||||
}
|
||||
slashPath := filepath.ToSlash(path)
|
||||
return strings.Contains(slashPath, "/tests/")
|
||||
return IsTestDir(path)
|
||||
}
|
||||
|
||||
return false
|
||||
|
||||
@@ -0,0 +1,269 @@
|
||||
package filter
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.flo.fo/FLO/dirmd/internal/config"
|
||||
)
|
||||
|
||||
func TestIsNonReadmeMarkdown(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
want bool
|
||||
}{
|
||||
{"readme_md_false", "README.md", false},
|
||||
{"readme_markdown_false", "README.markdown", false},
|
||||
{"readme_lower_false", "readme.md", false},
|
||||
{"other_md_true", "CHANGELOG.md", true},
|
||||
{"other_markdown_true", "LICENSE.markdown", true},
|
||||
{"txt_false", "README.txt", false},
|
||||
{"go_false", "main.go", false},
|
||||
{"empty_false", "", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsNonReadmeMarkdown(tt.path, filepath.Base(tt.path)); got != tt.want {
|
||||
t.Errorf("IsNonReadmeMarkdown(%q) = %v; want %v", tt.path, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsFrontendExt(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
want bool
|
||||
}{
|
||||
{"html_true", "page.html", true},
|
||||
{"htm_true", "page.htm", true},
|
||||
{"css_true", "style.css", true},
|
||||
{"js_true", "app.js", true},
|
||||
{"ts_true", "app.ts", true},
|
||||
{"vue_true", "component.vue", true},
|
||||
{"svelte_true", "app.svelte", true},
|
||||
{"gohtml_true", "template.gohtml", true},
|
||||
{"go_false", "main.go", false},
|
||||
{"md_false", "README.md", false},
|
||||
{"json_false", "config.json", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsFrontendExt(tt.path); got != tt.want {
|
||||
t.Errorf("IsFrontendExt(%q) = %v; want %v", tt.path, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsFrontendOrReadme(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
want bool
|
||||
}{
|
||||
{"html_true", "page.html", true},
|
||||
{"readme_true", "README.md", true},
|
||||
{"readme_lower_true", "readme.md", true},
|
||||
{"css_true", "style.css", true},
|
||||
{"go_false", "main.go", false},
|
||||
{"changelog_false", "CHANGELOG.md", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsFrontendOrReadme(tt.path, filepath.Base(tt.path)); got != tt.want {
|
||||
t.Errorf("IsFrontendOrReadme(%q) = %v; want %v", tt.path, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsTestDir(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
want bool
|
||||
}{
|
||||
{"tests_top_level_true", "tests/", true},
|
||||
{"tests_nested_true", "pkg/tests/unit/", true},
|
||||
{"test_single_letter_false", "test/", false},
|
||||
{"src_test_false_current_behavior", "src/test/java/com/example/", false},
|
||||
{"normal_false", "src/", false},
|
||||
{"tests_file_false", "testfile.go", false},
|
||||
{"empty_false", "", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsTestDir(tt.path); got != tt.want {
|
||||
t.Errorf("IsTestDir(%q) = %v; want %v", tt.path, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsTestFile(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
want bool
|
||||
}{
|
||||
{"go_test_true", "path/to/file_test.go", true},
|
||||
{"go_test_uppercase_true", "path/to/file_TEST.go", true},
|
||||
{"go_normal_false", "path/to/main.go", false},
|
||||
{"go_integration_test_true", "path/to/integration_test.go", true},
|
||||
{"java_test_suffix_true", "MyClassTest.java", true},
|
||||
{"java_tests_suffix_true", "MyClassTests.java", true},
|
||||
{"java_normal_false", "MyClass.java", false},
|
||||
{"java_src_test_true", "src/test/java/com/example/MyTest.java", true},
|
||||
{"py_test_prefix_true", "test_main.py", true},
|
||||
{"py_test_suffix_true", "main_test.py", true},
|
||||
{"py_conftest_true", "conftest.py", true},
|
||||
{"py_normal_false", "main.py", false},
|
||||
{"py_tests_dir_true", "tests/conftest.py", true},
|
||||
{"rust_cfg_false", "main.rs", false},
|
||||
{"c_false", "main.c", false},
|
||||
{"unknown_false", "README.md", false},
|
||||
{"empty_false", "", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsTestFile(tt.path, filepath.Base(tt.path)); got != tt.want {
|
||||
t.Errorf("IsTestFile(%q, %q) = %v; want %v", tt.path, filepath.Base(tt.path), got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldSkipFile(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
filter *Filter
|
||||
path string
|
||||
size int64
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "large_file_skipped",
|
||||
filter: &Filter{MaxSize: 1000, Extensions: []string{}, Ignores: []string{}},
|
||||
path: "big.bin",
|
||||
size: 2000,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "small_file_not_skipped",
|
||||
filter: &Filter{MaxSize: 1000, Extensions: []string{}, Ignores: []string{}},
|
||||
path: "tiny.txt",
|
||||
size: 500,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "excluded_extension_skipped",
|
||||
filter: &Filter{MaxSize: 1000000, Extensions: []string{".bin"}, Ignores: []string{}},
|
||||
path: "file.bin",
|
||||
size: 500,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "hidden_file_skipped",
|
||||
filter: &Filter{MaxSize: 1000000, Extensions: []string{}, Ignores: []string{}},
|
||||
path: ".gitignore",
|
||||
size: 500,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "omit_test_go_test",
|
||||
filter: &Filter{MaxSize: 1000000, Extensions: []string{}, Ignores: []string{}, OmitTest: true},
|
||||
path: "main_test.go",
|
||||
size: 500,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "omit_test_normal_go",
|
||||
filter: &Filter{MaxSize: 1000000, Extensions: []string{}, Ignores: []string{}, OmitTest: true},
|
||||
path: "main.go",
|
||||
size: 500,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "omit_md_changelog",
|
||||
filter: &Filter{MaxSize: 1000000, Extensions: []string{}, Ignores: []string{}, OmitMd: true},
|
||||
path: "CHANGELOG.md",
|
||||
size: 500,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "omit_md_readme_not",
|
||||
filter: &Filter{MaxSize: 1000000, Extensions: []string{}, Ignores: []string{}, OmitMd: true},
|
||||
path: "README.md",
|
||||
size: 500,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "omit_md_readme_markdown_not",
|
||||
filter: &Filter{MaxSize: 1000000, Extensions: []string{}, Ignores: []string{}, OmitMd: true},
|
||||
path: "README.markdown",
|
||||
size: 500,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "frontend_only_non_frontend",
|
||||
filter: &Filter{MaxSize: 1000000, Extensions: []string{}, Ignores: []string{}, FrontendOnly: true},
|
||||
path: "main.go",
|
||||
size: 500,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "frontend_only_readme",
|
||||
filter: &Filter{MaxSize: 1000000, Extensions: []string{}, Ignores: []string{}, FrontendOnly: true},
|
||||
path: "README.md",
|
||||
size: 500,
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
fi := fakeFileInfo{size: tt.size}
|
||||
if got := tt.filter.ShouldSkipFile(tt.path, filepath.Base(tt.path), fi); got != tt.want {
|
||||
t.Errorf("ShouldSkipFile() = %v; want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldSkipDir(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
filter *Filter
|
||||
dir string
|
||||
want bool
|
||||
}{
|
||||
{"hidden_dir", &Filter{Ignores: []string{}}, ".git", true},
|
||||
{"node_modules_with_defaults", &Filter{Ignores: config.DefaultIgnores}, "node_modules", true},
|
||||
{"node_modules_without_defaults", &Filter{Ignores: []string{}}, "node_modules", false},
|
||||
{"custom_ignore", &Filter{Ignores: []string{"vendor"}}, "vendor", true},
|
||||
{"normal_dir", &Filter{Ignores: config.DefaultIgnores}, "src", false},
|
||||
{"empty_ignores_no_skip", &Filter{Ignores: []string{}}, "src", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.filter.ShouldSkipDir(tt.dir); got != tt.want {
|
||||
t.Errorf("ShouldSkipDir(%q) = %v; want %v", tt.dir, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type fakeFileInfo struct {
|
||||
size int64
|
||||
}
|
||||
|
||||
func (f fakeFileInfo) Name() string { return "" }
|
||||
func (f fakeFileInfo) Size() int64 { return f.size }
|
||||
func (f fakeFileInfo) Mode() os.FileMode { return 0 }
|
||||
func (f fakeFileInfo) ModTime() time.Time { return time.Time{} }
|
||||
func (f fakeFileInfo) IsDir() bool { return false }
|
||||
func (f fakeFileInfo) Sys() any { return nil }
|
||||
Reference in New Issue
Block a user