fork/gdu
git clone https://git.julia.blue/fork/gdu
4faa51c
subvolume
1package analyze 2 3import ( 4"os" 5"slices" 6"testing" 7"time" 8 9"github.com/dundee/gdu/v5/internal/testdir" 10"github.com/dundee/gdu/v5/pkg/fs" 11"github.com/stretchr/testify/assert" 12) 13 14func TestStoredAnalyzerGetProgress (t * testing.T ) { 15analyzer := CreateStoredAnalyzer ("/tmp/test" ) 16progress := analyzer .GetProgress () 17assert .Equal (t ,int64 (0 ),progress .ItemCount ) 18} 19 20func TestStoredAnalyzerSetFollowSymlinks (t * testing.T ) { 21analyzer := CreateStoredAnalyzer ("/tmp/test" ) 22analyzer .SetFollowSymlinks (true ) 23assert .True (t ,analyzer .followSymlinks ) 24analyzer .SetFollowSymlinks (false ) 25assert .False (t ,analyzer .followSymlinks ) 26} 27 28func TestStoredAnalyzerSetShowAnnexedSize (t * testing.T ) { 29analyzer := CreateStoredAnalyzer ("/tmp/test" ) 30analyzer .SetShowAnnexedSize (true ) 31assert .True (t ,analyzer .gitAnnexedSize ) 32analyzer .SetShowAnnexedSize (false ) 33assert .False (t ,analyzer .gitAnnexedSize ) 34} 35 36func TestStoredDirGetFilesCached (t * testing.T ) { 37// Test when files are already cached 38files := make (fs.Files ,0 ) 39dir := & StoredDir { 40Dir :& Dir { 41File :& File { 42Name :"test" , 43 }, 44BasePath :"/test" , 45 }, 46cachedFiles :files , 47 } 48 49result := slices .Collect (dir .GetFiles (fs .SortByName ,fs .SortAsc )) 50assert .Equal (t ,len (files ),len (result )) 51} 52 53func TestStoredDirRemoveFile (t * testing.T ) { 54// Test RemoveFile functionality 55fin := testdir .CreateTestDir () 56defer fin () 57 58analyzer := CreateStoredAnalyzer ("/tmp/test" ) 59dir := analyzer .AnalyzeDir ( 60"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 61 ).(* StoredDir ) 62 63analyzer .GetDone ().Wait () 64 65// Remove a file 66files := slices .Collect (dir .GetFiles (fs .SortByName ,fs .SortAsc )) 67if len (files )> 0 { 68dir .RemoveFile (files [0 ]) 69 } 70} 71 72func TestStoredDirUpdateStats (t * testing.T ) { 73// Test UpdateStats functionality 74fin := testdir .CreateTestDir () 75defer fin () 76 77analyzer := CreateStoredAnalyzer ("/tmp/test" ) 78dir := analyzer .AnalyzeDir ( 79"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 80 ).(* StoredDir ) 81 82analyzer .GetDone ().Wait () 83 84dir .UpdateStats (make (fs.HardLinkedItems )) 85} 86 87func TestStoredDirUpdateStatsWithMtimeUpdate (t * testing.T ) { 88// Test UpdateStats with mtime updates 89fin := testdir .CreateTestDir () 90defer fin () 91 92analyzer := CreateStoredAnalyzer ("/tmp/test" ) 93dir := analyzer .AnalyzeDir ( 94"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 95 ).(* StoredDir ) 96 97analyzer .GetDone ().Wait () 98 99// Create a file with newer mtime 100file := & File { 101Name :"newfile" , 102Mtime :time .Now ().Add (time .Hour ), 103 } 104dir .AddFile (file ) 105 106dir .UpdateStats (make (fs.HardLinkedItems )) 107} 108 109func TestStoredDirUpdateStatsWithFlagUpdate (t * testing.T ) { 110// Test UpdateStats with flag updates 111fin := testdir .CreateTestDir () 112defer fin () 113 114analyzer := CreateStoredAnalyzer ("/tmp/test" ) 115dir := analyzer .AnalyzeDir ( 116"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 117 ).(* StoredDir ) 118 119analyzer .GetDone ().Wait () 120 121// Create a file with error flag 122file := & File { 123Name :"errorfile" , 124Flag :'!' , 125 } 126dir .AddFile (file ) 127 128dir .UpdateStats (make (fs.HardLinkedItems )) 129// Just test that UpdateStats runs without error 130// The flag behavior depends on the specific implementation 131} 132 133func TestStoredDirUpdateStatsWithDotFlag (t * testing.T ) { 134// Test UpdateStats with dot flag 135fin := testdir .CreateTestDir () 136defer fin () 137 138analyzer := CreateStoredAnalyzer ("/tmp/test" ) 139dir := analyzer .AnalyzeDir ( 140"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 141 ).(* StoredDir ) 142 143analyzer .GetDone ().Wait () 144 145// Create a file with dot flag 146file := & File { 147Name :"dotfile" , 148Flag :'.' , 149 } 150dir .AddFile (file ) 151 152dir .UpdateStats (make (fs.HardLinkedItems )) 153assert .Equal (t ,'.' ,dir .Flag ) 154} 155 156func TestStoredAnalyzerWithZip (t * testing.T ) { 157fin := testdir .CreateTestDir () 158defer fin () 159 160// Create valid zip 161createTestZipFile (t ,"test_dir/valid.zip" ) 162 163// Create invalid zip 164f ,err := os .Create ("test_dir/invalid.zip" ) 165assert .NoError (t ,err ) 166_ ,err = f .WriteString ("this is not a zip file" ) 167assert .NoError (t ,err ) 168f .Close () 169 170analyzer := CreateStoredAnalyzer ("/tmp/test" ) 171analyzer .SetArchiveBrowsing (true ) 172dir := analyzer .AnalyzeDir ( 173"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 174 ).(* StoredDir ) 175 176analyzer .GetDone ().Wait () 177 178// Check valid.zip 179var validZip fs.Item 180var invalidZip fs.Item 181 182for _ ,file := range dir .Files { 183if file .GetName ()== "valid.zip" { 184validZip = file 185 } 186if file .GetName ()== "invalid.zip" { 187invalidZip = file 188 } 189 } 190 191assert .NotNil (t ,validZip ) 192assert .True (t ,validZip .IsDir ()) 193assert .Greater (t ,validZip .GetSize (),int64 (0 )) 194 195assert .NotNil (t ,invalidZip ) 196assert .False (t ,invalidZip .IsDir ()) 197assert .Equal (t ,int64 (22 ),invalidZip .GetSize ()) 198}