mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
print line number in go test failures
This commit is contained in:
parent
f576d29563
commit
9692856020
1 changed files with 15 additions and 3 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -196,15 +197,26 @@ func fullTester(c *Command, input string) resulter {
|
||||||
return resulter{err, output, c}
|
return resulter{err, output, c}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logErr(t *testing.T, found, expected string) {
|
||||||
|
out := new(bytes.Buffer)
|
||||||
|
|
||||||
|
_, _, line, ok := runtime.Caller(2)
|
||||||
|
if ok {
|
||||||
|
fmt.Fprintf(out, "Line: %d ", line)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(out, "Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
|
||||||
|
t.Errorf(out.String())
|
||||||
|
}
|
||||||
|
|
||||||
func checkResultContains(t *testing.T, x resulter, check string) {
|
func checkResultContains(t *testing.T, x resulter, check string) {
|
||||||
if !strings.Contains(x.Output, check) {
|
if !strings.Contains(x.Output, check) {
|
||||||
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", check, x.Output)
|
logErr(t, x.Output, check)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkResultOmits(t *testing.T, x resulter, check string) {
|
func checkResultOmits(t *testing.T, x resulter, check string) {
|
||||||
if strings.Contains(x.Output, check) {
|
if strings.Contains(x.Output, check) {
|
||||||
t.Errorf("Unexpected response.\nExpecting to omit: \n %q\nGot:\n %q\n", check, x.Output)
|
logErr(t, x.Output, check)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,7 +226,7 @@ func checkOutputContains(t *testing.T, c *Command, check string) {
|
||||||
c.Execute()
|
c.Execute()
|
||||||
|
|
||||||
if !strings.Contains(buf.String(), check) {
|
if !strings.Contains(buf.String(), check) {
|
||||||
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", check, buf.String())
|
logErr(t, buf.String(), check)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue