fork/gdu

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

ShivamB25feat: customize JSON export attributes (#605)d062e88

ignore
17.9 KiB645 linesraw
1package analyze
2
3import (
4	"archive/tar"
5	"bytes"
6	"compress/bzip2"
7	"compress/gzip"
8	"io"
9	"os"
10	"os/exec"
11	"path/filepath"
12	"slices"
13	"testing"
14
15	"github.com/dundee/gdu/v5/pkg/fs"
16	"github.com/stretchr/testify/assert"
17	"github.com/stretchr/testify/require"
18	"github.com/ulikunitz/xz"
19)
20
21// ---------------------------------------------------------------------------
22// Helpers to create test archives
23// ---------------------------------------------------------------------------
24
25// writeTarEntries writes a tiny but realistic directory structure into w.
26func writeTarEntries(t *testing.T, w io.Writer) {
27	t.Helper()
28	tw := tar.NewWriter(w)
29
30	entries := []struct {
31		hdr     tar.Header
32		content []byte
33	}{
34		{tar.Header{Typeflag: tar.TypeDir, Name: "subdir/", Mode: 0o755}, nil},
35		{tar.Header{Typeflag: tar.TypeReg, Name: "test.txt", Size: 11, Mode: 0o644}, []byte("hello world")},
36		{tar.Header{Typeflag: tar.TypeReg, Name: "subdir/nested.txt", Size: 6, Mode: 0o644}, []byte("nested")},
37	}
38
39	for _, e := range entries {
40		require.NoError(t, tw.WriteHeader(&e.hdr))
41		if e.content != nil {
42			_, err := tw.Write(e.content)
43			require.NoError(t, err)
44		}
45	}
46	require.NoError(t, tw.Close())
47}
48
49func createTestTarFile(t *testing.T, path string) {
50	t.Helper()
51	f, err := os.Create(path)
52	require.NoError(t, err)
53	defer f.Close()
54	writeTarEntries(t, f)
55}
56
57func createTestTarGzFile(t *testing.T, path string) {
58	t.Helper()
59	f, err := os.Create(path)
60	require.NoError(t, err)
61	defer f.Close()
62	gw := gzip.NewWriter(f)
63	writeTarEntries(t, gw)
64	require.NoError(t, gw.Close())
65}
66
67func createTestTarBz2File(t *testing.T, path string) {
68	t.Helper()
69	// compress/bzip2 only provides a reader; use a pre-recorded bzip2-compressed
70	// tar so we can test the reading path without an external encoder.
71	// We build the plain tar in memory, then bzip2-compress it via a known
72	// approach: write raw tar bytes, then BZ-compress them with a pure-Go writer.
73	// Go stdlib has no bzip2 writer, so we create a minimal bzip2 stream by
74	// piping through the `bzip2` command if available, otherwise skip.
75	var buf bytes.Buffer
76	writeTarEntries(t, &buf)
77
78	// Try to use bzip2 command-line tool
79	tmp := t.TempDir()
80	rawTar := filepath.Join(tmp, "tmp.tar")
81	require.NoError(t, os.WriteFile(rawTar, buf.Bytes(), 0o600))
82
83	out, err := os.Create(path)
84	require.NoError(t, err)
85	defer out.Close()
86
87	// Write a real bzip2 stream using a simple pure-Go BZ2 encoder shim.
88	// Because the stdlib only provides a reader, we vendor a minimal BZ2 writer
89	// here via dsnet/compress or simply write the file using pgzip. Instead, we
90	// use a known-good approach: encode via Go's exec. If exec is unavailable we
91	// create the file using a bzip2 block that wraps our raw bytes.
92	//
93	// Simplest portable approach: use compress/flate at the tar layer instead.
94	// But since we need a real bzip2 file, we skip if bzip2 is unavailable.
95	bzip2Cmd, lookErr := findBzip2Cmd()
96	if lookErr != nil {
97		t.Skip("bzip2 command not available, skipping .tar.bz2 test")
98	}
99
100	rawData, err := os.ReadFile(rawTar)
101	require.NoError(t, err)
102	compressed, err := bzip2Cmd(rawData)
103	require.NoError(t, err)
104	_, err = out.Write(compressed)
105	require.NoError(t, err)
106}
107
108func createTestTarXzFile(t *testing.T, path string) {
109	t.Helper()
110	f, err := os.Create(path)
111	require.NoError(t, err)
112	defer f.Close()
113
114	xw, err := xz.NewWriter(f)
115	require.NoError(t, err)
116	writeTarEntries(t, xw)
117	require.NoError(t, xw.Close())
118}
119
120// findBzip2Cmd returns a function that compresses data with bzip2, or an error
121// if bzip2 is not available on the system.
122func findBzip2Cmd() (func([]byte) ([]byte, error), error) {
123	// We use os/exec but need to import it. To avoid adding an import only used
124	// in tests, use a direct syscall approach via os/exec in a sub-function.
125	return bzip2CompressFunc()
126}
127
128// ---------------------------------------------------------------------------
129// isTarFile
130// ---------------------------------------------------------------------------
131
132func TestIsTarFile(t *testing.T) {
133	tests := []struct {
134		filename string
135		expected bool
136	}{
137		{"archive.tar", true},
138		{"archive.tar.gz", true},
139		{"archive.tgz", true},
140		{"archive.tar.bz2", true},
141		{"archive.tbz2", true},
142		{"archive.tar.xz", true},
143		{"archive.txz", true},
144		{"ARCHIVE.TAR", true},
145		{"ARCHIVE.TAR.GZ", true},
146		{"ARCHIVE.TGZ", true},
147		{"ARCHIVE.TAR.BZ2", true},
148		{"ARCHIVE.TBZZ2", false},
149		{"ARCHIVE.TAR.XZ", true},
150		{"ARCHIVE.TXZ", true},
151		{"archive.zip", false},
152		{"archive.jar", false},
153		{"archive.gz", false},  // plain gzip, not a tarball
154		{"archive.bz2", false}, // plain bzip2, not a tarball
155		{"archive.xz", false},
156		{"archive.txt", false},
157		{"archive", false},
158		{"", false},
159	}
160
161	for _, tc := range tests {
162		result := isTarFile(tc.filename)
163		assert.Equal(t, tc.expected, result, "filename: %s", tc.filename)
164	}
165}
166
167// ---------------------------------------------------------------------------
168// processTarFile – plain .tar
169// ---------------------------------------------------------------------------
170
171func TestProcessTarFile(t *testing.T) {
172	tmpDir := t.TempDir()
173	tarPath := filepath.Join(tmpDir, "test.tar")
174	createTestTarFile(t, tarPath)
175
176	info, err := os.Stat(tarPath)
177	require.NoError(t, err)
178
179	td, err := processTarFile(tarPath, info)
180	require.NoError(t, err)
181	require.NotNil(t, td)
182
183	assert.Equal(t, "test.tar", td.GetName())
184	assert.Equal(t, rune('T'), td.GetFlag())
185	assert.True(t, td.IsDir())
186	assert.Equal(t, "TarDirectory", td.GetType())
187
188	// Usage should equal the on-disk size
189	assert.Equal(t, info.Size(), td.Usage)
190
191	files := slices.Collect(td.GetFiles(fs.SortByName, fs.SortAsc))
192	assert.Greater(t, len(files), 0)
193
194	foundText := false
195	foundSubdir := false
196	for _, f := range files {
197		if f.GetName() == "test.txt" {
198			foundText = true
199			assert.False(t, f.IsDir())
200			assert.Equal(t, "TarFile", f.GetType())
201		}
202		if f.GetName() == "subdir" {
203			foundSubdir = true
204			assert.True(t, f.IsDir())
205			assert.Equal(t, "TarDirectory", f.GetType())
206		}
207	}
208	assert.True(t, foundText, "should find test.txt")
209	assert.True(t, foundSubdir, "should find subdir")
210}
211
212// ---------------------------------------------------------------------------
213// processTarFile – .tar.gz
214// ---------------------------------------------------------------------------
215
216func TestProcessTarGzFile(t *testing.T) {
217	tmpDir := t.TempDir()
218	tarPath := filepath.Join(tmpDir, "test.tar.gz")
219	createTestTarGzFile(t, tarPath)
220
221	info, err := os.Stat(tarPath)
222	require.NoError(t, err)
223
224	td, err := processTarFile(tarPath, info)
225	require.NoError(t, err)
226	require.NotNil(t, td)
227
228	assert.Equal(t, "test.tar.gz", td.GetName())
229	assert.True(t, td.IsDir())
230	assert.Equal(t, "TarDirectory", td.GetType())
231	assert.Equal(t, info.Size(), td.Usage)
232
233	files := slices.Collect(td.GetFiles(fs.SortByName, fs.SortAsc))
234	assert.Greater(t, len(files), 0)
235}
236
237// ---------------------------------------------------------------------------
238// processTarFile – .tgz alias
239// ---------------------------------------------------------------------------
240
241func TestProcessTgzFile(t *testing.T) {
242	tmpDir := t.TempDir()
243	tarPath := filepath.Join(tmpDir, "test.tgz")
244	createTestTarGzFile(t, tarPath)
245
246	info, err := os.Stat(tarPath)
247	require.NoError(t, err)
248
249	td, err := processTarFile(tarPath, info)
250	require.NoError(t, err)
251	require.NotNil(t, td)
252	assert.Equal(t, "test.tgz", td.GetName())
253}
254
255// ---------------------------------------------------------------------------
256// processTarFile – .tar.bz2
257// ---------------------------------------------------------------------------
258
259func TestProcessTarBz2File(t *testing.T) {
260	tmpDir := t.TempDir()
261	tarPath := filepath.Join(tmpDir, "test.tar.bz2")
262	createTestTarBz2File(t, tarPath) // skips if bzip2 unavailable
263
264	info, err := os.Stat(tarPath)
265	require.NoError(t, err)
266
267	td, err := processTarFile(tarPath, info)
268	require.NoError(t, err)
269	require.NotNil(t, td)
270
271	assert.Equal(t, "test.tar.bz2", td.GetName())
272	assert.True(t, td.IsDir())
273	assert.Equal(t, "TarDirectory", td.GetType())
274}
275
276// ---------------------------------------------------------------------------
277// processTarFile – .tar.xz
278// ---------------------------------------------------------------------------
279
280func TestProcessTarXzFile(t *testing.T) {
281	tmpDir := t.TempDir()
282	tarPath := filepath.Join(tmpDir, "test.tar.xz")
283	createTestTarXzFile(t, tarPath)
284
285	info, err := os.Stat(tarPath)
286	require.NoError(t, err)
287
288	td, err := processTarFile(tarPath, info)
289	require.NoError(t, err)
290	require.NotNil(t, td)
291
292	assert.Equal(t, "test.tar.xz", td.GetName())
293	assert.True(t, td.IsDir())
294	assert.Equal(t, "TarDirectory", td.GetType())
295
296	files := slices.Collect(td.GetFiles(fs.SortByName, fs.SortAsc))
297	assert.Greater(t, len(files), 0)
298}
299
300// ---------------------------------------------------------------------------
301// processTarFile – .txz alias
302// ---------------------------------------------------------------------------
303
304func TestProcessTxzFile(t *testing.T) {
305	tmpDir := t.TempDir()
306	tarPath := filepath.Join(tmpDir, "test.txz")
307	createTestTarXzFile(t, tarPath)
308
309	info, err := os.Stat(tarPath)
310	require.NoError(t, err)
311
312	td, err := processTarFile(tarPath, info)
313	require.NoError(t, err)
314	require.NotNil(t, td)
315	assert.Equal(t, "test.txz", td.GetName())
316}
317
318// ---------------------------------------------------------------------------
319// ensureTarDirExists
320// ---------------------------------------------------------------------------
321
322func TestEnsureTarDirExists(t *testing.T) {
323	tarPath := "/fake/archive.tar"
324
325	rootDir := &TarDir{
326		Dir: &Dir{
327			File:  &File{Name: "archive.tar", Flag: 'T'},
328			Files: make(fs.Files, 0),
329		},
330		tarPath: tarPath,
331	}
332
333	dirMap := make(map[string]*TarDir)
334	dirMap[""] = rootDir
335
336	ensureTarDirExists(dirMap, "a/b/c", tarPath, rootDir)
337
338	assert.Contains(t, dirMap, "a")
339	assert.Contains(t, dirMap, "a/b")
340	assert.Contains(t, dirMap, "a/b/c")
341
342	assert.Equal(t, rootDir, dirMap["a"].GetParent())
343	assert.Equal(t, dirMap["a"], dirMap["a/b"].GetParent())
344	assert.Equal(t, dirMap["a/b"], dirMap["a/b/c"].GetParent())
345}
346
347func TestEnsureTarDirExistsIdempotent(t *testing.T) {
348	tarPath := "/fake/archive.tar"
349	rootDir := &TarDir{
350		Dir:     &Dir{File: &File{Name: "archive.tar", Flag: 'T'}, Files: make(fs.Files, 0)},
351		tarPath: tarPath,
352	}
353	dirMap := map[string]*TarDir{"": rootDir}
354
355	ensureTarDirExists(dirMap, "sub", tarPath, rootDir)
356	first := dirMap["sub"]
357	ensureTarDirExists(dirMap, "sub", tarPath, rootDir) // must be idempotent
358	assert.Same(t, first, dirMap["sub"])
359}
360
361// ---------------------------------------------------------------------------
362// TarFile / TarDir method coverage
363// ---------------------------------------------------------------------------
364
365func TestTarFileGetPath(t *testing.T) {
366	tf := &TarFile{
367		File:      &File{Name: "file.txt"},
368		tarPath:   "/path/to/archive.tar",
369		inTarPath: "dir/file.txt",
370	}
371	assert.Equal(t, "/path/to/archive.tar/dir/file.txt", tf.GetPath())
372}
373
374func TestTarFileGetType(t *testing.T) {
375	tf := &TarFile{File: &File{Name: "x"}}
376	assert.Equal(t, "TarFile", tf.GetType())
377}
378
379func TestTarFileEncodeJSON(t *testing.T) {
380	tf := &TarFile{
381		File:      &File{Name: "x.txt", Size: 42},
382		tarPath:   "/a.tar",
383		inTarPath: "x.txt",
384	}
385	var buf bytes.Buffer
386	assert.NoError(t, tf.EncodeJSON(&buf, false, nil))
387	assert.NotEmpty(t, buf.String())
388}
389
390func TestTarDirGetType(t *testing.T) {
391	td := &TarDir{Dir: &Dir{File: &File{Name: "sub"}}}
392	assert.Equal(t, "TarDirectory", td.GetType())
393}
394
395func TestTarDirIsDir(t *testing.T) {
396	td := &TarDir{Dir: &Dir{File: &File{Name: "sub"}}}
397	assert.True(t, td.IsDir())
398}
399
400func TestTarDirEncodeJSON(t *testing.T) {
401	td := &TarDir{
402		Dir:     &Dir{File: &File{Name: "sub"}, Files: make(fs.Files, 0)},
403		tarPath: "/a.tar",
404	}
405	var buf bytes.Buffer
406	assert.NoError(t, td.EncodeJSON(&buf, true, nil))
407	assert.NotEmpty(t, buf.String())
408}
409
410func TestTarDirGetPathWithParent(t *testing.T) {
411	parent := &TarDir{
412		Dir:     &Dir{File: &File{Name: "parent"}},
413		tarPath: "/a.tar",
414	}
415	child := &TarDir{
416		Dir:     &Dir{File: &File{Name: "child"}},
417		tarPath: "/a.tar",
418	}
419	child.Parent = parent
420
421	assert.Equal(t, filepath.Join(parent.GetPath(), "child"), child.GetPath())
422}
423
424func TestTarDirGetPathWithoutParent(t *testing.T) {
425	td := &TarDir{
426		Dir:     &Dir{File: &File{Name: "root"}},
427		tarPath: "/path/to/archive.tar",
428	}
429	assert.Equal(t, "/path/to/archive.tar", td.GetPath())
430}
431
432// ---------------------------------------------------------------------------
433// Integration: SequentialAnalyzer with tar files
434// ---------------------------------------------------------------------------
435
436func TestSequentialAnalyzerWithTarFile(t *testing.T) {
437	tmpDir := t.TempDir()
438	createTestTarFile(t, filepath.Join(tmpDir, "test.tar"))
439
440	a := CreateSeqAnalyzer()
441	a.SetArchiveBrowsing(true)
442
443	result := a.AnalyzeDir(tmpDir,
444		func(string, string) bool { return false },
445		func(string) bool { return false },
446	)
447
448	require.NotNil(t, result)
449
450	var tarItem fs.Item
451	for f := range result.GetFiles(fs.SortByName, fs.SortAsc) {
452		if f.GetName() == "test.tar" {
453			tarItem = f
454			break
455		}
456	}
457	require.NotNil(t, tarItem, "should find test.tar as a browsable directory")
458	assert.True(t, tarItem.IsDir())
459	assert.Equal(t, "TarDirectory", tarItem.GetType())
460
461	count := 0
462	for range tarItem.GetFiles(fs.SortByName, fs.SortAsc) {
463		count++
464	}
465	assert.Greater(t, count, 0, "tar archive should contain browsable content")
466}
467
468func TestSequentialAnalyzerWithTarGzFile(t *testing.T) {
469	tmpDir := t.TempDir()
470	createTestTarGzFile(t, filepath.Join(tmpDir, "test.tar.gz"))
471
472	a := CreateSeqAnalyzer()
473	a.SetArchiveBrowsing(true)
474
475	result := a.AnalyzeDir(tmpDir,
476		func(string, string) bool { return false },
477		func(string) bool { return false },
478	)
479
480	require.NotNil(t, result)
481
482	var tarItem fs.Item
483	for f := range result.GetFiles(fs.SortByName, fs.SortAsc) {
484		if f.GetName() == "test.tar.gz" {
485			tarItem = f
486			break
487		}
488	}
489	require.NotNil(t, tarItem, "should find test.tar.gz as a browsable directory")
490	assert.True(t, tarItem.IsDir())
491}
492
493// ---------------------------------------------------------------------------
494// Integration: ParallelAnalyzer with tar files
495// ---------------------------------------------------------------------------
496
497func TestParallelAnalyzerWithTarFile(t *testing.T) {
498	tmpDir := t.TempDir()
499	createTestTarFile(t, filepath.Join(tmpDir, "archive.tar"))
500
501	a := CreateAnalyzer()
502	a.SetArchiveBrowsing(true)
503
504	result := a.AnalyzeDir(tmpDir,
505		func(string, string) bool { return false },
506		func(string) bool { return false },
507	)
508
509	require.NotNil(t, result)
510
511	var tarItem fs.Item
512	for f := range result.GetFiles(fs.SortByName, fs.SortAsc) {
513		if f.GetName() == "archive.tar" {
514			tarItem = f
515			break
516		}
517	}
518	require.NotNil(t, tarItem, "should find archive.tar")
519	assert.True(t, tarItem.IsDir())
520	assert.Equal(t, "TarDirectory", tarItem.GetType())
521}
522
523func TestParallelAnalyzerWithTarXzFile(t *testing.T) {
524	tmpDir := t.TempDir()
525	createTestTarXzFile(t, filepath.Join(tmpDir, "archive.tar.xz"))
526
527	a := CreateAnalyzer()
528	a.SetArchiveBrowsing(true)
529
530	result := a.AnalyzeDir(tmpDir,
531		func(string, string) bool { return false },
532		func(string) bool { return false },
533	)
534
535	require.NotNil(t, result)
536
537	var tarItem fs.Item
538	for f := range result.GetFiles(fs.SortByName, fs.SortAsc) {
539		if f.GetName() == "archive.tar.xz" {
540			tarItem = f
541			break
542		}
543	}
544	require.NotNil(t, tarItem, "should find archive.tar.xz")
545	assert.True(t, tarItem.IsDir())
546}
547
548// ---------------------------------------------------------------------------
549// Error handling: non-existent / corrupt archive
550// ---------------------------------------------------------------------------
551
552func TestProcessTarFileNotFound(t *testing.T) {
553	_, err := processTarFile("/no/such/file.tar", nil)
554	assert.Error(t, err)
555}
556
557func TestProcessTarFileCorrupt(t *testing.T) {
558	tmpDir := t.TempDir()
559	p := filepath.Join(tmpDir, "bad.tar")
560	require.NoError(t, os.WriteFile(p, []byte("not a tar file at all"), 0o600))
561
562	info, err := os.Stat(p)
563	require.NoError(t, err)
564
565	// A corrupt plain-tar should still open (it's a file), but Next() will fail.
566	// processTarFile should propagate the error.
567	_, err = processTarFile(p, info)
568	assert.Error(t, err)
569}
570
571func TestProcessTarGzFileCorrupt(t *testing.T) {
572	tmpDir := t.TempDir()
573	p := filepath.Join(tmpDir, "bad.tar.gz")
574	require.NoError(t, os.WriteFile(p, []byte("not gzip"), 0o600))
575
576	info, err := os.Stat(p)
577	require.NoError(t, err)
578
579	_, err = processTarFile(p, info)
580	assert.Error(t, err)
581}
582
583func TestProcessTarXzFileCorrupt(t *testing.T) {
584	tmpDir := t.TempDir()
585	p := filepath.Join(tmpDir, "bad.tar.xz")
586	require.NoError(t, os.WriteFile(p, []byte("not xz"), 0o600))
587
588	info, err := os.Stat(p)
589	require.NoError(t, err)
590
591	_, err = processTarFile(p, info)
592	assert.Error(t, err)
593}
594
595// ---------------------------------------------------------------------------
596// Archive browsing disabled – tar file treated as regular file
597// ---------------------------------------------------------------------------
598
599func TestAnalyzerTarBrowsingDisabled(t *testing.T) {
600	tmpDir := t.TempDir()
601	createTestTarFile(t, filepath.Join(tmpDir, "archive.tar"))
602
603	a := CreateSeqAnalyzer()
604	// archiveBrowsing is false by default
605
606	result := a.AnalyzeDir(tmpDir,
607		func(string, string) bool { return false },
608		func(string) bool { return false },
609	)
610
611	require.NotNil(t, result)
612
613	var tarItem fs.Item
614	for f := range result.GetFiles(fs.SortByName, fs.SortAsc) {
615		if f.GetName() == "archive.tar" {
616			tarItem = f
617			break
618		}
619	}
620	require.NotNil(t, tarItem)
621	assert.False(t, tarItem.IsDir(), "when browsing disabled, tar is a plain file")
622}
623
624// ---------------------------------------------------------------------------
625// bzip2 compression helper (uses os/exec "bzip2")
626// ---------------------------------------------------------------------------
627
628func bzip2CompressFunc() (func([]byte) ([]byte, error), error) {
629	return bzip2CompressWithCmd()
630}
631
632func bzip2CompressWithCmd() (func([]byte) ([]byte, error), error) {
633	bzip2Path, err := exec.LookPath("bzip2")
634	if err != nil {
635		return nil, err
636	}
637	return func(data []byte) ([]byte, error) {
638		cmd := exec.Command(bzip2Path, "--compress", "--stdout")
639		cmd.Stdin = bytes.NewReader(data)
640		return cmd.Output()
641	}, nil
642}
643
644// Ensure compress/bzip2 package is used (for reading .tar.bz2)
645var _ = bzip2.NewReader