Save the temp dir before clearing environment variables

In some windows setups, os.TempDir() will return something like
"C:\WINDOWS" if none of the expected environment variables (TMP, TEMP,
USERPROFILE) are set, and then functions that try to create temporary
files or directories will fail since this is not writable.

Several tests in flag_test.go clear the environment and then set a small
number of specific environment variables for the test, triggering the
following error in TestFlagFromFile when I run `go test ./...` on a new
windows machine:

flag_test.go:1667: open C:\WINDOWS\urfave_cli_test851254863: Access is denied.

To work around this, we can check the temp directory before calling
os.Clearenv() and use that directory explcitly in functions that
take the temp directory.

This fixes part of #1105.
This commit is contained in:
Mostyn Bramley-Moore 2020-04-14 07:51:30 +02:00
parent 7a390105cb
commit 8d907b5329

View File

@ -14,6 +14,8 @@ import (
"time" "time"
) )
var osTempDir = os.TempDir()
var boolFlagTests = []struct { var boolFlagTests = []struct {
name string name string
expected string expected string
@ -1662,7 +1664,7 @@ func TestFlagFromFile(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_FOO", "123") os.Setenv("APP_FOO", "123")
temp, err := ioutil.TempFile("", "urfave_cli_test") temp, err := ioutil.TempFile(osTempDir, "urfave_cli_test")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return