Skip to content

Commit 9966970

Browse files
authored
Fixed tests so they work on localhost (#715)
* Fixed tests so they work on localhost The problem was that we now get a warning when we are missing repo information. That is available in CI but not on localhost. We just fake it. * Added goldenStdout and goldenStderr in tests
1 parent 8abd069 commit 9966970

2 files changed

Lines changed: 44 additions & 22 deletions

File tree

Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo
1010

1111
GOTESTSUM = $(shell which gotestsum || echo "~/go/bin/gotestsum")
1212

13+
# Fake CI env vars so that tests don't emit "Repo information will not be reported" warnings.
14+
# These are only used when running tests locally (real CI already sets them).
15+
FAKE_CI_ENV = GITHUB_RUN_NUMBER=1 GITHUB_SERVER_URL=https://github.com GITHUB_REPOSITORY=kosli-dev/cli GITHUB_REPOSITORY_ID=123456
16+
1317
ifdef VERSION
1418
BINARY_VERSION = $(VERSION)
1519
endif
@@ -100,7 +104,7 @@ setup_test_to_use_staging_server_image:
100104

101105
test_integration: deps vet ensure_network test_setup ## Run tests except the too slow ones
102106
@[ -e ~/.kosli.yml ] && mv ~/.kosli.yml ~/.kosli-renamed.yml || true
103-
@export KOSLI_TESTS=true && $(GOTESTSUM) -- --short -p=8 -coverprofile=cover.out ./...
107+
@export KOSLI_TESTS=true $(FAKE_CI_ENV) && $(GOTESTSUM) -- --short -p=8 -coverprofile=cover.out ./...
104108
@go tool cover -func=cover.out | grep total:
105109
@go tool cover -html=cover.out
106110
@[ -e ~/.kosli-renamed.yml ] && mv ~/.kosli-renamed.yml ~/.kosli.yml || true
@@ -109,19 +113,19 @@ test_integration: deps vet ensure_network test_setup ## Run tests except the too
109113
test_integration_full: deps vet ensure_network test_setup ## Run all tests
110114
@[ -e ~/.kosli.yml ] && mv ~/.kosli.yml ~/.kosli-renamed.yml || true
111115
@mkdir -p junit-test-results
112-
@export KOSLI_TESTS=true && $(GOTESTSUM) --junitfile junit-test-results/junit.xml -- -p=8 -coverprofile=cover.out ./...
116+
@export KOSLI_TESTS=true $(FAKE_CI_ENV) && $(GOTESTSUM) --junitfile junit-test-results/junit.xml -- -p=8 -coverprofile=cover.out ./...
113117
@go tool cover -func=cover.out
114118
@[ -e ~/.kosli-renamed.yml ] && mv ~/.kosli-renamed.yml ~/.kosli.yml || true
115119

116120

117121
test_integration_restart_server: test_setup_restart_server
118122
@[ -e ~/.kosli.yml ] && mv ~/.kosli.yml ~/.kosli-renamed.yml || true
119-
@export KOSLI_TESTS=true && $(GOTESTSUM) -- --short -p=8 -coverprofile=cover.out ./...
123+
@export KOSLI_TESTS=true $(FAKE_CI_ENV) && $(GOTESTSUM) -- --short -p=8 -coverprofile=cover.out ./...
120124
@go tool cover -html=cover.out
121125
@[ -e ~/.kosli-renamed.yml ] && mv ~/.kosli-renamed.yml ~/.kosli.yml || true
122126

123127
test_integration_single: test_setup
124-
@export KOSLI_TESTS=true && $(GOTESTSUM) -- -p=1 ./... -run "${TARGET}"
128+
@export KOSLI_TESTS=true $(FAKE_CI_ENV) && $(GOTESTSUM) -- -p=1 ./... -run "${TARGET}"
125129

126130

127131
test_docs: deps vet ensure_network test_setup

cmd/kosli/testHelpers.go

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"encoding/json"
7+
"io"
78
"os"
89
"path/filepath"
910
"regexp"
@@ -31,35 +32,42 @@ type cmdTestCase struct {
3132
goldenFile string
3233
goldenRegex string
3334
goldenJson []jsonCheck // Use like this for array {"[0].compliant", false}
35+
goldenStdout string // expected stdout only (exact match, ignored when empty)
36+
goldenStderr string // expected stderr only (exact match, ignored when empty)
3437
wantError bool
3538
additionalConfig interface{}
3639
}
3740

38-
// executeCommandStdinC executes a command as a user would and return the output
39-
// this creates a new kosli command that is run, but it cannot be used in other tests
40-
// because newRootCmd overwrites the global options
41-
func executeCommandC(cmd string) (*cobra.Command, string, error) {
41+
// executeCommandC executes a command as a user would and returns the output
42+
// split into combined, stdout-only, and stderr-only streams.
43+
// This creates a new kosli command that is run, but it cannot be used in other tests
44+
// because newRootCmd overwrites the global options.
45+
func executeCommandC(cmd string) (*cobra.Command, string, string, string, error) {
4246
args, err := shellwords.Parse(cmd)
4347
if err != nil {
44-
return nil, "", err
48+
return nil, "", "", "", err
4549
}
4650

47-
buf := new(bytes.Buffer)
51+
combinedBuf := new(bytes.Buffer)
52+
outBuf := new(bytes.Buffer)
53+
errBuf := new(bytes.Buffer)
4854

49-
root, err := newRootCmd(buf, buf, args)
55+
outWriter := io.MultiWriter(outBuf, combinedBuf)
56+
errWriter := io.MultiWriter(errBuf, combinedBuf)
57+
58+
root, err := newRootCmd(outWriter, errWriter, args)
5059
if err != nil {
51-
return nil, "", err
60+
return nil, "", "", "", err
5261
}
5362

5463
root.SilenceErrors = false
55-
root.SetOut(buf)
56-
root.SetErr(buf)
64+
root.SetOut(outWriter)
65+
root.SetErr(errWriter)
5766
root.SetArgs(args)
5867

5968
c, err := root.ExecuteC()
60-
output := buf.String()
6169

62-
return c, output, err
70+
return c, combinedBuf.String(), outBuf.String(), errBuf.String(), err
6371
}
6472

6573
// runTestCmd runs a table of cmd test cases
@@ -76,23 +84,33 @@ func runTestCmd(t *testing.T, tests []cmdTestCase) {
7684
t.Error("golden and goldenPath cannot be set together")
7785
}
7886
t.Logf("running cmd: %s", tt.cmd)
79-
_, out, err := executeCommandC(tt.cmd)
87+
_, combined, stdout, stderr, err := executeCommandC(tt.cmd)
8088
if (err != nil) != tt.wantError {
8189
t.Errorf("error expectation not matched\n\n WANT error is: %t\n\n but GOT: '%v'", tt.wantError, err)
8290
}
8391
if tt.golden != "" {
84-
if !bytes.Equal([]byte(tt.golden), []byte(out)) {
85-
t.Errorf("does not match golden\n\nWANT:\n'%s'\n\nGOT:\n'%s'\n", tt.golden, out)
92+
if !bytes.Equal([]byte(tt.golden), []byte(combined)) {
93+
t.Errorf("does not match golden\n\nWANT:\n'%s'\n\nGOT:\n'%s'\n", tt.golden, combined)
8694
}
8795
} else if tt.goldenFile != "" {
88-
if err := compareAgainstFile([]byte(out), goldenPath(tt.goldenFile)); err != nil {
96+
if err := compareAgainstFile([]byte(combined), goldenPath(tt.goldenFile)); err != nil {
8997
t.Error(err)
9098
}
9199
} else if tt.goldenRegex != "" {
92-
require.Regexp(t, tt.goldenRegex, out)
100+
require.Regexp(t, tt.goldenRegex, combined)
93101
} else if len(tt.goldenJson) > 0 {
94102
for _, check := range tt.goldenJson {
95-
goldenJsonContains(t, out, check.Path, check.Want)
103+
goldenJsonContains(t, combined, check.Path, check.Want)
104+
}
105+
}
106+
if tt.goldenStdout != "" {
107+
if !bytes.Equal([]byte(tt.goldenStdout), []byte(stdout)) {
108+
t.Errorf("stdout does not match goldenStdout\n\nWANT:\n'%s'\n\nGOT:\n'%s'\n", tt.goldenStdout, stdout)
109+
}
110+
}
111+
if tt.goldenStderr != "" {
112+
if !bytes.Equal([]byte(tt.goldenStderr), []byte(stderr)) {
113+
t.Errorf("stderr does not match goldenStderr\n\nWANT:\n'%s'\n\nGOT:\n'%s'\n", tt.goldenStderr, stderr)
96114
}
97115
}
98116
})

0 commit comments

Comments
 (0)