Skip to content

Commit

Permalink
Add combos tests
Browse files Browse the repository at this point in the history
Fix context tests
Fix context cloning
Fix getContext() to discard layers
  • Loading branch information
silphid committed Jul 25, 2021
1 parent ebfb487 commit d07ea8e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 39 deletions.
29 changes: 0 additions & 29 deletions src/cmd/tidy/tidy_test.go

This file was deleted.

55 changes: 55 additions & 0 deletions src/internal/combos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package yey

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetCombos(t *testing.T) {
ctx := Context{
Layers: Layers{
Layer{
Name: "layer1",
Contexts: map[string]Context{
"dev": {
Name: "dev",
Layers: Layers{
Layer{
Name: "childLayer",
Contexts: map[string]Context{
"dev1": {Name: "dev1"},
"dev2": {Name: "dev2"},
},
},
},
},
"stg": {Name: "stg"},
"prod": {Name: "prod"},
},
},
Layer{
Name: "layer2",
Contexts: map[string]Context{
"go": {Name: "go"},
"node": {Name: "node"},
},
},
},
}

expected := [][]string{
{"dev", "dev1", "go"},
{"dev", "dev1", "node"},
{"dev", "dev2", "go"},
{"dev", "dev2", "node"},
{"prod", "go"},
{"prod", "node"},
{"stg", "go"},
{"stg", "node"},
}

actual := ctx.GetCombos()

assert.Equal(t, expected, actual)
}
2 changes: 2 additions & 0 deletions src/internal/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (c Context) getContextRecursively(names []string) (Context, []string, error

// Merge layer context
layerContext, ok := layer.Contexts[name]
layerContext.Layers = nil
if !ok {
return Context{}, nil, fmt.Errorf("context %q not found in layer %q", name, layer.Name)
}
Expand All @@ -132,6 +133,7 @@ func (c Context) getContextRecursively(names []string) (Context, []string, error
}
}

ctx.Layers = nil
return ctx, names, nil
}

Expand Down
19 changes: 10 additions & 9 deletions src/internal/contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (
)

func loadContexts(baseFile, ctx1Key, ctx1File, ctx2Key, ctx2File string) Contexts {
return Contexts{
ctx := Contexts{
Context: loadContext(baseFile),
Layers: Layers{
Layer{
Name: "layerName",
Contexts: map[string]Context{
ctx1Key: loadContext(ctx1File),
ctx2Key: loadContext(ctx2File),
},
}
ctx.Layers = Layers{
Layer{
Name: "layer",
Contexts: map[string]Context{
ctx1Key: loadContext(ctx1File),
ctx2Key: loadContext(ctx2File),
},
},
}
return ctx
}

func TestGetContext(t *testing.T) {
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestGetContext(t *testing.T) {
},
{
name: "unknown",
error: `context "unknown" not found in layer "layerName"`,
error: `context "unknown" not found in layer "layer"`,
},
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Layers []Layer

// Clone returns a deep-copy of this layer
func (l Layers) Clone() Layers {
clone := Layers{}
var clone Layers
for _, layer := range l {
clone = append(clone, layer.Clone())
}
Expand Down

0 comments on commit d07ea8e

Please sign in to comment.