@@ -8,6 +8,14 @@ import (
88 "time"
99)
1010
11+ type testEnvState struct {
12+ env map [string ]* string
13+ }
14+
15+ func (env * testEnvState ) handleChange (variables map [string ]* string ) {
16+ env .env = variables
17+ }
18+
1119func TestShouldUpdateDotEnvOnChange (t * testing.T ) {
1220 context := test .NewTestDir (t )
1321 envFilePath := filepath .Join (context .Path , "../config/.env" )
@@ -18,19 +26,22 @@ func TestShouldUpdateDotEnvOnChange(t *testing.T) {
1826 t .Cleanup (func () {
1927 fileWatcher .Close ()
2028 })
21- var result map [string ]* string
22- env := CreateDotEnv (context .Path , func (variables map [string ]* string ) {
23- result = variables
24- })
29+ testEnv := & testEnvState {make (map [string ]* string )}
30+ env := CreateDotEnv (context .Path , testEnv .handleChange )
2531 err := fileWatcher .Watch (env )
2632 test .AssertNoError (t , err )
2733
28- test .AssertEqual (t , len (result ), 3 )
34+ test .AssertEqual (t , len (testEnv . env ), 3 )
2935
3036 os .WriteFile (envFilePath , []byte ("TEST = example" ), 0644 )
3137
32- time .Sleep (time .Millisecond * 100 )
38+ // This test is flaky on GitHub Actions, so we do this workaround
39+ counter := 0
40+ for counter < 20 && len (testEnv .env ) == 3 {
41+ time .Sleep (time .Millisecond * 50 )
42+ counter ++
43+ }
3344
34- test .AssertEqual (t , len (result ), 1 )
35- test .AssertEqual (t , readValue (t , result , "TEST" ), "example" )
45+ test .AssertEqual (t , len (testEnv . env ), 1 )
46+ test .AssertEqual (t , readValue (t , testEnv . env , "TEST" ), "example" )
3647}
0 commit comments