@@ -27,150 +27,196 @@ import (
2727func TestEnvPriority (t * testing.T ) {
2828 c := NewParallelCLI (t )
2929
30- projectDir := "./fixtures/environment/env-priority"
31-
3230 t .Run ("up" , func (t * testing.T ) {
3331 c .RunDockerOrExitError (t , "rmi" , "env-compose-priority" )
3432 c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose-with-env.yaml" ,
35- "--project-directory" , projectDir , " up" , "-d" , "--build" )
33+ "up" , "-d" , "--build" )
3634 })
3735
3836 // Full options activated
39- // 1. Compose file < -- Result expected
40- // 2. Shell environment variables
41- // 3. Environment file
42- // 4. Dockerfile
37+ // 1. Command Line (docker compose run --env <KEY[=VAL]>) < -- Result expected (From environment patched by --env-file)
38+ // 2. Compose File (service:: environment section)
39+ // 3. Compose File (service::env_file section file)
40+ // 4. Container Image ENV directive
4341 // 5. Variable is not defined
4442 t .Run ("compose file priority" , func (t * testing.T ) {
4543 cmd := c .NewDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose-with-env.yaml" ,
46- "--project-directory" , projectDir , "-- env-file" , "./fixtures/environment/env-priority/.env.override" , "run " ,
47- "--rm" , "-e" , "WHEREAMI" , "env-compose-priority" )
44+ "--env-file" , "./fixtures/environment/env-priority/.env.override" ,
45+ "run" , " --rm" , "-e" , "WHEREAMI" , "env-compose-priority" )
4846 cmd .Env = append (cmd .Env , "WHEREAMI=shell" )
4947 res := icmd .RunCmd (cmd )
50- assert .Equal (t , strings .TrimSpace (res .Stdout ()), "Compose File" )
48+ assert .Equal (t , strings .TrimSpace (res .Stdout ()), "override" )
49+ })
50+
51+ // Full options activated
52+ // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected
53+ // 2. Compose File (service::environment section)
54+ // 3. Compose File (service::env_file section file)
55+ // 4. Container Image ENV directive
56+ // 5. Variable is not defined
57+ t .Run ("compose file priority" , func (t * testing.T ) {
58+ cmd := c .NewDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose-with-env.yaml" ,
59+ "--env-file" , "./fixtures/environment/env-priority/.env.override" ,
60+ "run" , "--rm" , "-e" , "WHEREAMI=shell" , "env-compose-priority" )
61+ res := icmd .RunCmd (cmd )
62+ assert .Equal (t , strings .TrimSpace (res .Stdout ()), "shell" )
5163 })
5264
5365 // No Compose file, all other options
54- // 1. Compose file
55- // 2. Shell environment variables <-- Result expected
56- // 3. Environment file
57- // 4. Dockerfile
66+ // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by --env- file)
67+ // 2. Compose File (service:: environment section)
68+ // 3. Compose File (service::env_file section file)
69+ // 4. Container Image ENV directive
5870 // 5. Variable is not defined
5971 t .Run ("shell priority" , func (t * testing.T ) {
60- cmd := c .NewDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" , "--project-directory" ,
61- projectDir , "--env-file" , "./fixtures/environment/env-priority/.env.override" , "run" , "--rm" , "-e" ,
62- "WHEREAMI" , "env-compose-priority" )
72+ cmd := c .NewDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" ,
73+ "--env-file" , "./fixtures/environment/env-priority/.env.override" ,
74+ "run" , "--rm" , "-e" , "WHEREAMI" , "env-compose-priority" )
75+ cmd .Env = append (cmd .Env , "WHEREAMI=shell" )
76+ res := icmd .RunCmd (cmd )
77+ assert .Equal (t , strings .TrimSpace (res .Stdout ()), "override" )
78+ })
79+
80+ // No Compose file, all other options with env variable from OS environment
81+ // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment)
82+ // 2. Compose File (service::environment section)
83+ // 3. Compose File (service::env_file section file)
84+ // 4. Container Image ENV directive
85+ // 5. Variable is not defined
86+ t .Run ("shell priority file with default value" , func (t * testing.T ) {
87+ cmd := c .NewDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" ,
88+ "--env-file" , "./fixtures/environment/env-priority/.env.override.with.default" ,
89+ "run" , "--rm" , "-e" , "WHEREAMI" , "env-compose-priority" )
6390 cmd .Env = append (cmd .Env , "WHEREAMI=shell" )
6491 res := icmd .RunCmd (cmd )
6592 assert .Equal (t , strings .TrimSpace (res .Stdout ()), "shell" )
6693 })
6794
68- // No Compose file and env variable pass to the run command
69- // 1. Compose file
70- // 2. Shell environment variables <-- Result expected
71- // 3. Environment file
72- // 4. Dockerfile
95+ // No Compose file, all other options with env variable from OS environment
96+ // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment default value from file in --env-file)
97+ // 2. Compose File (service::environment section)
98+ // 3. Compose File (service::env_file section file)
99+ // 4. Container Image ENV directive
100+ // 5. Variable is not defined
101+ t .Run ("shell priority implicitly set" , func (t * testing.T ) {
102+ cmd := c .NewDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" ,
103+ "--env-file" , "./fixtures/environment/env-priority/.env.override.with.default" ,
104+ "run" , "--rm" , "-e" , "WHEREAMI" , "env-compose-priority" )
105+ res := icmd .RunCmd (cmd )
106+ assert .Equal (t , strings .TrimSpace (res .Stdout ()), "EnvFileDefaultValue" )
107+ })
108+
109+ // No Compose file and env variable pass to the run command
110+ // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected
111+ // 2. Compose File (service::environment section)
112+ // 3. Compose File (service::env_file section file)
113+ // 4. Container Image ENV directive
73114 // 5. Variable is not defined
74115 t .Run ("shell priority from run command" , func (t * testing.T ) {
75- res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" , "--project-directory" ,
76- projectDir , "--env-file" , "./fixtures/environment/env-priority/.env.override" , "run" , "--rm" , "-e " ,
77- "WHEREAMI=shell-run" , "env-compose-priority" )
116+ res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" ,
117+ "--env-file" , "./fixtures/environment/env-priority/.env.override" ,
118+ "run" , "--rm" , "-e" , " WHEREAMI=shell-run" , "env-compose-priority" )
78119 assert .Equal (t , strings .TrimSpace (res .Stdout ()), "shell-run" )
79120 })
80121
81- // No Compose file & no env variable but override env file
82- // 1. Compose file
83- // 2. Shell environment variables
84- // 3. Environment file <-- Result expected
85- // 4. Dockerfile
122+ // No Compose file & no env variable but override env file
123+ // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by .env as a default --env- file value)
124+ // 2. Compose File (service:: environment section)
125+ // 3. Compose File (service::env_file section file)
126+ // 4. Container Image ENV directive
86127 // 5. Variable is not defined
87128 t .Run ("override env file from compose" , func (t * testing.T ) {
88- res := c .RunDockerComposeCmd ("-f" , "./fixtures/environment/env-priority/compose-with-env-file.yaml" ,
89- "--project-directory" , projectDir ,
129+ res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose-with-env-file.yaml" ,
90130 "run" , "--rm" , "-e" , "WHEREAMI" , "env-compose-priority" )
91- assert .Equal (t , strings .TrimSpace (res .Stdout ()), "override " )
131+ assert .Equal (t , strings .TrimSpace (res .Stdout ()), "Env File " )
92132 })
93133
94- // No Compose file & no env variable but override env file
95- // 1. Compose file
96- // 2. Shell environment variables
97- // 3. Environment file <-- Result expected
98- // 4. Dockerfile
134+ // No Compose file & no env variable but override by default env file
135+ // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by --env- file value)
136+ // 2. Compose File (service:: environment section)
137+ // 3. Compose File (service::env_file section file)
138+ // 4. Container Image ENV directive
99139 // 5. Variable is not defined
100140 t .Run ("override env file" , func (t * testing.T ) {
101- res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" , "--project-directory" ,
102- projectDir , "--env-file" , "./fixtures/environment/env-priority/.env.override" , "run" , "--rm" , "-e " ,
103- "WHEREAMI" , "env-compose-priority" )
141+ res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" ,
142+ "--env-file" , "./fixtures/environment/env-priority/.env.override" ,
143+ "run" , "--rm" , "-e" , " WHEREAMI" , "env-compose-priority" )
104144 assert .Equal (t , strings .TrimSpace (res .Stdout ()), "override" )
105145 })
106146
107- // No Compose file & no env variable but override env file
108- // 1. Compose file
109- // 2. Shell environment variables
110- // 3. Environment file <-- Result expected
111- // 4. Dockerfile
147+ // No Compose file & no env variable but override env file
148+ // 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by --env- file value)
149+ // 2. Compose File (service:: environment section)
150+ // 3. Compose File (service::env_file section file)
151+ // 4. Container Image ENV directive
112152 // 5. Variable is not defined
113153 t .Run ("env file" , func (t * testing.T ) {
114- res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" , "--project-directory" ,
115- projectDir , "run" , "--rm" , "-e" , "WHEREAMI" , "env-compose-priority" )
154+ res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" ,
155+ "run" , "--rm" , "-e" , "WHEREAMI" , "env-compose-priority" )
116156 assert .Equal (t , strings .TrimSpace (res .Stdout ()), "Env File" )
117157 })
118158
119- // No Compose file & no env variable, using an empty override env file
120- // 1. Compose file
121- // 2. Shell environment variables
122- // 3. Environment file
123- // 4. Dockerfile <-- Result expected
159+ // No Compose file & no env variable, using an empty override env file
160+ // 1. Command Line (docker compose run --env <KEY[=VAL]>)
161+ // 2. Compose File (service:: environment section)
162+ // 3. Compose File (service::env_file section file)
163+ // 4. Container Image ENV directive <-- Result expected
124164 // 5. Variable is not defined
125165 t .Run ("use Dockerfile" , func (t * testing.T ) {
126- res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" , "--project-directory" ,
127- projectDir , "--env-file" , "./fixtures/environment/env-priority/.env.empty" , "run" , "--rm" , "-e" , "WHEREAMI " ,
128- "env-compose-priority" )
166+ res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" ,
167+ "--env-file" , "./fixtures/environment/env-priority/.env.empty" ,
168+ "run" , "--rm" , "-e" , "WHEREAMI" , " env-compose-priority" )
129169 assert .Equal (t , strings .TrimSpace (res .Stdout ()), "Dockerfile" )
130170 })
131171
132172 t .Run ("down" , func (t * testing.T ) {
133- c .RunDockerComposeCmd (t , "--project-directory " , projectDir , "down" )
173+ c .RunDockerComposeCmd (t , "--project-name " , "env-priority" , "down" )
134174 })
135175}
136176
137177func TestEnvInterpolation (t * testing.T ) {
138178 c := NewParallelCLI (t )
139179
140- projectDir := "./fixtures/environment/env-interpolation"
141-
142- // No variable defined in the Compose file and env variable pass to the run command
143- // 1. Compose file
144- // 2. Shell environment variables <-- Result expected
145- // 3. Environment file
146- // 4. Dockerfile
180+ // No variable defined in the Compose file and nor env variable pass to the run command
181+ // 1. Command Line (docker compose run --env <KEY[=VAL]>)
182+ // 2. Compose File (service::environment section) <-- Result expected (From environment patched by .env as a default --env-file value)
183+ // 3. Compose File (service::env_file section file)
184+ // 4. Container Image ENV directive
147185 // 5. Variable is not defined
148186 t .Run ("shell priority from run command" , func (t * testing.T ) {
149- cmd := c .NewDockerComposeCmd (t , "-f" , "./fixtures/environment/env-interpolation/compose.yaml" ,
150- "--project-directory" , projectDir , "config" )
187+ cmd := c .NewDockerComposeCmd (t , "-f" , "./fixtures/environment/env-interpolation/compose.yaml" , "config" )
151188 cmd .Env = append (cmd .Env , "WHEREAMI=shell" )
152189 res := icmd .RunCmd (cmd )
153- res .Assert (t , icmd.Expected {Out : `IMAGE: default_env:shell` })
190+ res .Assert (t , icmd.Expected {Out : `IMAGE: default_env:EnvFile` })
191+ })
192+
193+ // No variable defined in the Compose file and env variable pass to the run command
194+ // 1. Command Line (docker compose run --env <KEY[=VAL]>)
195+ // 2. Compose File (service::environment section) <-- Result expected (From environment patched by .env as a default --env-file value.
196+ // This variable has a default value in case of an absent variable in the OS environment)
197+ // 3. Compose File (service::env_file section file)
198+ // 4. Container Image ENV directive
199+ // 5. Variable is not defined
200+ t .Run ("shell priority from run command using default value fallback" , func (t * testing.T ) {
201+ c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-interpolation-default-value/compose.yaml" , "config" ).
202+ Assert (t , icmd.Expected {Out : `IMAGE: default_env:EnvFileDefaultValue` })
154203 })
155204}
156205
157206func TestCommentsInEnvFile (t * testing.T ) {
158207 c := NewParallelCLI (t )
159208
160- projectDir := "./fixtures/environment/env-file-comments"
161-
162209 t .Run ("comments in env files" , func (t * testing.T ) {
163210 c .RunDockerOrExitError (t , "rmi" , "env-file-comments" )
164211
165- c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-file-comments/compose.yaml" , "--project-directory" ,
166- projectDir , "up" , "-d" , "--build" )
212+ c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-file-comments/compose.yaml" , "up" , "-d" , "--build" )
167213
168214 res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-file-comments/compose.yaml" ,
169- "--project-directory" , projectDir , " run" , "--rm" , "-e" , "COMMENT" , "-e" , "NO_COMMENT" , "env-file-comments" )
215+ "run" , "--rm" , "-e" , "COMMENT" , "-e" , "NO_COMMENT" , "env-file-comments" )
170216
171217 res .Assert (t , icmd.Expected {Out : `COMMENT=1234` })
172218 res .Assert (t , icmd.Expected {Out : `NO_COMMENT=1234#5` })
173219
174- c .RunDockerComposeCmd (t , "--project-directory " , projectDir , "down" , "--rmi" , "all" )
220+ c .RunDockerComposeCmd (t , "--project-name " , "env-file-comments" , "down" , "--rmi" , "all" )
175221 })
176222}
0 commit comments