Skip to content

util/must: convenience wrapper for making many assertions #107418

@erikgrinaker

Description

@erikgrinaker

See #106508.

It's common to execute many assertions at once, see e.g. storage.assertMVCCIteratorInvariants(). Returning errors from each individual assertion can get tedious and clutter the code. It would be convenient to run many assertions in a block, if we can do so efficiently.

Here's an example of how this might look:

err := must.With(ctx, func(m *must.Must) {
  m.True(true, "yep")
  m.False(false, "again")
  value, err := someFunc()
  m.NoError(err, "someFunc failed")
  m.Equal(value, "foo", "someFunc returned invalid value")
})

This would be implemented by throwing panics on assertion failures, and recovering the panic in must.With. A specific panic value would be thrown, wrapping the assertion error, and only this value would be recovered -- other panics would be propagated.

must.With itself would handle the assertion failure like any other assertion failure, i.e. fatal or propagate.

Jira issue: CRDB-30031

Metadata

Metadata

Assignees

Labels

A-testingTesting tools and infrastructureC-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)T-testengTestEng Team

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions