Use T.TempDir for temporary dirs in tests (#3580)

This commit is contained in:
Cristian Dominguez 2021-05-07 10:21:26 +00:00 committed by GitHub
parent 25d79c4e16
commit 2f94adabb2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 28 additions and 40 deletions

View file

@ -175,11 +175,10 @@ func assertNextLineEquals(scanner *bufio.Scanner, expectedLine string) error {
}
func BenchmarkGenManToFile(b *testing.B) {
file, err := ioutil.TempFile("", "")
file, err := ioutil.TempFile(b.TempDir(), "")
if err != nil {
b.Fatal(err)
}
defer os.Remove(file.Name())
defer file.Close()
b.ResetTimer()

View file

@ -83,11 +83,10 @@ func TestGenMdTree(t *testing.T) {
}
func BenchmarkGenMarkdownToFile(b *testing.B) {
file, err := ioutil.TempFile("", "")
file, err := ioutil.TempFile(b.TempDir(), "")
if err != nil {
b.Fatal(err)
}
defer os.Remove(file.Name())
defer file.Close()
b.ResetTimer()

View file

@ -693,6 +693,9 @@ func Test_apiRun_inputFile(t *testing.T) {
contentLength: 10,
},
}
tempDir := t.TempDir()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
io, stdin, _, _ := iostreams.Test()
@ -702,13 +705,12 @@ func Test_apiRun_inputFile(t *testing.T) {
if tt.inputFile == "-" {
_, _ = stdin.Write(tt.inputContents)
} else {
f, err := ioutil.TempFile("", tt.inputFile)
f, err := ioutil.TempFile(tempDir, tt.inputFile)
if err != nil {
t.Fatal(err)
}
_, _ = f.Write(tt.inputContents)
f.Close()
t.Cleanup(func() { os.Remove(f.Name()) })
defer f.Close()
inputFile = f.Name()
}
@ -825,13 +827,13 @@ func Test_parseFields(t *testing.T) {
}
func Test_magicFieldValue(t *testing.T) {
f, err := ioutil.TempFile("", "gh-test")
f, err := ioutil.TempFile(t.TempDir(), "gh-test")
if err != nil {
t.Fatal(err)
}
defer f.Close()
fmt.Fprint(f, "file contents")
f.Close()
t.Cleanup(func() { os.Remove(f.Name()) })
io, _, _, _ := iostreams.Test()
@ -932,13 +934,13 @@ func Test_magicFieldValue(t *testing.T) {
}
func Test_openUserFile(t *testing.T) {
f, err := ioutil.TempFile("", "gh-test")
f, err := ioutil.TempFile(t.TempDir(), "gh-test")
if err != nil {
t.Fatal(err)
}
defer f.Close()
fmt.Fprint(f, "file contents")
f.Close()
t.Cleanup(func() { os.Remove(f.Name()) })
file, length, err := openUserFile(f.Name(), nil)
if err != nil {

View file

@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
@ -422,8 +421,9 @@ func TestIssueCreate_recover(t *testing.T) {
},
})
tmpfile, err := ioutil.TempFile(os.TempDir(), "testrecover*")
tmpfile, err := ioutil.TempFile(t.TempDir(), "testrecover*")
assert.NoError(t, err)
defer tmpfile.Close()
state := prShared.IssueMetadataState{
Title: "recovered title",

View file

@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"
@ -309,8 +308,9 @@ func TestPRCreate_recover(t *testing.T) {
},
})
tmpfile, err := ioutil.TempFile(os.TempDir(), "testrecover*")
tmpfile, err := ioutil.TempFile(t.TempDir(), "testrecover*")
assert.NoError(t, err)
defer tmpfile.Close()
state := prShared.IssueMetadataState{
Title: "recovered title",

View file

@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"testing"
"github.com/cli/cli/pkg/iostreams"
@ -70,6 +69,8 @@ func Test_PreserveInput(t *testing.T) {
},
}
tempDir := t.TempDir()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.state == nil {
@ -78,9 +79,9 @@ func Test_PreserveInput(t *testing.T) {
io, _, _, errOut := iostreams.Test()
tf, tferr := tmpfile()
tf, tferr := ioutil.TempFile(tempDir, "testfile*")
assert.NoError(t, tferr)
defer os.Remove(tf.Name())
defer tf.Close()
io.TempFileOverride = tf
@ -111,13 +112,3 @@ func Test_PreserveInput(t *testing.T) {
})
}
}
func tmpfile() (*os.File, error) {
dir := os.TempDir()
tmpfile, err := ioutil.TempFile(dir, "testfile*")
if err != nil {
return nil, err
}
return tmpfile, nil
}

View file

@ -7,7 +7,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/cli/cli/api"
@ -149,13 +148,13 @@ func TestNewCmdRun(t *testing.T) {
}
func Test_magicFieldValue(t *testing.T) {
f, err := ioutil.TempFile("", "gh-test")
f, err := ioutil.TempFile(t.TempDir(), "gh-test")
if err != nil {
t.Fatal(err)
}
defer f.Close()
fmt.Fprint(f, "file contents")
f.Close()
t.Cleanup(func() { os.Remove(f.Name()) })
io, _, _, _ := iostreams.Test()

View file

@ -261,12 +261,11 @@ func TestFindLegacy(t *testing.T) {
}
func TestExtractName(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "gh-cli")
tmpfile, err := ioutil.TempFile(t.TempDir(), "gh-cli")
if err != nil {
t.Fatal(err)
}
tmpfile.Close()
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()
type args struct {
filePath string
@ -322,12 +321,11 @@ about: This is how you report bugs
}
func TestExtractContents(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "gh-cli")
tmpfile, err := ioutil.TempFile(t.TempDir(), "gh-cli")
if err != nil {
t.Fatal(err)
}
tmpfile.Close()
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()
type args struct {
filePath string