fork/gdu

git clone https://git.julia.blue/fork/gdu

Quet Zalfeat(analyze): add SQLite analyzer and some assorted changes (#484)da638da

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