Skip to content

testing: T.TempDir doesn't work well with testing libraries #38850

@rogpeppe

Description

@rogpeppe

The new T.TempDir method is documented to return the same path on every call within the same test:

Subsequent calls to t.TempDir return the same directory.

This means that it's easy for tests that use TempDir to shoot themselves in the foot by overwriting or reading content owned by testing libraries. To use TempDir correctly in the presence of multiple packages you must use a unique subdirectory name, which is hard to choose, so you'd need to use ioutil.TempDir which loses the whole point of having TempDir in the first place.

I propose that every call to TempDir return a new temporary directory; tests can save the result in a local variable if need be.

If it seems like the implied semantics of the name TempDir no longer match the actual behaviour, another possible name would be Mkdir.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions