Files
gibidify/fileproc/walker_test.go
Ismo Vuorinen 3f65b813bd feat: update go to 1.25, add permissions and envs (#49)
* chore(ci): update go to 1.25, add permissions and envs
* fix(ci): update pr-lint.yml
* chore: update go, fix linting
* fix: tests and linting
* fix(lint): lint fixes, renovate should now pass
* fix: updates, security upgrades
* chore: workflow updates, lint
* fix: more lint, checkmake, and other fixes
* fix: more lint, convert scripts to POSIX compliant
* fix: simplify codeql workflow
* tests: increase test coverage, fix found issues
* fix(lint): editorconfig checking, add to linters
* fix(lint): shellcheck, add to linters
* fix(lint): apply cr comment suggestions
* fix(ci): remove step-security/harden-runner
* fix(lint): remove duplication, apply cr fixes
* fix(ci): tests in CI/CD pipeline
* chore(lint): deduplication of strings
* fix(lint): apply cr comment suggestions
* fix(ci): actionlint
* fix(lint): apply cr comment suggestions
* chore: lint, add deps management
2025-10-10 12:14:42 +03:00

106 lines
2.9 KiB
Go

package fileproc_test
import (
"path/filepath"
"testing"
"github.com/spf13/viper"
"github.com/ivuorinen/gibidify/fileproc"
"github.com/ivuorinen/gibidify/testutil"
)
func TestProdWalkerWithIgnore(t *testing.T) {
// Create a temporary directory structure.
rootDir := t.TempDir()
subDir := testutil.CreateTestDirectory(t, rootDir, "vendor")
// Write sample files
testutil.CreateTestFiles(t, rootDir, []testutil.FileSpec{
{Name: "file1.go", Content: "content"},
{Name: "file2.txt", Content: "content"},
})
testutil.CreateTestFile(t, subDir, "file_in_vendor.txt", []byte("content")) // should be ignored
// .gitignore that ignores *.txt and itself
gitignoreContent := `*.txt
.gitignore
`
testutil.CreateTestFile(t, rootDir, ".gitignore", []byte(gitignoreContent))
// Initialize config to ignore "vendor" directory
testutil.ResetViperConfig(t, "")
viper.Set("ignoreDirectories", []string{"vendor"})
// Run walker
w := fileproc.NewProdWalker()
found, err := w.Walk(rootDir)
testutil.MustSucceed(t, err, "walking directory")
// We expect only file1.go to appear
if len(found) != 1 {
t.Errorf("Expected 1 file to pass filters, got %d: %v", len(found), found)
}
if len(found) == 1 && filepath.Base(found[0]) != "file1.go" {
t.Errorf("Expected file1.go, got %s", found[0])
}
}
func TestProdWalkerBinaryCheck(t *testing.T) {
rootDir := t.TempDir()
// Create test files
testutil.CreateTestFiles(t, rootDir, []testutil.FileSpec{
{Name: "somefile.exe", Content: "fake-binary-content"},
{Name: "keep.go", Content: "package main"},
})
// Reset and load default config
testutil.ResetViperConfig(t, "")
// Reset FileTypeRegistry to ensure clean state
fileproc.ResetRegistryForTesting()
// Ensure cleanup runs even if test fails
t.Cleanup(fileproc.ResetRegistryForTesting)
// Run walker
w := fileproc.NewProdWalker()
found, err := w.Walk(rootDir)
testutil.MustSucceed(t, err, "walking directory")
// Only "keep.go" should be returned
if len(found) != 1 {
t.Errorf("Expected 1 file, got %d: %v", len(found), found)
}
if len(found) == 1 && filepath.Base(found[0]) != "keep.go" {
t.Errorf("Expected keep.go in results, got %s", found[0])
}
}
func TestProdWalkerSizeLimit(t *testing.T) {
rootDir := t.TempDir()
// Create test files
largeFileData := make([]byte, 6*1024*1024) // 6 MB
testutil.CreateTestFile(t, rootDir, "largefile.txt", largeFileData)
testutil.CreateTestFile(t, rootDir, "smallfile.go", []byte("package main"))
// Reset and load default config, which sets size limit to 5 MB
testutil.ResetViperConfig(t, "")
w := fileproc.NewProdWalker()
found, err := w.Walk(rootDir)
if err != nil {
t.Fatalf("Walk returned error: %v", err)
}
// We should only get the small file
if len(found) != 1 {
t.Errorf("Expected 1 file under size limit, got %d", len(found))
}
if len(found) == 1 && filepath.Base(found[0]) != "smallfile.go" {
t.Errorf("Expected smallfile.go, got %s", found[0])
}
}