@@ -122,7 +122,33 @@ func TestUnsetenv(t *testing.T) {
122122 }
123123}
124124
125- // TODO: add back TestClearenv() and fix the errors it finds
125+ func TestClearenv (t * testing.T ) {
126+ const testKey = "GO_TEST_CLEARENV"
127+ const testValue = "1"
128+
129+ // reset env
130+ defer func (origEnv []string ) {
131+ for _ , pair := range origEnv {
132+ // Environment variables on Windows can begin with =
133+ // https://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx
134+ i := strings .Index (pair [1 :], "=" ) + 1
135+ if err := Setenv (pair [:i ], pair [i + 1 :]); err != nil {
136+ t .Errorf ("Setenv(%q, %q) failed during reset: %v" , pair [:i ], pair [i + 1 :], err )
137+ }
138+ }
139+ }(Environ ())
140+
141+ if err := Setenv (testKey , testValue ); err != nil {
142+ t .Fatalf ("Setenv(%q, %q) failed: %v" , testKey , testValue , err )
143+ }
144+ if _ , ok := LookupEnv (testKey ); ! ok {
145+ t .Errorf ("Setenv(%q, %q) didn't set $%s" , testKey , testValue , testKey )
146+ }
147+ Clearenv ()
148+ if val , ok := LookupEnv (testKey ); ok {
149+ t .Errorf ("Clearenv() didn't clear $%s, remained with value %q" , testKey , val )
150+ }
151+ }
126152
127153func TestLookupEnv (t * testing.T ) {
128154 const smallpox = "SMALLPOX" // No one has smallpox.
0 commit comments