Skip to content

Commit

Permalink
Reuse existing env in TestMainImpl
Browse files Browse the repository at this point in the history
Signed-off-by: Mateusz Łoskot <[email protected]>
  • Loading branch information
mloskot committed Sep 23, 2024
1 parent 615fe9f commit bc6468e
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions httpbin/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"flag"
"fmt"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -540,9 +541,8 @@ func TestMainImpl(t *testing.T) {

testCases := map[string]struct {
args []string
optionsEnv map[string]string
env map[string]string
getHostname func() (string, error)
env []string
wantCode int
wantOut string
wantOutFn func(t *testing.T, out string)
Expand Down Expand Up @@ -607,7 +607,7 @@ func TestMainImpl(t *testing.T) {
}

buf := &bytes.Buffer{}
gotCode := mainImpl(tc.args, func(key string) string { return tc.optionsEnv[key] }, func() []string { return tc.env }, tc.getHostname, buf)
gotCode := mainImpl(tc.args, func(key string) string { return tc.env[key] }, func() []string { return environSlice(tc.env) }, tc.getHostname, buf)
out := buf.String()

if gotCode != tc.wantCode {
Expand All @@ -626,3 +626,11 @@ func TestMainImpl(t *testing.T) {
})
}
}

func environSlice(env map[string]string) []string {
envStrings := make([]string, 0, len(env))
for name, value := range env {
envStrings = append(envStrings, fmt.Sprintf("%s=%s", name, value))
}
return envStrings
}

0 comments on commit bc6468e

Please sign in to comment.