Skip to content

Commit 0504a5f

Browse files
committed
Add defensive check in TestMain to ensure environment variables are always set
Add check to ensure sharedContainer is not nil before setting environment variables. This helps catch issues where container creation succeeds but returns nil.
1 parent 1dffc80 commit 0504a5f

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

mysql/testcontainers_testmain.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,16 @@ func TestMain(m *testing.M) {
7373
}
7474

7575
// Set up environment variables for the shared container
76-
os.Setenv("MYSQL_ENDPOINT", sharedContainer.Endpoint)
77-
os.Setenv("MYSQL_USERNAME", sharedContainer.Username)
78-
os.Setenv("MYSQL_PASSWORD", sharedContainer.Password)
76+
// These MUST be set even if sharedContainer is nil (shouldn't happen, but be defensive)
77+
if sharedContainer != nil {
78+
os.Setenv("MYSQL_ENDPOINT", sharedContainer.Endpoint)
79+
os.Setenv("MYSQL_USERNAME", sharedContainer.Username)
80+
os.Setenv("MYSQL_PASSWORD", sharedContainer.Password)
81+
} else {
82+
// This should never happen, but if it does, fail loudly
83+
os.Stderr.WriteString(fmt.Sprintf("ERROR: startSharedMySQLContainer returned nil container without error for image '%s'\n", dockerImage))
84+
os.Exit(1)
85+
}
7986

8087
// Run all tests
8188
code := m.Run()

0 commit comments

Comments
 (0)