|
|
|
@@ -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
|
|
|
|
|