From 8d907b532953a230f009bb801bcdff5bdded6ed4 Mon Sep 17 00:00:00 2001 From: Mostyn Bramley-Moore Date: Tue, 14 Apr 2020 07:51:30 +0200 Subject: [PATCH] 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. --- flag_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/flag_test.go b/flag_test.go index 02f5d7d..8e4a9e8 100644 --- a/flag_test.go +++ b/flag_test.go @@ -14,6 +14,8 @@ import ( "time" ) +var osTempDir = os.TempDir() + var boolFlagTests = []struct { name string expected string @@ -1662,7 +1664,7 @@ func TestFlagFromFile(t *testing.T) { os.Clearenv() os.Setenv("APP_FOO", "123") - temp, err := ioutil.TempFile("", "urfave_cli_test") + temp, err := ioutil.TempFile(osTempDir, "urfave_cli_test") if err != nil { t.Error(err) return