fork/gdu

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

Daniel Mildefeat: allow scanning multiple directories (#653)34f3996

master
3.4 KiB100 linesraw
1package stdout
2
3import (
4	"bytes"
5	"os"
6	"path/filepath"
7	"testing"
8
9	"github.com/dundee/gdu/v5/pkg/analyze"
10	"github.com/stretchr/testify/assert"
11	"github.com/stretchr/testify/require"
12)
13
14func createMultiTestDirs(t *testing.T) (first, second string) {
15	t.Helper()
16
17	require.NoError(t, os.MkdirAll("multi_a/nested", os.ModePerm))
18	require.NoError(t, os.MkdirAll("multi_b/other", os.ModePerm))
19	require.NoError(t, os.WriteFile("multi_a/nested/file", []byte("hello"), 0o600))
20	require.NoError(t, os.WriteFile("multi_b/other/file2", []byte("go"), 0o600))
21
22	t.Cleanup(func() {
23		assert.NoError(t, os.RemoveAll("multi_a"))
24		assert.NoError(t, os.RemoveAll("multi_b"))
25	})
26
27	return "multi_a", "multi_b"
28}
29
30func TestAnalyzePathsListsEveryRoot(t *testing.T) {
31	first, second := createMultiTestDirs(t)
32	output := &bytes.Buffer{}
33
34	ui := CreateStdoutUI(output, false, false, false, false, false, false, false, "", 0, false, 0)
35	require.NoError(t, ui.AnalyzePaths([]string{first, second}))
36
37	assert.Contains(t, output.String(), first)
38	assert.Contains(t, output.String(), second)
39}
40
41// Roots from different parents can share a base name, so they are listed by
42// absolute path instead.
43func TestAnalyzePathsLabelsRootsWithTheirPath(t *testing.T) {
44	first := filepath.Join("outer_p", "data")
45	second := filepath.Join("outer_q", "data")
46	for _, dir := range []string{first, second} {
47		require.NoError(t, os.MkdirAll(dir, os.ModePerm))
48		require.NoError(t, os.WriteFile(filepath.Join(dir, "file"), []byte("hello"), 0o600))
49	}
50	t.Cleanup(func() {
51		assert.NoError(t, os.RemoveAll("outer_p"))
52		assert.NoError(t, os.RemoveAll("outer_q"))
53	})
54
55	output := &bytes.Buffer{}
56	ui := CreateStdoutUI(output, false, false, false, false, false, false, false, "", 0, false, 0)
57	require.NoError(t, ui.AnalyzePaths([]string{first, second}))
58
59	assert.Contains(t, output.String(), first)
60	assert.Contains(t, output.String(), second)
61	// the path is not given a second leading separator by the directory marker
62	assert.NotContains(t, output.String(), string(filepath.Separator)+first)
63}
64
65func TestAnalyzePathsSummarizeReportsCombinedTotal(t *testing.T) {
66	first, second := createMultiTestDirs(t)
67	output := &bytes.Buffer{}
68
69	ui := CreateStdoutUI(output, false, false, false, false, true, false, false, "", 0, false, 0)
70	require.NoError(t, ui.AnalyzePaths([]string{first, second}))
71
72	assert.NotEmpty(t, output.String())
73}
74
75func TestAnalyzePathsSwapsTopDirAnalyzerForFullTree(t *testing.T) {
76	first, second := createMultiTestDirs(t)
77	output := &bytes.Buffer{}
78
79	ui := CreateStdoutUI(output, false, false, false, false, false, false, false, "", 0, false, 0)
80	// the default stdout analyzer produces SimpleDir, which cannot be re-parented
81	_, isTopDirAnalyzer := ui.Analyzer.(*analyze.TopDirAnalyzer)
82	require.True(t, isTopDirAnalyzer)
83
84	require.NoError(t, ui.AnalyzePaths([]string{first, second}))
85
86	_, stillTopDirAnalyzer := ui.Analyzer.(*analyze.TopDirAnalyzer)
87	assert.False(t, stillTopDirAnalyzer)
88}
89
90func TestAnalyzePathsWithSinglePathBehavesLikeAnalyzePath(t *testing.T) {
91	first, _ := createMultiTestDirs(t)
92	output := &bytes.Buffer{}
93
94	ui := CreateStdoutUI(output, false, false, false, false, false, false, false, "", 0, false, 0)
95	require.NoError(t, ui.AnalyzePaths([]string{first}))
96
97	// contents of the single root are listed, not the root itself
98	assert.Contains(t, output.String(), "nested")
99	assert.NotContains(t, output.String(), analyze.VirtualRootName)
100}