@@ -2,6 +2,7 @@ package resolver
22
33import (
44 "context"
5+ "fmt"
56 "maps"
67 "os"
78 "path/filepath"
@@ -11,8 +12,7 @@ import (
1112func TestParseDotEnvFilesInto (t * testing.T ) {
1213 tests := []struct {
1314 name string
14- setupFiles map [string ]string // filename -> content
15- files []string // will be converted to absolute paths
15+ fileContents []string // each entry becomes .env0, .env1, etc.
1616 initialStore map [string ]string
1717 expected map [string ]string
1818 wantErr bool
@@ -21,14 +21,13 @@ func TestParseDotEnvFilesInto(t *testing.T) {
2121 }{
2222 {
2323 name : "when files list is empty, it must pass" ,
24- files : []string {},
24+ fileContents : []string {},
2525 initialStore : map [string ]string {},
2626 expected : map [string ]string {},
2727 wantErr : false ,
2828 },
2929 {
3030 name : "when file path is relative, it must fail" ,
31- files : []string {"relative/path/.env" },
3231 useRelPath : true ,
3332 wantErr : true ,
3433 },
@@ -37,15 +36,20 @@ func TestParseDotEnvFilesInto(t *testing.T) {
3736 useNonExistent : true ,
3837 wantErr : true ,
3938 },
39+ {
40+ name : "when dotenv file has invalid syntax, it must return a parse error" ,
41+ fileContents : []string {"INVALID LINE WITHOUT EQUALS\n ANOTHER BAD LINE" },
42+ initialStore : map [string ]string {},
43+ expected : map [string ]string {},
44+ wantErr : true ,
45+ },
4046 {
4147 name : "when file is valid, it must parse all key-value pairs" ,
42- setupFiles : map [string ]string {
43- ".env" : `KEY1=value1
44- KEY2=value2
45- KEY3="quoted value"
46- ` ,
48+ fileContents : []string {
49+ `KEY1=value1
50+ KEY2=value2
51+ KEY3="quoted value"` ,
4752 },
48- files : []string {".env" },
4953 initialStore : map [string ]string {},
5054 expected : map [string ]string {
5155 "KEY1" : "value1" ,
@@ -56,13 +60,10 @@ func TestParseDotEnvFilesInto(t *testing.T) {
5660 },
5761 {
5862 name : "when multiple files have same key, it must use value from last file" ,
59- setupFiles : map [string ]string {
60- ".env1" : `KEY1=value1
61- KEY2=original` ,
62- ".env2" : `KEY2=overridden
63- KEY3=value3` ,
63+ fileContents : []string {
64+ "KEY1=value1\n KEY2=original" ,
65+ "KEY2=overridden\n KEY3=value3" ,
6466 },
65- files : []string {".env1" , ".env2" },
6667 initialStore : map [string ]string {},
6768 expected : map [string ]string {
6869 "KEY1" : "value1" ,
@@ -72,11 +73,8 @@ KEY3=value3`,
7273 wantErr : false ,
7374 },
7475 {
75- name : "when store has existing keys, it must preserve non-overlapping values" ,
76- setupFiles : map [string ]string {
77- ".env" : `NEW_KEY=new_value` ,
78- },
79- files : []string {".env" },
76+ name : "when store has existing keys, it must preserve non-overlapping values" ,
77+ fileContents : []string {"NEW_KEY=new_value" },
8078 initialStore : map [string ]string {"EXISTING" : "existing_value" },
8179 expected : map [string ]string {
8280 "EXISTING" : "existing_value" ,
@@ -91,19 +89,18 @@ KEY3=value3`,
9189 var files []string
9290
9391 if tt .useRelPath {
94- files = tt . files
92+ files = [] string { "relative/path/.env" }
9593 } else if tt .useNonExistent {
9694 files = []string {"/non/existent/path/.env" }
9795 } else {
9896 tmpDir := t .TempDir ()
99- for filename , content := range tt .setupFiles {
97+ for i , content := range tt .fileContents {
98+ filename := fmt .Sprintf (".env%d" , i )
10099 path := filepath .Join (tmpDir , filename )
101100 if err := os .WriteFile (path , []byte (content ), 0644 ); err != nil {
102101 t .Fatalf ("failed to create temp file: %v" , err )
103102 }
104- }
105- for _ , f := range tt .files {
106- files = append (files , filepath .Join (tmpDir , f ))
103+ files = append (files , path )
107104 }
108105 }
109106
0 commit comments