2015-07-01 11:02:40 -04:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
func tmpFileWithContent(content string, t *testing.T) string {
|
2015-07-01 11:02:40 -04:00
|
|
|
tmpFile, err := ioutil.TempFile("", "envfile-test")
|
|
|
|
if err != nil {
|
2015-07-12 04:33:30 -04:00
|
|
|
t.Fatal(err)
|
2015-07-01 11:02:40 -04:00
|
|
|
}
|
|
|
|
defer tmpFile.Close()
|
|
|
|
|
|
|
|
tmpFile.WriteString(content)
|
2015-07-12 04:33:30 -04:00
|
|
|
return tmpFile.Name()
|
2015-07-01 11:02:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test ParseEnvFile for a file with a few well formatted lines
|
|
|
|
func TestParseEnvFileGoodFile(t *testing.T) {
|
|
|
|
content := `foo=bar
|
|
|
|
baz=quux
|
|
|
|
# comment
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
_foobar=foobaz
|
2015-09-28 14:26:20 -04:00
|
|
|
with.dots=working
|
|
|
|
and_underscore=working too
|
2015-07-01 11:02:40 -04:00
|
|
|
`
|
2015-08-21 16:28:01 -04:00
|
|
|
// Adding a newline + a line with pure whitespace.
|
|
|
|
// This is being done like this instead of the block above
|
|
|
|
// because it's common for editors to trim trailing whitespace
|
|
|
|
// from lines, which becomes annoying since that's the
|
|
|
|
// exact thing we need to test.
|
|
|
|
content += "\n \t "
|
2015-07-12 04:33:30 -04:00
|
|
|
tmpFile := tmpFileWithContent(content, t)
|
2015-07-01 11:02:40 -04:00
|
|
|
defer os.Remove(tmpFile)
|
|
|
|
|
|
|
|
lines, err := ParseEnvFile(tmpFile)
|
|
|
|
if err != nil {
|
2015-07-12 04:33:30 -04:00
|
|
|
t.Fatal(err)
|
2015-07-01 11:02:40 -04:00
|
|
|
}
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
expectedLines := []string{
|
2015-07-01 11:02:40 -04:00
|
|
|
"foo=bar",
|
|
|
|
"baz=quux",
|
2015-07-12 04:33:30 -04:00
|
|
|
"_foobar=foobaz",
|
2015-09-28 14:26:20 -04:00
|
|
|
"with.dots=working",
|
|
|
|
"and_underscore=working too",
|
2015-07-01 11:02:40 -04:00
|
|
|
}
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
if !reflect.DeepEqual(lines, expectedLines) {
|
2017-01-08 22:14:18 -05:00
|
|
|
t.Fatal("lines not equal to expectedLines")
|
2015-07-01 11:02:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test ParseEnvFile for an empty file
|
|
|
|
func TestParseEnvFileEmptyFile(t *testing.T) {
|
2015-07-12 04:33:30 -04:00
|
|
|
tmpFile := tmpFileWithContent("", t)
|
2015-07-01 11:02:40 -04:00
|
|
|
defer os.Remove(tmpFile)
|
|
|
|
|
|
|
|
lines, err := ParseEnvFile(tmpFile)
|
|
|
|
if err != nil {
|
2015-07-12 04:33:30 -04:00
|
|
|
t.Fatal(err)
|
2015-07-01 11:02:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(lines) != 0 {
|
|
|
|
t.Fatal("lines not empty; expected empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test ParseEnvFile for a non existent file
|
|
|
|
func TestParseEnvFileNonExistentFile(t *testing.T) {
|
|
|
|
_, err := ParseEnvFile("foo_bar_baz")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("ParseEnvFile succeeded; expected failure")
|
|
|
|
}
|
2015-07-12 04:33:30 -04:00
|
|
|
if _, ok := err.(*os.PathError); !ok {
|
|
|
|
t.Fatalf("Expected a PathError, got [%v]", err)
|
|
|
|
}
|
2015-07-01 11:02:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test ParseEnvFile for a badly formatted file
|
|
|
|
func TestParseEnvFileBadlyFormattedFile(t *testing.T) {
|
|
|
|
content := `foo=bar
|
|
|
|
f =quux
|
|
|
|
`
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
tmpFile := tmpFileWithContent(content, t)
|
2015-07-01 11:02:40 -04:00
|
|
|
defer os.Remove(tmpFile)
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
_, err := ParseEnvFile(tmpFile)
|
2015-07-01 11:02:40 -04:00
|
|
|
if err == nil {
|
2016-05-07 21:36:10 -04:00
|
|
|
t.Fatalf("Expected an ErrBadEnvVariable, got nothing")
|
2015-07-12 04:33:30 -04:00
|
|
|
}
|
|
|
|
if _, ok := err.(ErrBadEnvVariable); !ok {
|
2016-05-07 21:36:10 -04:00
|
|
|
t.Fatalf("Expected an ErrBadEnvVariable, got [%v]", err)
|
2015-07-12 04:33:30 -04:00
|
|
|
}
|
2015-09-28 14:26:20 -04:00
|
|
|
expectedMessage := "poorly formatted environment: variable 'f ' has white spaces"
|
2015-07-12 04:33:30 -04:00
|
|
|
if err.Error() != expectedMessage {
|
|
|
|
t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error())
|
2015-07-01 11:02:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// Test ParseEnvFile for a file with a line exceeding bufio.MaxScanTokenSize
|
2015-07-01 11:02:40 -04:00
|
|
|
func TestParseEnvFileLineTooLongFile(t *testing.T) {
|
|
|
|
content := strings.Repeat("a", bufio.MaxScanTokenSize+42)
|
|
|
|
content = fmt.Sprint("foo=", content)
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
tmpFile := tmpFileWithContent(content, t)
|
2015-07-01 11:02:40 -04:00
|
|
|
defer os.Remove(tmpFile)
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
_, err := ParseEnvFile(tmpFile)
|
2015-07-01 11:02:40 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("ParseEnvFile succeeded; expected failure")
|
|
|
|
}
|
|
|
|
}
|
2015-07-12 04:33:30 -04:00
|
|
|
|
|
|
|
// ParseEnvFile with a random file, pass through
|
|
|
|
func TestParseEnvFileRandomFile(t *testing.T) {
|
|
|
|
content := `first line
|
|
|
|
another invalid line`
|
|
|
|
tmpFile := tmpFileWithContent(content, t)
|
|
|
|
defer os.Remove(tmpFile)
|
|
|
|
|
|
|
|
_, err := ParseEnvFile(tmpFile)
|
|
|
|
if err == nil {
|
2016-05-07 21:36:10 -04:00
|
|
|
t.Fatalf("Expected an ErrBadEnvVariable, got nothing")
|
2015-07-12 04:33:30 -04:00
|
|
|
}
|
|
|
|
if _, ok := err.(ErrBadEnvVariable); !ok {
|
2017-01-08 22:14:18 -05:00
|
|
|
t.Fatalf("Expected an ErrBadEnvVariable, got [%v]", err)
|
2015-07-12 04:33:30 -04:00
|
|
|
}
|
2015-09-28 14:26:20 -04:00
|
|
|
expectedMessage := "poorly formatted environment: variable 'first line' has white spaces"
|
2015-07-12 04:33:30 -04:00
|
|
|
if err.Error() != expectedMessage {
|
|
|
|
t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error())
|
|
|
|
}
|
|
|
|
}
|