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.
 
 
 

314 lines
7.9 KiB

  1. // Disabling building of yaml 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 TestCommandYamlFileTest(t *testing.T) {
  13. app := cli.NewApp()
  14. set := flag.NewFlagSet("test", 0)
  15. ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
  16. defer os.Remove("current.yaml")
  17. test := []string{"test-cmd", "--load", "current.yaml"}
  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, NewYamlSourceFromFlagFunc("load"))
  35. err := command.Run(c)
  36. expect(t, err, nil)
  37. }
  38. func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
  39. app := cli.NewApp()
  40. set := flag.NewFlagSet("test", 0)
  41. ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
  42. defer os.Remove("current.yaml")
  43. os.Setenv("THE_TEST", "10")
  44. defer os.Setenv("THE_TEST", "")
  45. test := []string{"test-cmd", "--load", "current.yaml"}
  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, NewYamlSourceFromFlagFunc("load"))
  63. err := command.Run(c)
  64. expect(t, err, nil)
  65. }
  66. func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
  67. app := cli.NewApp()
  68. set := flag.NewFlagSet("test", 0)
  69. ioutil.WriteFile("current.yaml", []byte(`top:
  70. test: 15`), 0666)
  71. defer os.Remove("current.yaml")
  72. os.Setenv("THE_TEST", "10")
  73. defer os.Setenv("THE_TEST", "")
  74. test := []string{"test-cmd", "--load", "current.yaml"}
  75. set.Parse(test)
  76. c := cli.NewContext(app, set, nil)
  77. command := &cli.Command{
  78. Name: "test-cmd",
  79. Aliases: []string{"tc"},
  80. Usage: "this is for testing",
  81. Description: "testing",
  82. Action: func(c *cli.Context) error {
  83. val := c.Int("top.test")
  84. expect(t, val, 10)
  85. return nil
  86. },
  87. Flags: []cli.Flag{
  88. NewIntFlag(cli.IntFlag{Name: "top.test", EnvVar: "THE_TEST"}),
  89. cli.StringFlag{Name: "load"}},
  90. }
  91. command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
  92. err := command.Run(c)
  93. expect(t, err, nil)
  94. }
  95. func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
  96. app := cli.NewApp()
  97. set := flag.NewFlagSet("test", 0)
  98. ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
  99. defer os.Remove("current.yaml")
  100. test := []string{"test-cmd", "--load", "current.yaml", "--test", "7"}
  101. set.Parse(test)
  102. c := cli.NewContext(app, set, nil)
  103. command := &cli.Command{
  104. Name: "test-cmd",
  105. Aliases: []string{"tc"},
  106. Usage: "this is for testing",
  107. Description: "testing",
  108. Action: func(c *cli.Context) error {
  109. val := c.Int("test")
  110. expect(t, val, 7)
  111. return nil
  112. },
  113. Flags: []cli.Flag{
  114. NewIntFlag(cli.IntFlag{Name: "test"}),
  115. cli.StringFlag{Name: "load"}},
  116. }
  117. command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
  118. err := command.Run(c)
  119. expect(t, err, nil)
  120. }
  121. func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
  122. app := cli.NewApp()
  123. set := flag.NewFlagSet("test", 0)
  124. ioutil.WriteFile("current.yaml", []byte(`top:
  125. test: 15`), 0666)
  126. defer os.Remove("current.yaml")
  127. test := []string{"test-cmd", "--load", "current.yaml", "--top.test", "7"}
  128. set.Parse(test)
  129. c := cli.NewContext(app, set, nil)
  130. command := &cli.Command{
  131. Name: "test-cmd",
  132. Aliases: []string{"tc"},
  133. Usage: "this is for testing",
  134. Description: "testing",
  135. Action: func(c *cli.Context) error {
  136. val := c.Int("top.test")
  137. expect(t, val, 7)
  138. return nil
  139. },
  140. Flags: []cli.Flag{
  141. NewIntFlag(cli.IntFlag{Name: "top.test"}),
  142. cli.StringFlag{Name: "load"}},
  143. }
  144. command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
  145. err := command.Run(c)
  146. expect(t, err, nil)
  147. }
  148. func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
  149. app := cli.NewApp()
  150. set := flag.NewFlagSet("test", 0)
  151. ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
  152. defer os.Remove("current.yaml")
  153. test := []string{"test-cmd", "--load", "current.yaml"}
  154. set.Parse(test)
  155. c := cli.NewContext(app, set, nil)
  156. command := &cli.Command{
  157. Name: "test-cmd",
  158. Aliases: []string{"tc"},
  159. Usage: "this is for testing",
  160. Description: "testing",
  161. Action: func(c *cli.Context) error {
  162. val := c.Int("test")
  163. expect(t, val, 15)
  164. return nil
  165. },
  166. Flags: []cli.Flag{
  167. NewIntFlag(cli.IntFlag{Name: "test", Value: 7}),
  168. cli.StringFlag{Name: "load"}},
  169. }
  170. command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
  171. err := command.Run(c)
  172. expect(t, err, nil)
  173. }
  174. func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
  175. app := cli.NewApp()
  176. set := flag.NewFlagSet("test", 0)
  177. ioutil.WriteFile("current.yaml", []byte(`top:
  178. test: 15`), 0666)
  179. defer os.Remove("current.yaml")
  180. test := []string{"test-cmd", "--load", "current.yaml"}
  181. set.Parse(test)
  182. c := cli.NewContext(app, set, nil)
  183. command := &cli.Command{
  184. Name: "test-cmd",
  185. Aliases: []string{"tc"},
  186. Usage: "this is for testing",
  187. Description: "testing",
  188. Action: func(c *cli.Context) error {
  189. val := c.Int("top.test")
  190. expect(t, val, 15)
  191. return nil
  192. },
  193. Flags: []cli.Flag{
  194. NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7}),
  195. cli.StringFlag{Name: "load"}},
  196. }
  197. command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
  198. err := command.Run(c)
  199. expect(t, err, nil)
  200. }
  201. func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T) {
  202. app := cli.NewApp()
  203. set := flag.NewFlagSet("test", 0)
  204. ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
  205. defer os.Remove("current.yaml")
  206. os.Setenv("THE_TEST", "11")
  207. defer os.Setenv("THE_TEST", "")
  208. test := []string{"test-cmd", "--load", "current.yaml"}
  209. set.Parse(test)
  210. c := cli.NewContext(app, set, nil)
  211. command := &cli.Command{
  212. Name: "test-cmd",
  213. Aliases: []string{"tc"},
  214. Usage: "this is for testing",
  215. Description: "testing",
  216. Action: func(c *cli.Context) error {
  217. val := c.Int("test")
  218. expect(t, val, 11)
  219. return nil
  220. },
  221. Flags: []cli.Flag{
  222. NewIntFlag(cli.IntFlag{Name: "test", Value: 7, EnvVar: "THE_TEST"}),
  223. cli.StringFlag{Name: "load"}},
  224. }
  225. command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
  226. err := command.Run(c)
  227. expect(t, err, nil)
  228. }
  229. func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWinsNested(t *testing.T) {
  230. app := cli.NewApp()
  231. set := flag.NewFlagSet("test", 0)
  232. ioutil.WriteFile("current.yaml", []byte(`top:
  233. test: 15`), 0666)
  234. defer os.Remove("current.yaml")
  235. os.Setenv("THE_TEST", "11")
  236. defer os.Setenv("THE_TEST", "")
  237. test := []string{"test-cmd", "--load", "current.yaml"}
  238. set.Parse(test)
  239. c := cli.NewContext(app, set, nil)
  240. command := &cli.Command{
  241. Name: "test-cmd",
  242. Aliases: []string{"tc"},
  243. Usage: "this is for testing",
  244. Description: "testing",
  245. Action: func(c *cli.Context) error {
  246. val := c.Int("top.test")
  247. expect(t, val, 11)
  248. return nil
  249. },
  250. Flags: []cli.Flag{
  251. NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7, EnvVar: "THE_TEST"}),
  252. cli.StringFlag{Name: "load"}},
  253. }
  254. command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
  255. err := command.Run(c)
  256. expect(t, err, nil)
  257. }