testing: implement t.Setenv

This method has been added in Go 1.17 and is used in archive/zip
starting with Go 1.20. Therefore, this method is now needed in Go 1.20.

I've left out the parts that disable parallel execution of tests,
because we don't do that in TinyGo.

See:
* https://github.com/golang/go/issues/41260
* https://go-review.googlesource.com/c/go/+/260577
This commit is contained in:
Ayke van Laethem
2023-01-14 19:32:36 +01:00
committed by Ron Evans
parent 80077ef276
commit 33489d6344
2 changed files with 79 additions and 0 deletions
+22
View File
@@ -117,6 +117,7 @@ type TB interface {
Log(args ...interface{})
Logf(format string, args ...interface{})
Name() string
Setenv(key, value string)
Skip(args ...interface{})
SkipNow()
Skipf(format string, args ...interface{})
@@ -330,6 +331,27 @@ func (c *common) TempDir() string {
return dir
}
// Setenv calls os.Setenv(key, value) and uses Cleanup to
// restore the environment variable to its original value
// after the test.
func (c *common) Setenv(key, value string) {
prevValue, ok := os.LookupEnv(key)
if err := os.Setenv(key, value); err != nil {
c.Fatalf("cannot set environment variable: %v", err)
}
if ok {
c.Cleanup(func() {
os.Setenv(key, prevValue)
})
} else {
c.Cleanup(func() {
os.Unsetenv(key)
})
}
}
// runCleanup is called at the end of the test.
func (c *common) runCleanup() {
for {