Add more tests for modifycommand
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
*.exe
|
*.exe
|
||||||
|
.qodo
|
||||||
|
4
go.mod
4
go.mod
@@ -4,7 +4,9 @@ go 1.24.1
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bmatcuk/doublestar/v4 v4.8.1
|
github.com/bmatcuk/doublestar/v4 v4.8.1
|
||||||
|
github.com/stretchr/testify v1.10.0
|
||||||
github.com/yuin/gopher-lua v1.1.1
|
github.com/yuin/gopher-lua v1.1.1
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -13,12 +15,14 @@ require (
|
|||||||
github.com/ProtonMail/go-crypto v1.1.5 // indirect
|
github.com/ProtonMail/go-crypto v1.1.5 // indirect
|
||||||
github.com/cloudflare/circl v1.6.0 // indirect
|
github.com/cloudflare/circl v1.6.0 // indirect
|
||||||
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
|
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/emirpasic/gods v1.18.1 // indirect
|
github.com/emirpasic/gods v1.18.1 // indirect
|
||||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
||||||
github.com/go-git/go-billy/v5 v5.6.2 // indirect
|
github.com/go-git/go-billy/v5 v5.6.2 // indirect
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
||||||
github.com/kevinburke/ssh_config v1.2.0 // indirect
|
github.com/kevinburke/ssh_config v1.2.0 // indirect
|
||||||
github.com/pjbgf/sha1cd v0.3.2 // indirect
|
github.com/pjbgf/sha1cd v0.3.2 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
|
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
|
||||||
github.com/skeema/knownhosts v1.3.1 // indirect
|
github.com/skeema/knownhosts v1.3.1 // indirect
|
||||||
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
||||||
|
2
main.go
2
main.go
@@ -57,7 +57,7 @@ func main() {
|
|||||||
logger.Info("Initializing with log level: %s", level.String())
|
logger.Info("Initializing with log level: %s", level.String())
|
||||||
|
|
||||||
// The plan is:
|
// The plan is:
|
||||||
// Load all commands
|
// Load all commands
|
||||||
commands, err := utils.LoadCommands(args)
|
commands, err := utils.LoadCommands(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to load commands: %v", err)
|
logger.Error("Failed to load commands: %v", err)
|
||||||
|
@@ -44,8 +44,8 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
|
|||||||
// Same here, it's just string concatenation, it won't kill us
|
// Same here, it's just string concatenation, it won't kill us
|
||||||
// More important is that we don't fuck up the command
|
// More important is that we don't fuck up the command
|
||||||
// But we shouldn't be able to since it's passed by value
|
// But we shouldn't be able to since it's passed by value
|
||||||
previous := command.LuaExpr
|
previous := command.Lua
|
||||||
luaExpr := BuildLuaScript(command.LuaExpr)
|
luaExpr := BuildLuaScript(command.Lua)
|
||||||
logger.Debug("Transformed Lua expression: %q → %q", previous, luaExpr)
|
logger.Debug("Transformed Lua expression: %q → %q", previous, luaExpr)
|
||||||
|
|
||||||
// Process all regex matches
|
// Process all regex matches
|
||||||
|
@@ -28,7 +28,7 @@ func normalizeWhitespace(s string) string {
|
|||||||
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
|
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
|
||||||
command := utils.ModifyCommand{
|
command := utils.ModifyCommand{
|
||||||
Pattern: regex,
|
Pattern: regex,
|
||||||
LuaExpr: lua,
|
Lua: lua,
|
||||||
LogLevel: "TRACE",
|
LogLevel: "TRACE",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -11,7 +11,7 @@ import (
|
|||||||
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
|
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
|
||||||
command := utils.ModifyCommand{
|
command := utils.ModifyCommand{
|
||||||
Pattern: regex,
|
Pattern: regex,
|
||||||
LuaExpr: lua,
|
Lua: lua,
|
||||||
LogLevel: "TRACE",
|
LogLevel: "TRACE",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -6,11 +6,13 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/bmatcuk/doublestar/v4"
|
"github.com/bmatcuk/doublestar/v4"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ModifyCommand struct {
|
type ModifyCommand struct {
|
||||||
|
Name string `yaml:"name"`
|
||||||
Pattern string `yaml:"pattern"`
|
Pattern string `yaml:"pattern"`
|
||||||
LuaExpr string `yaml:"lua"`
|
Lua string `yaml:"lua"`
|
||||||
Files []string `yaml:"files"`
|
Files []string `yaml:"files"`
|
||||||
Git bool `yaml:"git"`
|
Git bool `yaml:"git"`
|
||||||
Reset bool `yaml:"reset"`
|
Reset bool `yaml:"reset"`
|
||||||
@@ -22,7 +24,7 @@ func (c *ModifyCommand) Validate() error {
|
|||||||
if c.Pattern == "" {
|
if c.Pattern == "" {
|
||||||
return fmt.Errorf("pattern is required")
|
return fmt.Errorf("pattern is required")
|
||||||
}
|
}
|
||||||
if c.LuaExpr == "" {
|
if c.Lua == "" {
|
||||||
return fmt.Errorf("lua expression is required")
|
return fmt.Errorf("lua expression is required")
|
||||||
}
|
}
|
||||||
if len(c.Files) == 0 {
|
if len(c.Files) == 0 {
|
||||||
@@ -143,7 +145,7 @@ func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
|
|||||||
|
|
||||||
command := ModifyCommand{
|
command := ModifyCommand{
|
||||||
Pattern: args[0],
|
Pattern: args[0],
|
||||||
LuaExpr: args[1],
|
Lua: args[1],
|
||||||
Files: args[2:],
|
Files: args[2:],
|
||||||
Git: *GitFlag,
|
Git: *GitFlag,
|
||||||
Reset: *ResetFlag,
|
Reset: *ResetFlag,
|
||||||
@@ -158,7 +160,39 @@ func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LoadCommandsFromCookFiles(s string) ([]ModifyCommand, error) {
|
func LoadCommandsFromCookFiles(s string) ([]ModifyCommand, error) {
|
||||||
return nil, nil
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get current working directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
commands := []ModifyCommand{}
|
||||||
|
cookFiles, err := doublestar.Glob(os.DirFS(cwd), "*.yaml")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to glob cook files: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cookFile := range cookFiles {
|
||||||
|
cookFileData, err := os.ReadFile(cookFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read cook file: %w", err)
|
||||||
|
}
|
||||||
|
newcommands, err := LoadCommandsFromCookFile(cookFileData)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to load commands from cook file: %w", err)
|
||||||
|
}
|
||||||
|
commands = append(commands, newcommands...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return commands, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadCommandsFromCookFile(cookFileData []byte) ([]ModifyCommand, error) {
|
||||||
|
commands := []ModifyCommand{}
|
||||||
|
err := yaml.Unmarshal(cookFileData, &commands)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to unmarshal cook file: %w", err)
|
||||||
|
}
|
||||||
|
return commands, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountGlobsBeforeDedup counts the total number of glob patterns across all commands before deduplication
|
// CountGlobsBeforeDedup counts the total number of glob patterns across all commands before deduplication
|
||||||
|
@@ -4,6 +4,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestModifyCommandValidate(t *testing.T) {
|
func TestModifyCommandValidate(t *testing.T) {
|
||||||
@@ -16,7 +18,7 @@ func TestModifyCommandValidate(t *testing.T) {
|
|||||||
name: "Valid command",
|
name: "Valid command",
|
||||||
command: ModifyCommand{
|
command: ModifyCommand{
|
||||||
Pattern: "test pattern",
|
Pattern: "test pattern",
|
||||||
LuaExpr: "test expression",
|
Lua: "test expression",
|
||||||
Files: []string{"file1", "file2"},
|
Files: []string{"file1", "file2"},
|
||||||
LogLevel: "INFO",
|
LogLevel: "INFO",
|
||||||
},
|
},
|
||||||
@@ -26,7 +28,7 @@ func TestModifyCommandValidate(t *testing.T) {
|
|||||||
name: "Missing pattern",
|
name: "Missing pattern",
|
||||||
command: ModifyCommand{
|
command: ModifyCommand{
|
||||||
Pattern: "",
|
Pattern: "",
|
||||||
LuaExpr: "test expression",
|
Lua: "test expression",
|
||||||
Files: []string{"file1", "file2"},
|
Files: []string{"file1", "file2"},
|
||||||
LogLevel: "INFO",
|
LogLevel: "INFO",
|
||||||
},
|
},
|
||||||
@@ -36,7 +38,7 @@ func TestModifyCommandValidate(t *testing.T) {
|
|||||||
name: "Missing LuaExpr",
|
name: "Missing LuaExpr",
|
||||||
command: ModifyCommand{
|
command: ModifyCommand{
|
||||||
Pattern: "test pattern",
|
Pattern: "test pattern",
|
||||||
LuaExpr: "",
|
Lua: "",
|
||||||
Files: []string{"file1", "file2"},
|
Files: []string{"file1", "file2"},
|
||||||
LogLevel: "INFO",
|
LogLevel: "INFO",
|
||||||
},
|
},
|
||||||
@@ -46,7 +48,7 @@ func TestModifyCommandValidate(t *testing.T) {
|
|||||||
name: "Missing files",
|
name: "Missing files",
|
||||||
command: ModifyCommand{
|
command: ModifyCommand{
|
||||||
Pattern: "test pattern",
|
Pattern: "test pattern",
|
||||||
LuaExpr: "test expression",
|
Lua: "test expression",
|
||||||
Files: []string{},
|
Files: []string{},
|
||||||
LogLevel: "INFO",
|
LogLevel: "INFO",
|
||||||
},
|
},
|
||||||
@@ -56,7 +58,7 @@ func TestModifyCommandValidate(t *testing.T) {
|
|||||||
name: "Default log level",
|
name: "Default log level",
|
||||||
command: ModifyCommand{
|
command: ModifyCommand{
|
||||||
Pattern: "test pattern",
|
Pattern: "test pattern",
|
||||||
LuaExpr: "test expression",
|
Lua: "test expression",
|
||||||
Files: []string{"file1", "file2"},
|
Files: []string{"file1", "file2"},
|
||||||
LogLevel: "",
|
LogLevel: "",
|
||||||
},
|
},
|
||||||
@@ -127,17 +129,17 @@ func TestAssociateFilesWithCommands(t *testing.T) {
|
|||||||
commands := []ModifyCommand{
|
commands := []ModifyCommand{
|
||||||
{
|
{
|
||||||
Pattern: "pattern1",
|
Pattern: "pattern1",
|
||||||
LuaExpr: "expr1",
|
Lua: "expr1",
|
||||||
Files: []string{"*.xml"},
|
Files: []string{"*.xml"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Pattern: "pattern2",
|
Pattern: "pattern2",
|
||||||
LuaExpr: "expr2",
|
Lua: "expr2",
|
||||||
Files: []string{"*.txt"},
|
Files: []string{"*.txt"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Pattern: "pattern3",
|
Pattern: "pattern3",
|
||||||
LuaExpr: "expr3",
|
Lua: "expr3",
|
||||||
Files: []string{"subdir/*"},
|
Files: []string{"subdir/*"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -229,17 +231,17 @@ func TestAggregateGlobs(t *testing.T) {
|
|||||||
commands := []ModifyCommand{
|
commands := []ModifyCommand{
|
||||||
{
|
{
|
||||||
Pattern: "pattern1",
|
Pattern: "pattern1",
|
||||||
LuaExpr: "expr1",
|
Lua: "expr1",
|
||||||
Files: []string{"*.xml", "*.txt"},
|
Files: []string{"*.xml", "*.txt"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Pattern: "pattern2",
|
Pattern: "pattern2",
|
||||||
LuaExpr: "expr2",
|
Lua: "expr2",
|
||||||
Files: []string{"*.xml", "*.json"},
|
Files: []string{"*.xml", "*.json"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Pattern: "pattern3",
|
Pattern: "pattern3",
|
||||||
LuaExpr: "expr3",
|
Lua: "expr3",
|
||||||
Files: []string{"subdir/*.xml"},
|
Files: []string{"subdir/*.xml"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -361,8 +363,8 @@ func TestLoadCommandFromArgs(t *testing.T) {
|
|||||||
t.Errorf("Expected pattern %q, got %q", tc.args[0], cmd.Pattern)
|
t.Errorf("Expected pattern %q, got %q", tc.args[0], cmd.Pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.LuaExpr != tc.args[1] {
|
if cmd.Lua != tc.args[1] {
|
||||||
t.Errorf("Expected LuaExpr %q, got %q", tc.args[1], cmd.LuaExpr)
|
t.Errorf("Expected LuaExpr %q, got %q", tc.args[1], cmd.Lua)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cmd.Files) != len(tc.args)-2 {
|
if len(cmd.Files) != len(tc.args)-2 {
|
||||||
@@ -385,3 +387,852 @@ func TestLoadCommandFromArgs(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Successfully unmarshal valid YAML data into ModifyCommand slice
|
||||||
|
func TestLoadCommandsFromCookFileSuccess(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
yamlData := []byte(`
|
||||||
|
- name: command1
|
||||||
|
pattern: "*.txt"
|
||||||
|
lua: replace
|
||||||
|
- name: command2
|
||||||
|
pattern: "*.go"
|
||||||
|
lua: delete
|
||||||
|
`)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
commands, err := LoadCommandsFromCookFile(yamlData)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
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, "replace", commands[0].Lua)
|
||||||
|
assert.Equal(t, "command2", commands[1].Name)
|
||||||
|
assert.Equal(t, "*.go", commands[1].Pattern)
|
||||||
|
assert.Equal(t, "delete", commands[1].Lua)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process YAML with comments or directives
|
||||||
|
func TestLoadCommandsFromCookFileWithComments(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
yamlData := []byte(`
|
||||||
|
# This is a comment
|
||||||
|
- name: command1
|
||||||
|
pattern: "*.txt"
|
||||||
|
lua: replace
|
||||||
|
# Another comment
|
||||||
|
- name: command2
|
||||||
|
pattern: "*.go"
|
||||||
|
lua: delete
|
||||||
|
`)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
commands, err := LoadCommandsFromCookFile(yamlData)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
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, "replace", commands[0].Lua)
|
||||||
|
assert.Equal(t, "command2", commands[1].Name)
|
||||||
|
assert.Equal(t, "*.go", commands[1].Pattern)
|
||||||
|
assert.Equal(t, "delete", commands[1].Lua)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle different YAML formatting styles (flow vs block)
|
||||||
|
func TestLoadCommandsFromCookFileWithFlowStyle(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
yamlData := []byte(`[ { name: command1, pattern: "*.txt", lua: replace }, { name: command2, pattern: "*.go", lua: delete } ]`)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
commands, err := LoadCommandsFromCookFile(yamlData)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
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, "replace", commands[0].Lua)
|
||||||
|
assert.Equal(t, "command2", commands[1].Name)
|
||||||
|
assert.Equal(t, "*.go", commands[1].Pattern)
|
||||||
|
assert.Equal(t, "delete", commands[1].Lua)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle nil or empty cookFileData (should return error)
|
||||||
|
func TestLoadCommandsFromCookFileNilOrEmptyData(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
var nilData []byte
|
||||||
|
emptyData := []byte{}
|
||||||
|
|
||||||
|
// Act
|
||||||
|
commandsNil, errNil := LoadCommandsFromCookFile(nilData)
|
||||||
|
commandsEmpty, errEmpty := LoadCommandsFromCookFile(emptyData)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.Nil(t, errNil)
|
||||||
|
assert.Empty(t, commandsNil)
|
||||||
|
assert.Nil(t, errEmpty)
|
||||||
|
assert.Empty(t, commandsEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle empty but valid YAML data (returning empty slice)
|
||||||
|
func TestLoadCommandsFromCookFileEmptyData(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
yamlData := []byte(``)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
commands, err := LoadCommandsFromCookFile(yamlData)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Empty(t, commands)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process YAML with multiple ModifyCommand entries correctly
|
||||||
|
func TestLoadCommandsFromCookFileWithMultipleEntries(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
yamlData := []byte(`
|
||||||
|
- name: command1
|
||||||
|
pattern: "*.txt"
|
||||||
|
lua: replace
|
||||||
|
- name: command2
|
||||||
|
pattern: "*.go"
|
||||||
|
lua: delete
|
||||||
|
- name: command3
|
||||||
|
pattern: "*.md"
|
||||||
|
lua: append
|
||||||
|
`)
|
||||||
|
|
||||||
|
// Act
|
||||||
|
commands, err := LoadCommandsFromCookFile(yamlData)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
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, "replace", commands[0].Lua)
|
||||||
|
assert.Equal(t, "command2", commands[1].Name)
|
||||||
|
assert.Equal(t, "*.go", commands[1].Pattern)
|
||||||
|
assert.Equal(t, "delete", commands[1].Lua)
|
||||||
|
assert.Equal(t, "command3", commands[2].Name)
|
||||||
|
assert.Equal(t, "*.md", commands[2].Pattern)
|
||||||
|
assert.Equal(t, "append", commands[2].Lua)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Valid command with minimum 3 arguments returns a ModifyCommand slice with correct values
|
||||||
|
func TestLoadCommandFromArgsWithValidArguments(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
oldGitFlag := GitFlag
|
||||||
|
oldResetFlag := ResetFlag
|
||||||
|
oldLogLevel := LogLevel
|
||||||
|
|
||||||
|
gitValue := true
|
||||||
|
resetValue := false
|
||||||
|
logLevelValue := "info"
|
||||||
|
|
||||||
|
GitFlag = &gitValue
|
||||||
|
ResetFlag = &resetValue
|
||||||
|
LogLevel = &logLevelValue
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
GitFlag = oldGitFlag
|
||||||
|
ResetFlag = oldResetFlag
|
||||||
|
LogLevel = oldLogLevel
|
||||||
|
}()
|
||||||
|
|
||||||
|
args := []string{"*.go", "return x", "file1.go", "file2.go"}
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
commands, err := LoadCommandFromArgs(args)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, commands, 1)
|
||||||
|
assert.Equal(t, "*.go", commands[0].Pattern)
|
||||||
|
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)
|
||||||
|
assert.Equal(t, false, commands[0].Reset)
|
||||||
|
assert.Equal(t, "info", commands[0].LogLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Less than 3 arguments returns an error with appropriate message
|
||||||
|
func TestLoadCommandFromArgsWithInsufficientArguments(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
oldGitFlag := GitFlag
|
||||||
|
oldResetFlag := ResetFlag
|
||||||
|
oldLogLevel := LogLevel
|
||||||
|
|
||||||
|
gitValue := false
|
||||||
|
resetValue := false
|
||||||
|
logLevelValue := "info"
|
||||||
|
|
||||||
|
GitFlag = &gitValue
|
||||||
|
ResetFlag = &resetValue
|
||||||
|
LogLevel = &logLevelValue
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
GitFlag = oldGitFlag
|
||||||
|
ResetFlag = oldResetFlag
|
||||||
|
LogLevel = oldLogLevel
|
||||||
|
}()
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
}{
|
||||||
|
{"empty args", []string{}},
|
||||||
|
{"one arg", []string{"*.go"}},
|
||||||
|
{"two args", []string{"*.go", "return x"}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
// Execute
|
||||||
|
commands, err := LoadCommandFromArgs(tc.args)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, commands)
|
||||||
|
assert.Contains(t, err.Error(), "at least 3 arguments are required")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pattern, Lua, and Files fields are correctly populated from args
|
||||||
|
func TestLoadCommandFromArgsPopulatesFieldsCorrectly(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
oldGitFlag := GitFlag
|
||||||
|
oldResetFlag := ResetFlag
|
||||||
|
oldLogLevel := LogLevel
|
||||||
|
|
||||||
|
gitValue := false
|
||||||
|
resetValue := false
|
||||||
|
logLevelValue := "debug"
|
||||||
|
|
||||||
|
GitFlag = &gitValue
|
||||||
|
ResetFlag = &resetValue
|
||||||
|
LogLevel = &logLevelValue
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
GitFlag = oldGitFlag
|
||||||
|
ResetFlag = oldResetFlag
|
||||||
|
LogLevel = oldLogLevel
|
||||||
|
}()
|
||||||
|
|
||||||
|
args := []string{"*.txt", "print('Hello')", "file1.txt", "file2.txt"}
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
commands, err := LoadCommandFromArgs(args)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, commands, 1)
|
||||||
|
assert.Equal(t, "*.txt", commands[0].Pattern)
|
||||||
|
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)
|
||||||
|
assert.Equal(t, false, commands[0].Reset)
|
||||||
|
assert.Equal(t, "debug", commands[0].LogLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Git flag is set to true when ResetFlag is true
|
||||||
|
func TestLoadCommandFromArgsSetsGitFlagWhenResetFlagIsTrue(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
oldGitFlag := GitFlag
|
||||||
|
oldResetFlag := ResetFlag
|
||||||
|
oldLogLevel := LogLevel
|
||||||
|
|
||||||
|
gitValue := false
|
||||||
|
resetValue := true
|
||||||
|
logLevelValue := "info"
|
||||||
|
|
||||||
|
GitFlag = &gitValue
|
||||||
|
ResetFlag = &resetValue
|
||||||
|
LogLevel = &logLevelValue
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
GitFlag = oldGitFlag
|
||||||
|
ResetFlag = oldResetFlag
|
||||||
|
LogLevel = oldLogLevel
|
||||||
|
}()
|
||||||
|
|
||||||
|
args := []string{"*.go", "return x", "file1.go", "file2.go"}
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
commands, err := LoadCommandFromArgs(args)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, commands, 1)
|
||||||
|
assert.Equal(t, true, commands[0].Git)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Figure out how to mock shit
|
||||||
|
// 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)
|
||||||
|
|
||||||
|
// // 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) {
|
||||||
|
// 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("")
|
||||||
|
|
||||||
|
// // 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] {
|
||||||
|
// t.Errorf("Expected command name %s, got: %s", expectedNames[i], cmd.Name)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// No YAML files exist in the current directory
|
||||||
|
func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) {
|
||||||
|
// Setup empty test directory
|
||||||
|
tempDir, err := os.MkdirTemp("", "cookfiles_empty_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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 non-yaml file to ensure it's not picked up
|
||||||
|
err = os.WriteFile("test.txt", []byte("not a yaml file"), 0644)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to write test.txt: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the logger was used correctly (if applicable)
|
||||||
|
// This would require mocking the logger, which isn't shown in the provided context
|
||||||
|
}
|
||||||
|
|
||||||
|
// Correctly appends commands from multiple cook files into a single slice
|
||||||
|
// func TestLoadCommandsFromCookFilesAppendsCommands(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)
|
||||||
|
//
|
||||||
|
// // 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) {
|
||||||
|
// 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("")
|
||||||
|
//
|
||||||
|
// // 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] {
|
||||||
|
// t.Errorf("Expected command name %s, got: %s", expectedNames[i], cmd.Name)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Current working directory cannot be accessed
|
||||||
|
// func TestLoadCommandsFromCookFilesCwdError(t *testing.T) {
|
||||||
|
// // Mock os.Getwd to simulate an error
|
||||||
|
// originalGetwd := os.Getwd
|
||||||
|
// os.Getwd = func() (string, error) {
|
||||||
|
// 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())
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Empty YAML files exist but contain no commands
|
||||||
|
// func TestLoadCommandsFromEmptyCookFiles(t *testing.T) {
|
||||||
|
// // Setup test directory with mock empty YAML file
|
||||||
|
// 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)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 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))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// LoadCommandsFromCookFile returns an error for a malformed YAML file
|
||||||
|
// func TestLoadCommandsFromCookFilesMalformedYAML(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)
|
||||||
|
//
|
||||||
|
// // 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")
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Directory contains both valid and invalid YAML files
|
||||||
|
// func TestLoadCommandsFromCookFilesWithInvalidYAML(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)
|
||||||
|
//
|
||||||
|
// // 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)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Handles relative paths in the file system correctly
|
||||||
|
// func TestLoadCommandsFromCookFilesWithRelativePaths(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)
|
||||||
|
//
|
||||||
|
// // 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) {
|
||||||
|
// 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("")
|
||||||
|
//
|
||||||
|
// // 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] {
|
||||||
|
// t.Errorf("Expected command name %s, got: %s", expectedNames[i], cmd.Name)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Maintains the order of commands as they appear in the files
|
||||||
|
// func TestLoadCommandsFromCookFilesOrder(t *testing.T) {
|
||||||
|
// // Setup test directory with mock YAML files
|
||||||
|
// tempDir, err := os.MkdirTemp("", "cookfiles_order_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)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 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) {
|
||||||
|
// 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("")
|
||||||
|
//
|
||||||
|
// // 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] {
|
||||||
|
// t.Errorf("Expected command name %s, got: %s", expectedNames[i], cmd.Name)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Handles non-YAML files that might match the glob pattern
|
||||||
|
// func TestLoadCommandsFromCookFilesIgnoresNonYamlFiles(t *testing.T) {
|
||||||
|
// // Setup test directory with mock 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)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 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))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
Reference in New Issue
Block a user