-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_builder_test.go
More file actions
166 lines (149 loc) · 3.73 KB
/
command_builder_test.go
File metadata and controls
166 lines (149 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
102
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package executor
import (
"os/exec"
"testing"
)
func TestBuildCommandWithCustomShell(t *testing.T) {
tests := []struct {
name string
shell string
scriptPath string
args []string
wantFirst string
}{
{
name: "Custom shell python",
shell: "python3",
scriptPath: "script.py",
args: []string{"arg1"},
wantFirst: "python3",
},
{
name: "Custom shell node",
shell: "node",
scriptPath: "script.js",
args: nil,
wantFirst: "node",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exec, err := New(Config{Args: tt.args})
if err != nil {
t.Fatalf("New() error: %v", err)
}
cmd := exec.buildCommand(tt.shell, tt.scriptPath, false)
// Check if command was built
if cmd == nil {
t.Fatal("buildCommand returned nil")
}
// Verify args contain the shell
found := false
for _, arg := range cmd.Args {
if arg == tt.wantFirst {
found = true
break
}
}
if !found {
t.Errorf("buildCommand args don't contain shell %v: %v", tt.wantFirst, cmd.Args)
}
})
}
}
func TestBuildCommandShellVariations(t *testing.T) {
tests := []struct {
shell string
scriptPath string
wantArgs []string
}{
{
shell: "SH",
scriptPath: "test.sh",
wantArgs: []string{"sh", "test.sh"},
},
{
shell: "BASH",
scriptPath: "test.sh",
wantArgs: []string{"bash", "test.sh"},
},
}
for _, tt := range tests {
t.Run(tt.shell, func(t *testing.T) {
exec, err := New(Config{Shell: tt.shell})
if err != nil {
t.Fatalf("New() error: %v", err)
}
cmd := exec.buildCommand(tt.shell, tt.scriptPath, false)
if cmd == nil {
t.Fatal("buildCommand returned nil")
}
})
}
}
func TestBuildCommandLookPath(t *testing.T) {
// Test that buildCommand creates a valid exec.Cmd
exec, err := New(Config{})
if err != nil {
t.Fatalf("New() error: %v", err)
}
cmd := exec.buildCommand("cmd", "test.bat", false)
// On Windows, cmd should be findable
if cmd.Path == "" {
t.Error("buildCommand created command with empty Path")
}
// Verify we can look up the command
_, err = execLookPath(cmd.Args[0])
if err != nil {
t.Logf("Command %v not found in PATH (may be platform-specific)", cmd.Args[0])
}
}
// Helper to wrap exec.LookPath for testing.
func execLookPath(file string) (string, error) {
return exec.LookPath(file)
}
func TestQuotePowerShellArg(t *testing.T) {
tests := []struct {
name string
arg string
want string
}{
{name: "empty string", arg: "", want: "''"},
{name: "simple arg", arg: "hello", want: "'hello'"},
{name: "arg with single quote", arg: "it's", want: "'it''s'"},
{name: "arg with multiple quotes", arg: "a'b'c", want: "'a''b''c'"},
{name: "arg with double dash", arg: "--skip-sync", want: "'--skip-sync'"},
{name: "arg with spaces", arg: "hello world", want: "'hello world'"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := quotePowerShellArg(tt.arg)
if got != tt.want {
t.Errorf("quotePowerShellArg(%q) = %q, want %q", tt.arg, got, tt.want)
}
})
}
}
func TestBuildPowerShellInlineCommand(t *testing.T) {
t.Run("no args returns script as-is", func(t *testing.T) {
e, err := New(Config{})
if err != nil {
t.Fatalf("New() error: %v", err)
}
got := e.buildPowerShellInlineCommand("Get-Date")
if got != "Get-Date" {
t.Errorf("got %q, want %q", got, "Get-Date")
}
})
t.Run("with args joins and quotes", func(t *testing.T) {
e, err := New(Config{Args: []string{"arg1", "it's"}})
if err != nil {
t.Fatalf("New() error: %v", err)
}
got := e.buildPowerShellInlineCommand("cmd")
want := "cmd 'arg1' 'it''s'"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
}