Skip to content

Commit

Permalink
fix: non deterministic behavior in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 committed Apr 6, 2023
1 parent 716755a commit fbe99d5
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 46 deletions.
94 changes: 61 additions & 33 deletions args/args_test.go
Expand Up @@ -33,11 +33,14 @@ func TestArgsV3(t *testing.T) {
{Task: "task-c"},
},
ExpectedGlobals: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"FOO": {Static: "bar"},
"BAR": {Static: "baz"},
"BAZ": {Static: "foo"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"FOO": {Static: "bar"},
"BAR": {Static: "baz"},
"BAZ": {Static: "foo"},
},
[]string{"FOO", "BAR", "BAZ"},
),
},
},
{
Expand All @@ -46,9 +49,12 @@ func TestArgsV3(t *testing.T) {
{Task: "task-a"},
},
ExpectedGlobals: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"CONTENT": {Static: "with some spaces"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"CONTENT": {Static: "with some spaces"},
},
[]string{"CONTENT"},
),
},
},
{
Expand All @@ -58,9 +64,12 @@ func TestArgsV3(t *testing.T) {
{Task: "task-b"},
},
ExpectedGlobals: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"FOO": {Static: "bar"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"FOO": {Static: "bar"},
},
[]string{"FOO"},
),
},
},
{
Expand All @@ -81,10 +90,13 @@ func TestArgsV3(t *testing.T) {
{Task: "default"},
},
ExpectedGlobals: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"FOO": {Static: "bar"},
"BAR": {Static: "baz"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"FOO": {Static: "bar"},
"BAR": {Static: "baz"},
},
[]string{"FOO", "BAR"},
),
},
},
}
Expand All @@ -94,7 +106,8 @@ func TestArgsV3(t *testing.T) {
calls, globals := args.ParseV3(test.Args...)
assert.Equal(t, test.ExpectedCalls, calls)
if test.ExpectedGlobals.Len() > 0 || globals.Len() > 0 {
assert.Equal(t, test.ExpectedGlobals, globals)
assert.Equal(t, test.ExpectedGlobals.Keys(), globals.Keys())
assert.Equal(t, test.ExpectedGlobals.Values(), globals.Values())
}
})
}
Expand All @@ -120,19 +133,25 @@ func TestArgsV2(t *testing.T) {
{
Task: "task-a",
Vars: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"FOO": {Static: "bar"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"FOO": {Static: "bar"},
},
[]string{"FOO"},
),
},
},
{Task: "task-b"},
{
Task: "task-c",
Vars: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"BAR": {Static: "baz"},
"BAZ": {Static: "foo"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"BAR": {Static: "baz"},
"BAZ": {Static: "foo"},
},
[]string{"BAR", "BAZ"},
),
},
},
},
Expand All @@ -143,9 +162,12 @@ func TestArgsV2(t *testing.T) {
{
Task: "task-a",
Vars: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"CONTENT": {Static: "with some spaces"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"CONTENT": {Static: "with some spaces"},
},
[]string{"CONTENT"},
),
},
},
},
Expand All @@ -157,9 +179,12 @@ func TestArgsV2(t *testing.T) {
{Task: "task-b"},
},
ExpectedGlobals: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"FOO": {Static: "bar"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"FOO": {Static: "bar"},
},
[]string{"FOO"},
),
},
},
{
Expand All @@ -180,10 +205,13 @@ func TestArgsV2(t *testing.T) {
{Task: "default"},
},
ExpectedGlobals: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"FOO": {Static: "bar"},
"BAR": {Static: "baz"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"FOO": {Static: "bar"},
"BAR": {Static: "baz"},
},
[]string{"FOO", "BAR"},
),
},
},
}
Expand Down
19 changes: 17 additions & 2 deletions internal/orderedmap/orderedmap.go
Expand Up @@ -27,14 +27,30 @@ func New[K constraints.Ordered, V any]() OrderedMap[K, V] {
}
}

// FromMap will create a new OrderedMap from the given map.
// FromMap will create a new OrderedMap from the given map. Since Golang maps
// are unordered, the order of the created OrderedMap will be random.
func FromMap[K constraints.Ordered, V any](m map[K]V) OrderedMap[K, V] {
om := New[K, V]()
om.m = m
om.s = maps.Keys(m)
return om
}

func FromMapWithOrder[K constraints.Ordered, V any](m map[K]V, order []K) OrderedMap[K, V] {
om := New[K, V]()
if len(m) != len(order) {
panic("length of map and order must be equal")
}
om.m = m
om.s = order
for key := range om.m {
if !slices.Contains(om.s, key) {
panic("order keys must match map keys")
}
}
return om
}

// Len will return the number of items in the map.
func (om *OrderedMap[K, V]) Len() int {
return len(om.s)
Expand Down Expand Up @@ -124,7 +140,6 @@ func (om *OrderedMap[K, V]) UnmarshalYAML(node *yaml.Node) error {
// Odd numbers contain the values
case yaml.MappingNode:
for i := 0; i < len(node.Content); i += 2 {

// Decode the key
keyNode := node.Content[i]
var k K
Expand Down
31 changes: 20 additions & 11 deletions taskfile/taskfile_test.go
Expand Up @@ -38,10 +38,13 @@ vars:
&taskfile.Cmd{},
&taskfile.Cmd{
Task: "another-task", Vars: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"PARAM1": {Static: "VALUE1"},
"PARAM2": {Static: "VALUE2"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"PARAM1": {Static: "VALUE1"},
"PARAM2": {Static: "VALUE2"},
},
[]string{"PARAM1", "PARAM2"},
),
},
},
},
Expand All @@ -55,9 +58,12 @@ vars:
&taskfile.Cmd{},
&taskfile.Cmd{
Task: "some_task", Vars: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"PARAM1": {Static: "var"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"PARAM1": {Static: "var"},
},
[]string{"PARAM1"},
),
},
Defer: true,
},
Expand All @@ -72,10 +78,13 @@ vars:
&taskfile.Dep{},
&taskfile.Dep{
Task: "another-task", Vars: &taskfile.Vars{
OrderedMap: orderedmap.FromMap(map[string]taskfile.Var{
"PARAM1": {Static: "VALUE1"},
"PARAM2": {Static: "VALUE2"},
}),
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
"PARAM1": {Static: "VALUE1"},
"PARAM2": {Static: "VALUE2"},
},
[]string{"PARAM1", "PARAM2"},
),
},
},
},
Expand Down

0 comments on commit fbe99d5

Please sign in to comment.