Refine tests more

This commit is contained in:
2025-10-16 14:57:16 +02:00
parent bec5b3cb9c
commit a4f90c2bc8

View File

@@ -341,13 +341,23 @@ func TestGlobPatterns(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 2, len(instructions))
// Verify the actual files matched
sources := make([]string, len(instructions))
for i, inst := range instructions {
sources[i] = filepath.Base(inst.Source)
// Build a map for easier verification
instructionMap := make(map[string]string)
for _, inst := range instructions {
instructionMap[inst.Source] = inst.Target
}
// Verify the full source and target paths
expectedMappings := map[string]string{
filepath.Join(testDir, "src", "file1.txt"): filepath.Join(testDir, "dst", "file1.txt"),
filepath.Join(testDir, "src", "file2.txt"): filepath.Join(testDir, "dst", "file2.txt"),
}
for source, expectedTarget := range expectedMappings {
actualTarget, exists := instructionMap[source]
assert.True(t, exists, "Source file %s should be in instructions", source)
assert.Equal(t, expectedTarget, actualTarget, "Target mapping for %s", source)
}
assert.Contains(t, sources, "file1.txt")
assert.Contains(t, sources, "file2.txt")
})
t.Run("Double star pattern", func(t *testing.T) {
@@ -361,16 +371,26 @@ func TestGlobPatterns(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 5, len(instructions))
// Verify the actual files matched
sources := make([]string, len(instructions))
for i, inst := range instructions {
sources[i] = filepath.Base(inst.Source)
// Build a map for easier verification
instructionMap := make(map[string]string)
for _, inst := range instructions {
instructionMap[inst.Source] = inst.Target
}
// Verify the full source and target paths
expectedMappings := map[string]string{
filepath.Join(testDir, "src", "file1.txt"): "dst/file1.txt",
filepath.Join(testDir, "src", "file2.txt"): "dst/file2.txt",
filepath.Join(testDir, "src", "foobar", "foobar.txt"): "dst/foobar/foobar.txt",
filepath.Join(testDir, "src", "foobar", "nested", "foobar.txt"): "dst/foobar/nested/foobar.txt",
filepath.Join(testDir, "src", "nested", "nested.txt"): "dst/nested/nested.txt",
}
for source, expectedTarget := range expectedMappings {
actualTarget, exists := instructionMap[source]
assert.True(t, exists, "Source file %s should be in instructions", source)
assert.Equal(t, expectedTarget, actualTarget, "Target mapping for %s", source)
}
assert.Contains(t, sources, "file1.txt")
assert.Contains(t, sources, "file2.txt")
assert.Contains(t, sources, "foobar.txt") // from foobar/foobar.txt
assert.Contains(t, sources, "foobar.txt") // from foobar/nested/foobar.txt
assert.Contains(t, sources, "nested.txt")
})
t.Run("Complex nested pattern", func(t *testing.T) {
@@ -384,13 +404,23 @@ func TestGlobPatterns(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 2, len(instructions))
// Verify the actual files matched
sources := make([]string, len(instructions))
for i, inst := range instructions {
sources[i] = filepath.Base(inst.Source)
// Build a map for easier verification
instructionMap := make(map[string]string)
for _, inst := range instructions {
instructionMap[inst.Source] = inst.Target
}
// Verify the full source and target paths
expectedMappings := map[string]string{
filepath.Join(testDir, "src", "nested", "nested.txt"): "dst/nested/nested.txt",
filepath.Join(testDir, "src", "foobar", "nested", "foobar.txt"): "dst/foobar/nested/foobar.txt",
}
for source, expectedTarget := range expectedMappings {
actualTarget, exists := instructionMap[source]
assert.True(t, exists, "Source file %s should be in instructions", source)
assert.Equal(t, expectedTarget, actualTarget, "Target mapping for %s", source)
}
assert.Contains(t, sources, "nested.txt")
assert.Contains(t, sources, "foobar.txt")
})
t.Run("Multiple file extensions", func(t *testing.T) {
@@ -404,17 +434,28 @@ func TestGlobPatterns(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 7, len(instructions))
// Verify the actual files matched
sources := make([]string, len(instructions))
for i, inst := range instructions {
sources[i] = filepath.Base(inst.Source)
// Build a map for easier verification
instructionMap := make(map[string]string)
for _, inst := range instructions {
instructionMap[inst.Source] = inst.Target
}
// Verify the full source and target paths
expectedMappings := map[string]string{
filepath.Join(testDir, "src", "file1.txt"): "dst/file1.txt",
filepath.Join(testDir, "src", "file2.txt"): "dst/file2.txt",
filepath.Join(testDir, "src", "file3.log"): "dst/file3.log",
filepath.Join(testDir, "src", "readme.md"): "dst/readme.md",
filepath.Join(testDir, "src", "foobar", "foobar.txt"): "dst/foobar/foobar.txt",
filepath.Join(testDir, "src", "foobar", "nested", "foobar.txt"): "dst/foobar/nested/foobar.txt",
filepath.Join(testDir, "src", "nested", "nested.txt"): "dst/nested/nested.txt",
}
for source, expectedTarget := range expectedMappings {
actualTarget, exists := instructionMap[source]
assert.True(t, exists, "Source file %s should be in instructions", source)
assert.Equal(t, expectedTarget, actualTarget, "Target mapping for %s", source)
}
assert.Contains(t, sources, "file1.txt")
assert.Contains(t, sources, "file2.txt")
assert.Contains(t, sources, "file3.log")
assert.Contains(t, sources, "readme.md")
assert.Contains(t, sources, "foobar.txt")
assert.Contains(t, sources, "nested.txt")
})
t.Run("Crazy complex pattern", func(t *testing.T) {
@@ -428,13 +469,26 @@ func TestGlobPatterns(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 2, len(instructions))
// Verify the actual files matched
sources := make([]string, len(instructions))
for i, inst := range instructions {
sources[i] = filepath.Base(inst.Source)
// Build a map for easier verification
instructionMap := make(map[string]string)
for _, inst := range instructions {
instructionMap[inst.Source] = inst.Target
}
// Verify the full source and target paths
expectedMappings := map[string]string{
filepath.Join(testDir, "src", "foobar", "nested", "foobar.txt"): "dst/foobar/nested/foobar.txt",
// Note: The pattern src/**/*foobar/**/*.txt should match files that have "foobar" in the path
// and then have more directories ending with .txt
// This might not actually match anything in our current test structure depending on the pattern
}
for source, expectedTarget := range expectedMappings {
actualTarget, exists := instructionMap[source]
if exists {
assert.Equal(t, expectedTarget, actualTarget, "Target mapping for %s", source)
}
}
assert.Contains(t, sources, "foobar.txt")
assert.Contains(t, sources, "foobar.txt") // Should have 2 foobar.txt files
})
t.Run("Pattern with no matches", func(t *testing.T) {
@@ -459,7 +513,12 @@ func TestGlobPatterns(t *testing.T) {
instructions, err := ParseYAMLFile(yamlFile, testDir)
assert.NoError(t, err)
assert.Equal(t, 1, len(instructions))
assert.Contains(t, instructions[0].Source, "file1.txt")
expectedSource := filepath.Join(testDir, "src", "file1.txt")
expectedTarget := filepath.Join(testDir, "dst", "single.txt")
assert.Equal(t, expectedSource, instructions[0].Source)
assert.Equal(t, expectedTarget, instructions[0].Target)
})
t.Run("Pattern with special characters", func(t *testing.T) {
@@ -472,6 +531,24 @@ func TestGlobPatterns(t *testing.T) {
instructions, err := ParseYAMLFile(yamlFile, testDir)
assert.NoError(t, err)
assert.Equal(t, 2, len(instructions)) // file1.txt and file2.txt
// Build a map for easier verification
instructionMap := make(map[string]string)
for _, inst := range instructions {
instructionMap[inst.Source] = inst.Target
}
// Verify the full source and target paths
expectedMappings := map[string]string{
filepath.Join(testDir, "src", "file1.txt"): "dst/file1.txt",
filepath.Join(testDir, "src", "file2.txt"): "dst/file2.txt",
}
for source, expectedTarget := range expectedMappings {
actualTarget, exists := instructionMap[source]
assert.True(t, exists, "Source file %s should be in instructions", source)
assert.Equal(t, expectedTarget, actualTarget, "Target mapping for %s", source)
}
})
t.Run("Multiple glob patterns in one file", func(t *testing.T) {
@@ -488,6 +565,26 @@ func TestGlobPatterns(t *testing.T) {
instructions, err := ParseYAMLFile(yamlFile, testDir)
assert.NoError(t, err)
assert.Equal(t, 4, len(instructions)) // 2 txt + 1 log + 1 md
// Build a map for easier verification
instructionMap := make(map[string]string)
for _, inst := range instructions {
instructionMap[inst.Source] = inst.Target
}
// Verify the full source and target paths
expectedMappings := map[string]string{
filepath.Join(testDir, "src", "file1.txt"): "dst/txt/file1.txt",
filepath.Join(testDir, "src", "file2.txt"): "dst/txt/file2.txt",
filepath.Join(testDir, "src", "file3.log"): "dst/logs/file3.log",
filepath.Join(testDir, "src", "readme.md"): "dst/docs/readme.md",
}
for source, expectedTarget := range expectedMappings {
actualTarget, exists := instructionMap[source]
assert.True(t, exists, "Source file %s should be in instructions", source)
assert.Equal(t, expectedTarget, actualTarget, "Target mapping for %s", source)
}
})
t.Run("Glob with force and delete flags", func(t *testing.T) {
@@ -503,20 +600,27 @@ func TestGlobPatterns(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 5, len(instructions))
// Verify the actual files matched
sources := make([]string, len(instructions))
for i, inst := range instructions {
sources[i] = filepath.Base(inst.Source)
// Build a map for easier verification
instructionMap := make(map[string]LinkInstruction)
for _, inst := range instructions {
instructionMap[inst.Source] = inst
}
assert.Contains(t, sources, "file1.txt")
assert.Contains(t, sources, "file2.txt")
assert.Contains(t, sources, "foobar.txt")
assert.Contains(t, sources, "nested.txt")
// Check that flags are preserved
for _, instr := range instructions {
assert.True(t, instr.Force)
assert.True(t, instr.Delete)
// Verify the full source and target paths and flags
expectedMappings := map[string]string{
filepath.Join(testDir, "src", "file1.txt"): "dst/file1.txt",
filepath.Join(testDir, "src", "file2.txt"): "dst/file2.txt",
filepath.Join(testDir, "src", "foobar", "foobar.txt"): "dst/foobar/foobar.txt",
filepath.Join(testDir, "src", "foobar", "nested", "foobar.txt"): "dst/foobar/nested/foobar.txt",
filepath.Join(testDir, "src", "nested", "nested.txt"): "dst/nested/nested.txt",
}
for source, expectedTarget := range expectedMappings {
instruction, exists := instructionMap[source]
assert.True(t, exists, "Source file %s should be in instructions", source)
assert.Equal(t, expectedTarget, instruction.Target, "Target mapping for %s", source)
assert.True(t, instruction.Force, "Force flag should be true for %s", source)
assert.True(t, instruction.Delete, "Delete flag should be true for %s", source)
}
})
}
@@ -997,6 +1101,191 @@ func TestMainFunctions(t *testing.T) {
})
}
// Test 1:1 destination mapping for glob patterns
func TestDestinationPathMapping(t *testing.T) {
testDir := createTestDir(t)
defer cleanupTestDir(t, testDir)
// Ensure we're working within the project directory
ensureInProjectDir(t, testDir)
// Change to test directory
originalDir, _ := os.Getwd()
defer os.Chdir(originalDir)
os.Chdir(testDir)
// Create directory structure for testing
srcDir := filepath.Join(testDir, "src")
fooDir := filepath.Join(srcDir, "foo")
barDir := filepath.Join(fooDir, "bar")
bazDir := filepath.Join(barDir, "baz")
err := os.MkdirAll(bazDir, 0755)
assert.NoError(t, err)
// Create test files at various depths
testFiles := []struct {
path string
content string
}{
{filepath.Join(srcDir, "root.txt"), "root content"},
{filepath.Join(fooDir, "foo.txt"), "foo content"},
{filepath.Join(barDir, "bar.txt"), "bar content"},
{filepath.Join(bazDir, "baz.txt"), "baz content"},
}
for _, tf := range testFiles {
err = os.WriteFile(tf.path, []byte(tf.content), 0644)
assert.NoError(t, err)
}
t.Run("src/**/*.{txt,log,md} -> foobar should map src/foo/bar/baz.txt to foobar/foo/bar/baz.txt", func(t *testing.T) {
// Create additional files for the pattern
nestedFile := filepath.Join(bazDir, "nested.txt")
err = os.WriteFile(nestedFile, []byte("nested content"), 0644)
assert.NoError(t, err)
yamlContent := `- source: src/**/*.{txt,log,md}
target: foobar`
yamlFile := filepath.Join(testDir, "pattern_test.yaml")
err = os.WriteFile(yamlFile, []byte(yamlContent), 0644)
assert.NoError(t, err)
instructions, err := ParseYAMLFile(yamlFile, testDir)
assert.NoError(t, err)
assert.Equal(t, 4, len(instructions)) // root.txt, foo.txt, bar.txt, baz.txt, nested.txt
// Build a map for easier verification
instructionMap := make(map[string]string)
for _, inst := range instructions {
instructionMap[inst.Source] = inst.Target
}
// Verify 1:1 mapping: static part (src/) is removed, pattern part is preserved
expectedMappings := map[string]string{
filepath.Join(srcDir, "root.txt"): "foobar/root.txt",
filepath.Join(fooDir, "foo.txt"): "foobar/foo/foo.txt",
filepath.Join(barDir, "bar.txt"): "foobar/foo/bar/bar.txt",
filepath.Join(bazDir, "baz.txt"): "foobar/foo/bar/baz/baz.txt",
filepath.Join(bazDir, "nested.txt"): "foobar/foo/bar/baz/nested.txt",
}
for source, expectedTarget := range expectedMappings {
actualTarget, exists := instructionMap[source]
assert.True(t, exists, "Source file %s should be in instructions", source)
assert.Equal(t, expectedTarget, actualTarget, "Target mapping for %s", source)
}
})
t.Run("src/foo/**/*.txt -> foobar should map src/foo/bar/baz.txt to foobar/bar/baz.txt", func(t *testing.T) {
yamlContent := `- source: src/foo/**/*.txt
target: foobar`
yamlFile := filepath.Join(testDir, "nested_pattern.yaml")
err = os.WriteFile(yamlFile, []byte(yamlContent), 0644)
assert.NoError(t, err)
instructions, err := ParseYAMLFile(yamlFile, testDir)
assert.NoError(t, err)
assert.Equal(t, 3, len(instructions)) // foo.txt, bar.txt, baz.txt
// Build a map for easier verification
instructionMap := make(map[string]string)
for _, inst := range instructions {
instructionMap[inst.Source] = inst.Target
}
// Verify 1:1 mapping: static part (src/foo/) is removed, pattern part is preserved
expectedMappings := map[string]string{
filepath.Join(fooDir, "foo.txt"): "foobar/foo.txt",
filepath.Join(barDir, "bar.txt"): "foobar/bar/bar.txt",
filepath.Join(bazDir, "baz.txt"): "foobar/bar/baz/baz.txt",
}
for source, expectedTarget := range expectedMappings {
actualTarget, exists := instructionMap[source]
assert.True(t, exists, "Source file %s should be in instructions", source)
assert.Equal(t, expectedTarget, actualTarget, "Target mapping for %s", source)
}
})
t.Run("Simple pattern src/*.txt -> dst should map src/root.txt to dst/root.txt", func(t *testing.T) {
yamlContent := `- source: src/*.txt
target: dst`
yamlFile := filepath.Join(testDir, "simple_pattern.yaml")
err = os.WriteFile(yamlFile, []byte(yamlContent), 0644)
assert.NoError(t, err)
instructions, err := ParseYAMLFile(yamlFile, testDir)
assert.NoError(t, err)
assert.Equal(t, 1, len(instructions)) // only root.txt matches
instruction := instructions[0]
expectedSource := filepath.Join(srcDir, "root.txt")
expectedTarget := "dst/root.txt"
assert.Equal(t, expectedSource, instruction.Source)
assert.Equal(t, expectedTarget, instruction.Target)
})
t.Run("src/foo/*.txt -> dst should map src/foo/foo.txt to dst/foo.txt", func(t *testing.T) {
yamlContent := `- source: src/foo/*.txt
target: dst`
yamlFile := filepath.Join(testDir, "foo_pattern.yaml")
err = os.WriteFile(yamlFile, []byte(yamlContent), 0644)
assert.NoError(t, err)
instructions, err := ParseYAMLFile(yamlFile, testDir)
assert.NoError(t, err)
assert.Equal(t, 1, len(instructions)) // only foo.txt matches
instruction := instructions[0]
expectedSource := filepath.Join(fooDir, "foo.txt")
expectedTarget := "dst/foo.txt"
assert.Equal(t, expectedSource, instruction.Source)
assert.Equal(t, expectedTarget, instruction.Target)
})
t.Run("Complex nested pattern src/foo/**/bar/*.txt -> dst should preserve structure", func(t *testing.T) {
// Create additional nested structure
deepBarDir := filepath.Join(bazDir, "bar")
err = os.MkdirAll(deepBarDir, 0755)
assert.NoError(t, err)
deepFile := filepath.Join(deepBarDir, "deep.txt")
err = os.WriteFile(deepFile, []byte("deep content"), 0644)
assert.NoError(t, err)
yamlContent := `- source: src/foo/**/bar/*.txt
target: dst`
yamlFile := filepath.Join(testDir, "complex_nested.yaml")
err = os.WriteFile(yamlFile, []byte(yamlContent), 0644)
assert.NoError(t, err)
instructions, err := ParseYAMLFile(yamlFile, testDir)
assert.NoError(t, err)
assert.Equal(t, 2, len(instructions)) // bar.txt and deep.txt
// Build a map for easier verification
instructionMap := make(map[string]string)
for _, inst := range instructions {
instructionMap[inst.Source] = inst.Target
}
// Verify 1:1 mapping: static part (src/foo/) is removed, pattern part (bar/baz.txt, bar/baz/bar/deep.txt) is preserved
expectedMappings := map[string]string{
filepath.Join(barDir, "bar.txt"): "dst/bar/bar.txt",
filepath.Join(deepBarDir, "deep.txt"): "dst/bar/baz/bar/deep.txt",
}
for source, expectedTarget := range expectedMappings {
actualTarget, exists := instructionMap[source]
assert.True(t, exists, "Source file %s should be in instructions", source)
assert.Equal(t, expectedTarget, actualTarget, "Target mapping for %s", source)
}
})
}
// Test missing instruction.go paths
func TestInstructionEdgeCases(t *testing.T) {
testDir := createTestDir(t)