Files
rollup/cmd/files_test.go

146 lines
3.7 KiB
Go

package cmd
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/tnypxl/rollup/internal/config"
)
func TestMatchGlob(t *testing.T) {
tests := []struct {
pattern string
path string
expected bool
}{
{"*.go", "file.go", true},
{"*.go", "file.txt", false},
{"**/*.go", "dir/file.go", true},
{"**/*.go", "dir/subdir/file.go", true},
{"dir/*.go", "dir/file.go", true},
{"dir/*.go", "otherdir/file.go", false},
}
for _, test := range tests {
result := matchGlob(test.pattern, test.path)
if result != test.expected {
t.Errorf("matchGlob(%q, %q) = %v; want %v", test.pattern, test.path, result, test.expected)
}
}
}
func TestIsCodeGenerated(t *testing.T) {
patterns := []string{"generated_*.go", "**/auto_*.go"}
tests := []struct {
path string
expected bool
}{
{"generated_file.go", true},
{"normal_file.go", false},
{"subdir/auto_file.go", true},
{"subdir/normal_file.go", false},
}
for _, test := range tests {
result := isCodeGenerated(test.path, patterns)
if result != test.expected {
t.Errorf("isCodeGenerated(%q, %v) = %v; want %v", test.path, patterns, result, test.expected)
}
}
}
func TestIsIgnored(t *testing.T) {
patterns := []string{"*.tmp", "**/*.log"}
tests := []struct {
path string
expected bool
}{
{"file.tmp", true},
{"file.go", false},
{"subdir/file.log", true},
{"subdir/file.txt", false},
}
for _, test := range tests {
result := isIgnored(test.path, patterns)
if result != test.expected {
t.Errorf("isIgnored(%q, %v) = %v; want %v", test.path, patterns, result, test.expected)
}
}
}
func TestRunRollup(t *testing.T) {
// Create a temporary directory for testing
tempDir, err := os.MkdirTemp("", "rollup_test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
// Create some test files
files := map[string]string{
"file1.go": "package main\n\nfunc main() {}\n",
"file2.txt": "This is a text file.\n",
"subdir/file3.go": "package subdir\n\nfunc Func() {}\n",
"subdir/file4.json": "{\"key\": \"value\"}\n",
}
for name, content := range files {
path := filepath.Join(tempDir, name)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatalf("Failed to write file: %v", err)
}
}
// Set up test configuration
cfg := &config.Config{
FileTypes: []string{"go", "txt"},
Ignore: []string{"*.json"},
CodeGenerated: []string{},
}
// Run the rollup
if err := runRollup(); err != nil {
t.Fatalf("runRollup() failed: %v", err)
}
// Check if the output file was created
outputFiles, err := filepath.Glob(filepath.Join(tempDir, "*.rollup.md"))
if err != nil {
t.Fatalf("Error globbing for output file: %v", err)
}
if len(outputFiles) == 0 {
allFiles, _ := filepath.Glob(filepath.Join(tempDir, "*"))
t.Fatalf("No rollup.md file found. Files in directory: %v", allFiles)
}
outputFile := outputFiles[0]
// Read the content of the output file
content, err := os.ReadFile(outputFile)
if err != nil {
t.Fatalf("Failed to read output file: %v", err)
}
// Check if the content includes the expected files
expectedContent := []string{
"# File: file1.go",
"# File: file2.txt",
"# File: subdir/file3.go",
}
for _, expected := range expectedContent {
if !strings.Contains(string(content), expected) {
t.Errorf("Output file does not contain expected content: %s", expected)
}
}
// Check if the ignored file is not included
if strings.Contains(string(content), "file4.json") {
t.Errorf("Output file contains ignored file: file4.json")
}
}