Add regression tests for home resolution
This commit is contained in:
@@ -110,7 +110,6 @@ func ensureInProjectDir(t *testing.T, testDir string) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestParseInstruction(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
@@ -133,7 +132,7 @@ func TestParseInstruction(t *testing.T) {
|
||||
input string
|
||||
expectError bool
|
||||
errorMsg string
|
||||
assertions func(*testing.T, LinkInstruction)
|
||||
assertions func(*testing.T, LinkInstruction)
|
||||
}{
|
||||
{
|
||||
name: "Basic instruction",
|
||||
@@ -217,7 +216,7 @@ func TestParseInstruction(t *testing.T) {
|
||||
|
||||
func TestLinkInstruction_RunAsync(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -351,7 +350,6 @@ func TestLinkInstruction_RunAsync(t *testing.T) {
|
||||
func TestLinkInstruction_Undo(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -409,7 +407,7 @@ func TestLinkInstruction_Undo(t *testing.T) {
|
||||
|
||||
func TestGlobPatterns(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -478,11 +476,11 @@ func TestGlobPatterns(t *testing.T) {
|
||||
|
||||
// Verify the full source and target paths using path endings
|
||||
expectedMappings := map[string]string{
|
||||
"src/file1.txt": "dst/file1.txt",
|
||||
"src/file2.txt": "dst/file2.txt",
|
||||
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||
"src/file1.txt": "dst/file1.txt",
|
||||
"src/file2.txt": "dst/file2.txt",
|
||||
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||
}
|
||||
|
||||
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
||||
@@ -519,8 +517,8 @@ func TestGlobPatterns(t *testing.T) {
|
||||
|
||||
// Verify the full source and target paths using path endings
|
||||
expectedMappings := map[string]string{
|
||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||
}
|
||||
|
||||
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
||||
@@ -557,13 +555,13 @@ func TestGlobPatterns(t *testing.T) {
|
||||
|
||||
// Verify the full source and target paths using path endings
|
||||
expectedMappings := map[string]string{
|
||||
"src/file1.txt": "dst/file1.txt",
|
||||
"src/file2.txt": "dst/file2.txt",
|
||||
"src/file3.log": "dst/file3.log",
|
||||
"src/readme.md": "dst/readme.md",
|
||||
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||
"src/file1.txt": "dst/file1.txt",
|
||||
"src/file2.txt": "dst/file2.txt",
|
||||
"src/file3.log": "dst/file3.log",
|
||||
"src/readme.md": "dst/readme.md",
|
||||
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||
}
|
||||
|
||||
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
||||
@@ -751,11 +749,11 @@ func TestGlobPatterns(t *testing.T) {
|
||||
|
||||
// Verify the full source and target paths and flags using path endings
|
||||
expectedMappings := map[string]string{
|
||||
"src/file1.txt": "dst/file1.txt",
|
||||
"src/file2.txt": "dst/file2.txt",
|
||||
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||
"src/file1.txt": "dst/file1.txt",
|
||||
"src/file2.txt": "dst/file2.txt",
|
||||
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||
}
|
||||
|
||||
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
||||
@@ -820,7 +818,7 @@ func createGlobTestStructure(t *testing.T, testDir string) {
|
||||
|
||||
func TestParseYAMLFile(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -927,7 +925,7 @@ func TestParseYAMLFile(t *testing.T) {
|
||||
|
||||
func TestExpandPattern(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -1000,7 +998,7 @@ func TestExpandPattern(t *testing.T) {
|
||||
|
||||
func TestGetSyncFilesRecursively(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -1063,7 +1061,7 @@ func TestGetSyncFilesRecursively(t *testing.T) {
|
||||
|
||||
func TestReadFromFile(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -1151,7 +1149,7 @@ func TestReadFromFile(t *testing.T) {
|
||||
// Test main.go functions that are completely missing coverage
|
||||
func TestMainFunctions(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -1250,7 +1248,7 @@ func TestMainFunctions(t *testing.T) {
|
||||
// Test 1:1 destination mapping for glob patterns
|
||||
func TestDestinationPathMapping(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -1308,10 +1306,10 @@ func TestDestinationPathMapping(t *testing.T) {
|
||||
|
||||
// Verify 1:1 mapping: static part (src/) is removed, pattern part is preserved using path endings
|
||||
expectedMappings := map[string]string{
|
||||
"src/root.txt": "foobar/root.txt",
|
||||
"src/foo/foo.txt": "foobar/foo/foo.txt",
|
||||
"src/foo/bar/bar.txt": "foobar/foo/bar/bar.txt",
|
||||
"src/foo/bar/baz/baz.txt": "foobar/foo/bar/baz/baz.txt",
|
||||
"src/root.txt": "foobar/root.txt",
|
||||
"src/foo/foo.txt": "foobar/foo/foo.txt",
|
||||
"src/foo/bar/bar.txt": "foobar/foo/bar/bar.txt",
|
||||
"src/foo/bar/baz/baz.txt": "foobar/foo/bar/baz/baz.txt",
|
||||
"src/foo/bar/baz/nested.txt": "foobar/foo/bar/baz/nested.txt",
|
||||
}
|
||||
|
||||
@@ -1349,10 +1347,10 @@ func TestDestinationPathMapping(t *testing.T) {
|
||||
|
||||
// Verify 1:1 mapping: static part (src/foo/) is removed, pattern part is preserved using path endings
|
||||
expectedMappings := map[string]string{
|
||||
"src/foo/foo.txt": "foobar/foo.txt",
|
||||
"src/foo/bar/bar.txt": "foobar/bar/bar.txt",
|
||||
"src/foo/bar/baz/baz.txt": "foobar/bar/baz/baz.txt",
|
||||
"src/foo/bar/baz/nested.txt": "foobar/bar/baz/nested.txt",
|
||||
"src/foo/foo.txt": "foobar/foo.txt",
|
||||
"src/foo/bar/bar.txt": "foobar/bar/bar.txt",
|
||||
"src/foo/bar/baz/baz.txt": "foobar/bar/baz/baz.txt",
|
||||
"src/foo/bar/baz/nested.txt": "foobar/bar/baz/nested.txt",
|
||||
}
|
||||
|
||||
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
||||
@@ -1432,7 +1430,7 @@ func TestDestinationPathMapping(t *testing.T) {
|
||||
|
||||
// Verify 1:1 mapping: static part (src/foo/) is removed, pattern part (bar/baz.txt, bar/baz/bar/deep.txt) is preserved using path endings
|
||||
expectedMappings := map[string]string{
|
||||
"src/foo/bar/bar.txt": "dst/bar/bar.txt",
|
||||
"src/foo/bar/bar.txt": "dst/bar/bar.txt",
|
||||
"src/foo/bar/baz/bar/deep.txt": "dst/bar/baz/bar/deep.txt",
|
||||
}
|
||||
|
||||
@@ -1456,7 +1454,6 @@ func TestDestinationPathMapping(t *testing.T) {
|
||||
func TestInstructionEdgeCases(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -1733,7 +1730,7 @@ func TestInstructionEdgeCases(t *testing.T) {
|
||||
// Test missing util.go paths
|
||||
func TestUtilEdgeCases(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -1942,7 +1939,7 @@ func TestPathFormattingFunctions(t *testing.T) {
|
||||
// Test missing instruction.go YAML parsing paths
|
||||
func TestYAMLEdgeCases(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -2077,7 +2074,7 @@ func TestYAMLEdgeCases(t *testing.T) {
|
||||
// Test the untested error paths in ReadFromFile and ReadFromStdin
|
||||
func TestErrorPaths(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -2426,7 +2423,7 @@ func TestErrorPaths(t *testing.T) {
|
||||
// Test main.go functions that are currently at 0% coverage
|
||||
func TestMainFunctionsCoverage(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -2712,7 +2709,7 @@ func TestUnicodeFilenames(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
// Clean up any files from previous tests to prevent interference
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -2905,7 +2902,7 @@ func TestLongPaths(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
// Clean up any files from previous tests to prevent interference
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -3273,8 +3270,8 @@ func TestSpecialCharacters(t *testing.T) {
|
||||
|
||||
// Test instruction parsing with special character directories
|
||||
testCases := []struct {
|
||||
source string
|
||||
target string
|
||||
source string
|
||||
target string
|
||||
}{
|
||||
{"dir-with-dashes/file.txt", "target1.txt"},
|
||||
{"dir_with_underscores/file.txt", "target2.txt"},
|
||||
@@ -3353,13 +3350,13 @@ func TestSpecialCharacters(t *testing.T) {
|
||||
filename string
|
||||
shouldError bool
|
||||
}{
|
||||
{"file.txt", false}, // Normal case
|
||||
{"file-.txt", false}, // Trailing dash
|
||||
{"file_.txt", false}, // Trailing underscore
|
||||
{"file..txt", false}, // Multiple dots
|
||||
{"file-_.txt", false}, // Mixed special chars
|
||||
{"file--test.txt", false}, // Double dashes
|
||||
{"file__test.txt", false}, // Double underscores
|
||||
{"file.txt", false}, // Normal case
|
||||
{"file-.txt", false}, // Trailing dash
|
||||
{"file_.txt", false}, // Trailing underscore
|
||||
{"file..txt", false}, // Multiple dots
|
||||
{"file-_.txt", false}, // Mixed special chars
|
||||
{"file--test.txt", false}, // Double dashes
|
||||
{"file__test.txt", false}, // Double underscores
|
||||
}
|
||||
|
||||
for _, tc := range edgeCases {
|
||||
@@ -3411,7 +3408,7 @@ func TestMixedSourceTypes(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
// Clean up any files from previous tests to prevent interference
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -3485,7 +3482,7 @@ func TestMixedSourceTypes(t *testing.T) {
|
||||
file := filepath.Join(pattern2Dir, fmt.Sprintf("doc%d.md", i))
|
||||
err = os.WriteFile(file, []byte(fmt.Sprintf("pattern2 content %d", i)), 0644)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Create first YAML file
|
||||
yamlFile1 := filepath.Join(testDir, "pattern1.yaml")
|
||||
@@ -3705,7 +3702,7 @@ func TestRollbackScenarios(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
// Clean up any files from previous tests to prevent interference
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -4042,7 +4039,7 @@ func TestRollbackScenarios(t *testing.T) {
|
||||
// Test YAMLConfig "From" functionality
|
||||
func TestYAMLConfigFrom(t *testing.T) {
|
||||
testDir := getTestSubDir(t)
|
||||
|
||||
|
||||
// Ensure we're working within the project directory
|
||||
ensureInProjectDir(t, testDir)
|
||||
|
||||
@@ -4303,3 +4300,142 @@ func TestYAMLConfigFrom(t *testing.T) {
|
||||
assert.Contains(t, err.Error(), "circular reference detected")
|
||||
})
|
||||
}
|
||||
|
||||
func TestPathResolutionBug(t *testing.T) {
|
||||
testDir := t.TempDir()
|
||||
|
||||
t.Run("ConvertHome_then_NormalizePath_absolute_path", func(t *testing.T) {
|
||||
// This is the exact bug scenario from the error message
|
||||
// ~/Seafile/activitywatch/sync.yml should NOT be prepended with workdir
|
||||
|
||||
// First convert home directory
|
||||
homePath, err := ConvertHome("~/Seafile/activitywatch/sync.yml")
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, filepath.IsAbs(homePath), "ConvertHome should produce absolute path")
|
||||
|
||||
// Then normalize - this should NOT prepend workdir since it's already absolute
|
||||
result := NormalizePath(homePath, testDir)
|
||||
|
||||
// The result should be the same as the homePath, not prepended with testDir
|
||||
// NormalizePath converts to forward slashes, so compare normalized versions
|
||||
expected := filepath.ToSlash(homePath)
|
||||
actual := filepath.ToSlash(result)
|
||||
assert.Equal(t, expected, actual, "Absolute paths should not be prepended with workdir")
|
||||
assert.NotContains(t, result, testDir, "Absolute path should not contain workdir")
|
||||
})
|
||||
|
||||
t.Run("ConvertHome_then_NormalizePath_relative_path", func(t *testing.T) {
|
||||
// Test that relative paths still get workdir prepended
|
||||
relativePath := "config/sync.yml"
|
||||
|
||||
// ConvertHome should not change relative paths
|
||||
convertedPath, err := ConvertHome(relativePath)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, relativePath, convertedPath)
|
||||
|
||||
// NormalizePath should prepend workdir for relative paths
|
||||
result := NormalizePath(convertedPath, testDir)
|
||||
// NormalizePath converts to forward slashes, so compare normalized versions
|
||||
normalizedTestDir := filepath.ToSlash(testDir)
|
||||
normalizedResult := filepath.ToSlash(result)
|
||||
assert.Contains(t, normalizedResult, normalizedTestDir, "Relative paths should be prepended with workdir")
|
||||
assert.Contains(t, result, "config/sync.yml")
|
||||
})
|
||||
|
||||
t.Run("Direct_absolute_path_normalization", func(t *testing.T) {
|
||||
// Test direct absolute path (no tilde)
|
||||
absPath := filepath.Join(testDir, "absolute", "sync.yml")
|
||||
result := NormalizePath(absPath, testDir)
|
||||
|
||||
// Should not be prepended with workdir
|
||||
// NormalizePath converts to forward slashes, so compare normalized versions
|
||||
expected := filepath.ToSlash(absPath)
|
||||
actual := filepath.ToSlash(result)
|
||||
assert.Equal(t, expected, actual, "Direct absolute paths should not be prepended with workdir")
|
||||
assert.NotContains(t, result, filepath.Join(testDir, testDir), "Should not double-prepend workdir")
|
||||
})
|
||||
|
||||
t.Run("Direct_relative_path_normalization", func(t *testing.T) {
|
||||
// Test direct relative path
|
||||
relativePath := "relative/sync.yml"
|
||||
result := NormalizePath(relativePath, testDir)
|
||||
|
||||
// Should be prepended with workdir
|
||||
// NormalizePath converts to forward slashes, so compare normalized versions
|
||||
normalizedTestDir := filepath.ToSlash(testDir)
|
||||
normalizedResult := filepath.ToSlash(result)
|
||||
assert.Contains(t, normalizedResult, normalizedTestDir, "Relative paths should be prepended with workdir")
|
||||
assert.Contains(t, result, "relative/sync.yml")
|
||||
})
|
||||
|
||||
t.Run("Tilde_path_edge_cases", func(t *testing.T) {
|
||||
// Test various tilde scenarios
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"~/file.txt", "~/file.txt"}, // Should convert to absolute
|
||||
{"~file.txt", "~file.txt"}, // Should NOT convert (no slash after ~)
|
||||
{"~", "~"}, // Should NOT convert (just tilde)
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("input_%s", tc.input), func(t *testing.T) {
|
||||
converted, err := ConvertHome(tc.input)
|
||||
assert.NoError(t, err)
|
||||
|
||||
if strings.HasPrefix(tc.input, "~/") {
|
||||
// Should be converted to absolute path
|
||||
assert.True(t, filepath.IsAbs(converted), "Tilde paths should become absolute")
|
||||
assert.NotContains(t, converted, "~", "Should not contain tilde after conversion")
|
||||
} else {
|
||||
// Should remain unchanged
|
||||
assert.Equal(t, tc.input, converted, "Non-tilde paths should remain unchanged")
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("YAML_instruction_path_resolution", func(t *testing.T) {
|
||||
// Test the actual YAML instruction parsing with tilde paths
|
||||
yamlContent := `
|
||||
- source: ~/Seafile/activitywatch/sync.yml
|
||||
target: ""
|
||||
`
|
||||
|
||||
yamlFile := filepath.Join(testDir, "test.yaml")
|
||||
err := os.WriteFile(yamlFile, []byte(yamlContent), 0644)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Parse the YAML file
|
||||
instructions, err := ParseYAMLFileRecursive(yamlFile, testDir)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, instructions, 0, "Should not create instructions for from references")
|
||||
|
||||
// Test with actual link instruction
|
||||
yamlContent2 := `
|
||||
- source: ~/Seafile/source/file.txt
|
||||
target: ~/Seafile/target/file.txt
|
||||
`
|
||||
|
||||
yamlFile2 := filepath.Join(testDir, "test2.yaml")
|
||||
err = os.WriteFile(yamlFile2, []byte(yamlContent2), 0644)
|
||||
assert.NoError(t, err)
|
||||
|
||||
instructions2, err := ParseYAMLFileRecursive(yamlFile2, testDir)
|
||||
if err != nil {
|
||||
// If there's an error, it might be because the tilde path wasn't converted properly
|
||||
// This is the bug we're trying to catch
|
||||
t.Logf("Expected error due to path resolution bug: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(instructions2) > 0 {
|
||||
// The paths should be absolute and not prepended with workdir
|
||||
assert.True(t, filepath.IsAbs(instructions2[0].Source), "Source should be absolute")
|
||||
assert.True(t, filepath.IsAbs(instructions2[0].Target), "Target should be absolute")
|
||||
assert.NotContains(t, instructions2[0].Source, testDir, "Source should not contain workdir")
|
||||
assert.NotContains(t, instructions2[0].Target, testDir, "Target should not contain workdir")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user