Skip to content

Commit e92186b

Browse files
reyortiz3claude
andcommitted
test(env): add unit tests for OSReader.LookupEnv
Covers present variable, present-but-empty, and absent variable to verify absent-vs-empty semantics of the new Reader.LookupEnv method. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e9f3c76 commit e92186b

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

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()

0 commit comments

Comments
 (0)