Skip to content

Use testing.T.TempDir() instead of ioutil.TempDir() in unit tests #9594

@invidian

Description

@invidian

Proposal

Use case. Why is this important?

Instead of writing the code below in many many unit tests:

      dir, err := ioutil.TempDir("", "wal_fuzz_live")
      require.NoError(t, err)
      defer func() {
        require.NoError(t, os.RemoveAll(dir))
      }()

Write this instead:

    dir := t.TempDir()

Advantages:

  • Error handling is implicit.
  • Cleanup is implicit.
  • Naming of directory derives from name of test, again, implicit.

Current occurences:

0 ✓ (40.6ms) 11:11:22 invidian@dellxps15mateusz ~/repos/prometheus (⎇  main)$ git show --no-patch
commit a6e6011d55ed913cb8e53968cead9a6a6fd84911 (HEAD -> main, upstream/main, upstream/HEAD)
Author: Furkan Türkal <furkan.turkal@trendyol.com>
Date:   Mon Oct 25 00:45:31 2021 +0300

    Add scrape_body_size_bytes metric (#9569)

    Fixes #9520

    Signed-off-by: Furkan <furkan.turkal@trendyol.com>
0 ✓ (37.3ms) 11:11:29 invidian@dellxps15mateusz ~/repos/prometheus (⎇  main)$ git grep ioutil.TempDir *_test.go | wc -l
129

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions