@@ -27,137 +27,196 @@ import (
27
27
func TestEnvPriority (t * testing.T ) {
28
28
c := NewParallelCLI (t )
29
29
30
- projectDir := "./fixtures/environment/env-priority"
31
-
32
30
t .Run ("up" , func (t * testing.T ) {
33
31
c .RunDockerOrExitError (t , "rmi" , "env-compose-priority" )
34
32
c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose-with-env.yaml" ,
35
- "--project-directory" , projectDir , " up" , "-d" , "--build" )
33
+ "up" , "-d" , "--build" )
36
34
})
37
35
38
36
// 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
43
41
// 5. Variable is not defined
44
42
t .Run ("compose file priority" , func (t * testing.T ) {
45
43
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" )
48
46
cmd .Env = append (cmd .Env , "WHEREAMI=shell" )
49
47
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" )
51
63
})
52
64
53
65
// 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
58
70
// 5. Variable is not defined
59
71
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" )
63
90
cmd .Env = append (cmd .Env , "WHEREAMI=shell" )
64
91
res := icmd .RunCmd (cmd )
65
92
assert .Equal (t , strings .TrimSpace (res .Stdout ()), "shell" )
66
93
})
67
94
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
73
114
// 5. Variable is not defined
74
115
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" )
78
119
assert .Equal (t , strings .TrimSpace (res .Stdout ()), "shell-run" )
79
120
})
80
121
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
127
+ // 5. Variable is not defined
128
+ t .Run ("override env file from compose" , func (t * testing.T ) {
129
+ res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose-with-env-file.yaml" ,
130
+ "run" , "--rm" , "-e" , "WHEREAMI" , "env-compose-priority" )
131
+ assert .Equal (t , strings .TrimSpace (res .Stdout ()), "Env File" )
132
+ })
133
+
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
86
139
// 5. Variable is not defined
87
140
t .Run ("override env file" , func (t * testing.T ) {
88
- res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" , "--project-directory" ,
89
- projectDir , "--env-file" , "./fixtures/environment/env-priority/.env.override" , "run" , "--rm" , "-e " ,
90
- "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" )
91
144
assert .Equal (t , strings .TrimSpace (res .Stdout ()), "override" )
92
145
})
93
146
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
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
99
152
// 5. Variable is not defined
100
153
t .Run ("env file" , func (t * testing.T ) {
101
- res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" , "--project-directory" ,
102
- 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" )
103
156
assert .Equal (t , strings .TrimSpace (res .Stdout ()), "Env File" )
104
157
})
105
158
106
- // No Compose file & no env variable, using an empty override env file
107
- // 1. Compose file
108
- // 2. Shell environment variables
109
- // 3. Environment file
110
- // 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
111
164
// 5. Variable is not defined
112
165
t .Run ("use Dockerfile" , func (t * testing.T ) {
113
- res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-priority/compose.yaml" , "--project-directory" ,
114
- projectDir , "--env-file" , "./fixtures/environment/env-priority/.env.empty" , "run" , "--rm" , "-e" , "WHEREAMI " ,
115
- "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" )
116
169
assert .Equal (t , strings .TrimSpace (res .Stdout ()), "Dockerfile" )
117
170
})
118
171
119
172
t .Run ("down" , func (t * testing.T ) {
120
- c .RunDockerComposeCmd (t , "--project-directory " , projectDir , "down" )
173
+ c .RunDockerComposeCmd (t , "--project-name " , "env-priority" , "down" )
121
174
})
122
175
}
123
176
124
177
func TestEnvInterpolation (t * testing.T ) {
125
178
c := NewParallelCLI (t )
126
179
127
- projectDir := "./fixtures/environment/env-interpolation"
128
-
129
- // No variable defined in the Compose file and env variable pass to the run command
130
- // 1. Compose file
131
- // 2. Shell environment variables <-- Result expected
132
- // 3. Environment file
133
- // 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
134
185
// 5. Variable is not defined
135
186
t .Run ("shell priority from run command" , func (t * testing.T ) {
136
- cmd := c .NewDockerComposeCmd (t , "-f" , "./fixtures/environment/env-interpolation/compose.yaml" ,
137
- "--project-directory" , projectDir , "config" )
187
+ cmd := c .NewDockerComposeCmd (t , "-f" , "./fixtures/environment/env-interpolation/compose.yaml" , "config" )
138
188
cmd .Env = append (cmd .Env , "WHEREAMI=shell" )
139
189
res := icmd .RunCmd (cmd )
140
- 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` })
141
203
})
142
204
}
143
205
144
206
func TestCommentsInEnvFile (t * testing.T ) {
145
207
c := NewParallelCLI (t )
146
208
147
- projectDir := "./fixtures/environment/env-file-comments"
148
-
149
209
t .Run ("comments in env files" , func (t * testing.T ) {
150
210
c .RunDockerOrExitError (t , "rmi" , "env-file-comments" )
151
211
152
- c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-file-comments/compose.yaml" , "--project-directory" ,
153
- projectDir , "up" , "-d" , "--build" )
212
+ c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-file-comments/compose.yaml" , "up" , "-d" , "--build" )
154
213
155
214
res := c .RunDockerComposeCmd (t , "-f" , "./fixtures/environment/env-file-comments/compose.yaml" ,
156
- "--project-directory" , projectDir , " run" , "--rm" , "-e" , "COMMENT" , "-e" , "NO_COMMENT" , "env-file-comments" )
215
+ "run" , "--rm" , "-e" , "COMMENT" , "-e" , "NO_COMMENT" , "env-file-comments" )
157
216
158
217
res .Assert (t , icmd.Expected {Out : `COMMENT=1234` })
159
218
res .Assert (t , icmd.Expected {Out : `NO_COMMENT=1234#5` })
160
219
161
- c .RunDockerComposeCmd (t , "--project-directory " , projectDir , "down" , "--rmi" , "all" )
220
+ c .RunDockerComposeCmd (t , "--project-name " , "env-file-comments" , "down" , "--rmi" , "all" )
162
221
})
163
222
}
0 commit comments