diff --git a/config/config.go b/config/config.go index 84423a0c..8a571979 100644 --- a/config/config.go +++ b/config/config.go @@ -90,11 +90,15 @@ func mapEnv(env []string) map[string]string { mapped := map[string]string{} for _, val := range env { - parts := strings.Split(val, "=") - if len(parts) < 2 { + sep := strings.Index(val, "=") + + if sep > 0 { + key := val[0:sep] + value := val[sep+1:] + mapped[key] = value + } else { fmt.Println("Bad environment: " + val) } - mapped[parts[0]] = parts[1] } return mapped diff --git a/config/config_test.go b/config/config_test.go index 944a3a3e..42bfad6b 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,7 +1,9 @@ package config -import "testing" -import "time" +import ( + "testing" + "time" +) func TestNew(t *testing.T) { defaults, err := New([]string{}) @@ -123,6 +125,93 @@ func Test_FunctionProcessAlternativeName(t *testing.T) { } } +func Test_FunctionProcessWithTargetArgument(t *testing.T) { + env := []string{ + `fprocess=node index.js`, + } + wantProcess := "node" + wantArguments := []string{"index.js"} + + actual, err := New(env) + if err != nil { + t.Errorf("Expected no errors") + } + + process, args := actual.Process() + if process != wantProcess { + t.Errorf("Want process %v, got: %v", wantProcess, process) + } + + if len(args) != len(wantArguments) { + t.Errorf("Want %d args, got: %d args", len(wantArguments), len(args)) + t.Fail() + } + + for i, wantArg := range wantArguments { + if args[i] != wantArg { + t.Errorf("Want arg%d: %s, got: %s", i, wantArg, args[i]) + } + } +} + +func Test_FunctionProcessWithFlag(t *testing.T) { + env := []string{ + `fprocess=node --this-is-a-flag`, + } + wantProcess := "node" + wantArguments := []string{"--this-is-a-flag"} + + actual, err := New(env) + if err != nil { + t.Errorf("Expected no errors") + } + + process, args := actual.Process() + if process != wantProcess { + t.Errorf("Want process %v, got: %v", wantProcess, process) + } + + if len(args) != len(wantArguments) { + t.Errorf("Want %d args, got: %d args", len(wantArguments), len(args)) + t.Fail() + } + + for i, wantArg := range wantArguments { + if args[i] != wantArg { + t.Errorf("Want arg%d: %s, got: %s", i, wantArg, args[i]) + } + } +} + +func Test_FunctionProcessWithFlagAndValue(t *testing.T) { + env := []string{ + `fprocess=node --this-is-a-flag=1234`, + } + wantProcess := "node" + wantArguments := []string{"--this-is-a-flag=1234"} + + actual, err := New(env) + if err != nil { + t.Errorf("Expected no errors") + } + + process, args := actual.Process() + if process != wantProcess { + t.Errorf("Want process %v, got: %v", wantProcess, process) + } + + if len(args) != len(wantArguments) { + t.Errorf("Want %d args, got: %d args", len(wantArguments), len(args)) + t.Fail() + } + + for i, wantArg := range wantArguments { + if args[i] != wantArg { + t.Errorf("Want arg%d: %s, got: %s", i, wantArg, args[i]) + } + } +} + func Test_PortOverride(t *testing.T) { env := []string{ "port=8081",