github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/opts/parse_test.go (about) 1 package opts 2 3 import ( 4 "testing" 5 6 "gotest.tools/v3/assert" 7 "gotest.tools/v3/fs" 8 ) 9 10 func TestReadKVEnvStrings(t *testing.T) { 11 emptyEnvFile := fs.NewFile(t, t.Name()) 12 defer emptyEnvFile.Remove() 13 14 // EMPTY_VAR should be set, but with an empty string as value 15 // FROM_ENV value should be substituted with FROM_ENV in the current environment 16 // NO_SUCH_ENV does not exist in the current environment, so should be omitted 17 envFile1 := fs.NewFile(t, t.Name(), fs.WithContent(`Z1=z 18 EMPTY_VAR= 19 FROM_ENV 20 NO_SUCH_ENV 21 `)) 22 defer envFile1.Remove() 23 envFile2 := fs.NewFile(t, t.Name(), fs.WithContent("Z2=z\nA2=a")) 24 defer envFile2.Remove() 25 t.Setenv("FROM_ENV", "from-env") 26 27 tests := []struct { 28 name string 29 files []string 30 overrides []string 31 expected []string 32 }{ 33 { 34 name: "empty", 35 }, 36 { 37 name: "empty file", 38 files: []string{emptyEnvFile.Path()}, 39 }, 40 { 41 name: "single file", 42 files: []string{envFile1.Path()}, 43 expected: []string{"Z1=z", "EMPTY_VAR=", "FROM_ENV=from-env"}, 44 }, 45 { 46 name: "two files", 47 files: []string{envFile1.Path(), envFile2.Path()}, 48 expected: []string{"Z1=z", "EMPTY_VAR=", "FROM_ENV=from-env", "Z2=z", "A2=a"}, 49 }, 50 { 51 name: "single file and override", 52 files: []string{envFile1.Path()}, 53 overrides: []string{"Z1=override", "EXTRA=extra"}, 54 expected: []string{"Z1=z", "EMPTY_VAR=", "FROM_ENV=from-env", "Z1=override", "EXTRA=extra"}, 55 }, 56 { 57 name: "overrides only", 58 overrides: []string{"Z1=z", "EMPTY_VAR="}, 59 expected: []string{"Z1=z", "EMPTY_VAR="}, 60 }, 61 } 62 63 for _, tc := range tests { 64 tc := tc 65 t.Run(tc.name, func(t *testing.T) { 66 envs, err := ReadKVEnvStrings(tc.files, tc.overrides) 67 assert.NilError(t, err) 68 assert.DeepEqual(t, tc.expected, envs) 69 }) 70 } 71 }