Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

200 Zeilen
5.3KB

  1. package env
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "testing"
  7. "git.thevis.us/skepto/crypto"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestFindKeyValue(t *testing.T) {
  11. Convey("env.findKeyValue", t, func() {
  12. k, v := findKeyValue(" test= value")
  13. So(k, ShouldEqual, "test")
  14. So(v, ShouldEqual, "value")
  15. k, v = findKeyValue(" test= value")
  16. So(k, ShouldEqual, "test")
  17. So(v, ShouldEqual, "value")
  18. k, v = findKeyValue("\ttest=\tvalue\t\n")
  19. So(k, ShouldEqual, "test")
  20. So(v, ShouldEqual, "value")
  21. k, v = findKeyValue("export Test=\"Example\"")
  22. So(k, ShouldEqual, "Test")
  23. So(v, ShouldEqual, "Example")
  24. k, v = findKeyValue(`export Secret_Keys=IK-vyX7OuiftwyasT6NXnEYyPMj8fEDssJZdppKOs8Y4hZTtWfUILer73RbsG78Q`)
  25. So(k, ShouldEqual, "Secret_Keys")
  26. So(v, ShouldEqual, "IK-vyX7OuiftwyasT6NXnEYyPMj8fEDssJZdppKOs8Y4hZTtWfUILer73RbsG78Q")
  27. })
  28. }
  29. func TestLoad(t *testing.T) {
  30. filename := "/tmp/.env"
  31. // plain value without quote
  32. if dotenv, err := os.Create(filename); err == nil {
  33. defer dotenv.Close()
  34. defer os.Remove(filename)
  35. secret := crypto.Random(64)
  36. buffer := bufio.NewWriter(dotenv)
  37. buffer.WriteString(fmt.Sprintf("secret=%s\n", secret))
  38. buffer.WriteString("app=myapp\n")
  39. buffer.WriteString("export exportation=myexports")
  40. buffer.Flush()
  41. Convey("env.Load (without quoting)", t, func() {
  42. Set("root", "/tmp")
  43. Load("/tmp/.env")
  44. So(String("secret"), ShouldEqual, secret)
  45. So(String("app"), ShouldEqual, "myapp")
  46. So(String("exportation"), ShouldEqual, "myexports")
  47. })
  48. }
  49. // value with `` quote
  50. if dotenv, err := os.Create(filename); err == nil {
  51. defer dotenv.Close()
  52. defer os.Remove(filename)
  53. secret := crypto.Random(64)
  54. buffer := bufio.NewWriter(dotenv)
  55. buffer.WriteString(fmt.Sprintf("secret='%s'\n", secret))
  56. buffer.WriteString("app='myapp'\n")
  57. buffer.WriteString("export account='username'\n")
  58. buffer.Flush()
  59. Convey("env.Load (with quoting)", t, func() {
  60. Set("root", "/tmp")
  61. Load("/tmp/.env")
  62. So(String("secret"), ShouldEqual, secret)
  63. So(String("app"), ShouldEqual, "myapp")
  64. So(String("account"), ShouldEqual, "username")
  65. })
  66. }
  67. // value with `"` quote
  68. if dotenv, err := os.Create(filename); err == nil {
  69. defer dotenv.Close()
  70. defer os.Remove(filename)
  71. secret := crypto.Random(64)
  72. buffer := bufio.NewWriter(dotenv)
  73. buffer.WriteString(fmt.Sprintf("secret=\"%s\"\n", secret))
  74. buffer.WriteString("app=\"myapp\"\n")
  75. buffer.WriteString("export account=\"username\"\n")
  76. buffer.Flush()
  77. Convey("env.Load (with single quoting)", t, func() {
  78. Set("root", "/tmp")
  79. Load("/tmp/.env")
  80. So(String("secret"), ShouldEqual, secret)
  81. So(String("app"), ShouldEqual, "myapp")
  82. So(String("account"), ShouldEqual, "username")
  83. })
  84. }
  85. }
  86. func TestMap(t *testing.T) {
  87. type Person struct {
  88. Username string
  89. Age uint
  90. Kids int
  91. Checked bool
  92. Money float64
  93. FirstName string `env:"FIRST_NAME"`
  94. Names []string
  95. }
  96. var person Person
  97. Convey("env.Map", t, func() {
  98. Set("Username", "abc")
  99. Set("Age", 100)
  100. Set("Kids", 2)
  101. Set("Checked", true)
  102. Set("Money", 1234567890.0987654321)
  103. Set("FIRST_NAME", "abc")
  104. Set("Names", "a,b,c,d,e,f,g")
  105. Map(&person)
  106. So(person.Username, ShouldEqual, "abc")
  107. So(person.Age, ShouldEqual, 100)
  108. So(person.Kids, ShouldEqual, 2)
  109. So(person.Checked, ShouldBeTrue)
  110. So(person.Money, ShouldEqual, 1234567890.0987654321)
  111. So(person.FirstName, ShouldEqual, "abc")
  112. So(person.Names, ShouldResemble, []string{"a", "b", "c", "d", "e", "f", "g"})
  113. })
  114. }
  115. func TestString(t *testing.T) {
  116. Convey("env.String", t, func() {
  117. So(String("NotFound"), ShouldEqual, "")
  118. Set("Found", "something")
  119. So(String("Found"), ShouldEqual, "something")
  120. So(String("NotFound", "default"), ShouldEqual, "default")
  121. })
  122. }
  123. func TestStrings(t *testing.T) {
  124. Convey("env.Strings", t, func() {
  125. So(Strings("StringList"), ShouldBeNil)
  126. Set("StringList", "a,b,c")
  127. So(Strings("StringList"), ShouldResemble, []string{"a", "b", "c"})
  128. So(Strings("NotFound", []string{"a", "b", "c"}), ShouldResemble, []string{"a", "b", "c"})
  129. })
  130. }
  131. func TestInt(t *testing.T) {
  132. Convey("env.Int", t, func() {
  133. So(Int("integer"), ShouldEqual, 0)
  134. Set("integer", 123)
  135. So(Int("integer"), ShouldEqual, 123)
  136. So(Int("NotFound", 123), ShouldEqual, 123)
  137. })
  138. }
  139. func TestInt64(t *testing.T) {
  140. Convey("env.Int", t, func() {
  141. So(Int64("int64"), ShouldEqual, 0)
  142. Set("int64", 123)
  143. So(Int64("int64"), ShouldEqual, 123)
  144. So(Int64("NotFound", 123), ShouldEqual, 123)
  145. })
  146. }
  147. func TestUint(t *testing.T) {
  148. Convey("env.Uint", t, func() {
  149. So(Uint("uint"), ShouldEqual, 0)
  150. Set("uint", 123)
  151. So(Uint("uint"), ShouldEqual, 123)
  152. So(Uint("NotFound", 123), ShouldEqual, 123)
  153. })
  154. }
  155. func TestUint64(t *testing.T) {
  156. Convey("env.Uint64", t, func() {
  157. So(Uint64("uint64"), ShouldEqual, 0)
  158. Set("uint64", 123)
  159. So(Uint64("uint64"), ShouldEqual, 123)
  160. So(Uint64("NotFound", 123), ShouldEqual, 123)
  161. })
  162. }
  163. func TestBool(t *testing.T) {
  164. Convey("env.Bool", t, func() {
  165. So(Bool("bool"), ShouldBeFalse)
  166. Set("bool", true)
  167. So(Bool("bool"), ShouldBeTrue)
  168. So(Bool("NotFound", true), ShouldBeTrue)
  169. })
  170. }
  171. func TestFloat(t *testing.T) {
  172. Convey("env.Float", t, func() {
  173. So(Float("float64"), ShouldEqual, 0.0)
  174. Set("float64", 12345678990.0987654321)
  175. So(Float("float64"), ShouldEqual, 12345678990.0987654321)
  176. So(Float("NotFound", 12345678990.0987654321), ShouldEqual, 12345678990.0987654321)
  177. })
  178. }