opts: remove deprecated io/ioutil and use t.Cleanup()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 38e62571fb)
Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Sebastiaan van Stijn 2022-02-25 15:35:13 +01:00 committed by Cory Snider
parent 3f681a2f64
commit d228386fa0
1 changed files with 18 additions and 23 deletions

View File

@ -2,22 +2,27 @@ package opts
import ( import (
"bufio" "bufio"
"fmt"
"io/ioutil"
"os" "os"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
) )
func tmpFileWithContent(content string, t *testing.T) string { func tmpFileWithContent(t *testing.T, content string) string {
tmpFile, err := ioutil.TempFile("", "envfile-test") t.Helper()
tmpFile, err := os.CreateTemp("", "envfile-test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer tmpFile.Close() defer tmpFile.Close()
tmpFile.WriteString(content) _, err = tmpFile.WriteString(content)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.Remove(tmpFile.Name())
})
return tmpFile.Name() return tmpFile.Name()
} }
@ -37,8 +42,7 @@ and_underscore=working too
// from lines, which becomes annoying since that's the // from lines, which becomes annoying since that's the
// exact thing we need to test. // exact thing we need to test.
content += "\n \t " content += "\n \t "
tmpFile := tmpFileWithContent(content, t) tmpFile := tmpFileWithContent(t, content)
defer os.Remove(tmpFile)
lines, err := ParseEnvFile(tmpFile) lines, err := ParseEnvFile(tmpFile)
if err != nil { if err != nil {
@ -60,8 +64,7 @@ and_underscore=working too
// Test ParseEnvFile for an empty file // Test ParseEnvFile for an empty file
func TestParseEnvFileEmptyFile(t *testing.T) { func TestParseEnvFileEmptyFile(t *testing.T) {
tmpFile := tmpFileWithContent("", t) tmpFile := tmpFileWithContent(t, "")
defer os.Remove(tmpFile)
lines, err := ParseEnvFile(tmpFile) lines, err := ParseEnvFile(tmpFile)
if err != nil { if err != nil {
@ -89,9 +92,7 @@ func TestParseEnvFileBadlyFormattedFile(t *testing.T) {
content := `foo=bar content := `foo=bar
f =quux f =quux
` `
tmpFile := tmpFileWithContent(t, content)
tmpFile := tmpFileWithContent(content, t)
defer os.Remove(tmpFile)
_, err := ParseEnvFile(tmpFile) _, err := ParseEnvFile(tmpFile)
if err == nil { if err == nil {
@ -108,11 +109,8 @@ func TestParseEnvFileBadlyFormattedFile(t *testing.T) {
// Test ParseEnvFile for a file with a line exceeding bufio.MaxScanTokenSize // Test ParseEnvFile for a file with a line exceeding bufio.MaxScanTokenSize
func TestParseEnvFileLineTooLongFile(t *testing.T) { func TestParseEnvFileLineTooLongFile(t *testing.T) {
content := strings.Repeat("a", bufio.MaxScanTokenSize+42) content := "foo=" + strings.Repeat("a", bufio.MaxScanTokenSize+42)
content = fmt.Sprint("foo=", content) tmpFile := tmpFileWithContent(t, content)
tmpFile := tmpFileWithContent(content, t)
defer os.Remove(tmpFile)
_, err := ParseEnvFile(tmpFile) _, err := ParseEnvFile(tmpFile)
if err == nil { if err == nil {
@ -124,8 +122,7 @@ func TestParseEnvFileLineTooLongFile(t *testing.T) {
func TestParseEnvFileRandomFile(t *testing.T) { func TestParseEnvFileRandomFile(t *testing.T) {
content := `first line content := `first line
another invalid line` another invalid line`
tmpFile := tmpFileWithContent(content, t) tmpFile := tmpFileWithContent(t, content)
defer os.Remove(tmpFile)
_, err := ParseEnvFile(tmpFile) _, err := ParseEnvFile(tmpFile)
if err == nil { if err == nil {
@ -146,8 +143,7 @@ func TestParseEnvVariableDefinitionsFile(t *testing.T) {
UNDEFINED_VAR UNDEFINED_VAR
HOME HOME
` `
tmpFile := tmpFileWithContent(content, t) tmpFile := tmpFileWithContent(t, content)
defer os.Remove(tmpFile)
variables, err := ParseEnvFile(tmpFile) variables, err := ParseEnvFile(tmpFile)
if nil != err { if nil != err {
@ -168,8 +164,7 @@ func TestParseEnvVariableWithNoNameFile(t *testing.T) {
content := `# comment= content := `# comment=
=blank variable names are an error case =blank variable names are an error case
` `
tmpFile := tmpFileWithContent(content, t) tmpFile := tmpFileWithContent(t, content)
defer os.Remove(tmpFile)
_, err := ParseEnvFile(tmpFile) _, err := ParseEnvFile(tmpFile)
if nil == err { if nil == err {