args_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package args_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/go-task/task/v3/args"
  7. "github.com/go-task/task/v3/internal/omap"
  8. "github.com/go-task/task/v3/taskfile/ast"
  9. )
  10. func TestArgs(t *testing.T) {
  11. tests := []struct {
  12. Args []string
  13. ExpectedCalls []*ast.Call
  14. ExpectedGlobals *ast.Vars
  15. }{
  16. {
  17. Args: []string{"task-a", "task-b", "task-c"},
  18. ExpectedCalls: []*ast.Call{
  19. {Task: "task-a"},
  20. {Task: "task-b"},
  21. {Task: "task-c"},
  22. },
  23. },
  24. {
  25. Args: []string{"task-a", "FOO=bar", "task-b", "task-c", "BAR=baz", "BAZ=foo"},
  26. ExpectedCalls: []*ast.Call{
  27. {Task: "task-a"},
  28. {Task: "task-b"},
  29. {Task: "task-c"},
  30. },
  31. ExpectedGlobals: &ast.Vars{
  32. OrderedMap: omap.FromMapWithOrder(
  33. map[string]ast.Var{
  34. "FOO": {Value: "bar"},
  35. "BAR": {Value: "baz"},
  36. "BAZ": {Value: "foo"},
  37. },
  38. []string{"FOO", "BAR", "BAZ"},
  39. ),
  40. },
  41. },
  42. {
  43. Args: []string{"task-a", "CONTENT=with some spaces"},
  44. ExpectedCalls: []*ast.Call{
  45. {Task: "task-a"},
  46. },
  47. ExpectedGlobals: &ast.Vars{
  48. OrderedMap: omap.FromMapWithOrder(
  49. map[string]ast.Var{
  50. "CONTENT": {Value: "with some spaces"},
  51. },
  52. []string{"CONTENT"},
  53. ),
  54. },
  55. },
  56. {
  57. Args: []string{"FOO=bar", "task-a", "task-b"},
  58. ExpectedCalls: []*ast.Call{
  59. {Task: "task-a"},
  60. {Task: "task-b"},
  61. },
  62. ExpectedGlobals: &ast.Vars{
  63. OrderedMap: omap.FromMapWithOrder(
  64. map[string]ast.Var{
  65. "FOO": {Value: "bar"},
  66. },
  67. []string{"FOO"},
  68. ),
  69. },
  70. },
  71. {
  72. Args: nil,
  73. ExpectedCalls: []*ast.Call{},
  74. },
  75. {
  76. Args: []string{},
  77. ExpectedCalls: []*ast.Call{},
  78. },
  79. {
  80. Args: []string{"FOO=bar", "BAR=baz"},
  81. ExpectedCalls: []*ast.Call{},
  82. ExpectedGlobals: &ast.Vars{
  83. OrderedMap: omap.FromMapWithOrder(
  84. map[string]ast.Var{
  85. "FOO": {Value: "bar"},
  86. "BAR": {Value: "baz"},
  87. },
  88. []string{"FOO", "BAR"},
  89. ),
  90. },
  91. },
  92. }
  93. for i, test := range tests {
  94. t.Run(fmt.Sprintf("TestArgs%d", i+1), func(t *testing.T) {
  95. calls, globals := args.Parse(test.Args...)
  96. assert.Equal(t, test.ExpectedCalls, calls)
  97. if test.ExpectedGlobals.Len() > 0 || globals.Len() > 0 {
  98. assert.Equal(t, test.ExpectedGlobals.Keys(), globals.Keys())
  99. assert.Equal(t, test.ExpectedGlobals.Values(), globals.Values())
  100. }
  101. })
  102. }
  103. }