Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add file path to failing tests to make debugging failing tests easier #4461

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
liamg marked this conversation as resolved.
Show resolved Hide resolved
--------------------------------------------------------------------------------
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