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
23 changes: 23 additions & 0 deletions env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
// SPDX-License-Identifier: Apache-2.0

// Package env provides abstractions for environment variable access
// to enable dependency injection and testing isolation.
package env

//go:generate mockgen -source=env.go -destination=mocks/mock_reader.go -package=mocks Reader

import "os"

// Reader defines an interface for environment variable access
type Reader interface {
Getenv(key string) string
}

// OSReader implements Reader using the standard os package
type OSReader struct{}

// Getenv returns the value of the environment variable named by the key
func (*OSReader) Getenv(key string) string {
return os.Getenv(key)
}
67 changes: 67 additions & 0 deletions env/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
// SPDX-License-Identifier: Apache-2.0

package env

import (
"os"
"testing"
)

func TestOSReader_Getenv(t *testing.T) { //nolint:paralleltest // Modifies environment variables
// Cannot run in parallel because it modifies environment variables
testKey := "TEST_ENV_VARIABLE_FOR_TESTING"
testValue := "test_value_123"

// Set an environment variable for testing
originalValue, wasSet := os.LookupEnv(testKey)
os.Setenv(testKey, testValue)
t.Cleanup(func() {
if wasSet {
os.Setenv(testKey, originalValue)
} else {
os.Unsetenv(testKey)
}
})

reader := &OSReader{}

tests := []struct {
name string
key string
want string
}{
{
name: "existing environment variable",
key: testKey,
want: testValue,
},
{
name: "non-existing environment variable",
key: "NONEXISTENT_ENV_VAR_TESTING_12345",
want: "",
},
{
name: "empty key",
key: "",
want: "",
},
}

for _, tt := range tests { //nolint:paralleltest // Test modifies environment variables
t.Run(tt.name, func(t *testing.T) {
// Cannot run in parallel because parent test modifies environment variables
got := reader.Getenv(tt.key)
if got != tt.want {
t.Errorf("OSReader.Getenv() = %v, want %v", got, tt.want)
}
})
}
}

// TestReader_InterfaceCompliance ensures OSReader implements the Reader interface
func TestReader_InterfaceCompliance(t *testing.T) {
t.Parallel()
var _ Reader = &OSReader{}
// If this compiles, the test passes
}
57 changes: 57 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.

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/stacklok/toolhive-core

go 1.25.6

require go.uber.org/mock v0.6.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
Loading