fork/gdu
git clone https://git.julia.blue/fork/gdu
34f3996
master
1package analyze 2 3import ( 4"testing" 5 6"github.com/dundee/gdu/v5/pkg/fs" 7"github.com/stretchr/testify/assert" 8) 9 10func createScannedRoot (basePath ,name string ,size int64 )* Dir { 11return & Dir { 12File :& File { 13Name :name , 14Size :size , 15 }, 16BasePath :basePath , 17ItemCount :1 , 18Files : fs.Files { 19& File {Name :"file" ,Size :size ,Usage :size }, 20 }, 21 } 22} 23 24func TestCreateVirtualRootDirKeepsRootPaths (t * testing.T ) { 25first := createScannedRoot ("/home/user" ,"projects" ,10 ) 26second := createScannedRoot ("/var" ,"log" ,20 ) 27 28root := CreateVirtualRootDir (first ,second ) 29 30// the roots gained a parent but must still report their real location 31assert .Equal (t ,"/home/user/projects" ,first .GetPath ()) 32assert .Equal (t ,"/var/log" ,second .GetPath ()) 33assert .Equal (t ,root ,first .GetParent ()) 34assert .Equal (t ,root ,second .GetParent ()) 35} 36 37func TestCreateVirtualRootDirHasNoFilesystemPath (t * testing.T ) { 38root := CreateVirtualRootDir (createScannedRoot ("/var" ,"log" ,1 )) 39 40assert .Equal (t ,VirtualRootName ,root .GetPath ()) 41assert .Nil (t ,root .GetParent ()) 42assert .True (t ,root .IsDir ()) 43} 44 45func TestCreateVirtualRootDirAggregatesStats (t * testing.T ) { 46root := CreateVirtualRootDir ( 47createScannedRoot ("/home/user" ,"projects" ,10 ), 48createScannedRoot ("/var" ,"log" ,20 ), 49 ) 50 51root .UpdateStats (make (fs.HardLinkedItems )) 52 53assert .Equal (t ,int64 (30 ),root .GetSize ()) 54assert .Equal (t ,int64 (30 ),root .GetUsage ()) 55// two roots with one file each, plus the roots and the virtual dir itself 56assert .Equal (t ,int64 (5 ),root .GetItemCount ()) 57} 58 59func TestCreateVirtualRootDirWithoutRoots (t * testing.T ) { 60root := CreateVirtualRootDir () 61 62root .UpdateStats (make (fs.HardLinkedItems )) 63 64assert .Equal (t ,VirtualRootName ,root .GetPath ()) 65assert .Equal (t ,int64 (1 ),root .GetItemCount ()) 66} 67 68func TestIsVirtualRootDir (t * testing.T ) { 69scanned := createScannedRoot ("/var" ,"log" ,1 ) 70root := CreateVirtualRootDir (scanned ) 71 72assert .True (t ,IsVirtualRootDir (root )) 73assert .False (t ,IsVirtualRootDir (scanned )) 74assert .False (t ,IsVirtualRootDir (& File {Name :VirtualRootName })) 75} 76 77// A real directory that happens to be called "(multiple)" must not be mistaken 78// for the virtual root, or gdu would refuse to operate on it. 79func TestIsVirtualRootDirIgnoresRealDirWithSameName (t * testing.T ) { 80scanned := createScannedRoot ("/home/user" ,VirtualRootName ,1 ) 81 82assert .False (t ,IsVirtualRootDir (scanned )) 83} 84 85func TestVirtualRootDirSubtractsStatsOnRemove (t * testing.T ) { 86first := createScannedRoot ("/home/user" ,"projects" ,10 ) 87second := createScannedRoot ("/var" ,"log" ,20 ) 88root := CreateVirtualRootDir (first ,second ) 89root .UpdateStats (make (fs.HardLinkedItems )) 90 91root .RemoveFile (second ) 92 93assert .Equal (t ,int64 (10 ),root .GetSize ()) 94assert .Equal (t ,int64 (10 ),root .GetUsage ()) 95}