From c8c52d918a6dd0cd1c89fc7c2267d2ef3425d154 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Fri, 24 May 2024 00:22:15 +0200 Subject: [PATCH] test: clean up tests for composer (#2532) ## Description Cleans up the composer tests to the newer testing style. Also marks test helper functions as helpers. ## Related Issue Relates to #2512 ## Checklist before merging - [x] Test, docs, adr added or updated as needed - [x] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/.github/CONTRIBUTING.md#developer-workflow) followed --------- Co-authored-by: schristoff <167717759+schristoff-du@users.noreply.github.com> Co-authored-by: razzle Co-authored-by: Lucas Rodriguez --- src/pkg/packager/composer/extensions.go | 14 --- src/pkg/packager/composer/list.go | 3 +- src/pkg/packager/composer/list_test.go | 125 ++++++++++-------------- 3 files changed, 56 insertions(+), 86 deletions(-) delete mode 100644 src/pkg/packager/composer/extensions.go diff --git a/src/pkg/packager/composer/extensions.go b/src/pkg/packager/composer/extensions.go deleted file mode 100644 index 77ac73c038..0000000000 --- a/src/pkg/packager/composer/extensions.go +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2021-Present The Zarf Authors - -// Package composer contains functions for composing components within Zarf packages. -package composer - -import ( - "github.com/defenseunicorns/zarf/src/extensions/bigbang" - "github.com/defenseunicorns/zarf/src/types" -) - -func composeExtensions(c *types.ZarfComponent, override types.ZarfComponent, relativeTo string) { - bigbang.Compose(c, override, relativeTo) -} diff --git a/src/pkg/packager/composer/list.go b/src/pkg/packager/composer/list.go index 3642ec562f..4f1a484ca3 100644 --- a/src/pkg/packager/composer/list.go +++ b/src/pkg/packager/composer/list.go @@ -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" @@ -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 } diff --git a/src/pkg/packager/composer/list_test.go b/src/pkg/packager/composer/list_test.go index 73e84a85fa..622647357c 100644 --- a/src/pkg/packager/composer/list_test.go +++ b/src/pkg/packager/composer/list_test.go @@ -19,19 +19,17 @@ import ( func TestNewImportChain(t *testing.T) { t.Parallel() - type testCase struct { - name string - head types.ZarfComponent - arch string - flavor string - expectedErrorMessage string - } - - testCases := []testCase{ + tests := []struct { + name string + head types.ZarfComponent + arch string + flavor string + expectedErr string + }{ { - name: "No Architecture", - head: types.ZarfComponent{}, - expectedErrorMessage: "architecture must be provided", + name: "No Architecture", + head: types.ZarfComponent{}, + expectedErr: "architecture must be provided", }, { name: "Circular Import", @@ -40,19 +38,18 @@ func TestNewImportChain(t *testing.T) { Path: ".", }, }, - arch: "amd64", - expectedErrorMessage: "detected circular import chain", + arch: "amd64", + expectedErr: "detected circular import chain", }, } 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.ErrorContains(t, err, tt.expectedErr) }) } } @@ -60,14 +57,6 @@ func TestNewImportChain(t *testing.T) { 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) @@ -76,27 +65,29 @@ func TestCompose(t *testing.T) { secondDirectoryActionDefault := filepath.Join(firstDirectory, "world-dc") firstDirectoryActionDefault := "hello-dc" - testCases := []testCase{ + tests := []struct { + name string + ic *ImportChain + expectedComposed types.ZarfComponent + }{ { name: "Single Component", - ic: createChainFromSlice([]types.ZarfComponent{ + ic: createChainFromSlice(t, []types.ZarfComponent{ { Name: "no-import", }, }), - returnError: false, expectedComposed: types.ZarfComponent{ Name: "no-import", }, }, { 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{ Name: "import-hello", // Files should always be appended with corrected directories @@ -243,19 +234,14 @@ 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) - } else { - require.EqualValues(t, &testCase.expectedComposed, composed) - } + composed, err := tt.ic.Compose() + require.NoError(t, err) + require.EqualValues(t, &tt.expectedComposed, composed) }) } } @@ -263,15 +249,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{ { @@ -316,7 +293,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{}, @@ -425,41 +409,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{