fork/gdu
git clone https://git.julia.blue/fork/gdu
d062e88
ignore
1package analyze 2 3import ( 4"testing" 5 6"github.com/dundee/gdu/v5/pkg/fs" 7"github.com/stretchr/testify/assert" 8) 9 10func TestTopDirAddUsage (t * testing.T ) { 11td := & TopDir {Name :"test" } 12td .AddUsage (100 ,200 ,5 ) 13assert .Equal (t ,int64 (100 ),td .Size .Load ()) 14assert .Equal (t ,int64 (200 ),td .Usage .Load ()) 15assert .Equal (t ,int64 (5 ),td .ItemCount .Load ()) 16 17td .AddUsage (50 ,80 ,3 ) 18assert .Equal (t ,int64 (150 ),td .Size .Load ()) 19assert .Equal (t ,int64 (280 ),td .Usage .Load ()) 20assert .Equal (t ,int64 (8 ),td .ItemCount .Load ()) 21} 22 23func TestTopDirSetFlag (t * testing.T ) { 24td := & TopDir {Name :"test" ,Flag :' ' } 25td .SetFlag ('!' ) 26assert .Equal (t ,'!' ,td .Flag ) 27} 28 29func TestSimpleDirGetName (t * testing.T ) { 30d := & SimpleDir {SimpleFile :SimpleFile {Name :"mydir" }} 31assert .Equal (t ,"mydir" ,d .GetName ()) 32} 33 34func TestSimpleDirGetSize (t * testing.T ) { 35d := & SimpleDir {SimpleFile :SimpleFile {Size :1234 }} 36assert .Equal (t ,int64 (1234 ),d .GetSize ()) 37} 38 39func TestSimpleDirGetUsage (t * testing.T ) { 40d := & SimpleDir {SimpleFile :SimpleFile {Usage :5678 }} 41assert .Equal (t ,int64 (5678 ),d .GetUsage ()) 42} 43 44func TestSimpleDirUpdateStats (t * testing.T ) { 45d := & SimpleDir { 46SimpleFile :SimpleFile {Name :"root" ,IsDir :true }, 47Files : []SimpleFile { 48 {Name :"file1" ,Size :100 ,Usage :200 ,ItemCount :1 }, 49 {Name :"file2" ,Size :50 ,Usage :80 ,ItemCount :1 }, 50 {Name :"subdir" ,Size :300 ,Usage :400 ,ItemCount :5 ,IsDir :true }, 51 }, 52 } 53 54d .UpdateStats (make (fs.HardLinkedItems )) 55 56assert .Equal (t ,int64 (100 + 50 + 300 ),d .Size ) 57assert .Equal (t ,int64 (200 + 80 + 400 ),d .Usage ) 58assert .Equal (t ,int64 (1 + 1 + 5 + 1 ),d .ItemCount ) 59} 60 61func TestEmptySimpleDirUpdateStats (t * testing.T ) { 62d := & SimpleDir { 63SimpleFile :SimpleFile {Name :"root" ,IsDir :true }, 64Files : []SimpleFile {}, 65 } 66 67d .UpdateStats (make (fs.HardLinkedItems )) 68 69assert .Equal (t ,int64 (512 ),d .Size ) 70assert .Equal (t ,int64 (0 ),d .Usage ) 71assert .Equal (t ,int64 (0 ),d .ItemCount ) 72} 73 74func TestSimpleDirUpdateStatsErrorFlag (t * testing.T ) { 75d := & SimpleDir { 76SimpleFile :SimpleFile {Name :"root" ,IsDir :true }, 77Files : []SimpleFile { 78 {Name :"sub" ,Size :100 ,Usage :200 ,ItemCount :1 ,Flag :'!' }, 79 }, 80 } 81 82d .UpdateStats (make (fs.HardLinkedItems )) 83assert .Equal (t ,'.' ,d .Flag ) 84} 85 86func TestSimpleDirUpdateStatsErrorFlagPreserved (t * testing.T ) { 87d := & SimpleDir { 88SimpleFile :SimpleFile {Name :"root" ,IsDir :true ,Flag :'!' }, 89Files : []SimpleFile { 90 {Name :"sub" ,Size :100 ,Usage :200 ,ItemCount :1 ,Flag :'!' }, 91 }, 92 } 93 94d .UpdateStats (make (fs.HardLinkedItems )) 95// '!' flag on dir should be preserved (not downgraded to '.') 96assert .Equal (t ,'!' ,d .Flag ) 97} 98 99func TestSimpleDirUpdateStatsDotFlag (t * testing.T ) { 100d := & SimpleDir { 101SimpleFile :SimpleFile {Name :"root" ,IsDir :true }, 102Files : []SimpleFile { 103 {Name :"sub" ,Size :100 ,Usage :200 ,ItemCount :1 ,Flag :'.' }, 104 }, 105 } 106 107d .UpdateStats (make (fs.HardLinkedItems )) 108assert .Equal (t ,'.' ,d .Flag ) 109} 110 111func TestSimpleDirGetFilesSort (t * testing.T ) { 112d := & SimpleDir { 113SimpleFile :SimpleFile {Name :"root" ,IsDir :true }, 114Files : []SimpleFile { 115 {Name :"small" ,Size :10 ,Usage :10 }, 116 {Name :"big" ,Size :100 ,Usage :100 }, 117 {Name :"medium" ,Size :50 ,Usage :50 }, 118 }, 119 } 120 121// Collect files sorted by size descending (default sort is desc for size) 122var names []string 123for item := range d .GetFiles (fs .SortBySize ,fs .SortAsc ) { 124names = append (names ,item .GetName ()) 125 } 126assert .Equal (t ,3 ,len (names )) 127} 128 129func TestSimpleDirGetFilesDirVsFile (t * testing.T ) { 130d := & SimpleDir { 131SimpleFile :SimpleFile {Name :"root" ,IsDir :true }, 132Files : []SimpleFile { 133 {Name :"afile" ,Size :10 ,Usage :10 ,IsDir :false }, 134 {Name :"adir" ,Size :100 ,Usage :100 ,IsDir :true ,ItemCount :5 }, 135 }, 136BasePath :"." , 137 } 138 139assert .True (t ,d .IsDir ()) 140assert .Equal (t ,"root" ,d .GetName ()) 141assert .Equal (t ,"./root" ,d .GetPath ()) 142 143var items []fs.Item 144for item := range d .GetFiles (fs .SortBySize ,fs .SortAsc ) { 145items = append (items ,item ) 146 } 147assert .Equal (t ,2 ,len (items )) 148 149// The dir entry should be a *Dir with ItemCount 150for _ ,item := range items { 151if item .GetName ()== "adir" { 152assert .True (t ,item .IsDir ()) 153assert .Equal (t ,int64 (5 ),item .GetItemCount ()) 154 } 155 } 156} 157 158func TestSimpleDirPanics (t * testing.T ) { 159d := & SimpleDir {} 160 161assert .Panics (t ,func () {d .GetFlag () }) 162assert .Panics (t ,func () {d .GetType () }) 163assert .Panics (t ,func () {d .GetMtime () }) 164assert .Panics (t ,func () {d .GetItemCount () }) 165assert .Panics (t ,func () {d .GetParent () }) 166assert .Panics (t ,func () {d .SetParent (nil ) }) 167assert .Panics (t ,func () {d .GetMultiLinkedInode () }) 168assert .Panics (t ,func () {d .EncodeJSON (nil ,false ,nil ) }) 169assert .Panics (t ,func () {d .GetItemStats (nil ,false ) }) 170assert .Panics (t ,func () {d .AddFile (nil ) }) 171assert .Panics (t ,func () { 172d .GetFilesLocked (fs .SortBySize ,fs .SortAsc ) 173 }) 174assert .Panics (t ,func () {d .RemoveFile (nil ) }) 175assert .Panics (t ,func () {d .RemoveFileByName ("x" ) }) 176assert .Panics (t ,func () {d .RLock () }) 177}