mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-07 04:23:41 +00:00
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:
committed by
Ron Evans
parent
80077ef276
commit
33489d6344
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user