Skip to content

Commit

Permalink
test: improves coverage for command line argument parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Oct 26, 2023
1 parent 99985c7 commit c9640f1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
18 changes: 7 additions & 11 deletions engine/jvm/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func findImposterJvmProcesses() ([]engine.ManagedMock, error) {
logger.Tracef("found JVM Imposter process %d: %v", p.Pid, cmdline)
port := determinePort(cmdline)
if port == 0 {
if determineTlsEnabled(cmdline) {
if isTlsEnabled(cmdline) {
port = 8443
} else {
port = 8080
Expand Down Expand Up @@ -64,25 +64,21 @@ func isImposterProc(cmdline []string, procName string) bool {
func determinePort(cmdline []string) int {
portRaw := readArg(cmdline, "listenPort", "-l")
if portRaw != "" {
port, err := strconv.Atoi(portRaw)
if err != nil {
return 0
if port, err := strconv.Atoi(portRaw); err == nil {
return port
}
return port
}
return 0
}

// determineTlsEnabled parses the command line arguments to the JVM process
// isTlsEnabled parses the command line arguments to the JVM process
// to determine if TLS is enabled
func determineTlsEnabled(cmdline []string) bool {
func isTlsEnabled(cmdline []string) bool {
tlsRaw := readArg(cmdline, "tlsEnabled", "-t")
if tlsRaw != "" {
tls, err := strconv.ParseBool(tlsRaw)
if err != nil {
return false
if tls, err := strconv.ParseBool(tlsRaw); err == nil {
return tls
}
return tls
}
return false
}
Expand Down
51 changes: 51 additions & 0 deletions engine/jvm/processes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package jvm

import "testing"

func Test_readArg(t *testing.T) {
type args struct {
cmdline []string
longArg string
shortArg string
}
tests := []struct {
name string
args args
want string
}{
{
name: "combined form",
args: args{
cmdline: []string{"--arg=VAL"},
longArg: "arg",
shortArg: "a",
},
want: "VAL",
},
{
name: "separate form",
args: args{
cmdline: []string{"--arg", "VAL"},
longArg: "arg",
shortArg: "a",
},
want: "VAL",
},
{
name: "separate form with short arg",
args: args{
cmdline: []string{"-a", "VAL"},
longArg: "arg",
shortArg: "a",
},
want: "VAL",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := readArg(tt.args.cmdline, tt.args.longArg, tt.args.shortArg); got != tt.want {
t.Errorf("readArg() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit c9640f1

Please sign in to comment.