fork/gdu
git clone https://git.julia.blue/fork/gdu
99a7f2a
ignore
1package analyze 2 3import ( 4"os" 5"path/filepath" 6"strconv" 7"sync" 8"testing" 9 10"github.com/dundee/gdu/v5/internal/common" 11"github.com/dundee/gdu/v5/pkg/fs" 12"github.com/stretchr/testify/assert" 13) 14 15// createWideTree builds a tree large enough to keep the parallel analyzer busy 16// so that GetCurrentDir can be read while the scan is still appending to it. 17func createWideTree (t * testing.T ) (string ,func ()) { 18t .Helper () 19root ,err := os .MkdirTemp ("" ,"gdu-preview-race" ) 20assert .NoError (t ,err ) 21for i := 0 ;i < 40 ;i ++ { 22sub := filepath .Join (root ,"dir" + strconv .Itoa (i )) 23assert .NoError (t ,os .MkdirAll (filepath .Join (sub ,"nested" ),os .ModePerm )) 24for j := 0 ;j < 20 ;j ++ { 25f := filepath .Join (sub ,"f" + strconv .Itoa (j )) 26assert .NoError (t ,os .WriteFile (f , []byte ("data" ),0o600 )) 27nf := filepath .Join (sub ,"nested" ,"f" + strconv .Itoa (j )) 28assert .NoError (t ,os .WriteFile (nf , []byte ("data" ),0o600 )) 29 } 30 } 31return root ,func () {_ = os .RemoveAll (root ) } 32} 33 34// TestPreviewWhileScanning exercises reading and computing stats on the live 35// directory tree while the analyzer is still building it. Run with -race to 36// detect any unsynchronized access between AddFile and the preview readers. 37func TestPreviewWhileScanning (t * testing.T ) { 38root ,fin := createWideTree (t ) 39defer fin () 40 41analyzer := CreateAnalyzer () 42 43var wg sync.WaitGroup 44stop := make (chan struct {}) 45 46// concurrently peek at the partial tree, mirroring what the TUI preview does 47wg .Add (1 ) 48go func () { 49defer wg .Done () 50for { 51select { 52case <- stop : 53return 54default : 55 } 56if cur := analyzer .GetCurrentDir ();cur != nil { 57cur .UpdateStats (make (fs.HardLinkedItems )) 58var total int64 59for item := range cur .GetFilesLocked (fs .SortBySize ,fs .SortDesc ) { 60total += item .GetSize () 61 } 62_ = total 63 } 64 } 65 }() 66 67dir := analyzer .AnalyzeDir ( 68root , 69func (_ ,_ string )bool {return false }, 70func (_ string )bool {return false }, 71 ) 72close (stop ) 73wg .Wait () 74 75dir .UpdateStats (make (fs.HardLinkedItems )) 76assert .Equal (t ,root ,dir .GetPath ()) 77// 40 dirs * (20 files + nested dir with 20 files) + nested dirs + root 78assert .Greater (t ,dir .GetItemCount (),int64 (40 * 40 )) 79 80// the analyzer exposes a point-in-time snapshot of the returned root 81var _ common.Analyzer = analyzer 82assert .Equal (t ,dir .GetPath (),analyzer .GetCurrentDir ().GetPath ()) 83} 84 85func TestCurrentDirSnapshotCannotOverwriteLiveStats (t * testing.T ) { 86live := & Dir { 87File :& File {Name :"root" ,Size :100 ,Usage :100 }, 88Files :make (fs.Files ,0 ,1 ), 89ItemCount :1 , 90 } 91analyzer := CreateAnalyzer () 92analyzer .setCurrentDir (live ) 93 94snapshot := analyzer .GetCurrentDir () 95live .AddFile (& File {Name :"complete" ,Size :7 ,Usage :5 ,Parent :live }) 96live .UpdateStats (make (fs.HardLinkedItems )) 97snapshot .UpdateStats (make (fs.HardLinkedItems )) 98 99assert .Equal (t ,int64 (7 ),live .GetSize ()) 100assert .Equal (t ,int64 (5 ),live .GetUsage ()) 101assert .Equal (t ,int64 (2 ),live .GetItemCount ()) 102assert .Empty (t ,itemNames (snapshot )) 103} 104 105func TestCurrentDirSnapshotPreservesArchiveItems (t * testing.T ) { 106live := & Dir { 107File :& File {Name :"root" }, 108BasePath :"/tmp" , 109Files :make (fs.Files ,0 ,2 ), 110 } 111zipDir := & ZipDir { 112Dir :& Dir { 113File :& File {Name :"archive.zip" ,Parent :live }, 114Files :make (fs.Files ,0 ,1 ), 115 }, 116zipPath :"/tmp/archive.zip" , 117 } 118zipDir .Files = append (zipDir .Files ,& ZipFile { 119File :& File {Name :"inside.zip" ,Parent :zipDir }, 120zipPath :"/tmp/archive.zip" , 121inZipPath :"inside.zip" , 122 }) 123tarDir := & TarDir { 124Dir :& Dir { 125File :& File {Name :"archive.tar" ,Parent :live }, 126Files :make (fs.Files ,0 ,1 ), 127 }, 128tarPath :"/tmp/archive.tar" , 129 } 130tarDir .Files = append (tarDir .Files ,& TarFile { 131File :& File {Name :"inside.tar" ,Parent :tarDir }, 132tarPath :"/tmp/archive.tar" , 133inTarPath :"inside.tar" , 134 }) 135live .Files = append (live .Files ,zipDir ,tarDir ) 136 137analyzer := CreateAnalyzer () 138analyzer .setCurrentDir (live ) 139snapshot := analyzer .GetCurrentDir () 140 141items := make (map [string ]fs.Item ) 142for item := range snapshot .GetFiles (fs .SortByName ,fs .SortAsc ) { 143items [item .GetName ()]= item 144 } 145zipSnapshot ,ok := items ["archive.zip" ].(* ZipDir ) 146assert .True (t ,ok ) 147assert .Equal (t ,zipDir .GetType (),zipSnapshot .GetType ()) 148assert .Equal (t ,zipDir .GetPath (),zipSnapshot .GetPath ()) 149for item := range zipSnapshot .GetFiles (fs .SortByName ,fs .SortAsc ) { 150_ ,isZipFile := item .(* ZipFile ) 151assert .True (t ,isZipFile ) 152assert .Equal (t ,zipDir .Files [0 ].GetPath (),item .GetPath ()) 153 } 154tarSnapshot ,ok := items ["archive.tar" ].(* TarDir ) 155assert .True (t ,ok ) 156assert .Equal (t ,tarDir .GetType (),tarSnapshot .GetType ()) 157assert .Equal (t ,tarDir .GetPath (),tarSnapshot .GetPath ()) 158for item := range tarSnapshot .GetFiles (fs .SortByName ,fs .SortAsc ) { 159_ ,isTarFile := item .(* TarFile ) 160assert .True (t ,isTarFile ) 161assert .Equal (t ,tarDir .Files [0 ].GetPath (),item .GetPath ()) 162 } 163}