Skip to content

Commit

Permalink
test: clean up tests for composer
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba committed May 23, 2024
1 parent 42e1bf9 commit 7776c76
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 71 deletions.
14 changes: 0 additions & 14 deletions src/pkg/packager/composer/extensions.go

This file was deleted.

3 changes: 2 additions & 1 deletion src/pkg/packager/composer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/zarf/src/extensions/bigbang"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/packager/deprecated"
"github.com/defenseunicorns/zarf/src/pkg/utils"
Expand Down Expand Up @@ -303,7 +304,7 @@ func (ic *ImportChain) Compose() (composed *types.ZarfComponent, err error) {
overrideResources(composed, node.ZarfComponent)
overrideActions(composed, node.ZarfComponent)

composeExtensions(composed, node.ZarfComponent, node.relativeToHead)
bigbang.Compose(composed, node.ZarfComponent, node.relativeToHead)

node = node.prev
}
Expand Down
103 changes: 47 additions & 56 deletions src/pkg/packager/composer/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ import (
func TestNewImportChain(t *testing.T) {
t.Parallel()

type testCase struct {
tests := []struct {
name string
head types.ZarfComponent
arch string
flavor string
expectedErrorMessage string
}

testCases := []testCase{
}{
{
name: "No Architecture",
head: types.ZarfComponent{},
Expand All @@ -45,29 +43,20 @@ func TestNewImportChain(t *testing.T) {
},
}
testPackageName := "test-package"
for _, testCase := range testCases {
testCase := testCase

t.Run(testCase.name, func(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

_, err := NewImportChain(testCase.head, 0, testPackageName, testCase.arch, testCase.flavor)
require.Contains(t, err.Error(), testCase.expectedErrorMessage)
_, err := NewImportChain(tt.head, 0, testPackageName, tt.arch, tt.flavor)
require.Contains(t, err.Error(), tt.expectedErrorMessage)
})
}
}

func TestCompose(t *testing.T) {
t.Parallel()

type testCase struct {
name string
ic *ImportChain
returnError bool
expectedComposed types.ZarfComponent
expectedErrorMessage string
}

firstDirectory := "hello"
secondDirectory := "world"
finalDirectory := filepath.Join(firstDirectory, secondDirectory)
Expand All @@ -76,10 +65,16 @@ func TestCompose(t *testing.T) {
secondDirectoryActionDefault := filepath.Join(firstDirectory, "world-dc")
firstDirectoryActionDefault := "hello-dc"

testCases := []testCase{
tests := []struct {
name string
ic *ImportChain
returnError bool
expectedComposed types.ZarfComponent
expectedErrorMessage string
}{
{
name: "Single Component",
ic: createChainFromSlice([]types.ZarfComponent{
ic: createChainFromSlice(t, []types.ZarfComponent{
{
Name: "no-import",
},
Expand All @@ -91,10 +86,10 @@ func TestCompose(t *testing.T) {
},
{
name: "Multiple Components",
ic: createChainFromSlice([]types.ZarfComponent{
createDummyComponent("hello", firstDirectory, "hello"),
createDummyComponent("world", secondDirectory, "world"),
createDummyComponent("today", "", "hello"),
ic: createChainFromSlice(t, []types.ZarfComponent{
createDummyComponent(t, "hello", firstDirectory, "hello"),
createDummyComponent(t, "world", secondDirectory, "world"),
createDummyComponent(t, "today", "", "hello"),
}),
returnError: false,
expectedComposed: types.ZarfComponent{
Expand Down Expand Up @@ -244,17 +239,16 @@ func TestCompose(t *testing.T) {
},
}

for _, testCase := range testCases {
testCase := testCase

t.Run(testCase.name, func(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

composed, err := testCase.ic.Compose()
if testCase.returnError {
require.Contains(t, err.Error(), testCase.expectedErrorMessage)
composed, err := tt.ic.Compose()
if tt.returnError {
require.Contains(t, err.Error(), tt.expectedErrorMessage)
} else {
require.EqualValues(t, &testCase.expectedComposed, composed)
require.EqualValues(t, &tt.expectedComposed, composed)
}
})
}
Expand All @@ -263,15 +257,6 @@ func TestCompose(t *testing.T) {
func TestMerging(t *testing.T) {
t.Parallel()

type testCase struct {
name string
ic *ImportChain
existingVars []variables.InteractiveVariable
existingConsts []variables.Constant
expectedVars []variables.InteractiveVariable
expectedConsts []variables.Constant
}

head := Node{
vars: []variables.InteractiveVariable{
{
Expand Down Expand Up @@ -316,7 +301,14 @@ func TestMerging(t *testing.T) {
tail.prev = &head
testIC := &ImportChain{head: &head, tail: &tail}

testCases := []testCase{
tests := []struct {
name string
ic *ImportChain
existingVars []variables.InteractiveVariable
existingConsts []variables.Constant
expectedVars []variables.InteractiveVariable
expectedConsts []variables.Constant
}{
{
name: "empty-ic",
ic: &ImportChain{},
Expand Down Expand Up @@ -425,41 +417,40 @@ func TestMerging(t *testing.T) {
},
}

for _, testCase := range testCases {
testCase := testCase

t.Run(testCase.name, func(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

mergedVars := testCase.ic.MergeVariables(testCase.existingVars)
require.EqualValues(t, testCase.expectedVars, mergedVars)
mergedVars := tt.ic.MergeVariables(tt.existingVars)
require.EqualValues(t, tt.expectedVars, mergedVars)

mergedConsts := testCase.ic.MergeConstants(testCase.existingConsts)
require.EqualValues(t, testCase.expectedConsts, mergedConsts)
mergedConsts := tt.ic.MergeConstants(tt.existingConsts)
require.EqualValues(t, tt.expectedConsts, mergedConsts)
})
}
}

func createChainFromSlice(components []types.ZarfComponent) (ic *ImportChain) {
func createChainFromSlice(t *testing.T, components []types.ZarfComponent) (ic *ImportChain) {
t.Helper()

ic = &ImportChain{}
testPackageName := "test-package"

if len(components) == 0 {
return ic
}

ic.append(components[0], 0, testPackageName, ".", nil, nil)
history := []string{}

for idx := 1; idx < len(components); idx++ {
history = append(history, components[idx-1].Import.Path)
ic.append(components[idx], idx, testPackageName, filepath.Join(history...), nil, nil)
}

return ic
}

func createDummyComponent(name, importDir, subName string) types.ZarfComponent {
func createDummyComponent(t *testing.T, name, importDir, subName string) types.ZarfComponent {
t.Helper()

return types.ZarfComponent{
Name: fmt.Sprintf("import-%s", name),
Import: types.ZarfComponentImport{
Expand Down

0 comments on commit 7776c76

Please sign in to comment.