From 2d523dfe64dff4162832a17f6ec23f1274e630fe Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 28 Mar 2025 01:08:48 +0100 Subject: [PATCH] Rename pattern to regex --- main.go | 4 +- processor/regex.go | 4 +- processor/regex_test.go | 2 +- regression/regression_test.go | 2 +- utils/modifycommand.go | 8 +- utils/modifycommand_test.go | 330 +++++++++++++++++----------------- 6 files changed, 175 insertions(+), 175 deletions(-) diff --git a/main.go b/main.go index dc0919a..a5e27ef 100644 --- a/main.go +++ b/main.go @@ -121,10 +121,10 @@ func main() { // Aggregate all the modifications and execute them modifications := []utils.ReplaceCommand{} for _, command := range commands { - logger.Info("Processing file %q with command %q", file, command.Pattern) + logger.Info("Processing file %q with command %q", file, command.Regex) commands, err := processor.ProcessRegex(fileDataStr, command) if err != nil { - logger.Error("Failed to process file %q with command %q: %v", file, command.Pattern, err) + logger.Error("Failed to process file %q with command %q: %v", file, command.Regex, err) return } modifications = append(modifications, commands...) diff --git a/processor/regex.go b/processor/regex.go index 638c42d..ec8178d 100644 --- a/processor/regex.go +++ b/processor/regex.go @@ -23,14 +23,14 @@ type CaptureGroup struct { // ProcessContent applies regex replacement with Lua processing func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceCommand, error) { var commands []utils.ReplaceCommand - logger.Trace("Processing regex: %q", command.Pattern) + logger.Trace("Processing regex: %q", command.Regex) // Start timing the regex processing startTime := time.Now() // We don't HAVE to do this multiple times for a pattern // But it's quick enough for us to not care - pattern := resolveRegexPlaceholders(command.Pattern) + pattern := resolveRegexPlaceholders(command.Regex) logger.Debug("Compiling regex pattern: %s", pattern) patternCompileStart := time.Now() diff --git a/processor/regex_test.go b/processor/regex_test.go index 575bf89..f70579c 100644 --- a/processor/regex_test.go +++ b/processor/regex_test.go @@ -33,7 +33,7 @@ func normalizeWhitespace(s string) string { func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) { command := utils.ModifyCommand{ - Pattern: regex, + Regex: regex, Lua: lua, LogLevel: "TRACE", } diff --git a/regression/regression_test.go b/regression/regression_test.go index 053585a..aca7a91 100644 --- a/regression/regression_test.go +++ b/regression/regression_test.go @@ -10,7 +10,7 @@ import ( func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) { command := utils.ModifyCommand{ - Pattern: regex, + Regex: regex, Lua: lua, LogLevel: "TRACE", } diff --git a/utils/modifycommand.go b/utils/modifycommand.go index 35cfa05..f95b884 100644 --- a/utils/modifycommand.go +++ b/utils/modifycommand.go @@ -11,7 +11,7 @@ import ( type ModifyCommand struct { Name string `yaml:"name"` - Pattern string `yaml:"pattern"` + Regex string `yaml:"regex"` Lua string `yaml:"lua"` Files []string `yaml:"files"` Git bool `yaml:"git"` @@ -21,7 +21,7 @@ type ModifyCommand struct { type CookFile []ModifyCommand func (c *ModifyCommand) Validate() error { - if c.Pattern == "" { + if c.Regex == "" { return fmt.Errorf("pattern is required") } if c.Lua == "" { @@ -49,7 +49,7 @@ func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[s continue } if matches { - logger.Debug("Found match for file %q and command %q", file, command.Pattern) + logger.Debug("Found match for file %q and command %q", file, command.Regex) fileCommands[file] = append(fileCommands[file], command) associationCount++ } @@ -147,7 +147,7 @@ func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) { } command := ModifyCommand{ - Pattern: args[0], + Regex: args[0], Lua: args[1], Files: args[2:], Git: *GitFlag, diff --git a/utils/modifycommand_test.go b/utils/modifycommand_test.go index 9274026..c8277f3 100644 --- a/utils/modifycommand_test.go +++ b/utils/modifycommand_test.go @@ -17,7 +17,7 @@ func TestModifyCommandValidate(t *testing.T) { { name: "Valid command", command: ModifyCommand{ - Pattern: "test pattern", + Regex: "test pattern", Lua: "test expression", Files: []string{"file1", "file2"}, LogLevel: "INFO", @@ -27,7 +27,7 @@ func TestModifyCommandValidate(t *testing.T) { { name: "Missing pattern", command: ModifyCommand{ - Pattern: "", + Regex: "", Lua: "test expression", Files: []string{"file1", "file2"}, LogLevel: "INFO", @@ -37,7 +37,7 @@ func TestModifyCommandValidate(t *testing.T) { { name: "Missing LuaExpr", command: ModifyCommand{ - Pattern: "test pattern", + Regex: "test pattern", Lua: "", Files: []string{"file1", "file2"}, LogLevel: "INFO", @@ -47,7 +47,7 @@ func TestModifyCommandValidate(t *testing.T) { { name: "Missing files", command: ModifyCommand{ - Pattern: "test pattern", + Regex: "test pattern", Lua: "test expression", Files: []string{}, LogLevel: "INFO", @@ -57,7 +57,7 @@ func TestModifyCommandValidate(t *testing.T) { { name: "Default log level", command: ModifyCommand{ - Pattern: "test pattern", + Regex: "test pattern", Lua: "test expression", Files: []string{"file1", "file2"}, LogLevel: "", @@ -128,19 +128,19 @@ func TestAssociateFilesWithCommands(t *testing.T) { // Define commands with different globs commands := []ModifyCommand{ { - Pattern: "pattern1", - Lua: "expr1", - Files: []string{"*.xml"}, + Regex: "pattern1", + Lua: "expr1", + Files: []string{"*.xml"}, }, { - Pattern: "pattern2", - Lua: "expr2", - Files: []string{"*.txt"}, + Regex: "pattern2", + Lua: "expr2", + Files: []string{"*.txt"}, }, { - Pattern: "pattern3", - Lua: "expr3", - Files: []string{"subdir/*"}, + Regex: "pattern3", + Lua: "expr3", + Files: []string{"subdir/*"}, }, } @@ -161,7 +161,7 @@ func TestAssociateFilesWithCommands(t *testing.T) { for file, cmds := range associations { t.Logf("File %s is associated with %d commands", file, len(cmds)) for i, cmd := range cmds { - t.Logf(" Command %d: Pattern=%s, Files=%v", i, cmd.Pattern, cmd.Files) + t.Logf(" Command %d: Pattern=%s, Files=%v", i, cmd.Regex, cmd.Files) } // Specific validation based on our file types @@ -230,19 +230,19 @@ func TestAssociateFilesWithCommands(t *testing.T) { func TestAggregateGlobs(t *testing.T) { commands := []ModifyCommand{ { - Pattern: "pattern1", - Lua: "expr1", - Files: []string{"*.xml", "*.txt"}, + Regex: "pattern1", + Lua: "expr1", + Files: []string{"*.xml", "*.txt"}, }, { - Pattern: "pattern2", - Lua: "expr2", - Files: []string{"*.xml", "*.json"}, + Regex: "pattern2", + Lua: "expr2", + Files: []string{"*.xml", "*.json"}, }, { - Pattern: "pattern3", - Lua: "expr3", - Files: []string{"subdir/*.xml"}, + Regex: "pattern3", + Lua: "expr3", + Files: []string{"subdir/*.xml"}, }, } @@ -359,8 +359,8 @@ func TestLoadCommandFromArgs(t *testing.T) { cmd := commands[0] // Check command properties - if cmd.Pattern != tc.args[0] { - t.Errorf("Expected pattern %q, got %q", tc.args[0], cmd.Pattern) + if cmd.Regex != tc.args[0] { + t.Errorf("Expected pattern %q, got %q", tc.args[0], cmd.Regex) } if cmd.Lua != tc.args[1] { @@ -407,10 +407,10 @@ func TestLoadCommandsFromCookFileSuccess(t *testing.T) { assert.NoError(t, err) assert.Len(t, commands, 2) assert.Equal(t, "command1", commands[0].Name) - assert.Equal(t, "*.txt", commands[0].Pattern) + assert.Equal(t, "*.txt", commands[0].Regex) assert.Equal(t, "replace", commands[0].Lua) assert.Equal(t, "command2", commands[1].Name) - assert.Equal(t, "*.go", commands[1].Pattern) + assert.Equal(t, "*.go", commands[1].Regex) assert.Equal(t, "delete", commands[1].Lua) } @@ -435,10 +435,10 @@ func TestLoadCommandsFromCookFileWithComments(t *testing.T) { assert.NoError(t, err) assert.Len(t, commands, 2) assert.Equal(t, "command1", commands[0].Name) - assert.Equal(t, "*.txt", commands[0].Pattern) + assert.Equal(t, "*.txt", commands[0].Regex) assert.Equal(t, "replace", commands[0].Lua) assert.Equal(t, "command2", commands[1].Name) - assert.Equal(t, "*.go", commands[1].Pattern) + assert.Equal(t, "*.go", commands[1].Regex) assert.Equal(t, "delete", commands[1].Lua) } @@ -454,10 +454,10 @@ func TestLoadCommandsFromCookFileWithFlowStyle(t *testing.T) { assert.NoError(t, err) assert.Len(t, commands, 2) assert.Equal(t, "command1", commands[0].Name) - assert.Equal(t, "*.txt", commands[0].Pattern) + assert.Equal(t, "*.txt", commands[0].Regex) assert.Equal(t, "replace", commands[0].Lua) assert.Equal(t, "command2", commands[1].Name) - assert.Equal(t, "*.go", commands[1].Pattern) + assert.Equal(t, "*.go", commands[1].Regex) assert.Equal(t, "delete", commands[1].Lua) } @@ -513,13 +513,13 @@ func TestLoadCommandsFromCookFileWithMultipleEntries(t *testing.T) { assert.NoError(t, err) assert.Len(t, commands, 3) assert.Equal(t, "command1", commands[0].Name) - assert.Equal(t, "*.txt", commands[0].Pattern) + assert.Equal(t, "*.txt", commands[0].Regex) assert.Equal(t, "replace", commands[0].Lua) assert.Equal(t, "command2", commands[1].Name) - assert.Equal(t, "*.go", commands[1].Pattern) + assert.Equal(t, "*.go", commands[1].Regex) assert.Equal(t, "delete", commands[1].Lua) assert.Equal(t, "command3", commands[2].Name) - assert.Equal(t, "*.md", commands[2].Pattern) + assert.Equal(t, "*.md", commands[2].Regex) assert.Equal(t, "append", commands[2].Lua) } @@ -582,7 +582,7 @@ func TestLoadCommandFromArgsWithValidArguments(t *testing.T) { // Assert assert.NoError(t, err) assert.Len(t, commands, 1) - assert.Equal(t, "*.go", commands[0].Pattern) + assert.Equal(t, "*.go", commands[0].Regex) assert.Equal(t, "return x", commands[0].Lua) assert.Equal(t, []string{"file1.go", "file2.go"}, commands[0].Files) assert.Equal(t, true, commands[0].Git) @@ -662,7 +662,7 @@ func TestLoadCommandFromArgsPopulatesFieldsCorrectly(t *testing.T) { // Assert assert.NoError(t, err) assert.Len(t, commands, 1) - assert.Equal(t, "*.txt", commands[0].Pattern) + assert.Equal(t, "*.txt", commands[0].Regex) assert.Equal(t, "print('Hello')", commands[0].Lua) assert.Equal(t, []string{"file1.txt", "file2.txt"}, commands[0].Files) assert.Equal(t, false, commands[0].Git) @@ -706,72 +706,72 @@ func TestLoadCommandFromArgsSetsGitFlagWhenResetFlagIsTrue(t *testing.T) { // Can't be asked doing that right now... // Successfully loads commands from multiple YAML files in the current directory // func TestLoadCommandsFromCookFilesSuccessfully(t *testing.T) { - // // Setup test directory with mock YAML files - // tempDir, err := os.MkdirTemp("", "cookfiles_test") - // if err != nil { - // t.Fatalf("Failed to create temp directory: %v", err) - // } - // defer os.RemoveAll(tempDir) +// // Setup test directory with mock YAML files +// tempDir, err := os.MkdirTemp("", "cookfiles_test") +// if err != nil { +// t.Fatalf("Failed to create temp directory: %v", err) +// } +// defer os.RemoveAll(tempDir) - // // Save current directory to restore later - // originalDir, err := os.Getwd() - // if err != nil { - // t.Fatalf("Failed to get current directory: %v", err) - // } +// // Save current directory to restore later +// originalDir, err := os.Getwd() +// if err != nil { +// t.Fatalf("Failed to get current directory: %v", err) +// } - // // Change to temp directory - // err = os.Chdir(tempDir) - // if err != nil { - // t.Fatalf("Failed to change directory: %v", err) - // } - // defer os.Chdir(originalDir) +// // Change to temp directory +// err = os.Chdir(tempDir) +// if err != nil { +// t.Fatalf("Failed to change directory: %v", err) +// } +// defer os.Chdir(originalDir) - // // Create mock YAML files - // yamlContent1 := []byte("commands:\n - name: command1\n type: test") - // yamlContent2 := []byte("commands:\n - name: command2\n type: test") +// // Create mock YAML files +// yamlContent1 := []byte("commands:\n - name: command1\n type: test") +// yamlContent2 := []byte("commands:\n - name: command2\n type: test") - // err = os.WriteFile("test1.yaml", yamlContent1, 0644) - // if err != nil { - // t.Fatalf("Failed to write test1.yaml: %v", err) - // } +// err = os.WriteFile("test1.yaml", yamlContent1, 0644) +// if err != nil { +// t.Fatalf("Failed to write test1.yaml: %v", err) +// } - // err = os.WriteFile("test2.yaml", yamlContent2, 0644) - // if err != nil { - // t.Fatalf("Failed to write test2.yaml: %v", err) - // } +// err = os.WriteFile("test2.yaml", yamlContent2, 0644) +// if err != nil { +// t.Fatalf("Failed to write test2.yaml: %v", err) +// } - // // Mock LoadCommandsFromCookFile to return expected commands - // originalLoadFunc := LoadCommandsFromCookFile - // defer func() { LoadCommandsFromCookFile = originalLoadFunc }() +// // Mock LoadCommandsFromCookFile to return expected commands +// originalLoadFunc := LoadCommandsFromCookFile +// defer func() { LoadCommandsFromCookFile = originalLoadFunc }() - // LoadCommandsFromCookFile = func(data []byte) ([]ModifyCommand, error) { - // var result []ModifyCommand - // if string(data) == string(yamlContent1) { - // result = []ModifyCommand{{Name: "command1", Type: "test"}} - // } else if string(data) == string(yamlContent2) { - // result = []ModifyCommand{{Name: "command2", Type: "test"}} - // } - // return result, nil - // } +// LoadCommandsFromCookFile = func(data []byte) ([]ModifyCommand, error) { +// var result []ModifyCommand +// if string(data) == string(yamlContent1) { +// result = []ModifyCommand{{Name: "command1", Type: "test"}} +// } else if string(data) == string(yamlContent2) { +// result = []ModifyCommand{{Name: "command2", Type: "test"}} +// } +// return result, nil +// } - // // Execute function - // commands, err := LoadCommandsFromCookFiles("") +// // Execute function +// commands, err := LoadCommandsFromCookFiles("") - // // Assertions - // if err != nil { - // t.Errorf("Expected no error, got: %v", err) - // } +// // Assertions +// if err != nil { +// t.Errorf("Expected no error, got: %v", err) +// } - // if len(commands) != 2 { - // t.Errorf("Expected 2 commands, got: %d", len(commands)) - // } +// if len(commands) != 2 { +// t.Errorf("Expected 2 commands, got: %d", len(commands)) +// } - // expectedNames := []string{"command1", "command2"} - // for i, cmd := range commands { - // if cmd.Name != expectedNames[i] { - // t.Errorf("Expected command name %s, got: %s", expectedNames[i], cmd.Name) - // } - // } +// expectedNames := []string{"command1", "command2"} +// for i, cmd := range commands { +// if cmd.Name != expectedNames[i] { +// t.Errorf("Expected command name %s, got: %s", expectedNames[i], cmd.Name) +// } +// } // } // No YAML files exist in the current directory @@ -826,38 +826,38 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // t.Fatalf("Failed to create temp directory: %v", err) // } // defer os.RemoveAll(tempDir) -// +// // // Save current directory to restore later // originalDir, err := os.Getwd() // if err != nil { // t.Fatalf("Failed to get current directory: %v", err) // } -// +// // // Change to temp directory // err = os.Chdir(tempDir) // if err != nil { // t.Fatalf("Failed to change directory: %v", err) // } // defer os.Chdir(originalDir) -// +// // // Create mock YAML files // yamlContent1 := []byte("commands:\n - name: command1\n type: test") // yamlContent2 := []byte("commands:\n - name: command2\n type: test") -// +// // err = os.WriteFile("test1.yaml", yamlContent1, 0644) // if err != nil { // t.Fatalf("Failed to write test1.yaml: %v", err) // } -// +// // err = os.WriteFile("test2.yaml", yamlContent2, 0644) // if err != nil { // t.Fatalf("Failed to write test2.yaml: %v", err) // } -// +// // // Mock LoadCommandsFromCookFile to return expected commands // originalLoadFunc := LoadCommandsFromCookFile // defer func() { LoadCommandsFromCookFile = originalLoadFunc }() -// +// // LoadCommandsFromCookFile = func(data []byte) ([]ModifyCommand, error) { // var result []ModifyCommand // if string(data) == string(yamlContent1) { @@ -867,19 +867,19 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // } // return result, nil // } -// +// // // Execute function // commands, err := LoadCommandsFromCookFiles("") -// +// // // Assertions // if err != nil { // t.Errorf("Expected no error, got: %v", err) // } -// +// // if len(commands) != 2 { // t.Errorf("Expected 2 commands, got: %d", len(commands)) // } -// +// // expectedNames := []string{"command1", "command2"} // for i, cmd := range commands { // if cmd.Name != expectedNames[i] { @@ -896,15 +896,15 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // return "", fmt.Errorf("mock error: unable to access cwd") // } // defer func() { os.Getwd = originalGetwd }() -// +// // // Execute function // _, err := LoadCommandsFromCookFiles("") -// +// // // Assertions // if err == nil { // t.Errorf("Expected error, got nil") // } -// +// // expectedErrorMsg := "failed to get current working directory: mock error: unable to access cwd" // if err.Error() != expectedErrorMsg { // t.Errorf("Expected error message '%s', got: '%s'", expectedErrorMsg, err.Error()) @@ -919,47 +919,47 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // t.Fatalf("Failed to create temp directory: %v", err) // } // defer os.RemoveAll(tempDir) -// +// // // Save current directory to restore later // originalDir, err := os.Getwd() // if err != nil { // t.Fatalf("Failed to get current directory: %v", err) // } -// +// // // Change to temp directory // err = os.Chdir(tempDir) // if err != nil { // t.Fatalf("Failed to change directory: %v", err) // } // defer os.Chdir(originalDir) -// +// // // Create mock empty YAML file // emptyYamlContent := []byte("") -// +// // err = os.WriteFile("empty.yaml", emptyYamlContent, 0644) // if err != nil { // t.Fatalf("Failed to write empty.yaml: %v", err) // } -// +// // // Mock LoadCommandsFromCookFile to return no commands for empty content // originalLoadFunc := LoadCommandsFromCookFile // defer func() { LoadCommandsFromCookFile = originalLoadFunc }() -// +// // LoadCommandsFromCookFile = func(data []byte) ([]ModifyCommand, error) { // if len(data) == 0 { // return []ModifyCommand{}, nil // } // return nil, fmt.Errorf("unexpected data") // } -// +// // // Execute function // commands, err := LoadCommandsFromCookFiles("") -// +// // // Assertions // if err != nil { // t.Errorf("Expected no error, got: %v", err) // } -// +// // if len(commands) != 0 { // t.Errorf("Expected 0 commands, got: %d", len(commands)) // } @@ -973,39 +973,39 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // t.Fatalf("Failed to create temp directory: %v", err) // } // defer os.RemoveAll(tempDir) -// +// // // Save current directory to restore later // originalDir, err := os.Getwd() // if err != nil { // t.Fatalf("Failed to get current directory: %v", err) // } -// +// // // Change to temp directory // err = os.Chdir(tempDir) // if err != nil { // t.Fatalf("Failed to change directory: %v", err) // } // defer os.Chdir(originalDir) -// +// // // Create a malformed YAML file // malformedYAMLContent := []byte("commands:\n - name: command1\n type") -// +// // err = os.WriteFile("malformed.yaml", malformedYAMLContent, 0644) // if err != nil { // t.Fatalf("Failed to write malformed.yaml: %v", err) // } -// +// // // Mock LoadCommandsFromCookFile to return an error for malformed YAML // originalLoadFunc := LoadCommandsFromCookFile // defer func() { LoadCommandsFromCookFile = originalLoadFunc }() -// +// // LoadCommandsFromCookFile = func(data []byte) ([]ModifyCommand, error) { // return nil, fmt.Errorf("malformed YAML") // } -// +// // // Execute function // _, err = LoadCommandsFromCookFiles("") -// +// // // Assertions // if err == nil { // t.Errorf("Expected error for malformed YAML, got none") @@ -1020,57 +1020,57 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // t.Fatalf("Failed to create temp directory: %v", err) // } // defer os.RemoveAll(tempDir) -// +// // // Save current directory to restore later // originalDir, err := os.Getwd() // if err != nil { // t.Fatalf("Failed to get current directory: %v", err) // } -// +// // // Change to temp directory // err = os.Chdir(tempDir) // if err != nil { // t.Fatalf("Failed to change directory: %v", err) // } // defer os.Chdir(originalDir) -// +// // // Create mock YAML files // validYAMLContent := []byte("commands:\n - name: validCommand\n type: test") // invalidYAMLContent := []byte("invalid_yaml_content") -// +// // err = os.WriteFile("valid.yaml", validYAMLContent, 0644) // if err != nil { // t.Fatalf("Failed to write valid.yaml: %v", err) // } -// +// // err = os.WriteFile("invalid.yaml", invalidYAMLContent, 0644) // if err != nil { // t.Fatalf("Failed to write invalid.yaml: %v", err) // } -// +// // // Mock LoadCommandsFromCookFile to return expected commands or error // originalLoadFunc := LoadCommandsFromCookFile // defer func() { LoadCommandsFromCookFile = originalLoadFunc }() -// +// // LoadCommandsFromCookFile = func(data []byte) ([]ModifyCommand, error) { // if string(data) == string(validYAMLContent) { // return []ModifyCommand{{Name: "validCommand", Type: "test"}}, nil // } // return nil, fmt.Errorf("invalid YAML content") // } -// +// // // Execute function // commands, err := LoadCommandsFromCookFiles("") -// +// // // Assertions // if err == nil { // t.Errorf("Expected error due to invalid YAML, got none") // } -// +// // if len(commands) != 1 { // t.Errorf("Expected 1 valid command, got: %d", len(commands)) // } -// +// // if commands[0].Name != "validCommand" { // t.Errorf("Expected command name 'validCommand', got: %s", commands[0].Name) // } @@ -1084,38 +1084,38 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // t.Fatalf("Failed to create temp directory: %v", err) // } // defer os.RemoveAll(tempDir) -// +// // // Save current directory to restore later // originalDir, err := os.Getwd() // if err != nil { // t.Fatalf("Failed to get current directory: %v", err) // } -// +// // // Change to temp directory // err = os.Chdir(tempDir) // if err != nil { // t.Fatalf("Failed to change directory: %v", err) // } // defer os.Chdir(originalDir) -// +// // // Create mock YAML files // yamlContent1 := []byte("commands:\n - name: command1\n type: test") // yamlContent2 := []byte("commands:\n - name: command2\n type: test") -// +// // err = os.WriteFile("test1.yaml", yamlContent1, 0644) // if err != nil { // t.Fatalf("Failed to write test1.yaml: %v", err) // } -// +// // err = os.WriteFile("test2.yaml", yamlContent2, 0644) // if err != nil { // t.Fatalf("Failed to write test2.yaml: %v", err) // } -// +// // // Mock LoadCommandsFromCookFile to return expected commands // originalLoadFunc := LoadCommandsFromCookFile // defer func() { LoadCommandsFromCookFile = originalLoadFunc }() -// +// // LoadCommandsFromCookFile = func(data []byte) ([]ModifyCommand, error) { // var result []ModifyCommand // if string(data) == string(yamlContent1) { @@ -1125,19 +1125,19 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // } // return result, nil // } -// +// // // Execute function // commands, err := LoadCommandsFromCookFiles("") -// +// // // Assertions // if err != nil { // t.Errorf("Expected no error, got: %v", err) // } -// +// // if len(commands) != 2 { // t.Errorf("Expected 2 commands, got: %d", len(commands)) // } -// +// // expectedNames := []string{"command1", "command2"} // for i, cmd := range commands { // if cmd.Name != expectedNames[i] { @@ -1154,38 +1154,38 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // t.Fatalf("Failed to create temp directory: %v", err) // } // defer os.RemoveAll(tempDir) -// +// // // Save current directory to restore later // originalDir, err := os.Getwd() // if err != nil { // t.Fatalf("Failed to get current directory: %v", err) // } -// +// // // Change to temp directory // err = os.Chdir(tempDir) // if err != nil { // t.Fatalf("Failed to change directory: %v", err) // } // defer os.Chdir(originalDir) -// +// // // Create mock YAML files // yamlContent1 := []byte("commands:\n - name: command1\n type: test") // yamlContent2 := []byte("commands:\n - name: command2\n type: test") -// +// // err = os.WriteFile("test1.yaml", yamlContent1, 0644) // if err != nil { // t.Fatalf("Failed to write test1.yaml: %v", err) // } -// +// // err = os.WriteFile("test2.yaml", yamlContent2, 0644) // if err != nil { // t.Fatalf("Failed to write test2.yaml: %v", err) // } -// +// // // Mock LoadCommandsFromCookFile to return expected commands // originalLoadFunc := LoadCommandsFromCookFile // defer func() { LoadCommandsFromCookFile = originalLoadFunc }() -// +// // LoadCommandsFromCookFile = func(data []byte) ([]ModifyCommand, error) { // var result []ModifyCommand // if string(data) == string(yamlContent1) { @@ -1195,19 +1195,19 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // } // return result, nil // } -// +// // // Execute function // commands, err := LoadCommandsFromCookFiles("") -// +// // // Assertions // if err != nil { // t.Errorf("Expected no error, got: %v", err) // } -// +// // if len(commands) != 2 { // t.Errorf("Expected 2 commands, got: %d", len(commands)) // } -// +// // expectedNames := []string{"command1", "command2"} // for i, cmd := range commands { // if cmd.Name != expectedNames[i] { @@ -1224,44 +1224,44 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) { // t.Fatalf("Failed to create temp directory: %v", err) // } // defer os.RemoveAll(tempDir) -// +// // // Save current directory to restore later // originalDir, err := os.Getwd() // if err != nil { // t.Fatalf("Failed to get current directory: %v", err) // } -// +// // // Change to temp directory // err = os.Chdir(tempDir) // if err != nil { // t.Fatalf("Failed to change directory: %v", err) // } // defer os.Chdir(originalDir) -// +// // // Create mock non-YAML file // nonYamlContent := []byte("This is not a YAML file") -// +// // err = os.WriteFile("test.txt", nonYamlContent, 0644) // if err != nil { // t.Fatalf("Failed to write test.txt: %v", err) // } -// +// // // Mock LoadCommandsFromCookFile to return no commands for non-YAML content // originalLoadFunc := LoadCommandsFromCookFile // defer func() { LoadCommandsFromCookFile = originalLoadFunc }() -// +// // LoadCommandsFromCookFile = func(data []byte) ([]ModifyCommand, error) { // return nil, fmt.Errorf("not a YAML file") // } -// +// // // Execute function // commands, err := LoadCommandsFromCookFiles("") -// +// // // Assertions // if err != nil { // t.Errorf("Expected no error, got: %v", err) // } -// +// // if len(commands) != 0 { // t.Errorf("Expected 0 commands, got: %d", len(commands)) // }