github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/testing/tmpdir.go (about) 1 package testing 2 3 import ( 4 "os" 5 "path/filepath" 6 7 "github.com/onsi/ginkgo/v2" 8 "github.com/pyroscope-io/pyroscope/pkg/util/bytesize" 9 ) 10 11 func DirStats(path string) (directories, files int, size bytesize.ByteSize) { 12 err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { 13 if err != nil { 14 return err 15 } 16 if info.IsDir() { 17 directories++ 18 } else { 19 files++ 20 size += bytesize.ByteSize(info.Size()) 21 } 22 return nil 23 }) 24 if err != nil { 25 return -1, -1, -1 26 } 27 28 return directories, files, size 29 } 30 31 func TmpDir(cb func(name string)) { 32 defer ginkgo.GinkgoRecover() 33 path, err := os.MkdirTemp("", "pyroscope-test-dir") 34 if err != nil { 35 panic(err) 36 } 37 defer os.RemoveAll(path) 38 39 cb(path) 40 } 41 42 type TmpDirectory struct { 43 Path string 44 } 45 46 func (t *TmpDirectory) Close() { 47 os.RemoveAll(t.Path) 48 } 49 50 func TmpDirSync() *TmpDirectory { 51 defer ginkgo.GinkgoRecover() 52 path, err := os.MkdirTemp("", "pyroscope-test-dir") 53 if err != nil { 54 panic(err) 55 } 56 return &TmpDirectory{Path: path} 57 }