fork/gdu
git clone https://git.julia.blue/fork/gdu
d062e88
subvolume
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 53assert .Equal (t ,"test_dir" ,dir .Name ) 54assert .Equal (t ,int64 (7 + 4096 * 3 ),dir .Size ) 55assert .Equal (t ,int64 (5 ),dir .ItemCount ) 56assert .True (t ,dir .IsDir ()) 57 58// test dir tree 59files := slices .Collect (dir .GetFiles (fs .SortByName ,fs .SortAsc )) 60assert .Equal (t ,"nested" ,files [0 ].GetName ()) 61 62nested := files [0 ].(* StoredDir ) 63nestedFiles := slices .Collect (nested .GetFiles (fs .SortByName ,fs .SortAsc )) 64assert .Equal (t ,"subnested" ,nestedFiles [1 ].GetName ()) 65 66// test file 67assert .Equal (t ,"file2" ,nestedFiles [0 ].GetName ()) 68assert .Equal (t ,int64 (2 ),nestedFiles [0 ].GetSize ()) 69assert .True (t ,int64 (4096 )<= nestedFiles [0 ].GetUsage ()) 70 71subnested := nestedFiles [1 ].(* StoredDir ) 72subnestedFiles := slices .Collect (subnested .GetFiles (fs .SortByName ,fs .SortAsc )) 73assert .Equal (t ,"file" ,subnestedFiles [0 ].GetName ()) 74assert .Equal (t ,int64 (5 ),subnestedFiles [0 ].GetSize ()) 75} 76 77func TestRemoveStoredFile (t * testing.T ) { 78fin := testdir .CreateTestDir () 79defer fin () 80 81a := CreateStoredAnalyzer ("/tmp/badger" ) 82dir := a .AnalyzeDir ( 83"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 84 ).(* StoredDir ) 85 86a .GetDone ().Wait () 87a .ResetProgress () 88 89dir .UpdateStats (make (fs.HardLinkedItems )) 90 91// test dir info 92assert .Equal (t ,"test_dir" ,dir .Name ) 93assert .Equal (t ,int64 (7 + 4096 * 3 ),dir .Size ) 94assert .Equal (t ,int64 (5 ),dir .ItemCount ) 95assert .True (t ,dir .IsDir ()) 96 97dirFiles := slices .Collect (dir .GetFiles (fs .SortByName ,fs .SortAsc )) 98subdir := dirFiles [0 ].(* StoredDir ) 99subdirFiles := slices .Collect (subdir .GetFiles (fs .SortByName ,fs .SortAsc )) 100subdir .RemoveFile (subdirFiles [0 ]) 101 102closeFn := DefaultStorage .Open () 103defer closeFn () 104stored ,err := DefaultStorage .GetDirForPath ("test_dir" ) 105assert .NoError (t ,err ) 106 107assert .Equal (t ,int64 (4 ),stored .GetItemCount ()) 108assert .Equal (t ,int64 (5 + 4096 * 3 ),stored .GetSize ()) 109 110storedFiles := slices .Collect (stored .GetFiles (fs .SortByName ,fs .SortAsc )) 111storedNested := storedFiles [0 ].(* StoredDir ) 112storedNestedFiles := slices .Collect (storedNested .GetFiles (fs .SortByName ,fs .SortAsc )) 113storedSubnested := storedNestedFiles [0 ].(* StoredDir ) 114storedSubnestedFiles := slices .Collect (storedSubnested .GetFiles (fs .SortByName ,fs .SortAsc )) 115file := storedSubnestedFiles [0 ] 116assert .Equal (t ,false ,file .IsDir ()) 117assert .Equal (t ,"file" ,file .GetName ()) 118assert .Equal (t ,"test_dir/nested/subnested" ,file .GetParent ().GetPath ()) 119} 120 121func TestParentDirGetNamePanics (t * testing.T ) { 122defer func () { 123if r := recover ();r != nil { 124assert .Equal (t ,"must not be called" ,r ) 125 } 126 }() 127dir := & ParentDir {} 128dir .GetName () 129} 130 131func TestParentDirGetFlagPanics (t * testing.T ) { 132defer func () { 133if r := recover ();r != nil { 134assert .Equal (t ,"must not be called" ,r ) 135 } 136 }() 137dir := & ParentDir {} 138dir .GetFlag () 139} 140 141func TestParentDirIsDirPanics (t * testing.T ) { 142defer func () { 143if r := recover ();r != nil { 144assert .Equal (t ,"must not be called" ,r ) 145 } 146 }() 147dir := & ParentDir {} 148dir .IsDir () 149} 150 151func TestParentDirGetSizePanics (t * testing.T ) { 152defer func () { 153if r := recover ();r != nil { 154assert .Equal (t ,"must not be called" ,r ) 155 } 156 }() 157dir := & ParentDir {} 158dir .GetSize () 159} 160 161func TestParentDirGetTypePanics (t * testing.T ) { 162defer func () { 163if r := recover ();r != nil { 164assert .Equal (t ,"must not be called" ,r ) 165 } 166 }() 167dir := & ParentDir {} 168dir .GetType () 169} 170 171func TestParentDirGetUsagePanics (t * testing.T ) { 172defer func () { 173if r := recover ();r != nil { 174assert .Equal (t ,"must not be called" ,r ) 175 } 176 }() 177dir := & ParentDir {} 178dir .GetUsage () 179} 180 181func TestParentDirGetMtimePanics (t * testing.T ) { 182defer func () { 183if r := recover ();r != nil { 184assert .Equal (t ,"must not be called" ,r ) 185 } 186 }() 187dir := & ParentDir {} 188dir .GetMtime () 189} 190 191func TestParentDirGetItemCountPanics (t * testing.T ) { 192defer func () { 193if r := recover ();r != nil { 194assert .Equal (t ,"must not be called" ,r ) 195 } 196 }() 197dir := & ParentDir {} 198dir .GetItemCount () 199} 200 201func TestParentDirGetParentPanics (t * testing.T ) { 202defer func () { 203if r := recover ();r != nil { 204assert .Equal (t ,"must not be called" ,r ) 205 } 206 }() 207dir := & ParentDir {} 208dir .GetParent () 209} 210 211func TestParentDirSetParentPanics (t * testing.T ) { 212defer func () { 213if r := recover ();r != nil { 214assert .Equal (t ,"must not be called" ,r ) 215 } 216 }() 217dir := & ParentDir {} 218dir .SetParent (nil ) 219} 220 221func TestParentDirGetMultiLinkedInodePanics (t * testing.T ) { 222defer func () { 223if r := recover ();r != nil { 224assert .Equal (t ,"must not be called" ,r ) 225 } 226 }() 227dir := & ParentDir {} 228dir .GetMultiLinkedInode () 229} 230 231func TestParentDirEncodeJSONPanics (t * testing.T ) { 232defer func () { 233if r := recover ();r != nil { 234assert .Equal (t ,"must not be called" ,r ) 235 } 236 }() 237dir := & ParentDir {} 238err := dir .EncodeJSON (nil ,false ,nil ) 239assert .NoError (t ,err ) 240} 241 242func TestParentDirUpdateStatsPanics (t * testing.T ) { 243defer func () { 244if r := recover ();r != nil { 245assert .Equal (t ,"must not be called" ,r ) 246 } 247 }() 248dir := & ParentDir {} 249dir .UpdateStats (nil ) 250} 251 252func TestParentDirAddFilePanics (t * testing.T ) { 253defer func () { 254if r := recover ();r != nil { 255assert .Equal (t ,"must not be called" ,r ) 256 } 257 }() 258dir := & ParentDir {} 259dir .AddFile (nil ) 260} 261 262func TestParentDirGetFilesPanics (t * testing.T ) { 263defer func () { 264if r := recover ();r != nil { 265assert .Equal (t ,"must not be called" ,r ) 266 } 267 }() 268dir := & ParentDir {} 269dir .GetFiles (fs .SortByName ,fs .SortAsc ) 270} 271 272func TestParentDirGetFilesLockedPanics (t * testing.T ) { 273defer func () { 274if r := recover ();r != nil { 275assert .Equal (t ,"must not be called" ,r ) 276 } 277 }() 278dir := & ParentDir {} 279dir .GetFilesLocked (fs .SortByName ,fs .SortAsc ) 280} 281 282func TestParentDirRLockPanics (t * testing.T ) { 283defer func () { 284if r := recover ();r != nil { 285assert .Equal (t ,"must not be called" ,r ) 286 } 287 }() 288dir := & ParentDir {} 289dir .RLock () 290} 291 292func TestParentDirRemoveFilePanics (t * testing.T ) { 293defer func () { 294if r := recover ();r != nil { 295assert .Equal (t ,"must not be called" ,r ) 296 } 297 }() 298dir := & ParentDir {} 299dir .RemoveFile (nil ) 300} 301 302func TestParentDirGetItemStatsPanics (t * testing.T ) { 303defer func () { 304if r := recover ();r != nil { 305assert .Equal (t ,"must not be called" ,r ) 306 } 307 }() 308dir := & ParentDir {} 309dir .GetItemStats (nil ,false ) 310}