fork/gdu
git clone https://git.julia.blue/fork/gdu
35cba00
master
1package analyze 2 3import ( 4"os" 5"path/filepath" 6"testing" 7 8"github.com/dundee/gdu/v5/internal/common" 9"github.com/dundee/gdu/v5/pkg/fs" 10"github.com/stretchr/testify/assert" 11) 12 13// itemStats is the reported accounting for one directory in a scanned tree. 14type itemStats struct { 15size int64 16usage int64 17itemCount int64 18flag rune 19} 20 21// createEmptyDirTree builds a tree that exercises every branch of gdu's 22// empty-directory rule: a leaf empty dir, a dir holding only an empty dir, a 23// chain of them, and a dir with real content to keep the normal path covered. 24func createEmptyDirTree (t * testing.T )string { 25t .Helper () 26 27root := t .TempDir () 28for _ ,dir := range []string {"empty" ,"onlyempty/sub" ,"deep/a/b/c" ,"withfile" } { 29assert .NoError (t ,os .MkdirAll (filepath .Join (root ,dir ),os .ModePerm )) 30 } 31assert .NoError (t ,os .WriteFile (filepath .Join (root ,"withfile" ,"f" ), []byte ("hello" ),0o600 )) 32 33return root 34} 35 36// collectStats walks a scanned tree and records the stats of every directory, 37// keyed by path relative to the scan root. 38func collectStats (root fs.Item )map [string ]itemStats { 39stats := make (map [string ]itemStats ) 40base := root .GetPath () 41 42var walk func (item fs.Item ) 43walk = func (item fs.Item ) { 44key ,err := filepath .Rel (base ,item .GetPath ()) 45if err != nil { 46key = item .GetPath () 47 } 48stats [key ]= itemStats { 49size :item .GetSize (), 50usage :item .GetUsage (), 51itemCount :item .GetItemCount (), 52flag :item .GetFlag (), 53 } 54for child := range item .GetFiles (fs .SortByName ,fs .SortAsc ) { 55if child .IsDir () { 56walk (child ) 57 } 58 } 59 } 60walk (root ) 61 62return stats 63} 64 65// scanWith runs an unfiltered scan. The nil file type filter is significant: 66// per common.UI.CreateFileTypeFilter, nil is how "no filtering" is expressed, 67// and analyzers that finalize stats during the scan read it to pick between 68// UpdateStats and UpdateStatsWithFileFiltering accounting. 69func scanWith (t * testing.T ,analyzer common.Analyzer ,root string )map [string ]itemStats { 70t .Helper () 71 72dir := analyzer .AnalyzeDir (root ,func (_ ,_ string )bool {return false },nil ) 73analyzer .GetDone ().Wait () 74dir .UpdateStats (make (fs.HardLinkedItems )) 75 76return collectStats (dir ) 77} 78 79// scanFilteringAllFiles runs a scan whose type filter rejects every file, so 80// the whole tree becomes empty directories and the collapse branch of 81// resolveDirStats applies. 82func scanFilteringAllFiles ( 83t * testing.T ,analyzer common.Analyzer ,root string , 84)map [string ]itemStats { 85t .Helper () 86 87dir := analyzer .AnalyzeDir ( 88root ,func (_ ,_ string )bool {return false },func (_ string )bool {return true }, 89 ) 90analyzer .GetDone ().Wait () 91dir .UpdateStatsWithFileFiltering (make (fs.HardLinkedItems )) 92 93return collectStats (dir ) 94} 95 96// TestEmptyDirStatsAcrossAnalyzers pins the empty-directory rule: a directory 97// with no counted children reports EmptyDirSize apparent size and zero disk 98// usage, and a tree of nothing but empty directories therefore uses no disk at 99// all. Every analyzer must agree, so the numbers gdu prints cannot depend on 100// whether --db was passed. 101func TestEmptyDirStatsAcrossAnalyzers (t * testing.T ) { 102root := createEmptyDirTree (t ) 103 104want := map [string ]itemStats { 105"." : {size :1541 ,usage :4096 ,itemCount :10 ,flag :' ' }, 106"empty" : {size :EmptyDirSize ,usage :0 ,itemCount :1 ,flag :'e' }, 107"onlyempty" : {size :EmptyDirSize ,usage :0 ,itemCount :2 ,flag :' ' }, 108"onlyempty/sub" : { 109size :EmptyDirSize ,usage :0 ,itemCount :1 ,flag :'e' , 110 }, 111"deep" : {size :EmptyDirSize ,usage :0 ,itemCount :4 ,flag :' ' }, 112"deep/a" : {size :EmptyDirSize ,usage :0 ,itemCount :3 ,flag :' ' }, 113"deep/a/b" : {size :EmptyDirSize ,usage :0 ,itemCount :2 ,flag :' ' }, 114"deep/a/b/c" : {size :EmptyDirSize ,usage :0 ,itemCount :1 ,flag :'e' }, 115 } 116 117parallel := scanWith (t ,CreateAnalyzer (),root ) 118 119// The reference values above are asserted against the in-memory analyzer 120// first, so a genuine change in the rule fails here rather than silently 121// re-baselining every analyzer at once. 122for path ,expected := range want { 123assert .Equal (t ,expected ,parallel [path ],"parallel analyzer at %q" ,path ) 124 } 125assert .Equal (t ,int64 (0 ),parallel ["empty" ].usage ,"an empty dir must use no disk" ) 126assert .Equal (t ,int64 (0 ),parallel ["deep" ].usage ,"a tree of empty dirs must use no disk" ) 127 128// Under file filtering the whole tree collapses, because every directory 129// ends up holding nothing but empty directories. 130wantFiltered := scanFilteringAllFiles (t ,CreateAnalyzer (),root ) 131assert .Equal (t ,int64 (1 ),wantFiltered ["." ].itemCount ) 132assert .Equal (t ,int64 (0 ),wantFiltered ["." ].usage ) 133 134for _ ,tc := range parityAnalyzers () { 135t .Run (tc .name ,func (t * testing.T ) { 136got := scanWith (t ,tc .analyzer (t ),root ) 137for path ,expected := range want { 138assert .Equal (t ,expected ,got [path ],"%s analyzer at %q" ,tc .name ,path ) 139 } 140 }) 141 142t .Run (tc .name + " filtering files" ,func (t * testing.T ) { 143got := scanFilteringAllFiles (t ,tc .analyzer (t ),root ) 144for path ,expected := range wantFiltered { 145assert .Equal (t ,expected ,got [path ],"%s analyzer at %q" ,tc .name ,path ) 146 } 147 }) 148 } 149} 150 151type parityAnalyzer struct { 152name string 153analyzer func (t * testing.T ) common.Analyzer 154} 155 156func parityAnalyzers () []parityAnalyzer { 157return []parityAnalyzer { 158 { 159name :"sequential" , 160analyzer :func (* testing.T ) common.Analyzer {return CreateSeqAnalyzer () }, 161 }, 162 { 163name :"stable order" , 164analyzer :func (* testing.T ) common.Analyzer {return CreateStableOrderAnalyzer () }, 165 }, 166 { 167name :"stored (badger)" , 168analyzer :func (t * testing.T ) common.Analyzer { 169return CreateStoredAnalyzer (filepath .Join (t .TempDir (),"badger" )) 170 }, 171 }, 172 { 173name :"sqlite" , 174analyzer :func (t * testing.T ) common.Analyzer { 175analyzer ,err := CreateSqliteAnalyzer (filepath .Join (t .TempDir (),"test.db" )) 176assert .NoError (t ,err ) 177t .Cleanup (func () {_ = analyzer .storage .Close () }) 178return analyzer 179 }, 180 }, 181 } 182}