Remove some unused shit and write tests for coverage

This commit is contained in:
2025-12-19 12:12:42 +01:00
parent 1df0263a42
commit da5b621cb6
19 changed files with 1892 additions and 390 deletions

View File

@@ -86,28 +86,17 @@ func SplitPattern(pattern string) (string, string) {
splitPatternLogger.Debug("Splitting pattern")
splitPatternLogger.Trace("Original pattern: %q", pattern)
// Resolve the pattern first to handle ~ expansion and make it absolute
resolvedPattern := ResolvePath(pattern)
splitPatternLogger.Trace("Resolved pattern: %q", resolvedPattern)
// Split the pattern first to separate static and wildcard parts
static, remainingPattern := doublestar.SplitPattern(pattern)
splitPatternLogger.Trace("After split: static=%q, pattern=%q", static, remainingPattern)
static, pattern := doublestar.SplitPattern(resolvedPattern)
// Resolve the static part to handle ~ expansion and make it absolute
// ResolvePath already normalizes to forward slashes
static = ResolvePath(static)
splitPatternLogger.Trace("Resolved static part: %q", static)
// Ensure static part is properly resolved
if static == "" {
cwd, err := os.Getwd()
if err != nil {
splitPatternLogger.Error("Error getting current working directory: %v", err)
return "", ""
}
static = cwd
splitPatternLogger.Debug("Static part is empty, defaulting to current working directory: %q", static)
} else {
// Static part should already be resolved by ResolvePath
static = strings.ReplaceAll(static, "\\", "/")
}
splitPatternLogger.Trace("Final static path: %q, Remaining pattern: %q", static, pattern)
return static, pattern
splitPatternLogger.Trace("Final static path: %q, Remaining pattern: %q", static, remainingPattern)
return static, remainingPattern
}
type FileCommandAssociation struct {
@@ -140,7 +129,7 @@ func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[s
static, pattern := SplitPattern(glob)
associateFilesLogger.Trace("Glob parts for %q → static=%q pattern=%q", glob, static, pattern)
// Use resolved file for matching
// Use resolved file for matching (already normalized to forward slashes by ResolvePath)
absFile := resolvedFile
associateFilesLogger.Trace("Absolute file path resolved for matching: %q", absFile)
@@ -283,9 +272,6 @@ func LoadCommands(args []string) ([]ModifyCommand, map[string]interface{}, error
loadCommandsLogger.Error("Failed to load TOML commands from argument %q: %v", arg, err)
return nil, nil, fmt.Errorf("failed to load commands from TOML files: %w", err)
}
for k, v := range newVariables {
variables[k] = v
}
} else {
// Default to YAML for .yml, .yaml, or any other extension
loadCommandsLogger.Debug("Loading YAML commands from %q", arg)
@@ -294,9 +280,9 @@ func LoadCommands(args []string) ([]ModifyCommand, map[string]interface{}, error
loadCommandsLogger.Error("Failed to load YAML commands from argument %q: %v", arg, err)
return nil, nil, fmt.Errorf("failed to load commands from cook files: %w", err)
}
for k, v := range newVariables {
variables[k] = v
}
}
for k, v := range newVariables {
variables[k] = v
}
loadCommandsLogger.Debug("Successfully loaded %d commands from %q", len(newCommands), arg)
@@ -485,24 +471,8 @@ func LoadCommandsFromTomlFile(tomlFileData []byte) ([]ModifyCommand, map[string]
}
}
// If we found commands in the wrapped structure, use those
if len(tomlData.Commands) > 0 {
commands = tomlData.Commands
loadTomlCommandLogger.Debug("Found %d commands in wrapped TOML structure", len(commands))
} else {
// Try to parse as direct array (similar to YAML format)
directCommands := []ModifyCommand{}
err = toml.Unmarshal(tomlFileData, &directCommands)
if err != nil {
loadTomlCommandLogger.Error("Failed to unmarshal TOML file data as direct array: %v", err)
return nil, nil, fmt.Errorf("failed to unmarshal TOML file as direct array: %w", err)
}
if len(directCommands) > 0 {
commands = directCommands
loadTomlCommandLogger.Debug("Found %d commands in direct TOML array", len(directCommands))
}
}
// Use commands from wrapped structure
commands = tomlData.Commands
loadTomlCommandLogger.Debug("Successfully unmarshaled %d commands and %d variables", len(commands), len(variables))
loadTomlCommandLogger.Trace("Unmarshaled commands: %v", commands)
loadTomlCommandLogger.Trace("Unmarshaled variables: %v", variables)