envfile_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package opts
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "reflect"
  8. "strings"
  9. "testing"
  10. )
  11. func tmpFileWithContent(content string, t *testing.T) string {
  12. tmpFile, err := ioutil.TempFile("", "envfile-test")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. defer tmpFile.Close()
  17. tmpFile.WriteString(content)
  18. return tmpFile.Name()
  19. }
  20. // Test ParseEnvFile for a file with a few well formatted lines
  21. func TestParseEnvFileGoodFile(t *testing.T) {
  22. content := `foo=bar
  23. baz=quux
  24. # comment
  25. _foobar=foobaz
  26. with.dots=working
  27. and_underscore=working too
  28. `
  29. // Adding a newline + a line with pure whitespace.
  30. // This is being done like this instead of the block above
  31. // because it's common for editors to trim trailing whitespace
  32. // from lines, which becomes annoying since that's the
  33. // exact thing we need to test.
  34. content += "\n \t "
  35. tmpFile := tmpFileWithContent(content, t)
  36. defer os.Remove(tmpFile)
  37. lines, err := ParseEnvFile(tmpFile)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. expectedLines := []string{
  42. "foo=bar",
  43. "baz=quux",
  44. "_foobar=foobaz",
  45. "with.dots=working",
  46. "and_underscore=working too",
  47. }
  48. if !reflect.DeepEqual(lines, expectedLines) {
  49. t.Fatal("lines not equal to expectedLines")
  50. }
  51. }
  52. // Test ParseEnvFile for an empty file
  53. func TestParseEnvFileEmptyFile(t *testing.T) {
  54. tmpFile := tmpFileWithContent("", t)
  55. defer os.Remove(tmpFile)
  56. lines, err := ParseEnvFile(tmpFile)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. if len(lines) != 0 {
  61. t.Fatal("lines not empty; expected empty")
  62. }
  63. }
  64. // Test ParseEnvFile for a non existent file
  65. func TestParseEnvFileNonExistentFile(t *testing.T) {
  66. _, err := ParseEnvFile("foo_bar_baz")
  67. if err == nil {
  68. t.Fatal("ParseEnvFile succeeded; expected failure")
  69. }
  70. if _, ok := err.(*os.PathError); !ok {
  71. t.Fatalf("Expected a PathError, got [%v]", err)
  72. }
  73. }
  74. // Test ParseEnvFile for a badly formatted file
  75. func TestParseEnvFileBadlyFormattedFile(t *testing.T) {
  76. content := `foo=bar
  77. f =quux
  78. `
  79. tmpFile := tmpFileWithContent(content, t)
  80. defer os.Remove(tmpFile)
  81. _, err := ParseEnvFile(tmpFile)
  82. if err == nil {
  83. t.Fatalf("Expected an ErrBadEnvVariable, got nothing")
  84. }
  85. if _, ok := err.(ErrBadEnvVariable); !ok {
  86. t.Fatalf("Expected an ErrBadEnvVariable, got [%v]", err)
  87. }
  88. expectedMessage := "poorly formatted environment: variable 'f ' has white spaces"
  89. if err.Error() != expectedMessage {
  90. t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error())
  91. }
  92. }
  93. // Test ParseEnvFile for a file with a line exceeding bufio.MaxScanTokenSize
  94. func TestParseEnvFileLineTooLongFile(t *testing.T) {
  95. content := strings.Repeat("a", bufio.MaxScanTokenSize+42)
  96. content = fmt.Sprint("foo=", content)
  97. tmpFile := tmpFileWithContent(content, t)
  98. defer os.Remove(tmpFile)
  99. _, err := ParseEnvFile(tmpFile)
  100. if err == nil {
  101. t.Fatal("ParseEnvFile succeeded; expected failure")
  102. }
  103. }
  104. // ParseEnvFile with a random file, pass through
  105. func TestParseEnvFileRandomFile(t *testing.T) {
  106. content := `first line
  107. another invalid line`
  108. tmpFile := tmpFileWithContent(content, t)
  109. defer os.Remove(tmpFile)
  110. _, err := ParseEnvFile(tmpFile)
  111. if err == nil {
  112. t.Fatalf("Expected an ErrBadEnvVariable, got nothing")
  113. }
  114. if _, ok := err.(ErrBadEnvVariable); !ok {
  115. t.Fatalf("Expected an ErrBadEnvVariable, got [%v]", err)
  116. }
  117. expectedMessage := "poorly formatted environment: variable 'first line' has white spaces"
  118. if err.Error() != expectedMessage {
  119. t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error())
  120. }
  121. }