Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Detect os.CreateTemp("", ...) #19

@ccoVeille

Description

@ccoVeille

Describe the solution you'd like
I found a new pattern you could support

file, err := os.CreateTemp("", "testfile")
if err != nil {
      t.Fail("creating temporary test file")
 }

If you take a look at os.CreateTemp code

https://cs.opensource.google/go/go/+/go1.22.4:src/os/tempfile.go;l=33

You will see os.CreateTemp creates a temporary folder is the dir is an empty string

The very same way os.MkdirTemp

https://cs.opensource.google/go/go/+/refs/tags/go1.22.4:src/os/tempfile.go;l=85

So your code should suggest this:

file, err := os.CreateTemp(t.TempDir(), "testfile")
if err != nil {
      t.Fail("creating temporary test file")
}

Please have look at https://pkg.go.dev/testing#T.TempDir

and its implementation https://cs.opensource.google/go/go/+/refs/tags/go1.22.4:src/testing/testing.go;l=1229

No need for cleaning up the file, then because the whole temp folder is nuked by the cleanup added via t.TempDir

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions