@@ -3,7 +3,6 @@ package os
33import (
44 "os"
55 "path/filepath"
6- "strconv"
76 "strings"
87 "sync"
98 "testing"
@@ -12,9 +11,10 @@ import (
1211 "github.com/stretchr/testify/require"
1312)
1413
15- // resetForTest resets global variables for testing
14+ // resetForTest resets all global state for testing
1615func resetForTest () {
1716 tempDirOnce = sync .OnceValues (initTempDir )
17+ initialized .Store (false )
1818}
1919
2020func TestTempDir (t * testing.T ) {
@@ -26,10 +26,8 @@ func TestTempDir(t *testing.T) {
2626
2727 got := TempDir ()
2828
29- // Should contain process ID
30- pid := os .Getpid ()
31- want := filepath .Join (os .TempDir (), "trivy-" + strconv .Itoa (pid ))
32- assert .Equal (t , want , got )
29+ // Should be under system temp dir with trivy- prefix
30+ assert .True (t , strings .HasPrefix (got , filepath .Join (os .TempDir (), "trivy-" )))
3331
3432 // Directory should exist
3533 _ , err := os .Stat (got )
@@ -72,10 +70,8 @@ func TestCreateTemp(t *testing.T) {
7270 _ , err = os .Stat (file .Name ())
7371 require .NoError (t , err )
7472
75- // File should be in our temp directory
76- pid := os .Getpid ()
77- expectedDir := filepath .Join (os .TempDir (), "trivy-" + strconv .Itoa (pid ))
78- assert .True (t , strings .HasPrefix (file .Name (), expectedDir ))
73+ // File should be under a trivy- prefixed temp directory
74+ assert .True (t , strings .HasPrefix (file .Name (), filepath .Join (os .TempDir (), "trivy-" )))
7975
8076 // Test with specific dir
8177 customDir := t .TempDir ()
@@ -123,9 +119,8 @@ func TestMkdirTemp(t *testing.T) {
123119 _ , err = os .Stat (dir )
124120 require .NoError (t , err )
125121
126- // Directory should be in our temp directory
127- wantParent := filepath .Join (os .TempDir (), "trivy-" + strconv .Itoa (os .Getpid ()))
128- assert .True (t , strings .HasPrefix (dir , wantParent ))
122+ // Directory should be under a trivy- prefixed temp directory
123+ assert .True (t , strings .HasPrefix (dir , filepath .Join (os .TempDir (), "trivy-" )))
129124
130125 // Test with specific dir
131126 customParent := t .TempDir ()
@@ -152,8 +147,8 @@ func TestCleanup(t *testing.T) {
152147 filename := file .Name ()
153148 require .NoError (t , file .Close ())
154149
155- // Directory should exist
156- dir := filepath . Join ( os . TempDir (), "trivy-" + strconv . Itoa ( os . Getpid ()) )
150+ // Get the trivy temp directory (parent of the file)
151+ dir := TempDir ()
157152 _ , err = os .Stat (dir )
158153 require .NoError (t , err )
159154
@@ -173,3 +168,22 @@ func TestCleanup(t *testing.T) {
173168 _ , err = os .Stat (dir )
174169 assert .ErrorIs (t , err , os .ErrNotExist )
175170}
171+
172+ func TestTempDirUniqueness (t * testing.T ) {
173+ // Each call to initTempDir should produce a unique directory
174+ resetForTest ()
175+ dir1 := TempDir ()
176+ t .Cleanup (func () {
177+ _ = os .RemoveAll (dir1 )
178+ })
179+
180+ // Reset and get another dir
181+ resetForTest ()
182+ dir2 := TempDir ()
183+ t .Cleanup (func () {
184+ _ = os .RemoveAll (dir2 )
185+ resetForTest ()
186+ })
187+
188+ assert .NotEqual (t , dir1 , dir2 , "two separate initializations should produce different directories" )
189+ }
0 commit comments