Skip to content

Commit 1a070db

Browse files
committed
Enhance platform-specific environment setup in tests
- Update TestBuildSecureEnvironment and TestSecureEnvironmentIntegration to conditionally set PATH and HOME based on the operating system. - Improve TestGetSystemEnvVar and TestRealWorldNpxScenario to reflect platform-specific paths for better test accuracy. - Ensure consistent handling of environment variables across different OS environments.
1 parent 5f3e5c7 commit 1a070db

2 files changed

Lines changed: 92 additions & 21 deletions

File tree

internal/secureenv/manager_test.go

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,19 @@ func TestBuildSecureEnvironment(t *testing.T) {
149149

150150
// Set up test environment
151151
os.Clearenv()
152-
os.Setenv("PATH", "/usr/bin:/bin")
153-
os.Setenv("HOME", "/home/user")
152+
153+
// Set platform-specific test paths
154+
var testPath, testHome string
155+
if runtime.GOOS == "windows" {
156+
testPath = "C:\\Windows\\System32;C:\\Windows"
157+
testHome = "C:\\Users\\testuser"
158+
} else {
159+
testPath = "/usr/bin:/bin"
160+
testHome = "/home/user"
161+
}
162+
163+
os.Setenv("PATH", testPath)
164+
os.Setenv("HOME", testHome)
154165
os.Setenv("SECRET_KEY", "secret123") // Should be filtered out
155166
os.Setenv("API_TOKEN", "token123") // Should be filtered out
156167
os.Setenv("LC_ALL", "en_US.UTF-8") // Should be included
@@ -175,10 +186,18 @@ func TestBuildSecureEnvironment(t *testing.T) {
175186

176187
// Should include allowed system variables with enhanced PATH discovery
177188
pathValue := envMap["PATH"]
178-
assert.Contains(t, pathValue, "/usr/bin", "PATH should contain /usr/bin")
179-
assert.Contains(t, pathValue, "/bin", "PATH should contain /bin")
189+
if runtime.GOOS == "windows" {
190+
// On Windows, PATH should contain Windows system paths
191+
assert.Contains(t, pathValue, "C:\\Windows\\System32", "PATH should contain C:\\Windows\\System32")
192+
assert.Contains(t, pathValue, "C:\\Windows", "PATH should contain C:\\Windows")
193+
} else {
194+
// On Unix/macOS, PATH should contain Unix system paths
195+
assert.Contains(t, pathValue, "/usr/bin", "PATH should contain /usr/bin")
196+
assert.Contains(t, pathValue, "/bin", "PATH should contain /bin")
197+
}
198+
180199
// Enhanced PATH discovery may include additional paths like /opt/homebrew/bin
181-
assert.Equal(t, "/home/user", envMap["HOME"])
200+
assert.Equal(t, testHome, envMap["HOME"])
182201
assert.Equal(t, "en_US.UTF-8", envMap["LC_ALL"])
183202

184203
// Should include custom variables
@@ -227,7 +246,12 @@ func TestGetSystemEnvVar(t *testing.T) {
227246
}()
228247

229248
// Set test environment
230-
testPath := "/test/bin:/test/usr/bin"
249+
var testPath string
250+
if runtime.GOOS == "windows" {
251+
testPath = "C:\\test\\bin;C:\\test\\usr\\bin"
252+
} else {
253+
testPath = "/test/bin:/test/usr/bin"
254+
}
231255
os.Setenv("PATH", testPath)
232256

233257
manager := NewManager(&EnvConfig{
@@ -273,8 +297,19 @@ func TestGetFilteredEnvCount(t *testing.T) {
273297

274298
// Set up test environment
275299
os.Clearenv()
276-
os.Setenv("PATH", "/usr/bin:/bin")
277-
os.Setenv("HOME", "/home/user")
300+
301+
// Set platform-specific test paths
302+
var testPath, testHome string
303+
if runtime.GOOS == "windows" {
304+
testPath = "C:\\Windows\\System32;C:\\Windows"
305+
testHome = "C:\\Users\\testuser"
306+
} else {
307+
testPath = "/usr/bin:/bin"
308+
testHome = "/home/user"
309+
}
310+
311+
os.Setenv("PATH", testPath)
312+
os.Setenv("HOME", testHome)
278313
os.Setenv("SECRET_KEY", "secret123")
279314
os.Setenv("API_TOKEN", "token123")
280315

@@ -400,7 +435,16 @@ func TestRealWorldNpxScenario(t *testing.T) {
400435
}()
401436

402437
// Set up realistic environment
403-
testPath := "/usr/local/bin:/usr/bin:/bin"
438+
var testPath string
439+
var expectedPaths []string
440+
if runtime.GOOS == "windows" {
441+
testPath = "C:\\Program Files\\nodejs;C:\\Windows\\System32;C:\\Windows"
442+
expectedPaths = []string{"C:\\Program Files\\nodejs", "C:\\Windows\\System32", "C:\\Windows"}
443+
} else {
444+
testPath = "/usr/local/bin:/usr/bin:/bin"
445+
expectedPaths = []string{"/usr/local/bin", "/usr/bin", "/bin"}
446+
}
447+
404448
os.Setenv("PATH", testPath)
405449

406450
// Create environment config for npx usage
@@ -415,9 +459,14 @@ func TestRealWorldNpxScenario(t *testing.T) {
415459
if strings.HasPrefix(envVar, "PATH=") {
416460
actualPath = envVar[5:] // Remove "PATH=" prefix
417461
// Enhanced path discovery should include the original test paths
418-
if strings.Contains(actualPath, "/usr/local/bin") &&
419-
strings.Contains(actualPath, "/usr/bin") &&
420-
strings.Contains(actualPath, "/bin") {
462+
foundAllPaths := true
463+
for _, expectedPath := range expectedPaths {
464+
if !strings.Contains(actualPath, expectedPath) {
465+
foundAllPaths = false
466+
break
467+
}
468+
}
469+
if foundAllPaths {
421470
foundPath = true
422471
break
423472
}

internal/upstream/secure_env_integration_test.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,19 @@ func TestSecureEnvironmentIntegration(t *testing.T) {
3434

3535
// Set up test environment with both safe and unsafe variables
3636
os.Clearenv()
37-
os.Setenv("PATH", "/usr/bin:/bin")
38-
os.Setenv("HOME", "/tmp/test-home")
37+
38+
// Set platform-specific test paths
39+
var testPath, testHome string
40+
if runtime.GOOS == "windows" {
41+
testPath = "C:\\Windows\\System32;C:\\Windows"
42+
testHome = "C:\\Users\\test-user"
43+
} else {
44+
testPath = "/usr/bin:/bin"
45+
testHome = "/tmp/test-home"
46+
}
47+
48+
os.Setenv("PATH", testPath)
49+
os.Setenv("HOME", testHome)
3950
os.Setenv("SECRET_API_KEY", "secret123") // Should be filtered out
4051
os.Setenv("DATABASE_PASSWORD", "dbpass123") // Should be filtered out
4152
os.Setenv("SAFE_TEST_VAR", "should_be_blocked") // Should be filtered out (not in allow list)
@@ -75,13 +86,18 @@ func TestSecureEnvironmentIntegration(t *testing.T) {
7586
}
7687

7788
// Should include safe system variables with enhanced PATH discovery
78-
assert.Contains(t, envMap["PATH"], "/usr/bin")
79-
assert.Contains(t, envMap["PATH"], "/bin")
80-
// Enhanced PATH should include additional system paths when available
8189
pathValue := envMap["PATH"]
82-
assert.True(t, strings.Contains(pathValue, "/usr/bin") && strings.Contains(pathValue, "/bin"),
83-
"PATH should contain basic system paths, got: %s", pathValue)
84-
assert.Equal(t, "/tmp/test-home", envMap["HOME"])
90+
if runtime.GOOS == "windows" {
91+
// On Windows, PATH should contain Windows system paths
92+
assert.Contains(t, pathValue, "C:\\Windows\\System32")
93+
assert.Contains(t, pathValue, "C:\\Windows")
94+
} else {
95+
// On Unix/macOS, PATH should contain Unix system paths
96+
assert.Contains(t, pathValue, "/usr/bin")
97+
assert.Contains(t, pathValue, "/bin")
98+
}
99+
// Enhanced PATH should include additional system paths when available
100+
assert.Equal(t, testHome, envMap["HOME"])
85101

86102
// Should include custom server variables
87103
assert.Equal(t, "custom_value", envMap["CUSTOM_SERVER_VAR"])
@@ -198,7 +214,13 @@ func TestEnvironmentInheritanceDisabled(t *testing.T) {
198214
}()
199215

200216
// Set up system environment
201-
os.Setenv("PATH", "/usr/bin:/bin")
217+
var testPath string
218+
if runtime.GOOS == "windows" {
219+
testPath = "C:\\Windows\\System32;C:\\Windows"
220+
} else {
221+
testPath = "/usr/bin:/bin"
222+
}
223+
os.Setenv("PATH", testPath)
202224

203225
// Create config with inheritance disabled
204226
cfg := config.DefaultConfig()

0 commit comments

Comments
 (0)