|
|
|
@@ -110,7 +110,6 @@ func ensureInProjectDir(t *testing.T, testDir string) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestParseInstruction(t *testing.T) {
|
|
|
|
|
testDir := getTestSubDir(t)
|
|
|
|
|
|
|
|
|
@@ -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)
|
|
|
|
|
|
|
|
|
@@ -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)
|
|
|
|
|
|
|
|
|
@@ -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")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|