Skip to content

Commit

Permalink
Allow flags with values
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
alexellis committed Dec 28, 2018
1 parent 85505a7 commit ba8afec
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
10 changes: 7 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 91 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import "testing"
import "time"
import (
"testing"
"time"
)

func TestNew(t *testing.T) {
defaults, err := New([]string{})
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit ba8afec

Please sign in to comment.