2021-10-20 13:50:59 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2024-10-28 18:27:14 +00:00
|
|
|
"encoding/json"
|
2021-10-20 13:50:59 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/acarl005/stripansi"
|
|
|
|
)
|
|
|
|
|
|
|
|
type traitAssertion func(tb testing.TB, stdout, stderr string, rc int)
|
|
|
|
|
2024-02-09 21:05:52 +00:00
|
|
|
func assertNoStderr(tb testing.TB, _, stderr string, _ int) {
|
|
|
|
tb.Helper()
|
|
|
|
if len(stderr) > 0 {
|
|
|
|
tb.Errorf("expected stderr to be empty, but wasn't")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 13:50:59 +00:00
|
|
|
func assertInOutput(data string) traitAssertion {
|
|
|
|
return func(tb testing.TB, stdout, stderr string, _ int) {
|
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
if !strings.Contains(stripansi.Strip(stderr), data) && !strings.Contains(stripansi.Strip(stdout), data) {
|
|
|
|
tb.Errorf("data=%q was NOT found in any output, but should have been there", data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertFailingReturnCode(tb testing.TB, _, _ string, rc int) {
|
|
|
|
tb.Helper()
|
|
|
|
if rc == 0 {
|
|
|
|
tb.Errorf("expected a failure but got rc=%d", rc)
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 14:51:58 +00:00
|
|
|
|
|
|
|
func assertSucceedingReturnCode(tb testing.TB, _, _ string, rc int) {
|
|
|
|
tb.Helper()
|
|
|
|
if rc != 0 {
|
2022-02-10 21:43:12 +00:00
|
|
|
tb.Errorf("expected to succeed but got rc=%d", rc)
|
2021-10-29 14:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-17 18:07:34 +00:00
|
|
|
|
|
|
|
func assertRowInStdOut(row []string) traitAssertion {
|
|
|
|
return func(tb testing.TB, stdout, stderr string, _ int) {
|
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
for _, line := range strings.Split(stdout, "\n") {
|
|
|
|
lineMatched := false
|
|
|
|
for _, column := range row {
|
|
|
|
if !strings.Contains(line, column) {
|
|
|
|
// it wasn't this line
|
|
|
|
lineMatched = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
lineMatched = true
|
|
|
|
}
|
|
|
|
if lineMatched {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// none of the lines matched
|
|
|
|
tb.Errorf("expected stdout to contain %s, but it did not", strings.Join(row, " "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNotInOutput(notWanted string) traitAssertion {
|
|
|
|
return func(tb testing.TB, stdout, stderr string, _ int) {
|
|
|
|
if strings.Contains(stdout, notWanted) {
|
|
|
|
tb.Errorf("got unwanted %s in stdout %s", notWanted, stdout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-28 18:27:14 +00:00
|
|
|
|
|
|
|
func assertJsonReport(tb testing.TB, stdout, _ string, _ int) {
|
|
|
|
tb.Helper()
|
|
|
|
var data interface{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(stdout), &data); err != nil {
|
|
|
|
tb.Errorf("expected to find a JSON report, but was unmarshalable: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertTableReport(tb testing.TB, stdout, _ string, _ int) {
|
|
|
|
tb.Helper()
|
|
|
|
if !strings.Contains(stdout, "NAME") || !strings.Contains(stdout, "LAST SUCCESSFUL RUN") {
|
|
|
|
tb.Errorf("expected to find a table report, but did not")
|
|
|
|
}
|
|
|
|
}
|