Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
dbd736ae81 | |||
ff76a5399c | |||
3f0791466b | |||
dc5eb9cb80 |
4
build.sh
4
build.sh
@@ -1,2 +1,2 @@
|
|||||||
GOOS=windows GOARCH=amd64 go build -o cln.exe .
|
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o cln.exe .
|
||||||
GOOS=linux GOARCH=amd64 go build -o cln .
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o cln .
|
||||||
|
@@ -83,6 +83,7 @@ const (
|
|||||||
// The acceptable range is [16, 231] but here we remove some very dark colors
|
// The acceptable range is [16, 231] but here we remove some very dark colors
|
||||||
// That make text unreadable on a dark terminal
|
// That make text unreadable on a dark terminal
|
||||||
// See https://www.hackitu.de/termcolor256/
|
// See https://www.hackitu.de/termcolor256/
|
||||||
|
// Wait - why are we hardcoding this? lol do for loops not exist in our universe?
|
||||||
var colors = []int{22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 57, 62, 63, 64, 65, 67, 68, 69, 70, 71, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 148, 149, 150, 151, 152, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 184, 185, 186, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 226, 227, 228, 229, 230}
|
var colors = []int{22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 57, 62, 63, 64, 65, 67, 68, 69, 70, 71, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 148, 149, 150, 151, 152, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 184, 185, 186, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 226, 227, 228, 229, 230}
|
||||||
var colorsIndex int = -1
|
var colorsIndex int = -1
|
||||||
var shuffled bool
|
var shuffled bool
|
||||||
|
49
home_test.go
Normal file
49
home_test.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHomeDirectoryPatternExpansion(t *testing.T) {
|
||||||
|
testDir := getTestSubDir(t)
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
|
||||||
|
// Get the actual home directory
|
||||||
|
homeDir, err := os.UserHomeDir()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Create a test directory in the home folder
|
||||||
|
testHomeDir := filepath.Join(homeDir, "synclib_test")
|
||||||
|
err = os.MkdirAll(testHomeDir, 0755)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
defer os.RemoveAll(testHomeDir) // Cleanup
|
||||||
|
|
||||||
|
// Create a test file in the home directory
|
||||||
|
testFile := filepath.Join(testHomeDir, "testhome.csv")
|
||||||
|
err = os.WriteFile(testFile, []byte("test content"), 0644)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Test the pattern with ~/ that should match the file
|
||||||
|
pattern := "~/synclib_test/testhome.csv"
|
||||||
|
links, err := ExpandPattern(pattern, testDir, "target.csv")
|
||||||
|
|
||||||
|
// This should work but currently fails due to the bug
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, len(links), "Pattern should match exactly 1 file")
|
||||||
|
|
||||||
|
if len(links) > 0 {
|
||||||
|
assert.Contains(t, links[0].Source, "testhome.csv")
|
||||||
|
assert.Equal(t, "target.csv", links[0].Target)
|
||||||
|
}
|
||||||
|
}
|
@@ -334,8 +334,13 @@ func preprocessInstructions(instructions []LinkInstruction, filename, workdir st
|
|||||||
|
|
||||||
// loadFromReference loads instructions from a referenced file
|
// loadFromReference loads instructions from a referenced file
|
||||||
func loadFromReference(fromFile, currentFile, workdir string, visited map[string]bool) ([]LinkInstruction, error) {
|
func loadFromReference(fromFile, currentFile, workdir string, visited map[string]bool) ([]LinkInstruction, error) {
|
||||||
|
// First convert home directory if it starts with ~
|
||||||
|
fromPath, err := ConvertHome(fromFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error converting home directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Convert relative paths to absolute paths based on the current file's directory
|
// Convert relative paths to absolute paths based on the current file's directory
|
||||||
fromPath := fromFile
|
|
||||||
if !filepath.IsAbs(fromPath) {
|
if !filepath.IsAbs(fromPath) {
|
||||||
currentDir := filepath.Dir(currentFile)
|
currentDir := filepath.Dir(currentFile)
|
||||||
fromPath = filepath.Join(currentDir, fromPath)
|
fromPath = filepath.Join(currentDir, fromPath)
|
||||||
@@ -351,8 +356,14 @@ func loadFromReference(fromFile, currentFile, workdir string, visited map[string
|
|||||||
|
|
||||||
// expandGlobs expands glob patterns in a single instruction
|
// expandGlobs expands glob patterns in a single instruction
|
||||||
func expandGlobs(instr LinkInstruction, filename, workdir string) ([]LinkInstruction, error) {
|
func expandGlobs(instr LinkInstruction, filename, workdir string) ([]LinkInstruction, error) {
|
||||||
LogSource("Expanding pattern source %s in YAML file %s", instr.Source, filename)
|
// Convert home directory (~) before expanding pattern
|
||||||
newlinks, err := ExpandPattern(instr.Source, workdir, instr.Target)
|
convertedSource, err := ConvertHome(instr.Source)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error converting home directory in source %s: %w", instr.Source, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
LogSource("Expanding pattern source %s in YAML file %s", convertedSource, filename)
|
||||||
|
newlinks, err := ExpandPattern(convertedSource, workdir, instr.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -429,6 +440,11 @@ func parseYAMLFileRecursive(filename, workdir string, visited map[string]bool) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ExpandPattern(source, workdir, target string) (links []LinkInstruction, err error) {
|
func ExpandPattern(source, workdir, target string) (links []LinkInstruction, err error) {
|
||||||
|
// Convert home directory (~) before splitting pattern
|
||||||
|
source, err = ConvertHome(source)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error converting home directory in source %s: %w", source, err)
|
||||||
|
}
|
||||||
static, pattern := doublestar.SplitPattern(source)
|
static, pattern := doublestar.SplitPattern(source)
|
||||||
if static == "" || static == "." {
|
if static == "" || static == "." {
|
||||||
static = workdir
|
static = workdir
|
||||||
|
@@ -110,7 +110,6 @@ func ensureInProjectDir(t *testing.T, testDir string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestParseInstruction(t *testing.T) {
|
func TestParseInstruction(t *testing.T) {
|
||||||
testDir := getTestSubDir(t)
|
testDir := getTestSubDir(t)
|
||||||
|
|
||||||
@@ -133,7 +132,7 @@ func TestParseInstruction(t *testing.T) {
|
|||||||
input string
|
input string
|
||||||
expectError bool
|
expectError bool
|
||||||
errorMsg string
|
errorMsg string
|
||||||
assertions func(*testing.T, LinkInstruction)
|
assertions func(*testing.T, LinkInstruction)
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Basic instruction",
|
name: "Basic instruction",
|
||||||
@@ -351,7 +350,6 @@ func TestLinkInstruction_RunAsync(t *testing.T) {
|
|||||||
func TestLinkInstruction_Undo(t *testing.T) {
|
func TestLinkInstruction_Undo(t *testing.T) {
|
||||||
testDir := getTestSubDir(t)
|
testDir := getTestSubDir(t)
|
||||||
|
|
||||||
|
|
||||||
// Ensure we're working within the project directory
|
// Ensure we're working within the project directory
|
||||||
ensureInProjectDir(t, testDir)
|
ensureInProjectDir(t, testDir)
|
||||||
|
|
||||||
@@ -478,11 +476,11 @@ func TestGlobPatterns(t *testing.T) {
|
|||||||
|
|
||||||
// Verify the full source and target paths using path endings
|
// Verify the full source and target paths using path endings
|
||||||
expectedMappings := map[string]string{
|
expectedMappings := map[string]string{
|
||||||
"src/file1.txt": "dst/file1.txt",
|
"src/file1.txt": "dst/file1.txt",
|
||||||
"src/file2.txt": "dst/file2.txt",
|
"src/file2.txt": "dst/file2.txt",
|
||||||
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
||||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||||
}
|
}
|
||||||
|
|
||||||
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
||||||
@@ -519,8 +517,8 @@ func TestGlobPatterns(t *testing.T) {
|
|||||||
|
|
||||||
// Verify the full source and target paths using path endings
|
// Verify the full source and target paths using path endings
|
||||||
expectedMappings := map[string]string{
|
expectedMappings := map[string]string{
|
||||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||||
}
|
}
|
||||||
|
|
||||||
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
||||||
@@ -557,13 +555,13 @@ func TestGlobPatterns(t *testing.T) {
|
|||||||
|
|
||||||
// Verify the full source and target paths using path endings
|
// Verify the full source and target paths using path endings
|
||||||
expectedMappings := map[string]string{
|
expectedMappings := map[string]string{
|
||||||
"src/file1.txt": "dst/file1.txt",
|
"src/file1.txt": "dst/file1.txt",
|
||||||
"src/file2.txt": "dst/file2.txt",
|
"src/file2.txt": "dst/file2.txt",
|
||||||
"src/file3.log": "dst/file3.log",
|
"src/file3.log": "dst/file3.log",
|
||||||
"src/readme.md": "dst/readme.md",
|
"src/readme.md": "dst/readme.md",
|
||||||
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
||||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||||
}
|
}
|
||||||
|
|
||||||
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
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
|
// Verify the full source and target paths and flags using path endings
|
||||||
expectedMappings := map[string]string{
|
expectedMappings := map[string]string{
|
||||||
"src/file1.txt": "dst/file1.txt",
|
"src/file1.txt": "dst/file1.txt",
|
||||||
"src/file2.txt": "dst/file2.txt",
|
"src/file2.txt": "dst/file2.txt",
|
||||||
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
"src/foobar/foobar.txt": "dst/foobar/foobar.txt",
|
||||||
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
"src/foobar/nested/foobar.txt": "dst/foobar/nested/foobar.txt",
|
||||||
"src/nested/nested.txt": "dst/nested/nested.txt",
|
"src/nested/nested.txt": "dst/nested/nested.txt",
|
||||||
}
|
}
|
||||||
|
|
||||||
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
||||||
@@ -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
|
// Verify 1:1 mapping: static part (src/) is removed, pattern part is preserved using path endings
|
||||||
expectedMappings := map[string]string{
|
expectedMappings := map[string]string{
|
||||||
"src/root.txt": "foobar/root.txt",
|
"src/root.txt": "foobar/root.txt",
|
||||||
"src/foo/foo.txt": "foobar/foo/foo.txt",
|
"src/foo/foo.txt": "foobar/foo/foo.txt",
|
||||||
"src/foo/bar/bar.txt": "foobar/foo/bar/bar.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/baz.txt": "foobar/foo/bar/baz/baz.txt",
|
||||||
"src/foo/bar/baz/nested.txt": "foobar/foo/bar/baz/nested.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
|
// Verify 1:1 mapping: static part (src/foo/) is removed, pattern part is preserved using path endings
|
||||||
expectedMappings := map[string]string{
|
expectedMappings := map[string]string{
|
||||||
"src/foo/foo.txt": "foobar/foo.txt",
|
"src/foo/foo.txt": "foobar/foo.txt",
|
||||||
"src/foo/bar/bar.txt": "foobar/bar/bar.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/baz.txt": "foobar/bar/baz/baz.txt",
|
||||||
"src/foo/bar/baz/nested.txt": "foobar/bar/baz/nested.txt",
|
"src/foo/bar/baz/nested.txt": "foobar/bar/baz/nested.txt",
|
||||||
}
|
}
|
||||||
|
|
||||||
for sourceEnd, expectedTargetEnd := range expectedMappings {
|
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
|
// 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{
|
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",
|
"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) {
|
func TestInstructionEdgeCases(t *testing.T) {
|
||||||
testDir := getTestSubDir(t)
|
testDir := getTestSubDir(t)
|
||||||
|
|
||||||
|
|
||||||
// Ensure we're working within the project directory
|
// Ensure we're working within the project directory
|
||||||
ensureInProjectDir(t, testDir)
|
ensureInProjectDir(t, testDir)
|
||||||
|
|
||||||
@@ -3273,8 +3270,8 @@ func TestSpecialCharacters(t *testing.T) {
|
|||||||
|
|
||||||
// Test instruction parsing with special character directories
|
// Test instruction parsing with special character directories
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
source string
|
source string
|
||||||
target string
|
target string
|
||||||
}{
|
}{
|
||||||
{"dir-with-dashes/file.txt", "target1.txt"},
|
{"dir-with-dashes/file.txt", "target1.txt"},
|
||||||
{"dir_with_underscores/file.txt", "target2.txt"},
|
{"dir_with_underscores/file.txt", "target2.txt"},
|
||||||
@@ -3353,13 +3350,13 @@ func TestSpecialCharacters(t *testing.T) {
|
|||||||
filename string
|
filename string
|
||||||
shouldError bool
|
shouldError bool
|
||||||
}{
|
}{
|
||||||
{"file.txt", false}, // Normal case
|
{"file.txt", false}, // Normal case
|
||||||
{"file-.txt", false}, // Trailing dash
|
{"file-.txt", false}, // Trailing dash
|
||||||
{"file_.txt", false}, // Trailing underscore
|
{"file_.txt", false}, // Trailing underscore
|
||||||
{"file..txt", false}, // Multiple dots
|
{"file..txt", false}, // Multiple dots
|
||||||
{"file-_.txt", false}, // Mixed special chars
|
{"file-_.txt", false}, // Mixed special chars
|
||||||
{"file--test.txt", false}, // Double dashes
|
{"file--test.txt", false}, // Double dashes
|
||||||
{"file__test.txt", false}, // Double underscores
|
{"file__test.txt", false}, // Double underscores
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range edgeCases {
|
for _, tc := range edgeCases {
|
||||||
@@ -3485,7 +3482,7 @@ func TestMixedSourceTypes(t *testing.T) {
|
|||||||
file := filepath.Join(pattern2Dir, fmt.Sprintf("doc%d.md", i))
|
file := filepath.Join(pattern2Dir, fmt.Sprintf("doc%d.md", i))
|
||||||
err = os.WriteFile(file, []byte(fmt.Sprintf("pattern2 content %d", i)), 0644)
|
err = os.WriteFile(file, []byte(fmt.Sprintf("pattern2 content %d", i)), 0644)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create first YAML file
|
// Create first YAML file
|
||||||
yamlFile1 := filepath.Join(testDir, "pattern1.yaml")
|
yamlFile1 := filepath.Join(testDir, "pattern1.yaml")
|
||||||
@@ -4303,3 +4300,142 @@ func TestYAMLConfigFrom(t *testing.T) {
|
|||||||
assert.Contains(t, err.Error(), "circular reference detected")
|
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