Skip to content

Commit 26bc986

Browse files
Daniel McIlvaneyCopilot
andauthored
feat: add Getenv to OSEnv interface (#460)
Adds Getenv(key string) string to the OSEnv interface, along with implementations in defaultOSEnv, TestOSEnv, and the mock. Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 9650b6c commit 26bc986

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

internal/app/azldev/systeminterfaces.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,7 @@ func (e *defaultOSEnv) Chdir(dir string) error {
103103
//nolint:wrapcheck // We are intentionally a pass-through.
104104
return os.Chdir(dir)
105105
}
106+
107+
func (e *defaultOSEnv) Getenv(key string) string {
108+
return os.Getenv(key)
109+
}

internal/global/opctx/interfaces.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ type OSEnv interface {
168168
Getwd() (string, error)
169169
// Change the process's current working directory.
170170
Chdir(path string) error
171+
// Retrieve the value of the environment variable named by the key.
172+
Getenv(key string) string
171173
// Checks if the current user is a member of the given group.
172174
IsCurrentUserMemberOf(groupName string) (bool, error)
173175
// Looks up the group ID for the given group name.

internal/global/opctx/opctx_test/opctx_mocks.go

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

internal/global/testctx/osenv.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,37 @@ package testctx
77
// to control the environment without mutating the host environment.
88
type TestOSEnv struct {
99
workingDir string
10+
envVars map[string]string
1011
}
1112

1213
// Constructs a new [TestOSEnv] instance with the default working directory initialized to "/".
1314
func NewTestOSEnv() *TestOSEnv {
1415
return &TestOSEnv{
1516
workingDir: "/",
17+
envVars: make(map[string]string),
1618
}
1719
}
1820

21+
// SetEnv sets a test environment variable on the [TestOSEnv] instance.
22+
//
23+
// Note that [SetEnv] is not part of the [opctx.OSEnv] interface, so it is only
24+
// available when working with [TestOSEnv] directly. The intended usage pattern
25+
// in tests is:
26+
//
27+
// env := NewTestOSEnv()
28+
// env.SetEnv("KEY", "VALUE")
29+
// ctx := NewCtx(
30+
// ctx,
31+
// WithOSEnv(env),
32+
// )
33+
//
34+
// This allows tests to configure environment variables without mutating the
35+
// real host environment while still passing an [opctx.OSEnv]-compatible value
36+
// into [NewCtx] via [WithOSEnv].
37+
func (env *TestOSEnv) SetEnv(key, value string) {
38+
env.envVars[key] = value
39+
}
40+
1941
// Getwd implements the [opctx.OSEnv] interface.
2042
func (env *TestOSEnv) Getwd() (string, error) {
2143
return env.workingDir, nil
@@ -40,3 +62,8 @@ func (env *TestOSEnv) LookupGroupID(groupName string) (gid int, err error) {
4062
// For testing purposes, we can return a fixed group ID.
4163
return testGroupID, nil
4264
}
65+
66+
// Getenv implements the [opctx.OSEnv] interface.
67+
func (env *TestOSEnv) Getenv(key string) string {
68+
return env.envVars[key]
69+
}

0 commit comments

Comments
 (0)