fork/gdu
git clone https://git.julia.blue/fork/gdu
da638da
master
1package analyze 2 3import ( 4"archive/zip" 5"os" 6"path/filepath" 7"testing" 8 9"github.com/dundee/gdu/v5/pkg/fs" 10"github.com/stretchr/testify/assert" 11) 12 13func TestSequentialAnalyzerWithZipFile (t * testing.T ) { 14// Create temporary directory and zip file 15tempDir := t .TempDir () 16zipPath := filepath .Join (tempDir ,"test.zip" ) 17 18// Create test zip file 19createTestZipFile (t ,zipPath ) 20 21// Create analyzer 22analyzer := CreateSeqAnalyzer () 23analyzer .SetArchiveBrowsing (true ) 24 25// Analyze directory (containing zip file) 26result := analyzer .AnalyzeDir (tempDir ,func (string ,string )bool {return false },func (string )bool {return false }) 27 28// Verify result 29assert .NotNil (t ,result ) 30assert .True (t ,result .IsDir ()) 31 32// Find zip file 33var zipItem fs.Item 34for file := range result .GetFiles (fs .SortByName ,fs .SortAsc ) { 35if file .GetName ()== "test.zip" { 36zipItem = file 37break 38 } 39 } 40 41assert .NotNil (t ,zipItem ,"should find zip file" ) 42assert .True (t ,zipItem .IsDir (),"zip file should be treated as directory" ) 43 44// Verify zip file content 45zipFilesCount := 0 46foundTextFile := false 47for file := range zipItem .GetFiles (fs .SortByName ,fs .SortAsc ) { 48zipFilesCount ++ 49if file .GetName ()== "test.txt" { 50foundTextFile = true 51assert .False (t ,file .IsDir ()) 52 } 53 } 54assert .Greater (t ,zipFilesCount ,0 ,"zip file should contain content" ) 55assert .True (t ,foundTextFile ,"should find test.txt in zip file" ) 56} 57 58func TestParallelAnalyzerWithZipFile (t * testing.T ) { 59// Create temporary directory and zip file 60tempDir := t .TempDir () 61zipPath := filepath .Join (tempDir ,"test.jar" )// test jar file 62 63// Create test jar file (actually a zip file) 64createTestZipFile (t ,zipPath ) 65 66// Create parallel analyzer 67analyzer := CreateAnalyzer () 68analyzer .SetArchiveBrowsing (true ) 69 70// Analyze directory 71result := analyzer .AnalyzeDir (tempDir ,func (string ,string )bool {return false },func (string )bool {return false }) 72 73// Verify result 74assert .NotNil (t ,result ) 75assert .True (t ,result .IsDir ()) 76 77// Find jar file 78var jarItem fs.Item 79for file := range result .GetFiles (fs .SortByName ,fs .SortAsc ) { 80if file .GetName ()== "test.jar" { 81jarItem = file 82break 83 } 84 } 85 86assert .NotNil (t ,jarItem ,"should find jar file" ) 87assert .True (t ,jarItem .IsDir (),"jar file should be treated as directory" ) 88 89// Verify jar file content 90jarFilesCount := 0 91for range jarItem .GetFiles (fs .SortByName ,fs .SortAsc ) { 92jarFilesCount ++ 93 } 94assert .Greater (t ,jarFilesCount ,0 ,"jar file should contain content" ) 95} 96 97func TestZipFileWithNestedStructure (t * testing.T ) { 98// Create temporary directory 99tempDir := t .TempDir () 100zipPath := filepath .Join (tempDir ,"nested.zip" ) 101 102// Create zip file with complex nested structure 103createComplexZipFile (t ,zipPath ) 104 105// Create analyzer 106analyzer := CreateSeqAnalyzer () 107analyzer .SetArchiveBrowsing (true ) 108 109// Analyze directory 110result := analyzer .AnalyzeDir (tempDir ,func (string ,string )bool {return false },func (string )bool {return false }) 111 112// Find zip file 113var zipItem fs.Item 114for file := range result .GetFiles (fs .SortByName ,fs .SortAsc ) { 115if file .GetName ()== "nested.zip" { 116zipItem = file 117break 118 } 119 } 120 121assert .NotNil (t ,zipItem ) 122 123// Find deeply nested directory 124var level1Dir fs.Item 125for file := range zipItem .GetFiles (fs .SortByName ,fs .SortAsc ) { 126if file .GetName ()== "level1" && file .IsDir () { 127level1Dir = file 128break 129 } 130 } 131assert .NotNil (t ,level1Dir ,"should find level1 directory" ) 132 133// Find level2 directory 134var level2Dir fs.Item 135for file := range level1Dir .GetFiles (fs .SortByName ,fs .SortAsc ) { 136if file .GetName ()== "level2" && file .IsDir () { 137level2Dir = file 138break 139 } 140 } 141assert .NotNil (t ,level2Dir ,"should find level2 directory" ) 142 143// Find deepest nested file 144foundDeepFile := false 145for file := range level2Dir .GetFiles (fs .SortByName ,fs .SortAsc ) { 146if file .GetName ()== "deep.txt" { 147foundDeepFile = true 148break 149 } 150 } 151assert .True (t ,foundDeepFile ,"should find deeply nested file" ) 152} 153 154// createComplexZipFile creates a zip file with complex nested structure 155func createComplexZipFile (t * testing.T ,zipPath string ) { 156file ,err := os .Create (zipPath ) 157assert .NoError (t ,err ) 158defer file .Close () 159 160zipWriter := zip .NewWriter (file ) 161defer zipWriter .Close () 162 163// Create multi-level nested structure 164files := []struct { 165name string 166content string 167 }{ 168 {"root.txt" ,"Root level file" }, 169 {"level1/file1.txt" ,"Level 1 file" }, 170 {"level1/level2/file2.txt" ,"Level 2 file" }, 171 {"level1/level2/deep.txt" ,"Deep nested file" }, 172 {"level1/level2/level3/file3.txt" ,"Level 3 file" }, 173 {"another/path/file.txt" ,"Another path file" }, 174 } 175 176for _ ,f := range files { 177writer ,err := zipWriter .Create (f .name ) 178assert .NoError (t ,err ) 179_ ,err = writer .Write ([]byte (f .content )) 180assert .NoError (t ,err ) 181 } 182}