From aba5e3a1d36bd4e86a285d3ed6e2beb3734347be Mon Sep 17 00:00:00 2001 From: Philip Conrad Date: Tue, 20 Dec 2022 19:39:15 -0500 Subject: [PATCH] tester/runner: Fix panic'ing case in utility function. (#5497) This commit fixes a panic from a utility function in the `opa test` codepath. After the ref-heads change in #4660, this particular function could be fed a ref that it didn't know how to work with, such as from the innocuous line `a[0] := 1`, and it would then panic. This was fixed by returning a dummy value instead of panic'ing. Fixes: #5496 Signed-off-by: Philip Conrad (cherry picked from commit a08934d5715214e9cdf257d09f94fec6a78e546c) --- tester/runner.go | 2 +- tester/runner_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tester/runner.go b/tester/runner.go index 9852882f8c..cae4730129 100644 --- a/tester/runner.go +++ b/tester/runner.go @@ -437,7 +437,7 @@ func ruleName(h *ast.Head) string { case ast.String: return string(last) default: - panic("unreachable") + return "" } } diff --git a/tester/runner_test.go b/tester/runner_test.go index 33241fc2be..a613bf2065 100644 --- a/tester/runner_test.go +++ b/tester/runner_test.go @@ -81,6 +81,11 @@ func testRun(t *testing.T, conf testRunConfig) map[string]*ast.Module { a.b.test_duplicate { false } a.b.test_duplicate { true } a.b.test_duplicate { true }`, + // Regression test for issue #5496. + "/d_test.rego": `package test + + a[0] := 1 + test_pass { true }`, } tests := expectedTestResults{ @@ -96,6 +101,7 @@ func testRun(t *testing.T, conf testRunConfig) map[string]*ast.Module { {"data.baz", "a.b.test_duplicate"}: {false, true, false}, {"data.baz", "a.b[\"test_duplicate#01\"]"}: {false, false, false}, {"data.baz", "a.b[\"test_duplicate#02\"]"}: {false, false, false}, + {"data.test", "test_pass"}: {false, false, false}, } var modules map[string]*ast.Module