From bf3f419b6e3fe79d4da6beec31ab43cde045806c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 17 Nov 2022 01:11:06 +0100 Subject: [PATCH] cli/command/stack: TestConfigMergeInterpolation: various fixes - Make the package-level configMergeTests local to the test itself. - Rename fields to better describe intent - Remove some redundant variables - Reverse "expected" and "actual" fields for consistency - Use assert.Check() to not fail early Signed-off-by: Sebastiaan van Stijn --- cli/command/stack/config_test.go | 78 +++++++++++++++----------------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/cli/command/stack/config_test.go b/cli/command/stack/config_test.go index 1fa2f04510..b4f50160fd 100644 --- a/cli/command/stack/config_test.go +++ b/cli/command/stack/config_test.go @@ -17,29 +17,30 @@ func TestConfigWithEmptyComposeFile(t *testing.T) { assert.ErrorContains(t, cmd.Execute(), `Please specify a Compose file`) } -var configMergeTests = []struct { - name string - skipInterpolation bool - first string - second string - merged string -}{ - { - name: "With Interpolation", - skipInterpolation: false, - first: `version: "3.7" +func TestConfigMergeInterpolation(t *testing.T) { + tests := []struct { + name string + skipInterpolation bool + fileOne string + fileTwo string + expected string + }{ + { + name: "With Interpolation", + skipInterpolation: false, + fileOne: `version: "3.7" services: foo: image: busybox:latest command: cat file1.txt `, - second: `version: "3.7" + fileTwo: `version: "3.7" services: foo: image: busybox:${VERSION} command: cat file2.txt `, - merged: `version: "3.7" + expected: `version: "3.7" services: foo: command: @@ -47,23 +48,23 @@ services: - file2.txt image: busybox:1.0 `, - }, - { - name: "Without Interpolation", - skipInterpolation: true, - first: `version: "3.7" + }, + { + name: "Without Interpolation", + skipInterpolation: true, + fileOne: `version: "3.7" services: foo: image: busybox:latest command: cat file1.txt `, - second: `version: "3.7" + fileTwo: `version: "3.7" services: foo: image: busybox:${VERSION} command: cat file2.txt `, - merged: `version: "3.7" + expected: `version: "3.7" services: foo: command: @@ -71,34 +72,27 @@ services: - file2.txt image: busybox:${VERSION} `, - }, -} + }, + } -func TestConfigMergeInterpolation(t *testing.T) { - for _, tt := range configMergeTests { - t.Run(tt.name, func(t *testing.T) { - firstConfig := []byte(tt.first) - secondConfig := []byte(tt.second) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + firstConfigData, err := loader.ParseYAML([]byte(tc.fileOne)) + assert.Check(t, err) + secondConfigData, err := loader.ParseYAML([]byte(tc.fileTwo)) + assert.Check(t, err) - firstConfigData, err := loader.ParseYAML(firstConfig) - assert.NilError(t, err) - secondConfigData, err := loader.ParseYAML(secondConfig) - assert.NilError(t, err) - - env := map[string]string{ - "VERSION": "1.0", - } - - cfg, err := outputConfig(composetypes.ConfigDetails{ + actual, err := outputConfig(composetypes.ConfigDetails{ ConfigFiles: []composetypes.ConfigFile{ {Config: firstConfigData, Filename: "firstConfig"}, {Config: secondConfigData, Filename: "secondConfig"}, }, - Environment: env, - }, tt.skipInterpolation) - assert.NilError(t, err) - - assert.Equal(t, cfg, tt.merged) + Environment: map[string]string{ + "VERSION": "1.0", + }, + }, tc.skipInterpolation) + assert.Check(t, err) + assert.Equal(t, tc.expected, actual) }) } }