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 .
|
||||
GOOS=linux GOARCH=amd64 go build -o cln .
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o cln.exe .
|
||||
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
|
||||
// That make text unreadable on a dark terminal
|
||||
// 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 colorsIndex int = -1
|
||||
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
|
||||
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
|
||||
fromPath := fromFile
|
||||
if !filepath.IsAbs(fromPath) {
|
||||
currentDir := filepath.Dir(currentFile)
|
||||
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
|
||||
func expandGlobs(instr LinkInstruction, filename, workdir string) ([]LinkInstruction, error) {
|
||||
LogSource("Expanding pattern source %s in YAML file %s", instr.Source, filename)
|
||||
newlinks, err := ExpandPattern(instr.Source, workdir, instr.Target)
|
||||
// Convert home directory (~) before expanding pattern
|
||||
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 {
|
||||
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) {
|
||||
// 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)
|
||||
if static == "" || static == "." {
|
||||
static = workdir
|
||||
|
@@ -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")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user