mirror of
https://github.com/moby/moby.git
synced 2026-01-11 10:41:43 +00:00
Calling "defer assert.NilError(t, someCommand())" executes "someCommand()" immediately, but doesn't assert the error until the defer. https://go.dev/play/p/EO--y7OYerg package main import ( "errors" "testing" "gotest.tools/v3/assert" ) func TestDefer(t *testing.T) { doSomething := func() error { t.Log("doSomething failed with an error!") return errors.New("foo error") } defer assert.NilError(t, doSomething()) t.Log("running test") t.Log("running test") t.Log("running test") } Produces: === RUN TestDefer prog_test.go:12: doSomething failed with an error! prog_test.go:18: running test prog_test.go:19: running test prog_test.go:20: running test prog_test.go:21: assertion failed: error is not nil: foo error --- FAIL: TestDefer (0.00s) FAIL Signed-off-by: Sebastiaan van Stijn <github@gone.nl>