Skip to content

Commit faff956

Browse files
authored
Merge pull request #72 from stacklok/feat/config-server-idp-config
feat(env): add LookupEnv to Reader interface
2 parents 0ae9018 + e92186b commit faff956

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

env/env.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "os"
1010
// Reader defines an interface for environment variable access
1111
type Reader interface {
1212
Getenv(key string) string
13+
LookupEnv(key string) (string, bool)
1314
}
1415

1516
// OSReader implements Reader using the standard os package
@@ -19,3 +20,8 @@ type OSReader struct{}
1920
func (*OSReader) Getenv(key string) string {
2021
return os.Getenv(key)
2122
}
23+
24+
// LookupEnv returns the value of the environment variable named by the key
25+
func (*OSReader) LookupEnv(key string) (string, bool) {
26+
return os.LookupEnv(key)
27+
}

env/env_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,65 @@ func TestOSReader_Getenv(t *testing.T) { //nolint:paralleltest // Modifies envir
5959
}
6060
}
6161

62+
func TestOSReader_LookupEnv(t *testing.T) { //nolint:paralleltest // Modifies environment variables
63+
testKey := "TEST_LOOKUP_ENV_VARIABLE_FOR_TESTING"
64+
testValue := "lookup_test_value_123"
65+
66+
originalValue, wasSet := os.LookupEnv(testKey)
67+
t.Cleanup(func() {
68+
if wasSet {
69+
os.Setenv(testKey, originalValue)
70+
} else {
71+
os.Unsetenv(testKey)
72+
}
73+
})
74+
75+
reader := &OSReader{}
76+
77+
tests := []struct {
78+
name string
79+
setup func()
80+
key string
81+
wantVal string
82+
wantFound bool
83+
}{
84+
{
85+
name: "existing variable returns value and true",
86+
setup: func() { os.Setenv(testKey, testValue) },
87+
key: testKey,
88+
wantVal: testValue,
89+
wantFound: true,
90+
},
91+
{
92+
name: "variable set to empty string returns empty and true",
93+
setup: func() { os.Setenv(testKey, "") },
94+
key: testKey,
95+
wantVal: "",
96+
wantFound: true,
97+
},
98+
{
99+
name: "absent variable returns empty and false",
100+
setup: func() { os.Unsetenv(testKey) },
101+
key: testKey,
102+
wantVal: "",
103+
wantFound: false,
104+
},
105+
}
106+
107+
for _, tt := range tests { //nolint:paralleltest // Test modifies environment variables
108+
t.Run(tt.name, func(t *testing.T) {
109+
tt.setup()
110+
gotVal, gotFound := reader.LookupEnv(tt.key)
111+
if gotVal != tt.wantVal {
112+
t.Errorf("OSReader.LookupEnv() val = %q, want %q", gotVal, tt.wantVal)
113+
}
114+
if gotFound != tt.wantFound {
115+
t.Errorf("OSReader.LookupEnv() found = %v, want %v", gotFound, tt.wantFound)
116+
}
117+
})
118+
}
119+
}
120+
62121
// TestReader_InterfaceCompliance ensures OSReader implements the Reader interface
63122
func TestReader_InterfaceCompliance(t *testing.T) {
64123
t.Parallel()

env/mocks/mock_reader.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)