MCPcopy
hub / github.com/docker/compose / TestDisplayInterpolationVariables

Function TestDisplayInterpolationVariables

cmd/compose/options_test.go:210–272  ·  cmd/compose/options_test.go::TestDisplayInterpolationVariables
(t *testing.T)

Source from the content-addressed store, hash-verified

208}
209
210func TestDisplayInterpolationVariables(t *testing.T) {
211 ctrl := gomock.NewController(t)
212 defer ctrl.Finish()
213
214 tmpDir := t.TempDir()
215
216 // Create a temporary compose file
217 composeContent := `
218services:
219 app:
220 image: nginx
221 environment:
222 - TEST_VAR=${TEST_VAR:?required} # required with default
223 - API_KEY=${API_KEY:?} # required without default
224 - DEBUG=${DEBUG:-true} # optional with default
225 - UNSET_VAR # optional without default
226`
227 composePath := filepath.Join(tmpDir, "docker-compose.yml")
228 assert.NilError(t, os.WriteFile(composePath, []byte(composeContent), 0o644))
229
230 buf := new(bytes.Buffer)
231 cli := mocks.NewMockCli(ctrl)
232 cli.EXPECT().Out().Return(streams.NewOut(buf)).AnyTimes()
233
234 // Create ProjectOptions with the temporary compose file
235 projectOptions := &ProjectOptions{
236 ConfigPaths: []string{composePath},
237 }
238
239 // Set up the context with necessary environment variables
240 t.Setenv("TEST_VAR", "test-value")
241 t.Setenv("API_KEY", "123456")
242
243 // Extract variables from the model
244 info, noVariables, err := extractInterpolationVariablesFromModel(t.Context(), cli, projectOptions, []string{})
245 assert.NilError(t, err)
246 assert.Assert(t, noVariables == false)
247
248 // Display the variables
249 displayInterpolationVariables(cli.Out(), info)
250
251 // Expected output format with proper spacing
252 expected := "\nFound the following variables in configuration:\n" +
253 "VARIABLE VALUE SOURCE REQUIRED DEFAULT\n" +
254 "API_KEY 123456 environment yes \n" +
255 "DEBUG true compose file no true\n" +
256 "TEST_VAR test-value environment yes \n"
257
258 // Normalize spaces and newlines for comparison
259 normalizeSpaces := func(s string) string {
260 // Replace multiple spaces with a single space
261 s = strings.Join(strings.Fields(strings.TrimSpace(s)), " ")
262 return s
263 }
264
265 actualOutput := buf.String()
266
267 // Compare normalized strings

Callers

nothing calls this directly

Calls 8

OutMethod · 0.95
EXPECTMethod · 0.95
NewMockCliFunction · 0.92
TempDirMethod · 0.80
WriteFileMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected