testify icon indicating copy to clipboard operation
testify copied to clipboard

require: fix invalid examples in doc comments (codegen)

Open PCloud63514 opened this issue 4 months ago • 2 comments

Summary

Correct invalid examples in require package doc comments. require does not return a boolean value; its responsibility is to fail immediately.

Changes

Updated examples to reflect proper require usage without if. Added a new helper requireCommentParseIf to sanitize and transform invalid doc comments in bulk, ensuring consistency across the package.

Removed conditional forms such as:

if require.NoError(t, err) {
    // ...
}

Updated examples to demonstrate proper usage of require:

// NoError asserts that a function returned no error (i.e. `nil`).
//
//	  actualObj, err := SomeFunction()
//	  require.NoError(t, err)
//	  require.Equal(t, expectedObj, actualObj)
func NoError(t TestingT, err error, msgAndArgs ...interface{})

Motivation

Unlike assert, require functions do not return a boolean value. The previous examples suggested patterns such as if require.NoError(t, err) { ... }, which are invalid. This PR fixes those examples and introduces an automated parser (requireCommentParseIf) to prevent such mistakes from persisting in the future.

Related issues

Closes #1776

PCloud63514 avatar Aug 20 '25 15:08 PCloud63514

@ccoVeille Thanks, I also felt the code was a bit messy, so I tried to simplify it further.

Regarding your comment about tests: since this is only used for code generation, I'm not sure if it makes sense to add dedicated tests here. Do you have any suggestion for a good approach?

PCloud63514 avatar Aug 21 '25 11:08 PCloud63514

Is there anything more I should do, or anything I might be misunderstanding?

PCloud63514 avatar Sep 17 '25 00:09 PCloud63514