mirror of https://github.com/docker/cli.git
Merge pull request #5499 from austinvazquez/cherry-pick-b129660dd39f566c6cdc692467a99bb422001a98-to-27.x
[27.x backport] opts: cleanup ParseEnvFile tests
This commit is contained in:
commit
4c5eb4c301
|
@ -3,27 +3,20 @@ package opts
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
|
is "gotest.tools/v3/assert/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func tmpFileWithContent(t *testing.T, content string) string {
|
func tmpFileWithContent(t *testing.T, content string) string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
tmpFile, err := os.CreateTemp("", "envfile-test")
|
fileName := filepath.Join(t.TempDir(), "envfile")
|
||||||
if err != nil {
|
err := os.WriteFile(fileName, []byte(content), 0o644)
|
||||||
t.Fatal(err)
|
assert.NilError(t, err)
|
||||||
}
|
return fileName
|
||||||
defer tmpFile.Close()
|
|
||||||
|
|
||||||
_, err = tmpFile.WriteString(content)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
_ = os.Remove(tmpFile.Name())
|
|
||||||
})
|
|
||||||
return tmpFile.Name()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test ParseEnvFile for a file with a few well formatted lines
|
// Test ParseEnvFile for a file with a few well formatted lines
|
||||||
|
@ -45,9 +38,7 @@ and_underscore=working too
|
||||||
tmpFile := tmpFileWithContent(t, content)
|
tmpFile := tmpFileWithContent(t, content)
|
||||||
|
|
||||||
lines, err := ParseEnvFile(tmpFile)
|
lines, err := ParseEnvFile(tmpFile)
|
||||||
if err != nil {
|
assert.NilError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedLines := []string{
|
expectedLines := []string{
|
||||||
"foo=bar",
|
"foo=bar",
|
||||||
|
@ -57,9 +48,7 @@ and_underscore=working too
|
||||||
"and_underscore=working too",
|
"and_underscore=working too",
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(lines, expectedLines) {
|
assert.Check(t, is.DeepEqual(lines, expectedLines))
|
||||||
t.Fatal("lines not equal to expectedLines")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test ParseEnvFile for an empty file
|
// Test ParseEnvFile for an empty file
|
||||||
|
@ -67,24 +56,14 @@ func TestParseEnvFileEmptyFile(t *testing.T) {
|
||||||
tmpFile := tmpFileWithContent(t, "")
|
tmpFile := tmpFileWithContent(t, "")
|
||||||
|
|
||||||
lines, err := ParseEnvFile(tmpFile)
|
lines, err := ParseEnvFile(tmpFile)
|
||||||
if err != nil {
|
assert.NilError(t, err)
|
||||||
t.Fatal(err)
|
assert.Check(t, is.Len(lines, 0))
|
||||||
}
|
|
||||||
|
|
||||||
if len(lines) != 0 {
|
|
||||||
t.Fatal("lines not empty; expected empty")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test ParseEnvFile for a non existent file
|
// Test ParseEnvFile for a non existent file
|
||||||
func TestParseEnvFileNonExistentFile(t *testing.T) {
|
func TestParseEnvFileNonExistentFile(t *testing.T) {
|
||||||
_, err := ParseEnvFile("foo_bar_baz")
|
_, err := ParseEnvFile("no_such_file")
|
||||||
if err == nil {
|
assert.Check(t, is.ErrorType(err, os.IsNotExist))
|
||||||
t.Fatal("ParseEnvFile succeeded; expected failure")
|
|
||||||
}
|
|
||||||
if _, ok := err.(*os.PathError); !ok {
|
|
||||||
t.Fatalf("Expected a PathError, got [%v]", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test ParseEnvFile for a badly formatted file
|
// Test ParseEnvFile for a badly formatted file
|
||||||
|
@ -95,16 +74,8 @@ func TestParseEnvFileBadlyFormattedFile(t *testing.T) {
|
||||||
tmpFile := tmpFileWithContent(t, content)
|
tmpFile := tmpFileWithContent(t, content)
|
||||||
|
|
||||||
_, err := ParseEnvFile(tmpFile)
|
_, err := ParseEnvFile(tmpFile)
|
||||||
if err == nil {
|
const expectedMessage = "variable 'f ' contains whitespaces"
|
||||||
t.Fatalf("Expected an ErrBadKey, got nothing")
|
assert.Check(t, is.ErrorContains(err, expectedMessage))
|
||||||
}
|
|
||||||
if _, ok := err.(ErrBadKey); !ok {
|
|
||||||
t.Fatalf("Expected an ErrBadKey, got [%v]", err)
|
|
||||||
}
|
|
||||||
expectedMessage := "poorly formatted environment: variable 'f ' contains whitespaces"
|
|
||||||
if err.Error() != expectedMessage {
|
|
||||||
t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test ParseEnvFile for a file with a line exceeding bufio.MaxScanTokenSize
|
// Test ParseEnvFile for a file with a line exceeding bufio.MaxScanTokenSize
|
||||||
|
@ -113,9 +84,8 @@ func TestParseEnvFileLineTooLongFile(t *testing.T) {
|
||||||
tmpFile := tmpFileWithContent(t, content)
|
tmpFile := tmpFileWithContent(t, content)
|
||||||
|
|
||||||
_, err := ParseEnvFile(tmpFile)
|
_, err := ParseEnvFile(tmpFile)
|
||||||
if err == nil {
|
const expectedMessage = "bufio.Scanner: token too long"
|
||||||
t.Fatal("ParseEnvFile succeeded; expected failure")
|
assert.Check(t, is.ErrorContains(err, expectedMessage))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseEnvFile with a random file, pass through
|
// ParseEnvFile with a random file, pass through
|
||||||
|
@ -125,38 +95,24 @@ another invalid line`
|
||||||
tmpFile := tmpFileWithContent(t, content)
|
tmpFile := tmpFileWithContent(t, content)
|
||||||
|
|
||||||
_, err := ParseEnvFile(tmpFile)
|
_, err := ParseEnvFile(tmpFile)
|
||||||
if err == nil {
|
const expectedMessage = "variable 'first line' contains whitespaces"
|
||||||
t.Fatalf("Expected an ErrBadKey, got nothing")
|
assert.Check(t, is.ErrorContains(err, expectedMessage))
|
||||||
}
|
|
||||||
if _, ok := err.(ErrBadKey); !ok {
|
|
||||||
t.Fatalf("Expected an ErrBadKey, got [%v]", err)
|
|
||||||
}
|
|
||||||
expectedMessage := "poorly formatted environment: variable 'first line' contains whitespaces"
|
|
||||||
if err.Error() != expectedMessage {
|
|
||||||
t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseEnvFile with environment variable import definitions
|
// ParseEnvFile with environment variable import definitions
|
||||||
func TestParseEnvVariableDefinitionsFile(t *testing.T) {
|
func TestParseEnvVariableDefinitionsFile(t *testing.T) {
|
||||||
content := `# comment=
|
content := `# comment=
|
||||||
UNDEFINED_VAR
|
UNDEFINED_VAR
|
||||||
HOME
|
DEFINED_VAR
|
||||||
`
|
`
|
||||||
tmpFile := tmpFileWithContent(t, content)
|
tmpFile := tmpFileWithContent(t, content)
|
||||||
|
|
||||||
|
t.Setenv("DEFINED_VAR", "defined-value")
|
||||||
variables, err := ParseEnvFile(tmpFile)
|
variables, err := ParseEnvFile(tmpFile)
|
||||||
if nil != err {
|
assert.NilError(t, err)
|
||||||
t.Fatal("There must not be any error")
|
|
||||||
}
|
|
||||||
|
|
||||||
if "HOME="+os.Getenv("HOME") != variables[0] {
|
expectedLines := []string{"DEFINED_VAR=defined-value"}
|
||||||
t.Fatal("the HOME variable is not properly imported as the first variable (but it is the only one to import)")
|
assert.Check(t, is.DeepEqual(variables, expectedLines))
|
||||||
}
|
|
||||||
|
|
||||||
if len(variables) != 1 {
|
|
||||||
t.Fatal("exactly one variable is imported (as the other one is not set at all)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseEnvFile with empty variable name
|
// ParseEnvFile with empty variable name
|
||||||
|
@ -167,7 +123,6 @@ func TestParseEnvVariableWithNoNameFile(t *testing.T) {
|
||||||
tmpFile := tmpFileWithContent(t, content)
|
tmpFile := tmpFileWithContent(t, content)
|
||||||
|
|
||||||
_, err := ParseEnvFile(tmpFile)
|
_, err := ParseEnvFile(tmpFile)
|
||||||
if nil == err {
|
const expectedMessage = "no variable name on line '=blank variable names are an error case'"
|
||||||
t.Fatal("if a variable has no name parsing an environment file must fail")
|
assert.Check(t, is.ErrorContains(err, expectedMessage))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue