2023-12-29 20:32:03 +00:00
|
|
|
package ast_test
|
2017-07-02 15:30:50 -03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-04-06 11:18:41 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-03-28 11:27:49 -03:00
|
|
|
"gopkg.in/yaml.v3"
|
2021-01-07 11:48:33 -03:00
|
|
|
|
2023-12-29 20:32:03 +00:00
|
|
|
"github.com/go-task/task/v3/taskfile/ast"
|
2017-07-02 15:30:50 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCmdParse(t *testing.T) {
|
2024-12-12 01:47:10 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-02 15:30:50 -03:00
|
|
|
const (
|
|
|
|
yamlCmd = `echo "a string command"`
|
|
|
|
yamlDep = `"task-name"`
|
|
|
|
yamlTaskCall = `
|
|
|
|
task: another-task
|
2017-07-05 20:07:24 -03:00
|
|
|
vars:
|
2017-07-02 15:30:50 -03:00
|
|
|
PARAM1: VALUE1
|
|
|
|
PARAM2: VALUE2
|
|
|
|
`
|
2022-01-02 16:38:06 -05:00
|
|
|
yamlDeferredCall = `defer: { task: some_task, vars: { PARAM1: "var" } }`
|
2021-12-15 00:03:37 -05:00
|
|
|
yamlDeferredCmd = `defer: echo 'test'`
|
2017-07-02 15:30:50 -03:00
|
|
|
)
|
|
|
|
tests := []struct {
|
|
|
|
content string
|
2023-03-30 20:03:59 +00:00
|
|
|
v any
|
|
|
|
expected any
|
2017-07-02 15:30:50 -03:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
yamlCmd,
|
2023-12-29 20:32:03 +00:00
|
|
|
&ast.Cmd{},
|
|
|
|
&ast.Cmd{Cmd: `echo "a string command"`},
|
2017-07-02 15:30:50 -03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
yamlTaskCall,
|
2023-12-29 20:32:03 +00:00
|
|
|
&ast.Cmd{},
|
|
|
|
&ast.Cmd{
|
2024-12-30 17:54:36 +00:00
|
|
|
Task: "another-task",
|
|
|
|
Vars: ast.NewVars(
|
|
|
|
&ast.VarElement{
|
|
|
|
Key: "PARAM1",
|
|
|
|
Value: ast.Var{
|
|
|
|
Value: "VALUE1",
|
2023-04-06 12:07:57 +01:00
|
|
|
},
|
2024-12-30 17:54:36 +00:00
|
|
|
},
|
|
|
|
&ast.VarElement{
|
|
|
|
Key: "PARAM2",
|
|
|
|
Value: ast.Var{
|
|
|
|
Value: "VALUE2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2023-04-06 12:07:57 +01:00
|
|
|
},
|
2017-07-02 15:30:50 -03:00
|
|
|
},
|
2021-12-15 00:03:37 -05:00
|
|
|
{
|
|
|
|
yamlDeferredCmd,
|
2023-12-29 20:32:03 +00:00
|
|
|
&ast.Cmd{},
|
|
|
|
&ast.Cmd{Cmd: "echo 'test'", Defer: true},
|
2021-12-15 00:03:37 -05:00
|
|
|
},
|
|
|
|
{
|
2022-01-02 16:38:06 -05:00
|
|
|
yamlDeferredCall,
|
2023-12-29 20:32:03 +00:00
|
|
|
&ast.Cmd{},
|
|
|
|
&ast.Cmd{
|
2024-12-30 17:54:36 +00:00
|
|
|
Task: "some_task",
|
|
|
|
Vars: ast.NewVars(
|
|
|
|
&ast.VarElement{
|
|
|
|
Key: "PARAM1",
|
|
|
|
Value: ast.Var{
|
|
|
|
Value: "var",
|
2023-04-06 12:07:57 +01:00
|
|
|
},
|
2024-12-30 17:54:36 +00:00
|
|
|
},
|
|
|
|
),
|
2023-04-06 12:07:57 +01:00
|
|
|
Defer: true,
|
|
|
|
},
|
2021-12-15 00:03:37 -05:00
|
|
|
},
|
2017-07-02 15:30:50 -03:00
|
|
|
{
|
|
|
|
yamlDep,
|
2023-12-29 20:32:03 +00:00
|
|
|
&ast.Dep{},
|
|
|
|
&ast.Dep{Task: "task-name"},
|
2017-07-02 15:30:50 -03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
yamlTaskCall,
|
2023-12-29 20:32:03 +00:00
|
|
|
&ast.Dep{},
|
|
|
|
&ast.Dep{
|
2024-12-30 17:54:36 +00:00
|
|
|
Task: "another-task",
|
|
|
|
Vars: ast.NewVars(
|
|
|
|
&ast.VarElement{
|
|
|
|
Key: "PARAM1",
|
|
|
|
Value: ast.Var{
|
|
|
|
Value: "VALUE1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&ast.VarElement{
|
|
|
|
Key: "PARAM2",
|
|
|
|
Value: ast.Var{
|
|
|
|
Value: "VALUE2",
|
2023-04-06 12:07:57 +01:00
|
|
|
},
|
2024-12-30 17:54:36 +00:00
|
|
|
},
|
|
|
|
),
|
2023-04-06 12:07:57 +01:00
|
|
|
},
|
2017-07-02 15:30:50 -03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
err := yaml.Unmarshal([]byte(test.content), test.v)
|
2023-04-06 11:18:41 +01:00
|
|
|
require.NoError(t, err)
|
2017-07-02 15:30:50 -03:00
|
|
|
assert.Equal(t, test.expected, test.v)
|
|
|
|
}
|
|
|
|
}
|