decred / politeia

ISC License
110 stars 75 forks source link

test: use `T.TempDir` to create temporary test directory #1654

Closed Juneezee closed 1 year ago

Juneezee commented 2 years ago

A testing cleanup.

This pull request replaces os.MkdirTemp with t.TempDir. We can use the T.TempDir function from the testing package to create temporary directory. The directory created by T.TempDir is automatically removed when the test and all its subtests complete.

This saves us at least 2 lines (error check, and cleanup) on every instance, or in some cases adds cleanup that we forgot.

Reference: https://pkg.go.dev/testing#T.TempDir

func TestFoo(t *testing.T) {
    // before
    tmpDir, err := os.MkdirTemp("", "")
    if err != nil {
        t.Fatal(err)
    }
    defer os.RemoveAll(tmpDir)

    // now
    tmpDir := t.TempDir()
}
Juneezee commented 1 year ago

Hello @amass01. I apologize for the explicit mention, but I wanted to follow up on this pull request I created some time ago.

I would be grateful if you could take some time to review the changes and provide me with your feedback. If there are any specific concerns or questions you have about the changes, I am more than willing to help address them.

Thank you for your time and consideration. I truly appreciate it. :heart: