Skip to content

Commit ba8afec

Browse files
committed
Allow flags with values
- fixes issue with naive string/split solution for finding process and arguments. Tested with unit tests. Found by: omrishtam (thank you) - fixes: #42 Signed-off-by: Alex Ellis (VMware) <[email protected]>
1 parent 85505a7 commit ba8afec

File tree

2 files changed

+98
-5
lines changed

2 files changed

+98
-5
lines changed

config/config.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,15 @@ func mapEnv(env []string) map[string]string {
9090
mapped := map[string]string{}
9191

9292
for _, val := range env {
93-
parts := strings.Split(val, "=")
94-
if len(parts) < 2 {
93+
sep := strings.Index(val, "=")
94+
95+
if sep > 0 {
96+
key := val[0:sep]
97+
value := val[sep+1:]
98+
mapped[key] = value
99+
} else {
95100
fmt.Println("Bad environment: " + val)
96101
}
97-
mapped[parts[0]] = parts[1]
98102
}
99103

100104
return mapped

config/config_test.go

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package config
22

3-
import "testing"
4-
import "time"
3+
import (
4+
"testing"
5+
"time"
6+
)
57

68
func TestNew(t *testing.T) {
79
defaults, err := New([]string{})
@@ -123,6 +125,93 @@ func Test_FunctionProcessAlternativeName(t *testing.T) {
123125
}
124126
}
125127

128+
func Test_FunctionProcessWithTargetArgument(t *testing.T) {
129+
env := []string{
130+
`fprocess=node index.js`,
131+
}
132+
wantProcess := "node"
133+
wantArguments := []string{"index.js"}
134+
135+
actual, err := New(env)
136+
if err != nil {
137+
t.Errorf("Expected no errors")
138+
}
139+
140+
process, args := actual.Process()
141+
if process != wantProcess {
142+
t.Errorf("Want process %v, got: %v", wantProcess, process)
143+
}
144+
145+
if len(args) != len(wantArguments) {
146+
t.Errorf("Want %d args, got: %d args", len(wantArguments), len(args))
147+
t.Fail()
148+
}
149+
150+
for i, wantArg := range wantArguments {
151+
if args[i] != wantArg {
152+
t.Errorf("Want arg%d: %s, got: %s", i, wantArg, args[i])
153+
}
154+
}
155+
}
156+
157+
func Test_FunctionProcessWithFlag(t *testing.T) {
158+
env := []string{
159+
`fprocess=node --this-is-a-flag`,
160+
}
161+
wantProcess := "node"
162+
wantArguments := []string{"--this-is-a-flag"}
163+
164+
actual, err := New(env)
165+
if err != nil {
166+
t.Errorf("Expected no errors")
167+
}
168+
169+
process, args := actual.Process()
170+
if process != wantProcess {
171+
t.Errorf("Want process %v, got: %v", wantProcess, process)
172+
}
173+
174+
if len(args) != len(wantArguments) {
175+
t.Errorf("Want %d args, got: %d args", len(wantArguments), len(args))
176+
t.Fail()
177+
}
178+
179+
for i, wantArg := range wantArguments {
180+
if args[i] != wantArg {
181+
t.Errorf("Want arg%d: %s, got: %s", i, wantArg, args[i])
182+
}
183+
}
184+
}
185+
186+
func Test_FunctionProcessWithFlagAndValue(t *testing.T) {
187+
env := []string{
188+
`fprocess=node --this-is-a-flag=1234`,
189+
}
190+
wantProcess := "node"
191+
wantArguments := []string{"--this-is-a-flag=1234"}
192+
193+
actual, err := New(env)
194+
if err != nil {
195+
t.Errorf("Expected no errors")
196+
}
197+
198+
process, args := actual.Process()
199+
if process != wantProcess {
200+
t.Errorf("Want process %v, got: %v", wantProcess, process)
201+
}
202+
203+
if len(args) != len(wantArguments) {
204+
t.Errorf("Want %d args, got: %d args", len(wantArguments), len(args))
205+
t.Fail()
206+
}
207+
208+
for i, wantArg := range wantArguments {
209+
if args[i] != wantArg {
210+
t.Errorf("Want arg%d: %s, got: %s", i, wantArg, args[i])
211+
}
212+
}
213+
}
214+
126215
func Test_PortOverride(t *testing.T) {
127216
env := []string{
128217
"port=8081",

0 commit comments

Comments
 (0)