Skip to content

Commit 38e6257

Browse files
committed
opts: remove deprecated io/ioutil and use t.Cleanup()
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 85754c9 commit 38e6257

1 file changed

Lines changed: 18 additions & 23 deletions

File tree

opts/envfile_test.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@ package opts
22

33
import (
44
"bufio"
5-
"fmt"
6-
"io/ioutil"
75
"os"
86
"reflect"
97
"strings"
108
"testing"
119
)
1210

13-
func tmpFileWithContent(content string, t *testing.T) string {
14-
tmpFile, err := ioutil.TempFile("", "envfile-test")
11+
func tmpFileWithContent(t *testing.T, content string) string {
12+
t.Helper()
13+
tmpFile, err := os.CreateTemp("", "envfile-test")
1514
if err != nil {
1615
t.Fatal(err)
1716
}
1817
defer tmpFile.Close()
1918

20-
tmpFile.WriteString(content)
19+
_, err = tmpFile.WriteString(content)
20+
if err != nil {
21+
t.Fatal(err)
22+
}
23+
t.Cleanup(func() {
24+
_ = os.Remove(tmpFile.Name())
25+
})
2126
return tmpFile.Name()
2227
}
2328

@@ -37,8 +42,7 @@ and_underscore=working too
3742
// from lines, which becomes annoying since that's the
3843
// exact thing we need to test.
3944
content += "\n \t "
40-
tmpFile := tmpFileWithContent(content, t)
41-
defer os.Remove(tmpFile)
45+
tmpFile := tmpFileWithContent(t, content)
4246

4347
lines, err := ParseEnvFile(tmpFile)
4448
if err != nil {
@@ -60,8 +64,7 @@ and_underscore=working too
6064

6165
// Test ParseEnvFile for an empty file
6266
func TestParseEnvFileEmptyFile(t *testing.T) {
63-
tmpFile := tmpFileWithContent("", t)
64-
defer os.Remove(tmpFile)
67+
tmpFile := tmpFileWithContent(t, "")
6568

6669
lines, err := ParseEnvFile(tmpFile)
6770
if err != nil {
@@ -89,9 +92,7 @@ func TestParseEnvFileBadlyFormattedFile(t *testing.T) {
8992
content := `foo=bar
9093
f =quux
9194
`
92-
93-
tmpFile := tmpFileWithContent(content, t)
94-
defer os.Remove(tmpFile)
95+
tmpFile := tmpFileWithContent(t, content)
9596

9697
_, err := ParseEnvFile(tmpFile)
9798
if err == nil {
@@ -108,11 +109,8 @@ func TestParseEnvFileBadlyFormattedFile(t *testing.T) {
108109

109110
// Test ParseEnvFile for a file with a line exceeding bufio.MaxScanTokenSize
110111
func TestParseEnvFileLineTooLongFile(t *testing.T) {
111-
content := strings.Repeat("a", bufio.MaxScanTokenSize+42)
112-
content = fmt.Sprint("foo=", content)
113-
114-
tmpFile := tmpFileWithContent(content, t)
115-
defer os.Remove(tmpFile)
112+
content := "foo=" + strings.Repeat("a", bufio.MaxScanTokenSize+42)
113+
tmpFile := tmpFileWithContent(t, content)
116114

117115
_, err := ParseEnvFile(tmpFile)
118116
if err == nil {
@@ -124,8 +122,7 @@ func TestParseEnvFileLineTooLongFile(t *testing.T) {
124122
func TestParseEnvFileRandomFile(t *testing.T) {
125123
content := `first line
126124
another invalid line`
127-
tmpFile := tmpFileWithContent(content, t)
128-
defer os.Remove(tmpFile)
125+
tmpFile := tmpFileWithContent(t, content)
129126

130127
_, err := ParseEnvFile(tmpFile)
131128
if err == nil {
@@ -146,8 +143,7 @@ func TestParseEnvVariableDefinitionsFile(t *testing.T) {
146143
UNDEFINED_VAR
147144
HOME
148145
`
149-
tmpFile := tmpFileWithContent(content, t)
150-
defer os.Remove(tmpFile)
146+
tmpFile := tmpFileWithContent(t, content)
151147

152148
variables, err := ParseEnvFile(tmpFile)
153149
if nil != err {
@@ -168,8 +164,7 @@ func TestParseEnvVariableWithNoNameFile(t *testing.T) {
168164
content := `# comment=
169165
=blank variable names are an error case
170166
`
171-
tmpFile := tmpFileWithContent(content, t)
172-
defer os.Remove(tmpFile)
167+
tmpFile := tmpFileWithContent(t, content)
173168

174169
_, err := ParseEnvFile(tmpFile)
175170
if nil == err {

0 commit comments

Comments
 (0)