Skip to content

all: replace os.MkdirTemp with T.TempDir #45402

Open
@perillo

Description

@perillo

Following #45182 I propose to change all the calls to os.MkdirTemp in tests with T.TempDir.
The os.MkdirTemp is used 145 times in tests.

Currently os.MkdirTemp is not used consistently:

  • some tests use t.Name() as name
  • some tests use the test name as a literal string as name
  • some tests use a short name, or the test name with lowercase or inverted
    (as an example in os_test.go: TestDirentRepeat -> direntRepeat-test)

In the tests for the os package there is a support function:

func newDir(testName string, t *testing.T) (name string) {
	name, err := os.MkdirTemp(localTmp(), "_Go_"+testName)
	if err != nil {
		t.Fatalf("TempDir %s: %s", testName, err)
	}
	return
}

This function can be probably removed.

One possible issue is mixing the use of defer and T.Cleanup, as an example in case of chdir to a temporary directory, resulting in an incorrect cleanup order; one example is https://golang.org/cl/307189 but there seems to be other possible cases.

I would like to submit the change, but a lot of packages are involved; how should I proceed?
Probably I should start with the std module followed by cmd and misc (?).
Should I submit one change for package instead of one change for module?

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.TestingAn issue that has been verified to require only test changes, not just a test failure.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions