-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
False positive: os.TempDir() can't always be replaced by t.TempDir() #14
Comments
Hello Thanks for the feedback, The main reason why I am searching for dir := os.TempDir()
file, err := os.CreateTemp(dir, "test-xxx-*")
// handle error
// defer / t.Cleanup to remove file versus dir := t.TempDir()
file, err := os.CreateTemp(dir, "test-xxx-*")
// handle error
// no need for defer / t.Cleanup to remove file, tempdir will be removed after test since I can't easily check if os.CreateTemp was called with the os.TempDir or t.TempDir, I simplify the linter by searching all calls to I can imagine two possible solutions:
I think the option 1 seems a quick win on this case. What do you think @alexandear ? Now, about this test in specific: there is a workaround: on https://github.com/gohugoio/hugo/blob/master/helpers/path_test.go#L511C13-L511C64 we can see that we verify if the this helper uses https://github.com/spf13/afero/blob/v1.11.0/util.go#L104 since it calls os.TempDir and os.TempDir check for a env var $ git diff
diff --git a/helpers/path_test.go b/helpers/path_test.go
index 6f3699589..2b8eea934 100644
--- a/helpers/path_test.go
+++ b/helpers/path_test.go
@@ -487,7 +487,9 @@ func TestWriteToDisk(t *testing.T) {
}
func TestGetTempDir(t *testing.T) {
- dir := os.TempDir()
+ dir := t.TempDir()
+ t.Setenv("TMPDIR", dir)
+
if helpers.FilePathSeparator != dir[len(dir)-1:] {
dir = dir + helpers.FilePathSeparator
}
$ go test ./helpers/...
ok github.com/gohugoio/hugo/helpers 1.531s It is, indeed, possible on this example change |
Hi, thank you for the quick and meaningful reply. I believe this would be a good solution:
Thank you for providing the workaround for |
Describe the bug
There are instances where replacing
os.TempDir
witht.TempDir()
results in test failures.To Reproduce
Steps to reproduce the behavior:
git checkout 7ee36b371
.ttempdir ./helpers/...
Expected behavior
The
ttempdir
should not report any issues.Actual behavior
The following issue is reported:
Desktop:
Additional context
os.TempDir
on the line 490 cannot be replaced byt.TempDir
. Attempting to do so results in the following error:Details
Please note that the descriptions of
os.TempDir
andt.TempDir
are slightly different.https://pkg.go.dev/os#TempDir
https://pkg.go.dev/testing#T.TempDir
The text was updated successfully, but these errors were encountered: