From 6b15a7289518aad83ba74216cb08bd71151a74f9 Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Tue, 22 Feb 2022 12:17:50 -0800 Subject: [PATCH] src/os: add back TestClearenv Removed as flaky in #2603 --- src/os/env_test.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/os/env_test.go b/src/os/env_test.go index 40721b63..8489a976 100644 --- a/src/os/env_test.go +++ b/src/os/env_test.go @@ -122,7 +122,33 @@ func TestUnsetenv(t *testing.T) { } } -// TODO: add back TestClearenv() and fix the errors it finds +func TestClearenv(t *testing.T) { + const testKey = "GO_TEST_CLEARENV" + const testValue = "1" + + // reset env + defer func(origEnv []string) { + for _, pair := range origEnv { + // Environment variables on Windows can begin with = + // https://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx + i := strings.Index(pair[1:], "=") + 1 + if err := Setenv(pair[:i], pair[i+1:]); err != nil { + t.Errorf("Setenv(%q, %q) failed during reset: %v", pair[:i], pair[i+1:], err) + } + } + }(Environ()) + + if err := Setenv(testKey, testValue); err != nil { + t.Fatalf("Setenv(%q, %q) failed: %v", testKey, testValue, err) + } + if _, ok := LookupEnv(testKey); !ok { + t.Errorf("Setenv(%q, %q) didn't set $%s", testKey, testValue, testKey) + } + Clearenv() + if val, ok := LookupEnv(testKey); ok { + t.Errorf("Clearenv() didn't clear $%s, remained with value %q", testKey, val) + } +} func TestLookupEnv(t *testing.T) { const smallpox = "SMALLPOX" // No one has smallpox.