fork/gdu
git clone https://git.julia.blue/fork/gdu
da638da
subvolume
1package analyze 2 3import ( 4"archive/zip" 5"os" 6"path/filepath" 7"slices" 8"testing" 9 10"github.com/dundee/gdu/v5/pkg/fs" 11"github.com/stretchr/testify/assert" 12) 13 14func TestIsZipFile (t * testing.T ) { 15tests := []struct { 16filename string 17expected bool 18 }{ 19 {"test.zip" ,true }, 20 {"test.jar" ,true }, 21 {"TEST.ZIP" ,true }, 22 {"TEST.JAR" ,true }, 23 {"test.txt" ,false }, 24 {"test.tar.gz" ,false }, 25 {"test" ,false }, 26 {"" ,false }, 27 } 28 29for _ ,test := range tests { 30result := isZipFile (test .filename ) 31assert .Equal (t ,test .expected ,result ,"filename: %s" ,test .filename ) 32 } 33} 34 35func TestProcessZipFile (t * testing.T ) { 36// Create temporary zip file 37tempDir := t .TempDir () 38zipPath := filepath .Join (tempDir ,"test.zip" ) 39 40// Create zip file 41createTestZipFile (t ,zipPath ) 42 43// Get file info 44info ,err := os .Stat (zipPath ) 45assert .NoError (t ,err ) 46 47// Process zip file 48zipDir ,err := processZipFile (zipPath ,info ) 49assert .NoError (t ,err ) 50assert .NotNil (t ,zipDir ) 51 52// Verify zip directory properties 53assert .Equal (t ,"test.zip" ,zipDir .GetName (),"Name must include extension" ) 54assert .Equal (t ,rune ('Z' ),zipDir .GetFlag ()) 55assert .True (t ,zipDir .IsDir ()) 56assert .Equal (t ,"ZipDirectory" ,zipDir .GetType ()) 57 58// Verify file structure 59files := slices .Collect (zipDir .GetFiles (fs .SortByName ,fs .SortAsc )) 60assert .Greater (t ,len (files ),0 ) 61 62// Debug: print all files 63t .Logf ("Found %d files in zip:" ,len (files )) 64for _ ,file := range files { 65t .Logf (" - %s (isDir: %t, type: %s)" ,file .GetName (),file .IsDir (),file .GetType ()) 66 } 67 68// Find files 69foundTextFile := false 70foundSubdir := false 71 72for _ ,file := range files { 73if file .GetName ()== "test.txt" { 74foundTextFile = true 75assert .False (t ,file .IsDir ()) 76assert .Equal (t ,"ZipFile" ,file .GetType ()) 77 } 78if file .GetName ()== "subdir" { 79foundSubdir = true 80assert .True (t ,file .IsDir ()) 81assert .Equal (t ,"ZipDirectory" ,file .GetType ()) 82 } 83 } 84 85assert .True (t ,foundTextFile ,"should find test.txt file" ) 86assert .True (t ,foundSubdir ,"should find subdir directory" ) 87} 88 89func TestGetZipFileSize (t * testing.T ) { 90// Create temporary zip file 91tempDir := t .TempDir () 92zipPath := filepath .Join (tempDir ,"test.zip" ) 93 94// Create zip file 95createTestZipFile (t ,zipPath ) 96 97// Get size 98uncompressed ,compressed ,err := getZipFileSize (zipPath ) 99assert .NoError (t ,err ) 100assert .Greater (t ,uncompressed ,int64 (0 )) 101assert .Greater (t ,compressed ,int64 (0 )) 102// Note: for small files, compressed size might be larger 103t .Logf ("Uncompressed size: %d, Compressed size: %d" ,uncompressed ,compressed ) 104} 105 106func TestEnsureZipDirExists (t * testing.T ) { 107tempDir := t .TempDir () 108zipPath := filepath .Join (tempDir ,"test.zip" ) 109 110// Create root directory 111rootDir := & ZipDir { 112Dir :& Dir { 113File :& File { 114Name :"test.zip" , 115Flag :'Z' , 116 }, 117Files :make (fs.Files ,0 ), 118 }, 119zipPath :zipPath , 120 } 121 122dirMap := make (map [string ]* ZipDir ) 123dirMap ["" ]= rootDir 124 125// Ensure nested directory structure is created 126ensureZipDirExists (dirMap ,"dir1/dir2/dir3" ,zipPath ,rootDir ) 127 128// Verify directory structure 129assert .Contains (t ,dirMap ,"dir1" ) 130assert .Contains (t ,dirMap ,"dir1/dir2" ) 131assert .Contains (t ,dirMap ,"dir1/dir2/dir3" ) 132 133// Verify parent-child relationships 134dir1 := dirMap ["dir1" ] 135assert .Equal (t ,rootDir ,dir1 .GetParent ()) 136 137dir2 := dirMap ["dir1/dir2" ] 138assert .Equal (t ,dir1 ,dir2 .GetParent ()) 139 140dir3 := dirMap ["dir1/dir2/dir3" ] 141assert .Equal (t ,dir2 ,dir3 .GetParent ()) 142} 143 144// createTestZipFile creates a test zip file 145func createTestZipFile (t * testing.T ,zipPath string ) { 146file ,err := os .Create (zipPath ) 147assert .NoError (t ,err ) 148defer file .Close () 149 150zipWriter := zip .NewWriter (file ) 151defer zipWriter .Close () 152 153// Add root directory file 154writer ,err := zipWriter .Create ("test.txt" ) 155assert .NoError (t ,err ) 156_ ,err = writer .Write ([]byte ("Hello, this is a test file!" )) 157assert .NoError (t ,err ) 158 159// Add subdirectory files 160// We don't need to use the writer for the directory entry, avoid SA4006 161_ ,err = zipWriter .Create ("subdir/" ) 162assert .NoError (t ,err ) 163 164writer ,err = zipWriter .Create ("subdir/nested.txt" ) 165assert .NoError (t ,err ) 166_ ,err = writer .Write ([]byte ("This is a nested file." )) 167assert .NoError (t ,err ) 168 169// Add deeper directory structure 170writer ,err = zipWriter .Create ("dir1/dir2/deep.txt" ) 171assert .NoError (t ,err ) 172_ ,err = writer .Write ([]byte ("Deep nested file content." )) 173assert .NoError (t ,err ) 174}