Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "os"
// Reader defines an interface for environment variable access
type Reader interface {
Getenv(key string) string
LookupEnv(key string) (string, bool)
}

// OSReader implements Reader using the standard os package
Expand All @@ -19,3 +20,8 @@ type OSReader struct{}
func (*OSReader) Getenv(key string) string {
return os.Getenv(key)
}

// LookupEnv returns the value of the environment variable named by the key
func (*OSReader) LookupEnv(key string) (string, bool) {
return os.LookupEnv(key)
}
59 changes: 59 additions & 0 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,65 @@ func TestOSReader_Getenv(t *testing.T) { //nolint:paralleltest // Modifies envir
}
}

func TestOSReader_LookupEnv(t *testing.T) { //nolint:paralleltest // Modifies environment variables
testKey := "TEST_LOOKUP_ENV_VARIABLE_FOR_TESTING"
testValue := "lookup_test_value_123"

originalValue, wasSet := os.LookupEnv(testKey)
t.Cleanup(func() {
if wasSet {
os.Setenv(testKey, originalValue)
} else {
os.Unsetenv(testKey)
}
})

reader := &OSReader{}

tests := []struct {
name string
setup func()
key string
wantVal string
wantFound bool
}{
{
name: "existing variable returns value and true",
setup: func() { os.Setenv(testKey, testValue) },
key: testKey,
wantVal: testValue,
wantFound: true,
},
{
name: "variable set to empty string returns empty and true",
setup: func() { os.Setenv(testKey, "") },
key: testKey,
wantVal: "",
wantFound: true,
},
{
name: "absent variable returns empty and false",
setup: func() { os.Unsetenv(testKey) },
key: testKey,
wantVal: "",
wantFound: false,
},
}

for _, tt := range tests { //nolint:paralleltest // Test modifies environment variables
t.Run(tt.name, func(t *testing.T) {
tt.setup()
gotVal, gotFound := reader.LookupEnv(tt.key)
if gotVal != tt.wantVal {
t.Errorf("OSReader.LookupEnv() val = %q, want %q", gotVal, tt.wantVal)
}
if gotFound != tt.wantFound {
t.Errorf("OSReader.LookupEnv() found = %v, want %v", gotFound, tt.wantFound)
}
})
}
}

// TestReader_InterfaceCompliance ensures OSReader implements the Reader interface
func TestReader_InterfaceCompliance(t *testing.T) {
t.Parallel()
Expand Down
15 changes: 15 additions & 0 deletions env/mocks/mock_reader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading