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`)
}
var configMergeTests = []struct {
func TestConfigMergeInterpolation(t *testing.T) {
tests := []struct {
name string
skipInterpolation bool
first string
second string
merged string
fileOne string
fileTwo string
expected string
}{
{
name: "With Interpolation",
skipInterpolation: false,
first: `version: "3.7"
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:
@ -51,19 +52,19 @@ services:
{
name: "Without Interpolation",
skipInterpolation: true,
first: `version: "3.7"
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:
@ -74,31 +75,24 @@ services:
},
}
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)
})
}
}