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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-17 01:11:06 +01:00
parent 4d2fb68b93
commit bf3f419b6e
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 36 additions and 42 deletions

View File

@ -17,29 +17,30 @@ func TestConfigWithEmptyComposeFile(t *testing.T) {
assert.ErrorContains(t, cmd.Execute(), `Please specify a Compose file`) assert.ErrorContains(t, cmd.Execute(), `Please specify a Compose file`)
} }
var configMergeTests = []struct { func TestConfigMergeInterpolation(t *testing.T) {
name string tests := []struct {
skipInterpolation bool name string
first string skipInterpolation bool
second string fileOne string
merged string fileTwo string
}{ expected string
{ }{
name: "With Interpolation", {
skipInterpolation: false, name: "With Interpolation",
first: `version: "3.7" skipInterpolation: false,
fileOne: `version: "3.7"
services: services:
foo: foo:
image: busybox:latest image: busybox:latest
command: cat file1.txt command: cat file1.txt
`, `,
second: `version: "3.7" fileTwo: `version: "3.7"
services: services:
foo: foo:
image: busybox:${VERSION} image: busybox:${VERSION}
command: cat file2.txt command: cat file2.txt
`, `,
merged: `version: "3.7" expected: `version: "3.7"
services: services:
foo: foo:
command: command:
@ -47,23 +48,23 @@ services:
- file2.txt - file2.txt
image: busybox:1.0 image: busybox:1.0
`, `,
}, },
{ {
name: "Without Interpolation", name: "Without Interpolation",
skipInterpolation: true, skipInterpolation: true,
first: `version: "3.7" fileOne: `version: "3.7"
services: services:
foo: foo:
image: busybox:latest image: busybox:latest
command: cat file1.txt command: cat file1.txt
`, `,
second: `version: "3.7" fileTwo: `version: "3.7"
services: services:
foo: foo:
image: busybox:${VERSION} image: busybox:${VERSION}
command: cat file2.txt command: cat file2.txt
`, `,
merged: `version: "3.7" expected: `version: "3.7"
services: services:
foo: foo:
command: command:
@ -71,34 +72,27 @@ services:
- file2.txt - file2.txt
image: busybox:${VERSION} image: busybox:${VERSION}
`, `,
}, },
} }
func TestConfigMergeInterpolation(t *testing.T) { for _, tc := range tests {
for _, tt := range configMergeTests { t.Run(tc.name, func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { firstConfigData, err := loader.ParseYAML([]byte(tc.fileOne))
firstConfig := []byte(tt.first) assert.Check(t, err)
secondConfig := []byte(tt.second) secondConfigData, err := loader.ParseYAML([]byte(tc.fileTwo))
assert.Check(t, err)
firstConfigData, err := loader.ParseYAML(firstConfig) actual, err := outputConfig(composetypes.ConfigDetails{
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{
ConfigFiles: []composetypes.ConfigFile{ ConfigFiles: []composetypes.ConfigFile{
{Config: firstConfigData, Filename: "firstConfig"}, {Config: firstConfigData, Filename: "firstConfig"},
{Config: secondConfigData, Filename: "secondConfig"}, {Config: secondConfigData, Filename: "secondConfig"},
}, },
Environment: env, Environment: map[string]string{
}, tt.skipInterpolation) "VERSION": "1.0",
assert.NilError(t, err) },
}, tc.skipInterpolation)
assert.Equal(t, cfg, tt.merged) assert.Check(t, err)
assert.Equal(t, tc.expected, actual)
}) })
} }
} }