Add error assertion helpers
Overview
We are starting to use errors.Is
and errors.As
quite a lot on our codebase, especially inside of tests. However to us to test this we are using assert.True
. This is fine however when there is a failure it's not very clear what is false/true
and the user has to understand what is going on to actually understand the failure. We usually add a message something like expected err %T, but got %T
but this is turning out to be duplication.
Example of duplication:
- https://gitlab.com/gitlab-org/gitlab-runner/-/blob/1dc6f122b21b52d947a9cab29ad25e90bb4c369a/helpers/container/helperimage/info_test.go#L30
- https://gitlab.com/gitlab-org/gitlab-runner/-/blob/1dc6f122b21b52d947a9cab29ad25e90bb4c369a/helpers/container/windows/version_test.go#L42
- https://gitlab.com/gitlab-org/gitlab-runner/-/blob/1dc6f122b21b52d947a9cab29ad25e90bb4c369a/helpers/container/helperimage/windows_info_test.go#L66
- https://gitlab.com/gitlab-org/gitlab-runner/-/blob/715e1faf23aae61190a036f84031ecd68a0a72c2/executors/custom/integration_test.go#L178
- https://gitlab.com/gitlab-org/gitlab-runner/-/blob/715e1faf23aae61190a036f84031ecd68a0a72c2/executors/custom/integration_test.go#L218
- https://gitlab.com/gitlab-org/gitlab-runner/-/blob/715e1faf23aae61190a036f84031ecd68a0a72c2/executors/custom/integration_test.go#L198
Proposal
Quick/still has some duplication
Implement something similar to what we did in the autoscaler with our own assertions library, ideally, this should be split into its own library and used through the projects.
However, this has some drawbacks:
- Yet another library we have to maintainer
- There will be assertion libraries that the project will use which can be confusing.
Ideal situtation
We contribute this to the assertion library in https://github.com/stretchr/testify by adding a new method. To keep backward compatibility for the greater community we can use build tags to specify the go version 1.13 or higher.
This has a few benefits:
- We don't end up with two libraries to do assertion
- We upstream the "need" for this.