Skip to content

Commit

Permalink
feat: Add file path to failing tests to make debugging failing tests …
Browse files Browse the repository at this point in the history
…easier (#4461)

Signed-off-by: Liam Galvin <liamgalvin@protonmail.com>
  • Loading branch information
liamg committed Mar 22, 2022
1 parent 8387989 commit 90b91e6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
20 changes: 10 additions & 10 deletions tester/reporter.go
Expand Up @@ -11,10 +11,9 @@ import (
"io"
"strings"

"github.com/open-policy-agent/opa/topdown"

"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/cover"
"github.com/open-policy-agent/opa/topdown"
)

// Reporter defines the interface for reporting test results.
Expand Down Expand Up @@ -71,19 +70,20 @@ func (r PrettyReporter) Report(ch chan *Result) error {
}

// Report individual tests.
var lastFile string
for _, tr := range results {

if tr.Pass() && r.BenchmarkResults {
dirty = true
fmt.Fprintln(r.Output, r.fmtBenchmark(tr))
} else if r.Verbose {
dirty = true
fmt.Fprintln(r.Output, tr)
if len(tr.Output) > 0 {
fmt.Fprintln(r.Output)
fmt.Fprintln(newIndentingWriter(r.Output), strings.TrimSpace(string(tr.Output)))
fmt.Fprintln(r.Output)
} else if r.Verbose || !tr.Pass() {
if tr.Location != nil && tr.Location.File != lastFile {
if lastFile != "" {
fmt.Fprintln(r.Output, "")
}
fmt.Fprintf(r.Output, "%s:\n", tr.Location.File)
lastFile = tr.Location.File
}
} else if !tr.Pass() {
dirty = true
fmt.Fprintln(r.Output, tr)
if len(tr.Output) > 0 {
Expand Down
43 changes: 42 additions & 1 deletion tester/reporter_test.go
Expand Up @@ -36,29 +36,44 @@ func TestPrettyReporterVerbose(t *testing.T) {
Package: "data.foo.bar",
Name: "test_baz",
Trace: getFakeTraceEvents(),
Location: &ast.Location{
File: "policy1.rego",
},
},
{
Package: "data.foo.bar",
Name: "test_qux",
Error: fmt.Errorf("some err"),
Trace: getFakeTraceEvents(),
Location: &ast.Location{
File: "policy1.rego",
},
},
{
Package: "data.foo.bar",
Name: "test_corge",
Fail: true,
Trace: getFakeTraceEvents(),
Location: &ast.Location{
File: "policy2.rego",
},
},
{
Package: "data.foo.bar",
Name: "todo_test_qux",
Skip: true,
Trace: nil,
Location: &ast.Location{
File: "policy2.rego",
},
},
{
Package: "data.foo.bar",
Name: "test_contains_print",
Output: []byte("fake print output\n"),
Location: &ast.Location{
File: "policy3.rego",
},
},
}

Expand All @@ -80,11 +95,16 @@ data.foo.bar.test_corge: FAIL (0s)
SUMMARY
--------------------------------------------------------------------------------
policy1.rego:
data.foo.bar.test_baz: PASS (0s)
data.foo.bar.test_qux: ERROR (0s)
some err
policy2.rego:
data.foo.bar.test_corge: FAIL (0s)
data.foo.bar.todo_test_qux: SKIPPED
policy3.rego:
data.foo.bar.test_contains_print: PASS (0s)
fake print output
Expand Down Expand Up @@ -113,35 +133,53 @@ func TestPrettyReporter(t *testing.T) {
Package: "data.foo.bar",
Name: "test_baz",
Trace: getFakeTraceEvents(),
Location: &ast.Location{
File: "policy1.rego",
},
},
{
Package: "data.foo.bar",
Name: "test_qux",
Error: fmt.Errorf("some err"),
Trace: getFakeTraceEvents(),
Location: &ast.Location{
File: "policy1.rego",
},
},
{
Package: "data.foo.bar",
Name: "test_corge",
Fail: true,
Trace: getFakeTraceEvents(),
Location: &ast.Location{
File: "policy1.rego",
},
},
{
Package: "data.foo.bar",
Name: "todo_test_qux",
Skip: true,
Trace: nil,
Location: &ast.Location{
File: "policy1.rego",
},
},
{
Package: "data.foo.bar",
Name: "test_contains_print_pass",
Output: []byte("fake print output\n"),
Location: &ast.Location{
File: "policy1.rego",
},
},
{
Package: "data.foo.bar",
Name: "test_contains_print_fail",
Fail: true,
Output: []byte("fake print output2\n"),
Location: &ast.Location{
File: "policy2.rego",
},
},
}

Expand All @@ -154,10 +192,13 @@ func TestPrettyReporter(t *testing.T) {
t.Fatal(err)
}

exp := `data.foo.bar.test_qux: ERROR (0s)
exp := `policy1.rego:
data.foo.bar.test_qux: ERROR (0s)
some err
data.foo.bar.test_corge: FAIL (0s)
data.foo.bar.todo_test_qux: SKIPPED
policy2.rego:
data.foo.bar.test_contains_print_fail: FAIL (0s)
fake print output2
Expand Down

0 comments on commit 90b91e6

Please sign in to comment.