Skip to content

Conversation

@johanfylling
Copy link
Contributor

Local variable values in test report

When enabled (--var-values flag), the failure report for tests will contain values for variables local to the failing expression. e.g.:

FAILURES
--------------------------------------------------------------------------------
data.test.test_foo: FAIL (0ms)

  test.rego:8:
    	x == y + z
    	|    |   |
    	|    |   3
    	|    y + z: 5
    	|    y: 2
    	1

SUMMARY
--------------------------------------------------------------------------------
test.rego:
data.test.test_foo: FAIL (0ms)
--------------------------------------------------------------------------------
FAIL: 1/1

Local variable values in pretty trace

When enabled on eval and test commands (--var-values flag), local variable values are added to pretty trace:

query:1          Enter data.test.p = _                                {}
query:1          | Eval data.test.p = _                               {}
query:1          | Index data.test.p (matched 1 rule, early exit)     {}
/test.rego:4     | Enter data.test.p                                  {}
/test.rego:5     | | Eval x = 1                                       {}
/test.rego:6     | | Eval y = 2                                       {}
/test.rego:7     | | Eval z = 3                                       {}
/test.rego:8     | | Eval minus(z, y, __local3__)                     {y: 2, z: 3}
/test.rego:8     | | Eval x = __local3__                              {__local3__: 1, x: 1}
/test.rego:4     | | Exit data.test.p early                           {}
query:1          | Exit data.test.p = _                               {_: true, data.test.p: true}
query:1          Redo data.test.p = _                                 {_: true, data.test.p: true}
query:1          | Redo data.test.p = _                               {_: true, data.test.p: true}
/test.rego:4     | Redo data.test.p                                   {}
/test.rego:8     | | Redo x = __local3__                              {__local3__: 1, x: 1}
/test.rego:8     | | Redo minus(z, y, __local3__)                     {__local3__: 1, y: 2, z: 3}
/test.rego:7     | | Redo z = 3                                       {z: 3}
/test.rego:6     | | Redo y = 2                                       {y: 2}
/test.rego:5     | | Redo x = 1                                       {x: 1}

Fixing: #2546

Local variable values in test report
------------------------------------

When enabled (`--var-values` flag), the failure report for tests will contain values for variables local to the failing expression. e.g.:

```
FAILURES
--------------------------------------------------------------------------------
data.test.test_foo: FAIL (0ms)

  test.rego:8:
    	x == y + z
    	|    |   |
    	|    |   3
    	|    y + z: 5
    	|    y: 2
    	1

SUMMARY
--------------------------------------------------------------------------------
test.rego:
data.test.test_foo: FAIL (0ms)
--------------------------------------------------------------------------------
FAIL: 1/1
```

Local variable values in pretty trace
-------------------------------------

When enabled on `eval` and `test` commands (`--var-values` flag), local variable values are added to pretty trace:

```
query:1          Enter data.test.p = _                                {}
query:1          | Eval data.test.p = _                               {}
query:1          | Index data.test.p (matched 1 rule, early exit)     {}
/test.rego:4     | Enter data.test.p                                  {}
/test.rego:5     | | Eval x = 1                                       {}
/test.rego:6     | | Eval y = 2                                       {}
/test.rego:7     | | Eval z = 3                                       {}
/test.rego:8     | | Eval minus(z, y, __local3__)                     {y: 2, z: 3}
/test.rego:8     | | Eval x = __local3__                              {__local3__: 1, x: 1}
/test.rego:4     | | Exit data.test.p early                           {}
query:1          | Exit data.test.p = _                               {_: true, data.test.p: true}
query:1          Redo data.test.p = _                                 {_: true, data.test.p: true}
query:1          | Redo data.test.p = _                               {_: true, data.test.p: true}
/test.rego:4     | Redo data.test.p                                   {}
/test.rego:8     | | Redo x = __local3__                              {__local3__: 1, x: 1}
/test.rego:8     | | Redo minus(z, y, __local3__)                     {__local3__: 1, y: 2, z: 3}
/test.rego:7     | | Redo z = 3                                       {z: 3}
/test.rego:6     | | Redo y = 2                                       {y: 2}
/test.rego:5     | | Redo x = 1                                       {x: 1}
```

Fixing: open-policy-agent#2546

Signed-off-by: Johan Fylling <[email protected]>
@netlify
Copy link

netlify bot commented Jun 12, 2024

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 4ea0538
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/667a99d0d1b8ff000899632e
😎 Deploy Preview https://deploy-preview-6815--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Signed-off-by: Johan Fylling <[email protected]>
@johanfylling
Copy link
Contributor Author

The linter fails because it gets confused by some newer golang stuff: #6817

testCommand.Flags().BoolVar(&testParams.benchmark, "bench", false, "benchmark the unit tests")
testCommand.Flags().StringVarP(&testParams.runRegex, "run", "r", "", "run only test cases matching the regular expression.")
testCommand.Flags().BoolVarP(&testParams.watch, "watch", "w", false, "watch command line files for changes")
testCommand.Flags().BoolVar(&testParams.varValues, "var-values", false, "show local variable values in test output")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming suggestions appreciated 😄.

@johanfylling johanfylling marked this pull request as ready for review June 14, 2024 14:36
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments after reading through the change, skimming over a few lines here and there 😎

ast/compile.go Outdated
useTypeCheckAnnotations bool // whether to provide annotated information (schemas) to the type checker
allowUndefinedFuncCalls bool // don't error on calls to unknown functions.
evalMode CompilerEvalMode
rewriteTestRulesToCaptureUnboundDynamics bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Dynamics" -- it's not a term that's common in ast or topdown, is it? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True 😄, I think that trickled up from the rewriteDynamics*() naming convention we have in the compiler. Naming suggestions are welcome :). It's a bit too long for my liking too ..

return false
})
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would be possible to add a few tests cases showing of the rewriting that's happening here? I'm afraid I don't understand it just from the code 😅 Maybe I just haven't found them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll add some of those 👍 .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some tests for this compiler stage. Hope they make things more clear.

| | |
| | {"read": ["alice", "bob"], "write": ["jane"]}
| "jane"
["write"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 This is going to be a tremendous help!

matrix:
os: [ubuntu-22.04, macos-14]
version: ["1.20"]
version: ["1.21"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.20 isn't a supported Go version anyways. I think we simply forgot to bump this when we bumped the build version to 0.22.

@johanfylling johanfylling requested a review from srenatus June 25, 2024 09:38
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tests and the rename! LGTM

@johanfylling johanfylling merged commit 96ecf38 into open-policy-agent:main Jun 25, 2024
@johanfylling johanfylling deleted the test_pretty_var_values branch June 25, 2024 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants