Skip to content

Commit 7c85db6

Browse files
authored
Merge pull request #5494 from thaJeztah/opts_test_cleanup
opts: cleanup ParseEnvFile tests
2 parents 30e9abb + b129660 commit 7c85db6

1 file changed

Lines changed: 27 additions & 72 deletions

File tree

opts/envfile_test.go

Lines changed: 27 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,20 @@ package opts
33
import (
44
"bufio"
55
"os"
6-
"reflect"
6+
"path/filepath"
77
"strings"
88
"testing"
9+
10+
"gotest.tools/v3/assert"
11+
is "gotest.tools/v3/assert/cmp"
912
)
1013

1114
func tmpFileWithContent(t *testing.T, content string) string {
1215
t.Helper()
13-
tmpFile, err := os.CreateTemp("", "envfile-test")
14-
if err != nil {
15-
t.Fatal(err)
16-
}
17-
defer tmpFile.Close()
18-
19-
_, err = tmpFile.WriteString(content)
20-
if err != nil {
21-
t.Fatal(err)
22-
}
23-
t.Cleanup(func() {
24-
_ = os.Remove(tmpFile.Name())
25-
})
26-
return tmpFile.Name()
16+
fileName := filepath.Join(t.TempDir(), "envfile")
17+
err := os.WriteFile(fileName, []byte(content), 0o644)
18+
assert.NilError(t, err)
19+
return fileName
2720
}
2821

2922
// Test ParseEnvFile for a file with a few well formatted lines
@@ -45,9 +38,7 @@ and_underscore=working too
4538
tmpFile := tmpFileWithContent(t, content)
4639

4740
lines, err := ParseEnvFile(tmpFile)
48-
if err != nil {
49-
t.Fatal(err)
50-
}
41+
assert.NilError(t, err)
5142

5243
expectedLines := []string{
5344
"foo=bar",
@@ -57,34 +48,22 @@ and_underscore=working too
5748
"and_underscore=working too",
5849
}
5950

60-
if !reflect.DeepEqual(lines, expectedLines) {
61-
t.Fatal("lines not equal to expectedLines")
62-
}
51+
assert.Check(t, is.DeepEqual(lines, expectedLines))
6352
}
6453

6554
// Test ParseEnvFile for an empty file
6655
func TestParseEnvFileEmptyFile(t *testing.T) {
6756
tmpFile := tmpFileWithContent(t, "")
6857

6958
lines, err := ParseEnvFile(tmpFile)
70-
if err != nil {
71-
t.Fatal(err)
72-
}
73-
74-
if len(lines) != 0 {
75-
t.Fatal("lines not empty; expected empty")
76-
}
59+
assert.NilError(t, err)
60+
assert.Check(t, is.Len(lines, 0))
7761
}
7862

7963
// Test ParseEnvFile for a non existent file
8064
func TestParseEnvFileNonExistentFile(t *testing.T) {
81-
_, err := ParseEnvFile("foo_bar_baz")
82-
if err == nil {
83-
t.Fatal("ParseEnvFile succeeded; expected failure")
84-
}
85-
if _, ok := err.(*os.PathError); !ok {
86-
t.Fatalf("Expected a PathError, got [%v]", err)
87-
}
65+
_, err := ParseEnvFile("no_such_file")
66+
assert.Check(t, is.ErrorType(err, os.IsNotExist))
8867
}
8968

9069
// Test ParseEnvFile for a badly formatted file
@@ -95,16 +74,8 @@ func TestParseEnvFileBadlyFormattedFile(t *testing.T) {
9574
tmpFile := tmpFileWithContent(t, content)
9675

9776
_, err := ParseEnvFile(tmpFile)
98-
if err == nil {
99-
t.Fatalf("Expected an ErrBadKey, got nothing")
100-
}
101-
if _, ok := err.(ErrBadKey); !ok {
102-
t.Fatalf("Expected an ErrBadKey, got [%v]", err)
103-
}
104-
expectedMessage := "poorly formatted environment: variable 'f ' contains whitespaces"
105-
if err.Error() != expectedMessage {
106-
t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error())
107-
}
77+
const expectedMessage = "variable 'f ' contains whitespaces"
78+
assert.Check(t, is.ErrorContains(err, expectedMessage))
10879
}
10980

11081
// Test ParseEnvFile for a file with a line exceeding bufio.MaxScanTokenSize
@@ -113,9 +84,8 @@ func TestParseEnvFileLineTooLongFile(t *testing.T) {
11384
tmpFile := tmpFileWithContent(t, content)
11485

11586
_, err := ParseEnvFile(tmpFile)
116-
if err == nil {
117-
t.Fatal("ParseEnvFile succeeded; expected failure")
118-
}
87+
const expectedMessage = "bufio.Scanner: token too long"
88+
assert.Check(t, is.ErrorContains(err, expectedMessage))
11989
}
12090

12191
// ParseEnvFile with a random file, pass through
@@ -125,38 +95,24 @@ another invalid line`
12595
tmpFile := tmpFileWithContent(t, content)
12696

12797
_, err := ParseEnvFile(tmpFile)
128-
if err == nil {
129-
t.Fatalf("Expected an ErrBadKey, got nothing")
130-
}
131-
if _, ok := err.(ErrBadKey); !ok {
132-
t.Fatalf("Expected an ErrBadKey, got [%v]", err)
133-
}
134-
expectedMessage := "poorly formatted environment: variable 'first line' contains whitespaces"
135-
if err.Error() != expectedMessage {
136-
t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error())
137-
}
98+
const expectedMessage = "variable 'first line' contains whitespaces"
99+
assert.Check(t, is.ErrorContains(err, expectedMessage))
138100
}
139101

140102
// ParseEnvFile with environment variable import definitions
141103
func TestParseEnvVariableDefinitionsFile(t *testing.T) {
142104
content := `# comment=
143105
UNDEFINED_VAR
144-
HOME
106+
DEFINED_VAR
145107
`
146108
tmpFile := tmpFileWithContent(t, content)
147109

110+
t.Setenv("DEFINED_VAR", "defined-value")
148111
variables, err := ParseEnvFile(tmpFile)
149-
if nil != err {
150-
t.Fatal("There must not be any error")
151-
}
152-
153-
if "HOME="+os.Getenv("HOME") != variables[0] {
154-
t.Fatal("the HOME variable is not properly imported as the first variable (but it is the only one to import)")
155-
}
112+
assert.NilError(t, err)
156113

157-
if len(variables) != 1 {
158-
t.Fatal("exactly one variable is imported (as the other one is not set at all)")
159-
}
114+
expectedLines := []string{"DEFINED_VAR=defined-value"}
115+
assert.Check(t, is.DeepEqual(variables, expectedLines))
160116
}
161117

162118
// ParseEnvFile with empty variable name
@@ -167,7 +123,6 @@ func TestParseEnvVariableWithNoNameFile(t *testing.T) {
167123
tmpFile := tmpFileWithContent(t, content)
168124

169125
_, err := ParseEnvFile(tmpFile)
170-
if nil == err {
171-
t.Fatal("if a variable has no name parsing an environment file must fail")
172-
}
126+
const expectedMessage = "no variable name on line '=blank variable names are an error case'"
127+
assert.Check(t, is.ErrorContains(err, expectedMessage))
173128
}

0 commit comments

Comments
 (0)