From d07ea8ea093070aa7de49cf78a1a5240cba01089 Mon Sep 17 00:00:00 2001 From: Mathieu Frenette Date: Sun, 25 Jul 2021 14:26:46 -0400 Subject: [PATCH] Add combos tests Fix context tests Fix context cloning Fix getContext() to discard layers --- src/cmd/tidy/tidy_test.go | 29 ------------------ src/internal/combos_test.go | 55 +++++++++++++++++++++++++++++++++++ src/internal/context.go | 2 ++ src/internal/contexts_test.go | 19 ++++++------ src/internal/layers.go | 2 +- 5 files changed, 68 insertions(+), 39 deletions(-) delete mode 100644 src/cmd/tidy/tidy_test.go create mode 100644 src/internal/combos_test.go diff --git a/src/cmd/tidy/tidy_test.go b/src/cmd/tidy/tidy_test.go deleted file mode 100644 index 07e0279..0000000 --- a/src/cmd/tidy/tidy_test.go +++ /dev/null @@ -1,29 +0,0 @@ -package tidy - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestForEachPossibleNameCombination(t *testing.T) { - allNames := [][]string{ - {"dev", "stg", "prod"}, - {"go", "node"}, - } - expected := [][]string{ - {"dev", "go"}, - {"dev", "node"}, - {"stg", "go"}, - {"stg", "node"}, - {"prod", "go"}, - {"prod", "node"}, - } - var actual [][]string - forEachPossibleNameCombination(allNames, nil, func(combo []string) error { - actual = append(actual, combo) - return nil - }) - - assert.Equal(t, expected, actual) -} diff --git a/src/internal/combos_test.go b/src/internal/combos_test.go new file mode 100644 index 0000000..a3cd53e --- /dev/null +++ b/src/internal/combos_test.go @@ -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) +} diff --git a/src/internal/context.go b/src/internal/context.go index fe11bc0..eeec2d7 100644 --- a/src/internal/context.go +++ b/src/internal/context.go @@ -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) } @@ -132,6 +133,7 @@ func (c Context) getContextRecursively(names []string) (Context, []string, error } } + ctx.Layers = nil return ctx, names, nil } diff --git a/src/internal/contexts_test.go b/src/internal/contexts_test.go index 7264cda..a85c629 100644 --- a/src/internal/contexts_test.go +++ b/src/internal/contexts_test.go @@ -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) { @@ -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"`, }, } diff --git a/src/internal/layers.go b/src/internal/layers.go index b6bd4df..08a7087 100644 --- a/src/internal/layers.go +++ b/src/internal/layers.go @@ -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()) }