args_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/taskfile/ast"
  8. )
  9. func TestArgs(t *testing.T) {
  10. tests := []struct {
  11. Args []string
  12. ExpectedCalls []*ast.Call
  13. ExpectedGlobals *ast.Vars
  14. }{
  15. {
  16. Args: []string{"task-a", "task-b", "task-c"},
  17. ExpectedCalls: []*ast.Call{
  18. {Task: "task-a"},
  19. {Task: "task-b"},
  20. {Task: "task-c"},
  21. },
  22. },
  23. {
  24. Args: []string{"task-a", "FOO=bar", "task-b", "task-c", "BAR=baz", "BAZ=foo"},
  25. ExpectedCalls: []*ast.Call{
  26. {Task: "task-a"},
  27. {Task: "task-b"},
  28. {Task: "task-c"},
  29. },
  30. ExpectedGlobals: ast.NewVars(
  31. &ast.VarElement{
  32. Key: "FOO",
  33. Value: ast.Var{
  34. Value: "bar",
  35. },
  36. },
  37. &ast.VarElement{
  38. Key: "BAR",
  39. Value: ast.Var{
  40. Value: "baz",
  41. },
  42. },
  43. &ast.VarElement{
  44. Key: "BAZ",
  45. Value: ast.Var{
  46. Value: "foo",
  47. },
  48. },
  49. ),
  50. },
  51. {
  52. Args: []string{"task-a", "CONTENT=with some spaces"},
  53. ExpectedCalls: []*ast.Call{
  54. {Task: "task-a"},
  55. },
  56. ExpectedGlobals: ast.NewVars(
  57. &ast.VarElement{
  58. Key: "CONTENT",
  59. Value: ast.Var{
  60. Value: "with some spaces",
  61. },
  62. },
  63. ),
  64. },
  65. {
  66. Args: []string{"FOO=bar", "task-a", "task-b"},
  67. ExpectedCalls: []*ast.Call{
  68. {Task: "task-a"},
  69. {Task: "task-b"},
  70. },
  71. ExpectedGlobals: ast.NewVars(
  72. &ast.VarElement{
  73. Key: "FOO",
  74. Value: ast.Var{
  75. Value: "bar",
  76. },
  77. },
  78. ),
  79. },
  80. {
  81. Args: nil,
  82. ExpectedCalls: []*ast.Call{},
  83. },
  84. {
  85. Args: []string{},
  86. ExpectedCalls: []*ast.Call{},
  87. },
  88. {
  89. Args: []string{"FOO=bar", "BAR=baz"},
  90. ExpectedCalls: []*ast.Call{},
  91. ExpectedGlobals: ast.NewVars(
  92. &ast.VarElement{
  93. Key: "FOO",
  94. Value: ast.Var{
  95. Value: "bar",
  96. },
  97. },
  98. &ast.VarElement{
  99. Key: "BAR",
  100. Value: ast.Var{
  101. Value: "baz",
  102. },
  103. },
  104. ),
  105. },
  106. }
  107. for i, test := range tests {
  108. t.Run(fmt.Sprintf("TestArgs%d", i+1), func(t *testing.T) {
  109. calls, globals := args.Parse(test.Args...)
  110. assert.Equal(t, test.ExpectedCalls, calls)
  111. if test.ExpectedGlobals.Len() > 0 || globals.Len() > 0 {
  112. assert.Equal(t, test.ExpectedGlobals, globals)
  113. assert.Equal(t, test.ExpectedGlobals, globals)
  114. }
  115. })
  116. }
  117. }