fork/gdu
git clone https://git.julia.blue/fork/gdu
99a7f2a
subvolume
1package testanalyze 2 3import ( 4"testing" 5"time" 6 7"github.com/dundee/gdu/v5/internal/common" 8"github.com/dundee/gdu/v5/pkg/analyze" 9"github.com/dundee/gdu/v5/pkg/fs" 10"github.com/stretchr/testify/assert" 11) 12 13// Compile-time check that MockedAnalyzer implements common.Analyzer 14var _ common.Analyzer = (* MockedAnalyzer )(nil ) 15 16func TestAnalyzeDir (t * testing.T ) { 17a := & MockedAnalyzer {} 18result := a .AnalyzeDir ("." ,nil ,nil ) 19 20assert .NotNil (t ,result ) 21assert .True (t ,result .IsDir ()) 22assert .Equal (t ,"test_dir" ,result .GetName ()) 23assert .Equal (t ,int64 (1e12 + 1 ),result .GetUsage ()) 24assert .Equal (t ,int64 (1e12 + 2 ),result .GetSize ()) 25assert .Equal (t ,int64 (12 ),result .GetItemCount ()) 26assert .Equal (t , 27time .Date (2021 ,8 ,27 ,22 ,23 ,24 ,0 ,time .UTC ), 28result .GetMtime (), 29 ) 30 31dir := result .(* analyze.Dir ) 32assert .Equal (t ,"." ,dir .BasePath ) 33assert .Len (t ,dir .Files ,4 ) 34 35// Verify children names and types 36names := make ([]string ,len (dir .Files )) 37for i ,f := range dir .Files { 38names [i ]= f .GetName () 39 } 40assert .Equal (t , []string {"aaa" ,"bbb" ,"ccc" ,"ddd" },names ) 41 42// Verify "aaa" is a dir with TB-range size 43aaa := dir .Files [0 ] 44assert .True (t ,aaa .IsDir ()) 45assert .Equal (t ,int64 (1e12 + 1 ),aaa .GetUsage ()) 46assert .Equal (t ,int64 (1e12 + 2 ),aaa .GetSize ()) 47assert .Equal (t ,result ,aaa .GetParent ()) 48assert .Equal (t , 49time .Date (2021 ,8 ,27 ,22 ,23 ,27 ,0 ,time .UTC ), 50aaa .GetMtime (), 51 ) 52 53// Verify "bbb" is a dir with GB-range size 54bbb := dir .Files [1 ] 55assert .True (t ,bbb .IsDir ()) 56assert .Equal (t ,int64 (1e9 + 1 ),bbb .GetUsage ()) 57assert .Equal (t ,int64 (1e9 + 2 ),bbb .GetSize ()) 58assert .Equal (t ,result ,bbb .GetParent ()) 59 60// Verify "ccc" is a dir with MB-range size 61ccc := dir .Files [2 ] 62assert .True (t ,ccc .IsDir ()) 63assert .Equal (t ,int64 (1e6 + 1 ),ccc .GetUsage ()) 64assert .Equal (t ,int64 (1e6 + 2 ),ccc .GetSize ()) 65assert .Equal (t ,result ,ccc .GetParent ()) 66 67// Verify "ddd" is a file with KB-range size 68ddd := dir .Files [3 ] 69assert .False (t ,ddd .IsDir ()) 70assert .Equal (t ,int64 (1e3 + 1 ),ddd .GetUsage ()) 71assert .Equal (t ,int64 (1e3 + 2 ),ddd .GetSize ()) 72assert .Equal (t ,result ,ddd .GetParent ()) 73} 74 75func TestGetProgress (t * testing.T ) { 76a := & MockedAnalyzer {} 77progress := a .GetProgress () 78 79assert .Equal (t ,int64 (0 ),progress .ItemCount ) 80} 81 82func TestGetDone (t * testing.T ) { 83a := & MockedAnalyzer {} 84done := a .GetDone () 85 86assert .NotNil (t ,done ) 87 88// The channel should be already closed (broadcast), so receiving should not block 89select { 90case <- done : 91// expected: channel was broadcast 92case <- time .After (time .Second ): 93t .Fatal ("GetDone channel was not broadcast" ) 94 } 95} 96 97func TestResetProgressClearsCancellation (t * testing.T ) { 98a := & MockedAnalyzer {} 99a .Cancel () 100assert .True (t ,a .IsCancelled ()) 101 102a .ResetProgress () 103assert .False (t ,a .IsCancelled ()) 104} 105 106func TestSetFollowSymlinks (t * testing.T ) { 107a := & MockedAnalyzer {} 108assert .NotPanics (t ,func () { 109a .SetFollowSymlinks (true ) 110a .SetFollowSymlinks (false ) 111 }) 112} 113 114func TestSetShowAnnexedSize (t * testing.T ) { 115a := & MockedAnalyzer {} 116assert .NotPanics (t ,func () { 117a .SetShowAnnexedSize (true ) 118a .SetShowAnnexedSize (false ) 119 }) 120} 121 122func TestSetTimeFilter (t * testing.T ) { 123a := & MockedAnalyzer {} 124assert .NotPanics (t ,func () { 125a .SetTimeFilter (func (mtime time.Time )bool {return true }) 126a .SetTimeFilter (nil ) 127 }) 128} 129 130func TestSetArchiveBrowsing (t * testing.T ) { 131a := & MockedAnalyzer {} 132assert .NotPanics (t ,func () { 133a .SetArchiveBrowsing (true ) 134a .SetArchiveBrowsing (false ) 135 }) 136} 137 138func TestSetFileTypeFilter (t * testing.T ) { 139a := & MockedAnalyzer {} 140assert .NotPanics (t ,func () { 141a .SetFileTypeFilter (func (name string )bool {return false }) 142a .SetFileTypeFilter (nil ) 143 }) 144} 145 146func TestItemFromDirWithErr (t * testing.T ) { 147err := ItemFromDirWithErr (nil ,nil ) 148 149assert .NotNil (t ,err ) 150assert .Equal (t ,"Failed" ,err .Error ()) 151} 152 153func TestItemFromDirWithSleep (t * testing.T ) { 154// Create a minimal dir + file structure for remove.ItemFromDir 155dir := & analyze.Dir { 156File :& analyze.File { 157Name :"parent" , 158Usage :5000 , 159Size :5000 , 160 }, 161BasePath :t .TempDir (), 162 } 163file := & analyze.File { 164Name :"child" , 165Usage :1000 , 166Size :1000 , 167Parent :dir , 168 } 169dir .Files = fs.Files {file } 170 171start := time .Now () 172err := ItemFromDirWithSleep (dir ,file ) 173elapsed := time .Since (start ) 174 175// Should take at least 500ms due to sleep 176assert .GreaterOrEqual (t ,elapsed .Milliseconds (),int64 (500 )) 177// os.RemoveAll returns nil for non-existent paths 178assert .NoError (t ,err ) 179} 180 181func TestItemFromDirWithSleepAndErr (t * testing.T ) { 182start := time .Now () 183err := ItemFromDirWithSleepAndErr (nil ,nil ) 184elapsed := time .Since (start ) 185 186assert .NotNil (t ,err ) 187assert .Equal (t ,"Failed" ,err .Error ()) 188assert .GreaterOrEqual (t ,elapsed .Milliseconds (),int64 (500 )) 189}