precondition_test.go 976 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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/taskfile/ast"
  8. )
  9. func TestPreconditionParse(t *testing.T) {
  10. tests := []struct {
  11. content string
  12. v any
  13. expected any
  14. }{
  15. {
  16. "test -f foo.txt",
  17. &ast.Precondition{},
  18. &ast.Precondition{Sh: `test -f foo.txt`, Msg: "`test -f foo.txt` failed"},
  19. },
  20. {
  21. "sh: '[ 1 = 0 ]'",
  22. &ast.Precondition{},
  23. &ast.Precondition{Sh: "[ 1 = 0 ]", Msg: "[ 1 = 0 ] failed"},
  24. },
  25. {
  26. `
  27. sh: "[ 1 = 2 ]"
  28. msg: "1 is not 2"
  29. `,
  30. &ast.Precondition{},
  31. &ast.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2"},
  32. },
  33. {
  34. `
  35. sh: "[ 1 = 2 ]"
  36. msg: "1 is not 2"
  37. `,
  38. &ast.Precondition{},
  39. &ast.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2"},
  40. },
  41. }
  42. for _, test := range tests {
  43. err := yaml.Unmarshal([]byte(test.content), test.v)
  44. require.NoError(t, err)
  45. assert.Equal(t, test.expected, test.v)
  46. }
  47. }