2017-09-07 18:57:06 +02:00
|
|
|
package args_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2021-01-07 16:48:33 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2020-08-19 10:59:58 +02:00
|
|
|
"github.com/go-task/task/v3/args"
|
|
|
|
"github.com/go-task/task/v3/taskfile"
|
2017-09-07 18:57:06 +02:00
|
|
|
)
|
|
|
|
|
2020-08-17 02:26:10 +02:00
|
|
|
func TestArgsV3(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Args []string
|
|
|
|
ExpectedCalls []taskfile.Call
|
|
|
|
ExpectedGlobals *taskfile.Vars
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Args: []string{"task-a", "task-b", "task-c"},
|
|
|
|
ExpectedCalls: []taskfile.Call{
|
|
|
|
{Task: "task-a"},
|
|
|
|
{Task: "task-b"},
|
|
|
|
{Task: "task-c"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Args: []string{"task-a", "FOO=bar", "task-b", "task-c", "BAR=baz", "BAZ=foo"},
|
|
|
|
ExpectedCalls: []taskfile.Call{
|
|
|
|
{Task: "task-a"},
|
|
|
|
{Task: "task-b"},
|
|
|
|
{Task: "task-c"},
|
|
|
|
},
|
|
|
|
ExpectedGlobals: &taskfile.Vars{
|
|
|
|
Keys: []string{"FOO", "BAR", "BAZ"},
|
|
|
|
Mapping: map[string]taskfile.Var{
|
|
|
|
"FOO": taskfile.Var{Static: "bar"},
|
|
|
|
"BAR": taskfile.Var{Static: "baz"},
|
|
|
|
"BAZ": taskfile.Var{Static: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Args: []string{"task-a", "CONTENT=with some spaces"},
|
|
|
|
ExpectedCalls: []taskfile.Call{
|
|
|
|
{Task: "task-a"},
|
|
|
|
},
|
|
|
|
ExpectedGlobals: &taskfile.Vars{
|
|
|
|
Keys: []string{"CONTENT"},
|
|
|
|
Mapping: map[string]taskfile.Var{
|
|
|
|
"CONTENT": taskfile.Var{Static: "with some spaces"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Args: []string{"FOO=bar", "task-a", "task-b"},
|
|
|
|
ExpectedCalls: []taskfile.Call{
|
|
|
|
{Task: "task-a"},
|
|
|
|
{Task: "task-b"},
|
|
|
|
},
|
|
|
|
ExpectedGlobals: &taskfile.Vars{
|
|
|
|
Keys: []string{"FOO"},
|
|
|
|
Mapping: map[string]taskfile.Var{
|
|
|
|
"FOO": {Static: "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Args: nil,
|
|
|
|
ExpectedCalls: []taskfile.Call{
|
|
|
|
{Task: "default"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Args: []string{},
|
|
|
|
ExpectedCalls: []taskfile.Call{
|
|
|
|
{Task: "default"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Args: []string{"FOO=bar", "BAR=baz"},
|
|
|
|
ExpectedCalls: []taskfile.Call{
|
|
|
|
{Task: "default"},
|
|
|
|
},
|
|
|
|
ExpectedGlobals: &taskfile.Vars{
|
|
|
|
Keys: []string{"FOO", "BAR"},
|
|
|
|
Mapping: map[string]taskfile.Var{
|
|
|
|
"FOO": {Static: "bar"},
|
|
|
|
"BAR": {Static: "baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("TestArgs%d", i+1), func(t *testing.T) {
|
|
|
|
calls, globals := args.ParseV3(test.Args...)
|
|
|
|
assert.Equal(t, test.ExpectedCalls, calls)
|
2021-03-20 16:56:19 +02:00
|
|
|
if test.ExpectedGlobals.Len() > 0 || globals.Len() > 0 {
|
|
|
|
assert.Equal(t, test.ExpectedGlobals, globals)
|
|
|
|
}
|
2020-08-17 02:26:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArgsV2(t *testing.T) {
|
2017-09-07 18:57:06 +02:00
|
|
|
tests := []struct {
|
2019-05-11 16:06:47 +02:00
|
|
|
Args []string
|
|
|
|
ExpectedCalls []taskfile.Call
|
2020-03-29 21:54:59 +02:00
|
|
|
ExpectedGlobals *taskfile.Vars
|
2017-09-07 18:57:06 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
Args: []string{"task-a", "task-b", "task-c"},
|
2019-05-11 16:06:47 +02:00
|
|
|
ExpectedCalls: []taskfile.Call{
|
2017-09-07 18:57:06 +02:00
|
|
|
{Task: "task-a"},
|
|
|
|
{Task: "task-b"},
|
|
|
|
{Task: "task-c"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Args: []string{"task-a", "FOO=bar", "task-b", "task-c", "BAR=baz", "BAZ=foo"},
|
2019-05-11 16:06:47 +02:00
|
|
|
ExpectedCalls: []taskfile.Call{
|
2017-09-07 18:57:06 +02:00
|
|
|
{
|
|
|
|
Task: "task-a",
|
2020-03-29 21:54:59 +02:00
|
|
|
Vars: &taskfile.Vars{
|
|
|
|
Keys: []string{"FOO"},
|
|
|
|
Mapping: map[string]taskfile.Var{
|
|
|
|
"FOO": taskfile.Var{Static: "bar"},
|
|
|
|
},
|
2017-09-07 18:57:06 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{Task: "task-b"},
|
|
|
|
{
|
|
|
|
Task: "task-c",
|
2020-03-29 21:54:59 +02:00
|
|
|
Vars: &taskfile.Vars{
|
|
|
|
Keys: []string{"BAR", "BAZ"},
|
|
|
|
Mapping: map[string]taskfile.Var{
|
|
|
|
"BAR": taskfile.Var{Static: "baz"},
|
|
|
|
"BAZ": taskfile.Var{Static: "foo"},
|
|
|
|
},
|
2017-09-07 18:57:06 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Args: []string{"task-a", "CONTENT=with some spaces"},
|
2019-05-11 16:06:47 +02:00
|
|
|
ExpectedCalls: []taskfile.Call{
|
2017-09-07 18:57:06 +02:00
|
|
|
{
|
|
|
|
Task: "task-a",
|
2020-03-29 21:54:59 +02:00
|
|
|
Vars: &taskfile.Vars{
|
|
|
|
Keys: []string{"CONTENT"},
|
|
|
|
Mapping: map[string]taskfile.Var{
|
|
|
|
"CONTENT": taskfile.Var{Static: "with some spaces"},
|
|
|
|
},
|
2017-09-07 18:57:06 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-05-11 16:06:47 +02:00
|
|
|
Args: []string{"FOO=bar", "task-a", "task-b"},
|
|
|
|
ExpectedCalls: []taskfile.Call{
|
|
|
|
{Task: "task-a"},
|
|
|
|
{Task: "task-b"},
|
|
|
|
},
|
2020-03-29 21:54:59 +02:00
|
|
|
ExpectedGlobals: &taskfile.Vars{
|
|
|
|
Keys: []string{"FOO"},
|
|
|
|
Mapping: map[string]taskfile.Var{
|
|
|
|
"FOO": {Static: "bar"},
|
|
|
|
},
|
2019-05-11 16:06:47 +02:00
|
|
|
},
|
2017-09-07 18:57:06 +02:00
|
|
|
},
|
2019-12-07 21:48:23 +02:00
|
|
|
{
|
|
|
|
Args: nil,
|
|
|
|
ExpectedCalls: []taskfile.Call{
|
|
|
|
{Task: "default"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Args: []string{},
|
|
|
|
ExpectedCalls: []taskfile.Call{
|
|
|
|
{Task: "default"},
|
|
|
|
},
|
|
|
|
},
|
2019-12-07 21:20:36 +02:00
|
|
|
{
|
|
|
|
Args: []string{"FOO=bar", "BAR=baz"},
|
|
|
|
ExpectedCalls: []taskfile.Call{
|
|
|
|
{Task: "default"},
|
|
|
|
},
|
2020-03-29 21:54:59 +02:00
|
|
|
ExpectedGlobals: &taskfile.Vars{
|
|
|
|
Keys: []string{"FOO", "BAR"},
|
|
|
|
Mapping: map[string]taskfile.Var{
|
|
|
|
"FOO": {Static: "bar"},
|
|
|
|
"BAR": {Static: "baz"},
|
|
|
|
},
|
2019-12-07 21:20:36 +02:00
|
|
|
},
|
|
|
|
},
|
2017-09-07 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("TestArgs%d", i+1), func(t *testing.T) {
|
2020-08-17 02:26:10 +02:00
|
|
|
calls, globals := args.ParseV2(test.Args...)
|
2019-05-11 16:06:47 +02:00
|
|
|
assert.Equal(t, test.ExpectedCalls, calls)
|
2021-03-20 16:56:19 +02:00
|
|
|
if test.ExpectedGlobals.Len() > 0 || globals.Len() > 0 {
|
|
|
|
assert.Equal(t, test.ExpectedGlobals, globals)
|
|
|
|
}
|
|
|
|
|
2017-09-07 18:57:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|