stripe / skycfg

Skycfg is an extension library for the Starlark language that adds support for constructing Protocol Buffer messages.
Apache License 2.0
648 stars 54 forks source link

Support custom error message in assertion module #121

Open LINKIWI opened 5 months ago

LINKIWI commented 5 months ago

This PR proposes the addition of an optional secondary argument to ctx.assert(...) which enables unit test callsites to specify a custom assertion failure error message.

This looks like, for example:

def test_foo(ctx):
    ctx.assert(False)

def test_foo_message(ctx):
    ctx.assert(False, "foo error message")

Executing these tests results in:

[foo.sky:2:15] assertion failed
...
[foo.sky:4:15] assertion failed: foo error message
...

I added corresponding unit test coverage.

$ bazelisk test --cache_test_results=no //go/...
INFO: Invocation ID: bc5157d2-13f6-4d45-84b3-42aae65fc926
INFO: Analyzed 10 targets (0 packages loaded, 137 targets configured).
INFO: Found 5 targets and 5 test targets...
INFO: Elapsed time: 2.453s, Critical Path: 0.51s
INFO: 6 processes: 1 internal, 5 processwrapper-sandbox.
INFO: Build completed successfully, 6 total actions
//go/assertmodule:assertmodule_test                                      PASSED in 0.3s
//go/hashmodule:hashmodule_test                                          PASSED in 0.4s
//go/protomodule:protomodule_test                                        PASSED in 0.5s
//go/urlmodule:urlmodule_test                                            PASSED in 0.4s
//go/yamlmodule:yamlmodule_test                                          PASSED in 0.3s