Skip to content

Conversation

@majewsky
Copy link
Contributor

@majewsky majewsky commented Oct 8, 2025

These unify all the various ad-hoc test helper methods that we created over the years into two interfaces:

  • assert.ErrEqual is for checking non-fatal errors during test execution.
  • must.SucceedT and must.ReturnT are for catching fatal errors during test setup/teardown. They are testing.T-based variants of the established functions of the same name without T.

As the code comments indicate, I have agonized over these function signatures for a long time. This changeset may not be the optimal solution, but in the end, I feel that having these is better than having ad-hoc helper functions scattered all over all our codebases.

This unifies all the various ad-hoc test helper methods that we created
over the years into one interface.
As the code comment in ReturnT indicates, I have agonized over these
function signatures for a long time. This might still not be the optimal
solution, but in the end, having this is better than having ad-hoc
helper functions splattered all over the place.
assert/assert.go Outdated
Comment on lines 54 to 66
if actual == nil {
if expected == nil {
// defense in depth: this should have been covered by the previous case branch
return true
}
t.Errorf("expected error stack to contain %q, but got no error", expected.Error())
return false
}
if expected == nil {
// defense in depth: this should have been covered by the previous case branch
t.Errorf("expected success, but got error: %s", actual.Error())
return false
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if actual == nil {
if expected == nil {
// defense in depth: this should have been covered by the previous case branch
return true
}
t.Errorf("expected error stack to contain %q, but got no error", expected.Error())
return false
}
if expected == nil {
// defense in depth: this should have been covered by the previous case branch
t.Errorf("expected success, but got error: %s", actual.Error())
return false
}
if expected == nil {
// defense in depth: this should have been covered by the previous case branch
t.Errrof("this should not happen)
return true
}
if actual == nil {
t.Errorf("expected error stack to contain %q, but got no error", expected.Error())
return false
}

Copy link
Contributor Author

@majewsky majewsky Oct 8, 2025

Choose a reason for hiding this comment

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

I don't want to error out on this. Whether these "defense in depth" branches are taken or not depends on how you read this specific part in the spec:

Instead of a type, a case may use the predeclared identifier nil; that case is selected when the expression in the TypeSwitchGuard is a nil interface value.

I could swear that, in earlier Go versions, this only matched the plain nil value and not e.g. error(nil), i.e. an any-typed value containing an error interface instance with the contained error nil. It appears to be different now, but this part of the spec has not changed since Go 1.10, so I'm assuming this to be undefined behavior and thus took care to make those branches behave the same, so that it does not matter what choices the compiler implementers take in the future.

@github-actions
Copy link

github-actions bot commented Oct 8, 2025

Merging this branch changes the coverage (1 decrease, 2 increase)

Impacted Packages Coverage Δ 🤖
github.com/sapcc/go-bits/assert 26.25% (+19.69%) 🎉
github.com/sapcc/go-bits/httptest 89.41% (ø)
github.com/sapcc/go-bits/internal/testutil 100.00% (+100.00%) 🌟
github.com/sapcc/go-bits/must 33.33% (-41.67%) 💀 💀 💀 💀
github.com/sapcc/go-bits/osext 66.67% (ø)

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/sapcc/go-bits/assert/assert.go 77.78% (+27.78%) 810 (+618) 630 (+534) 180 (+84) 🌟
github.com/sapcc/go-bits/internal/testutil/mockt.go 100.00% (+100.00%) 12 (+12) 12 (+12) 0 🌟
github.com/sapcc/go-bits/must/must.go 33.33% (-41.67%) 54 (+30) 18 36 (+30) 💀 💀 💀 💀

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

Changed unit test files

  • github.com/sapcc/go-bits/assert/assert_test.go
  • github.com/sapcc/go-bits/httptest/handler_test.go
  • github.com/sapcc/go-bits/osext/env_test.go

@majewsky majewsky merged commit 0560184 into master Oct 8, 2025
7 checks passed
@majewsky majewsky deleted the assert-errequal branch October 8, 2025 14:38
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.

3 participants