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.
 
 
 

337 lines
9.4 KiB

  1. package altsrc
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "testing"
  8. "time"
  9. "gopkg.in/urfave/cli.v1"
  10. )
  11. type testApplyInputSource struct {
  12. Flag FlagInputSourceExtension
  13. FlagName string
  14. FlagSetName string
  15. Expected string
  16. ContextValueString string
  17. ContextValue flag.Value
  18. EnvVarValue string
  19. EnvVarName string
  20. MapValue interface{}
  21. }
  22. func TestGenericApplyInputSourceValue(t *testing.T) {
  23. v := &Parser{"abc", "def"}
  24. c := runTest(t, testApplyInputSource{
  25. Flag: NewGenericFlag(cli.GenericFlag{Name: "test", Value: &Parser{}}),
  26. FlagName: "test",
  27. MapValue: v,
  28. })
  29. expect(t, v, c.Generic("test"))
  30. }
  31. func TestGenericApplyInputSourceMethodContextSet(t *testing.T) {
  32. p := &Parser{"abc", "def"}
  33. c := runTest(t, testApplyInputSource{
  34. Flag: NewGenericFlag(cli.GenericFlag{Name: "test", Value: &Parser{}}),
  35. FlagName: "test",
  36. MapValue: &Parser{"efg", "hig"},
  37. ContextValueString: p.String(),
  38. })
  39. expect(t, p, c.Generic("test"))
  40. }
  41. func TestGenericApplyInputSourceMethodEnvVarSet(t *testing.T) {
  42. c := runTest(t, testApplyInputSource{
  43. Flag: NewGenericFlag(cli.GenericFlag{Name: "test", Value: &Parser{}, EnvVar: "TEST"}),
  44. FlagName: "test",
  45. MapValue: &Parser{"efg", "hij"},
  46. EnvVarName: "TEST",
  47. EnvVarValue: "abc,def",
  48. })
  49. expect(t, &Parser{"abc", "def"}, c.Generic("test"))
  50. }
  51. func TestStringSliceApplyInputSourceValue(t *testing.T) {
  52. c := runTest(t, testApplyInputSource{
  53. Flag: NewStringSliceFlag(cli.StringSliceFlag{Name: "test"}),
  54. FlagName: "test",
  55. MapValue: []string{"hello", "world"},
  56. })
  57. expect(t, c.StringSlice("test"), []string{"hello", "world"})
  58. }
  59. func TestStringSliceApplyInputSourceMethodContextSet(t *testing.T) {
  60. c := runTest(t, testApplyInputSource{
  61. Flag: NewStringSliceFlag(cli.StringSliceFlag{Name: "test"}),
  62. FlagName: "test",
  63. MapValue: []string{"hello", "world"},
  64. ContextValueString: "ohno",
  65. })
  66. expect(t, c.StringSlice("test"), []string{"ohno"})
  67. }
  68. func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
  69. c := runTest(t, testApplyInputSource{
  70. Flag: NewStringSliceFlag(cli.StringSliceFlag{Name: "test", EnvVar: "TEST"}),
  71. FlagName: "test",
  72. MapValue: []string{"hello", "world"},
  73. EnvVarName: "TEST",
  74. EnvVarValue: "oh,no",
  75. })
  76. expect(t, c.StringSlice("test"), []string{"oh", "no"})
  77. }
  78. func TestIntSliceApplyInputSourceValue(t *testing.T) {
  79. c := runTest(t, testApplyInputSource{
  80. Flag: NewIntSliceFlag(cli.IntSliceFlag{Name: "test"}),
  81. FlagName: "test",
  82. MapValue: []int{1, 2},
  83. })
  84. expect(t, c.IntSlice("test"), []int{1, 2})
  85. }
  86. func TestIntSliceApplyInputSourceMethodContextSet(t *testing.T) {
  87. c := runTest(t, testApplyInputSource{
  88. Flag: NewIntSliceFlag(cli.IntSliceFlag{Name: "test"}),
  89. FlagName: "test",
  90. MapValue: []int{1, 2},
  91. ContextValueString: "3",
  92. })
  93. expect(t, c.IntSlice("test"), []int{3})
  94. }
  95. func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
  96. c := runTest(t, testApplyInputSource{
  97. Flag: NewIntSliceFlag(cli.IntSliceFlag{Name: "test", EnvVar: "TEST"}),
  98. FlagName: "test",
  99. MapValue: []int{1, 2},
  100. EnvVarName: "TEST",
  101. EnvVarValue: "3,4",
  102. })
  103. expect(t, c.IntSlice("test"), []int{3, 4})
  104. }
  105. func TestBoolApplyInputSourceMethodSet(t *testing.T) {
  106. c := runTest(t, testApplyInputSource{
  107. Flag: NewBoolFlag(cli.BoolFlag{Name: "test"}),
  108. FlagName: "test",
  109. MapValue: true,
  110. })
  111. expect(t, true, c.Bool("test"))
  112. }
  113. func TestBoolApplyInputSourceMethodContextSet(t *testing.T) {
  114. c := runTest(t, testApplyInputSource{
  115. Flag: NewBoolFlag(cli.BoolFlag{Name: "test"}),
  116. FlagName: "test",
  117. MapValue: false,
  118. ContextValueString: "true",
  119. })
  120. expect(t, true, c.Bool("test"))
  121. }
  122. func TestBoolApplyInputSourceMethodEnvVarSet(t *testing.T) {
  123. c := runTest(t, testApplyInputSource{
  124. Flag: NewBoolFlag(cli.BoolFlag{Name: "test", EnvVar: "TEST"}),
  125. FlagName: "test",
  126. MapValue: false,
  127. EnvVarName: "TEST",
  128. EnvVarValue: "true",
  129. })
  130. expect(t, true, c.Bool("test"))
  131. }
  132. func TestBoolTApplyInputSourceMethodSet(t *testing.T) {
  133. c := runTest(t, testApplyInputSource{
  134. Flag: NewBoolTFlag(cli.BoolTFlag{Name: "test"}),
  135. FlagName: "test",
  136. MapValue: false,
  137. })
  138. expect(t, false, c.BoolT("test"))
  139. }
  140. func TestBoolTApplyInputSourceMethodContextSet(t *testing.T) {
  141. c := runTest(t, testApplyInputSource{
  142. Flag: NewBoolTFlag(cli.BoolTFlag{Name: "test"}),
  143. FlagName: "test",
  144. MapValue: true,
  145. ContextValueString: "false",
  146. })
  147. expect(t, false, c.BoolT("test"))
  148. }
  149. func TestBoolTApplyInputSourceMethodEnvVarSet(t *testing.T) {
  150. c := runTest(t, testApplyInputSource{
  151. Flag: NewBoolTFlag(cli.BoolTFlag{Name: "test", EnvVar: "TEST"}),
  152. FlagName: "test",
  153. MapValue: true,
  154. EnvVarName: "TEST",
  155. EnvVarValue: "false",
  156. })
  157. expect(t, false, c.BoolT("test"))
  158. }
  159. func TestStringApplyInputSourceMethodSet(t *testing.T) {
  160. c := runTest(t, testApplyInputSource{
  161. Flag: NewStringFlag(cli.StringFlag{Name: "test"}),
  162. FlagName: "test",
  163. MapValue: "hello",
  164. })
  165. expect(t, "hello", c.String("test"))
  166. }
  167. func TestStringApplyInputSourceMethodContextSet(t *testing.T) {
  168. c := runTest(t, testApplyInputSource{
  169. Flag: NewStringFlag(cli.StringFlag{Name: "test"}),
  170. FlagName: "test",
  171. MapValue: "hello",
  172. ContextValueString: "goodbye",
  173. })
  174. expect(t, "goodbye", c.String("test"))
  175. }
  176. func TestStringApplyInputSourceMethodEnvVarSet(t *testing.T) {
  177. c := runTest(t, testApplyInputSource{
  178. Flag: NewStringFlag(cli.StringFlag{Name: "test", EnvVar: "TEST"}),
  179. FlagName: "test",
  180. MapValue: "hello",
  181. EnvVarName: "TEST",
  182. EnvVarValue: "goodbye",
  183. })
  184. expect(t, "goodbye", c.String("test"))
  185. }
  186. func TestIntApplyInputSourceMethodSet(t *testing.T) {
  187. c := runTest(t, testApplyInputSource{
  188. Flag: NewIntFlag(cli.IntFlag{Name: "test"}),
  189. FlagName: "test",
  190. MapValue: 15,
  191. })
  192. expect(t, 15, c.Int("test"))
  193. }
  194. func TestIntApplyInputSourceMethodContextSet(t *testing.T) {
  195. c := runTest(t, testApplyInputSource{
  196. Flag: NewIntFlag(cli.IntFlag{Name: "test"}),
  197. FlagName: "test",
  198. MapValue: 15,
  199. ContextValueString: "7",
  200. })
  201. expect(t, 7, c.Int("test"))
  202. }
  203. func TestIntApplyInputSourceMethodEnvVarSet(t *testing.T) {
  204. c := runTest(t, testApplyInputSource{
  205. Flag: NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "TEST"}),
  206. FlagName: "test",
  207. MapValue: 15,
  208. EnvVarName: "TEST",
  209. EnvVarValue: "12",
  210. })
  211. expect(t, 12, c.Int("test"))
  212. }
  213. func TestDurationApplyInputSourceMethodSet(t *testing.T) {
  214. c := runTest(t, testApplyInputSource{
  215. Flag: NewDurationFlag(cli.DurationFlag{Name: "test"}),
  216. FlagName: "test",
  217. MapValue: time.Duration(30 * time.Second),
  218. })
  219. expect(t, time.Duration(30*time.Second), c.Duration("test"))
  220. }
  221. func TestDurationApplyInputSourceMethodContextSet(t *testing.T) {
  222. c := runTest(t, testApplyInputSource{
  223. Flag: NewDurationFlag(cli.DurationFlag{Name: "test"}),
  224. FlagName: "test",
  225. MapValue: time.Duration(30 * time.Second),
  226. ContextValueString: time.Duration(15 * time.Second).String(),
  227. })
  228. expect(t, time.Duration(15*time.Second), c.Duration("test"))
  229. }
  230. func TestDurationApplyInputSourceMethodEnvVarSet(t *testing.T) {
  231. c := runTest(t, testApplyInputSource{
  232. Flag: NewDurationFlag(cli.DurationFlag{Name: "test", EnvVar: "TEST"}),
  233. FlagName: "test",
  234. MapValue: time.Duration(30 * time.Second),
  235. EnvVarName: "TEST",
  236. EnvVarValue: time.Duration(15 * time.Second).String(),
  237. })
  238. expect(t, time.Duration(15*time.Second), c.Duration("test"))
  239. }
  240. func TestFloat64ApplyInputSourceMethodSet(t *testing.T) {
  241. c := runTest(t, testApplyInputSource{
  242. Flag: NewFloat64Flag(cli.Float64Flag{Name: "test"}),
  243. FlagName: "test",
  244. MapValue: 1.3,
  245. })
  246. expect(t, 1.3, c.Float64("test"))
  247. }
  248. func TestFloat64ApplyInputSourceMethodContextSet(t *testing.T) {
  249. c := runTest(t, testApplyInputSource{
  250. Flag: NewFloat64Flag(cli.Float64Flag{Name: "test"}),
  251. FlagName: "test",
  252. MapValue: 1.3,
  253. ContextValueString: fmt.Sprintf("%v", 1.4),
  254. })
  255. expect(t, 1.4, c.Float64("test"))
  256. }
  257. func TestFloat64ApplyInputSourceMethodEnvVarSet(t *testing.T) {
  258. c := runTest(t, testApplyInputSource{
  259. Flag: NewFloat64Flag(cli.Float64Flag{Name: "test", EnvVar: "TEST"}),
  260. FlagName: "test",
  261. MapValue: 1.3,
  262. EnvVarName: "TEST",
  263. EnvVarValue: fmt.Sprintf("%v", 1.4),
  264. })
  265. expect(t, 1.4, c.Float64("test"))
  266. }
  267. func runTest(t *testing.T, test testApplyInputSource) *cli.Context {
  268. inputSource := &MapInputSource{valueMap: map[interface{}]interface{}{test.FlagName: test.MapValue}}
  269. set := flag.NewFlagSet(test.FlagSetName, flag.ContinueOnError)
  270. c := cli.NewContext(nil, set, nil)
  271. if test.EnvVarName != "" && test.EnvVarValue != "" {
  272. os.Setenv(test.EnvVarName, test.EnvVarValue)
  273. defer os.Setenv(test.EnvVarName, "")
  274. }
  275. test.Flag.Apply(set)
  276. if test.ContextValue != nil {
  277. flag := set.Lookup(test.FlagName)
  278. flag.Value = test.ContextValue
  279. }
  280. if test.ContextValueString != "" {
  281. set.Set(test.FlagName, test.ContextValueString)
  282. }
  283. test.Flag.ApplyInputSourceValue(c, inputSource)
  284. return c
  285. }
  286. type Parser [2]string
  287. func (p *Parser) Set(value string) error {
  288. parts := strings.Split(value, ",")
  289. if len(parts) != 2 {
  290. return fmt.Errorf("invalid format")
  291. }
  292. (*p)[0] = parts[0]
  293. (*p)[1] = parts[1]
  294. return nil
  295. }
  296. func (p *Parser) String() string {
  297. return fmt.Sprintf("%s,%s", p[0], p[1])
  298. }