MCPcopy Create free account
hub / github.com/compose-spec/compose-go / TestEnvVariablePrecedence

Function TestEnvVariablePrecedence

cli/options_test.go:345–386  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

343}
344
345func TestEnvVariablePrecedence(t *testing.T) {
346 testcases := []struct {
347 name string
348 dotEnv string
349 osEnv []string
350 expected types.Mapping
351 }{
352 {
353 "no value set in environment",
354 "FOO=foo\nBAR=${FOO}",
355 nil,
356 types.Mapping{
357 "FOO": "foo",
358 "BAR": "foo",
359 },
360 },
361 {
362 "conflict with value set in environment",
363 "FOO=foo\nBAR=${FOO}",
364 []string{"FOO=zot"},
365 types.Mapping{
366 "FOO": "zot",
367 "BAR": "zot",
368 },
369 },
370 }
371
372 for _, test := range testcases {
373 t.Run(test.name, func(t *testing.T) {
374 wd := t.TempDir()
375 err := os.WriteFile(filepath.Join(wd, ".env"), []byte(test.dotEnv), 0o700)
376 assert.NilError(t, err)
377 options, err := NewProjectOptions(nil,
378 // First load os.Env variable, higher in precedence rule
379 WithEnv(test.osEnv),
380 // Then load dotEnv file
381 WithWorkingDirectory(wd), WithEnvFiles(), WithDotEnv)
382 assert.NilError(t, err)
383 assert.DeepEqual(t, test.expected, options.Environment)
384 })
385 }
386}

Callers

nothing calls this directly

Calls 4

NewProjectOptionsFunction · 0.85
WithEnvFunction · 0.85
WithWorkingDirectoryFunction · 0.85
WithEnvFilesFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…