You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

311 lines
7.9 KiB

  1. // Disabling building of toml support in cases where golang is 1.0 or 1.1
  2. // as the encoding library is not implemented or supported.
  3. // +build go1.2
  4. package altsrc
  5. import (
  6. "flag"
  7. "io/ioutil"
  8. "os"
  9. "testing"
  10. "gopkg.in/urfave/cli.v1"
  11. )
  12. func TestCommandTomFileTest(t *testing.T) {
  13. app := cli.NewApp()
  14. set := flag.NewFlagSet("test", 0)
  15. ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
  16. defer os.Remove("current.toml")
  17. test := []string{"test-cmd", "--load", "current.toml"}
  18. set.Parse(test)
  19. c := cli.NewContext(app, set, nil)
  20. command := &cli.Command{
  21. Name: "test-cmd",
  22. Aliases: []string{"tc"},
  23. Usage: "this is for testing",
  24. Description: "testing",
  25. Action: func(c *cli.Context) error {
  26. val := c.Int("test")
  27. expect(t, val, 15)
  28. return nil
  29. },
  30. Flags: []cli.Flag{
  31. NewIntFlag(cli.IntFlag{Name: "test"}),
  32. cli.StringFlag{Name: "load"}},
  33. }
  34. command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
  35. err := command.Run(c)
  36. expect(t, err, nil)
  37. }
  38. func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) {
  39. app := cli.NewApp()
  40. set := flag.NewFlagSet("test", 0)
  41. ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
  42. defer os.Remove("current.toml")
  43. os.Setenv("THE_TEST", "10")
  44. defer os.Setenv("THE_TEST", "")
  45. test := []string{"test-cmd", "--load", "current.toml"}
  46. set.Parse(test)
  47. c := cli.NewContext(app, set, nil)
  48. command := &cli.Command{
  49. Name: "test-cmd",
  50. Aliases: []string{"tc"},
  51. Usage: "this is for testing",
  52. Description: "testing",
  53. Action: func(c *cli.Context) error {
  54. val := c.Int("test")
  55. expect(t, val, 10)
  56. return nil
  57. },
  58. Flags: []cli.Flag{
  59. NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}),
  60. cli.StringFlag{Name: "load"}},
  61. }
  62. command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
  63. err := command.Run(c)
  64. expect(t, err, nil)
  65. }
  66. func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) {
  67. app := cli.NewApp()
  68. set := flag.NewFlagSet("test", 0)
  69. ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
  70. defer os.Remove("current.toml")
  71. os.Setenv("THE_TEST", "10")
  72. defer os.Setenv("THE_TEST", "")
  73. test := []string{"test-cmd", "--load", "current.toml"}
  74. set.Parse(test)
  75. c := cli.NewContext(app, set, nil)
  76. command := &cli.Command{
  77. Name: "test-cmd",
  78. Aliases: []string{"tc"},
  79. Usage: "this is for testing",
  80. Description: "testing",
  81. Action: func(c *cli.Context) error {
  82. val := c.Int("top.test")
  83. expect(t, val, 10)
  84. return nil
  85. },
  86. Flags: []cli.Flag{
  87. NewIntFlag(cli.IntFlag{Name: "top.test", EnvVar: "THE_TEST"}),
  88. cli.StringFlag{Name: "load"}},
  89. }
  90. command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
  91. err := command.Run(c)
  92. expect(t, err, nil)
  93. }
  94. func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) {
  95. app := cli.NewApp()
  96. set := flag.NewFlagSet("test", 0)
  97. ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
  98. defer os.Remove("current.toml")
  99. test := []string{"test-cmd", "--load", "current.toml", "--test", "7"}
  100. set.Parse(test)
  101. c := cli.NewContext(app, set, nil)
  102. command := &cli.Command{
  103. Name: "test-cmd",
  104. Aliases: []string{"tc"},
  105. Usage: "this is for testing",
  106. Description: "testing",
  107. Action: func(c *cli.Context) error {
  108. val := c.Int("test")
  109. expect(t, val, 7)
  110. return nil
  111. },
  112. Flags: []cli.Flag{
  113. NewIntFlag(cli.IntFlag{Name: "test"}),
  114. cli.StringFlag{Name: "load"}},
  115. }
  116. command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
  117. err := command.Run(c)
  118. expect(t, err, nil)
  119. }
  120. func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) {
  121. app := cli.NewApp()
  122. set := flag.NewFlagSet("test", 0)
  123. ioutil.WriteFile("current.toml", []byte(`[top]
  124. test = 15`), 0666)
  125. defer os.Remove("current.toml")
  126. test := []string{"test-cmd", "--load", "current.toml", "--top.test", "7"}
  127. set.Parse(test)
  128. c := cli.NewContext(app, set, nil)
  129. command := &cli.Command{
  130. Name: "test-cmd",
  131. Aliases: []string{"tc"},
  132. Usage: "this is for testing",
  133. Description: "testing",
  134. Action: func(c *cli.Context) error {
  135. val := c.Int("top.test")
  136. expect(t, val, 7)
  137. return nil
  138. },
  139. Flags: []cli.Flag{
  140. NewIntFlag(cli.IntFlag{Name: "top.test"}),
  141. cli.StringFlag{Name: "load"}},
  142. }
  143. command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
  144. err := command.Run(c)
  145. expect(t, err, nil)
  146. }
  147. func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) {
  148. app := cli.NewApp()
  149. set := flag.NewFlagSet("test", 0)
  150. ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
  151. defer os.Remove("current.toml")
  152. test := []string{"test-cmd", "--load", "current.toml"}
  153. set.Parse(test)
  154. c := cli.NewContext(app, set, nil)
  155. command := &cli.Command{
  156. Name: "test-cmd",
  157. Aliases: []string{"tc"},
  158. Usage: "this is for testing",
  159. Description: "testing",
  160. Action: func(c *cli.Context) error {
  161. val := c.Int("test")
  162. expect(t, val, 15)
  163. return nil
  164. },
  165. Flags: []cli.Flag{
  166. NewIntFlag(cli.IntFlag{Name: "test", Value: 7}),
  167. cli.StringFlag{Name: "load"}},
  168. }
  169. command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
  170. err := command.Run(c)
  171. expect(t, err, nil)
  172. }
  173. func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) {
  174. app := cli.NewApp()
  175. set := flag.NewFlagSet("test", 0)
  176. ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
  177. defer os.Remove("current.toml")
  178. test := []string{"test-cmd", "--load", "current.toml"}
  179. set.Parse(test)
  180. c := cli.NewContext(app, set, nil)
  181. command := &cli.Command{
  182. Name: "test-cmd",
  183. Aliases: []string{"tc"},
  184. Usage: "this is for testing",
  185. Description: "testing",
  186. Action: func(c *cli.Context) error {
  187. val := c.Int("top.test")
  188. expect(t, val, 15)
  189. return nil
  190. },
  191. Flags: []cli.Flag{
  192. NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7}),
  193. cli.StringFlag{Name: "load"}},
  194. }
  195. command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
  196. err := command.Run(c)
  197. expect(t, err, nil)
  198. }
  199. func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T) {
  200. app := cli.NewApp()
  201. set := flag.NewFlagSet("test", 0)
  202. ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
  203. defer os.Remove("current.toml")
  204. os.Setenv("THE_TEST", "11")
  205. defer os.Setenv("THE_TEST", "")
  206. test := []string{"test-cmd", "--load", "current.toml"}
  207. set.Parse(test)
  208. c := cli.NewContext(app, set, nil)
  209. command := &cli.Command{
  210. Name: "test-cmd",
  211. Aliases: []string{"tc"},
  212. Usage: "this is for testing",
  213. Description: "testing",
  214. Action: func(c *cli.Context) error {
  215. val := c.Int("test")
  216. expect(t, val, 11)
  217. return nil
  218. },
  219. Flags: []cli.Flag{
  220. NewIntFlag(cli.IntFlag{Name: "test", Value: 7, EnvVar: "THE_TEST"}),
  221. cli.StringFlag{Name: "load"}},
  222. }
  223. command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
  224. err := command.Run(c)
  225. expect(t, err, nil)
  226. }
  227. func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWinsNested(t *testing.T) {
  228. app := cli.NewApp()
  229. set := flag.NewFlagSet("test", 0)
  230. ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
  231. defer os.Remove("current.toml")
  232. os.Setenv("THE_TEST", "11")
  233. defer os.Setenv("THE_TEST", "")
  234. test := []string{"test-cmd", "--load", "current.toml"}
  235. set.Parse(test)
  236. c := cli.NewContext(app, set, nil)
  237. command := &cli.Command{
  238. Name: "test-cmd",
  239. Aliases: []string{"tc"},
  240. Usage: "this is for testing",
  241. Description: "testing",
  242. Action: func(c *cli.Context) error {
  243. val := c.Int("top.test")
  244. expect(t, val, 11)
  245. return nil
  246. },
  247. Flags: []cli.Flag{
  248. NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7, EnvVar: "THE_TEST"}),
  249. cli.StringFlag{Name: "load"}},
  250. }
  251. command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
  252. err := command.Run(c)
  253. expect(t, err, nil)
  254. }