fork/gdu
git clone https://git.julia.blue/fork/gdu
35cba00
master
1package analyze 2 3import ( 4"bytes" 5"encoding/gob" 6"fmt" 7"slices" 8"testing" 9 10"github.com/dundee/gdu/v5/internal/testdir" 11"github.com/dundee/gdu/v5/pkg/fs" 12"github.com/stretchr/testify/assert" 13) 14 15func TestEncDec (t * testing.T ) { 16var d fs.Item = & StoredDir { 17Dir :& Dir { 18File :& File { 19Name :"xxx" , 20 }, 21BasePath :"/yyy" , 22 }, 23 } 24 25b := & bytes.Buffer {} 26enc := gob .NewEncoder (b ) 27err := enc .Encode (d ) 28assert .NoError (t ,err ) 29 30var x fs.Item = & StoredDir {} 31dec := gob .NewDecoder (b ) 32err = dec .Decode (x ) 33assert .NoError (t ,err ) 34 35fmt .Println (d ,x ) 36assert .Equal (t ,d .GetName (),x .GetName ()) 37} 38 39func TestStoredAnalyzer (t * testing.T ) { 40fin := testdir .CreateTestDir () 41defer fin () 42 43a := CreateStoredAnalyzer ("/tmp/badger" ) 44dir := a .AnalyzeDir ( 45"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 46 ).(* StoredDir ) 47 48a .GetDone ().Wait () 49 50dir .UpdateStatsWithFileFiltering (make (fs.HardLinkedItems )) 51 52// test dir info; must match the parallel analyzer on the same fixture 53// (see TestAnalyzeDir in dir_test.go) 54assert .Equal (t ,"test_dir" ,dir .Name ) 55assert .Equal (t ,int64 (7 ),dir .Size ) 56assert .Equal (t ,int64 (5 ),dir .ItemCount ) 57assert .True (t ,dir .IsDir ()) 58 59// test dir tree 60files := slices .Collect (dir .GetFiles (fs .SortByName ,fs .SortAsc )) 61assert .Equal (t ,"nested" ,files [0 ].GetName ()) 62 63nested := files [0 ].(* StoredDir ) 64nestedFiles := slices .Collect (nested .GetFiles (fs .SortByName ,fs .SortAsc )) 65assert .Equal (t ,"subnested" ,nestedFiles [1 ].GetName ()) 66 67// test file 68assert .Equal (t ,"file2" ,nestedFiles [0 ].GetName ()) 69assert .Equal (t ,int64 (2 ),nestedFiles [0 ].GetSize ()) 70assert .True (t ,int64 (4096 )<= nestedFiles [0 ].GetUsage ()) 71 72subnested := nestedFiles [1 ].(* StoredDir ) 73subnestedFiles := slices .Collect (subnested .GetFiles (fs .SortByName ,fs .SortAsc )) 74assert .Equal (t ,"file" ,subnestedFiles [0 ].GetName ()) 75assert .Equal (t ,int64 (5 ),subnestedFiles [0 ].GetSize ()) 76} 77 78func TestRemoveStoredFile (t * testing.T ) { 79fin := testdir .CreateTestDir () 80defer fin () 81 82a := CreateStoredAnalyzer ("/tmp/badger" ) 83dir := a .AnalyzeDir ( 84"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 85 ).(* StoredDir ) 86 87a .GetDone ().Wait () 88a .ResetProgress () 89 90dir .UpdateStats (make (fs.HardLinkedItems )) 91 92// test dir info 93assert .Equal (t ,"test_dir" ,dir .Name ) 94assert .Equal (t ,int64 (7 ),dir .Size ) 95assert .Equal (t ,int64 (5 ),dir .ItemCount ) 96assert .True (t ,dir .IsDir ()) 97 98dirFiles := slices .Collect (dir .GetFiles (fs .SortByName ,fs .SortAsc )) 99subdir := dirFiles [0 ].(* StoredDir ) 100subdirFiles := slices .Collect (subdir .GetFiles (fs .SortByName ,fs .SortAsc )) 101subdir .RemoveFile (subdirFiles [0 ]) 102 103closeFn := DefaultStorage .Open () 104defer closeFn () 105stored ,err := DefaultStorage .GetDirForPath ("test_dir" ) 106assert .NoError (t ,err ) 107 108assert .Equal (t ,int64 (4 ),stored .GetItemCount ()) 109assert .Equal (t ,int64 (5 ),stored .GetSize ()) 110 111storedFiles := slices .Collect (stored .GetFiles (fs .SortByName ,fs .SortAsc )) 112storedNested := storedFiles [0 ].(* StoredDir ) 113storedNestedFiles := slices .Collect (storedNested .GetFiles (fs .SortByName ,fs .SortAsc )) 114storedSubnested := storedNestedFiles [0 ].(* StoredDir ) 115storedSubnestedFiles := slices .Collect (storedSubnested .GetFiles (fs .SortByName ,fs .SortAsc )) 116file := storedSubnestedFiles [0 ] 117assert .Equal (t ,false ,file .IsDir ()) 118assert .Equal (t ,"file" ,file .GetName ()) 119assert .Equal (t ,"test_dir/nested/subnested" ,file .GetParent ().GetPath ()) 120} 121 122func TestParentDirGetNamePanics (t * testing.T ) { 123defer func () { 124if r := recover ();r != nil { 125assert .Equal (t ,"must not be called" ,r ) 126 } 127 }() 128dir := & ParentDir {} 129dir .GetName () 130} 131 132func TestParentDirGetFlagPanics (t * testing.T ) { 133defer func () { 134if r := recover ();r != nil { 135assert .Equal (t ,"must not be called" ,r ) 136 } 137 }() 138dir := & ParentDir {} 139dir .GetFlag () 140} 141 142func TestParentDirIsDirPanics (t * testing.T ) { 143defer func () { 144if r := recover ();r != nil { 145assert .Equal (t ,"must not be called" ,r ) 146 } 147 }() 148dir := & ParentDir {} 149dir .IsDir () 150} 151 152func TestParentDirGetSizePanics (t * testing.T ) { 153defer func () { 154if r := recover ();r != nil { 155assert .Equal (t ,"must not be called" ,r ) 156 } 157 }() 158dir := & ParentDir {} 159dir .GetSize () 160} 161 162func TestParentDirGetTypePanics (t * testing.T ) { 163defer func () { 164if r := recover ();r != nil { 165assert .Equal (t ,"must not be called" ,r ) 166 } 167 }() 168dir := & ParentDir {} 169dir .GetType () 170} 171 172func TestParentDirGetUsagePanics (t * testing.T ) { 173defer func () { 174if r := recover ();r != nil { 175assert .Equal (t ,"must not be called" ,r ) 176 } 177 }() 178dir := & ParentDir {} 179dir .GetUsage () 180} 181 182func TestParentDirGetMtimePanics (t * testing.T ) { 183defer func () { 184if r := recover ();r != nil { 185assert .Equal (t ,"must not be called" ,r ) 186 } 187 }() 188dir := & ParentDir {} 189dir .GetMtime () 190} 191 192func TestParentDirGetItemCountPanics (t * testing.T ) { 193defer func () { 194if r := recover ();r != nil { 195assert .Equal (t ,"must not be called" ,r ) 196 } 197 }() 198dir := & ParentDir {} 199dir .GetItemCount () 200} 201 202func TestParentDirGetParentPanics (t * testing.T ) { 203defer func () { 204if r := recover ();r != nil { 205assert .Equal (t ,"must not be called" ,r ) 206 } 207 }() 208dir := & ParentDir {} 209dir .GetParent () 210} 211 212func TestParentDirSetParentPanics (t * testing.T ) { 213defer func () { 214if r := recover ();r != nil { 215assert .Equal (t ,"must not be called" ,r ) 216 } 217 }() 218dir := & ParentDir {} 219dir .SetParent (nil ) 220} 221 222func TestParentDirGetMultiLinkedInodePanics (t * testing.T ) { 223defer func () { 224if r := recover ();r != nil { 225assert .Equal (t ,"must not be called" ,r ) 226 } 227 }() 228dir := & ParentDir {} 229dir .GetMultiLinkedInode () 230} 231 232func TestParentDirEncodeJSONPanics (t * testing.T ) { 233defer func () { 234if r := recover ();r != nil { 235assert .Equal (t ,"must not be called" ,r ) 236 } 237 }() 238dir := & ParentDir {} 239err := dir .EncodeJSON (nil ,false ,nil ) 240assert .NoError (t ,err ) 241} 242 243func TestParentDirUpdateStatsPanics (t * testing.T ) { 244defer func () { 245if r := recover ();r != nil { 246assert .Equal (t ,"must not be called" ,r ) 247 } 248 }() 249dir := & ParentDir {} 250dir .UpdateStats (nil ) 251} 252 253func TestParentDirAddFilePanics (t * testing.T ) { 254defer func () { 255if r := recover ();r != nil { 256assert .Equal (t ,"must not be called" ,r ) 257 } 258 }() 259dir := & ParentDir {} 260dir .AddFile (nil ) 261} 262 263func TestParentDirGetFilesPanics (t * testing.T ) { 264defer func () { 265if r := recover ();r != nil { 266assert .Equal (t ,"must not be called" ,r ) 267 } 268 }() 269dir := & ParentDir {} 270dir .GetFiles (fs .SortByName ,fs .SortAsc ) 271} 272 273func TestParentDirGetFilesLockedPanics (t * testing.T ) { 274defer func () { 275if r := recover ();r != nil { 276assert .Equal (t ,"must not be called" ,r ) 277 } 278 }() 279dir := & ParentDir {} 280dir .GetFilesLocked (fs .SortByName ,fs .SortAsc ) 281} 282 283func TestParentDirRLockPanics (t * testing.T ) { 284defer func () { 285if r := recover ();r != nil { 286assert .Equal (t ,"must not be called" ,r ) 287 } 288 }() 289dir := & ParentDir {} 290dir .RLock () 291} 292 293func TestParentDirRemoveFilePanics (t * testing.T ) { 294defer func () { 295if r := recover ();r != nil { 296assert .Equal (t ,"must not be called" ,r ) 297 } 298 }() 299dir := & ParentDir {} 300dir .RemoveFile (nil ) 301} 302 303func TestParentDirGetItemStatsPanics (t * testing.T ) { 304defer func () { 305if r := recover ();r != nil { 306assert .Equal (t ,"must not be called" ,r ) 307 } 308 }() 309dir := & ParentDir {} 310dir .GetItemStats (nil ,false ) 311}