@@ -83,3 +83,57 @@ services:
83
83
assert .NilError (t , err )
84
84
assert .Equal (t , "hi\n " , string (testB ))
85
85
}
86
+
87
+ func TestComposeUpEnvFile (t * testing.T ) {
88
+ if runtime .GOOS == "windows" {
89
+ t .Skip ("Skipping test on Windows" )
90
+ }
91
+
92
+ base := testutil .NewBase (t )
93
+
94
+ // Create a temporary directory for the test
95
+ tmpDir := t .TempDir ()
96
+ fmt .Printf ("Created temporary directory: %s\n " , tmpDir )
97
+
98
+ // Create an .env file
99
+ envFilePath := filepath .Join (tmpDir , ".env" )
100
+ envFileContent := `
101
+ TEST_VAR1=Hello
102
+ TEST_VAR2=World
103
+ `
104
+ err := os .WriteFile (envFilePath , []byte (envFileContent ), 0644 )
105
+ assert .NilError (t , err )
106
+ fmt .Printf ("Created .env file at: %s\n " , envFilePath )
107
+ fmt .Printf ("Env file content:\n %s\n " , envFileContent )
108
+
109
+ // Create docker-compose.yml
110
+ dockerComposeYAML := fmt .Sprintf (`
111
+ version: '3'
112
+ services:
113
+ test:
114
+ image: %s
115
+ command: sh -c 'echo $TEST_VAR1 $TEST_VAR2 > /tmp/test_output'
116
+ ` , testutil .CommonImage )
117
+
118
+ comp := testutil .NewComposeDir (t , dockerComposeYAML )
119
+ defer comp .CleanUp ()
120
+ fmt .Printf ("Created docker-compose.yml at: %s\n " , comp .YAMLFullPath ())
121
+ fmt .Printf ("Docker Compose YAML content:\n %s\n " , dockerComposeYAML )
122
+
123
+ // Run compose up with env-file
124
+ upCmd := base .ComposeCmd ("-f" , comp .YAMLFullPath (), "--env-file" , envFilePath , "up" )
125
+ upCmd .AssertOK ()
126
+ defer base .ComposeCmd ("-f" , comp .YAMLFullPath (), "down" ).AssertOK ()
127
+
128
+ // Check if the environment variables were correctly passed and used
129
+ containerID := base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "-q" ).OutLines ()[0 ]
130
+ fmt .Printf ("Container ID: %s\n " , containerID )
131
+
132
+ execCmd := base .Cmd ("exec" , containerID , "cat" , "/tmp/test_output" )
133
+ out := execCmd .Out ()
134
+
135
+ fmt .Printf ("Command output: %s\n " , out )
136
+
137
+ assert .Equal (t , "Hello World\n " , out )
138
+ fmt .Println ("Test completed successfully" )
139
+ }
0 commit comments