taskfile_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package ast_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "gopkg.in/yaml.v3"
  7. "github.com/go-task/task/v3/internal/omap"
  8. "github.com/go-task/task/v3/taskfile/ast"
  9. )
  10. func TestCmdParse(t *testing.T) {
  11. const (
  12. yamlCmd = `echo "a string command"`
  13. yamlDep = `"task-name"`
  14. yamlTaskCall = `
  15. task: another-task
  16. vars:
  17. PARAM1: VALUE1
  18. PARAM2: VALUE2
  19. `
  20. yamlDeferredCall = `defer: { task: some_task, vars: { PARAM1: "var" } }`
  21. yamlDeferredCmd = `defer: echo 'test'`
  22. )
  23. tests := []struct {
  24. content string
  25. v any
  26. expected any
  27. }{
  28. {
  29. yamlCmd,
  30. &ast.Cmd{},
  31. &ast.Cmd{Cmd: `echo "a string command"`},
  32. },
  33. {
  34. yamlTaskCall,
  35. &ast.Cmd{},
  36. &ast.Cmd{
  37. Task: "another-task", Vars: &ast.Vars{
  38. OrderedMap: omap.FromMapWithOrder(
  39. map[string]ast.Var{
  40. "PARAM1": {Value: "VALUE1"},
  41. "PARAM2": {Value: "VALUE2"},
  42. },
  43. []string{"PARAM1", "PARAM2"},
  44. ),
  45. },
  46. },
  47. },
  48. {
  49. yamlDeferredCmd,
  50. &ast.Cmd{},
  51. &ast.Cmd{Cmd: "echo 'test'", Defer: true},
  52. },
  53. {
  54. yamlDeferredCall,
  55. &ast.Cmd{},
  56. &ast.Cmd{
  57. Task: "some_task", Vars: &ast.Vars{
  58. OrderedMap: omap.FromMapWithOrder(
  59. map[string]ast.Var{
  60. "PARAM1": {Value: "var"},
  61. },
  62. []string{"PARAM1"},
  63. ),
  64. },
  65. Defer: true,
  66. },
  67. },
  68. {
  69. yamlDep,
  70. &ast.Dep{},
  71. &ast.Dep{Task: "task-name"},
  72. },
  73. {
  74. yamlTaskCall,
  75. &ast.Dep{},
  76. &ast.Dep{
  77. Task: "another-task", Vars: &ast.Vars{
  78. OrderedMap: omap.FromMapWithOrder(
  79. map[string]ast.Var{
  80. "PARAM1": {Value: "VALUE1"},
  81. "PARAM2": {Value: "VALUE2"},
  82. },
  83. []string{"PARAM1", "PARAM2"},
  84. ),
  85. },
  86. },
  87. },
  88. }
  89. for _, test := range tests {
  90. err := yaml.Unmarshal([]byte(test.content), test.v)
  91. require.NoError(t, err)
  92. assert.Equal(t, test.expected, test.v)
  93. }
  94. }