Skip to content

sloppyTestFuncName incorrectly identifies helper function #1350

@rliebz

Description

@rliebz

Example Code

I have the following test helper, which temporarily clears all environment variables:

func stashEnv(t *testing.T) {
	t.Helper()

	environ := os.Environ()

	t.Cleanup(func() {
		for _, val := range environ {
			parts := strings.Split(val, "=")
			os.Setenv(parts[0], parts[1])
		}
	})

	os.Clearenv()
}

Expected

I would not expect advice to rename it as a test, since it is a test helper.

Actual

Gocritic offers me the following advice:

sloppyTestFuncName: function stashEnv should be of form TestXXX(t *testing.T)

Additional Information

If I change the test name to testStashEnv, I get the following advice, which is good advice:

sloppyTestFuncName: function testStashEnv looks like a test helper, consider to change 1st param to 'tb testing.TB' 

If I follow that advice with the original function name, both errors go away:

func stashEnv(t testing.TB) { 
  // ...
}

The rule logic seems to consider test a special prefix that means "test helper", but in this particular case I think stashEnv is a more appropriate name than anything starting with test might be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions