fork/gdu

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

ShivamB25feat: stop scanning and keep partial results (#606)99a7f2a

ignore
5.9 KiB252 linesraw
1// Package common contains commong logic and interfaces used across Gdu
2// nolint: revive //Why: this is common package
3package common
4
5import (
6	"os"
7	"testing"
8
9	"github.com/dundee/gdu/v5/pkg/fs"
10	"github.com/stretchr/testify/assert"
11)
12
13func TestFormatNumber(t *testing.T) {
14	res := FormatNumber(1234567890)
15	assert.Equal(t, "1,234,567,890", res)
16}
17
18func TestSetFollowSymlinks(t *testing.T) {
19	ui := UI{
20		Analyzer: &MockedAnalyzer{},
21	}
22	ui.SetFollowSymlinks(true)
23
24	assert.Equal(t, true, ui.Analyzer.(*MockedAnalyzer).FollowSymlinks)
25}
26
27func TestSetShowAnnexedSize(t *testing.T) {
28	ui := UI{
29		Analyzer: &MockedAnalyzer{},
30	}
31	ui.SetShowAnnexedSize(true)
32
33	assert.Equal(t, true, ui.Analyzer.(*MockedAnalyzer).ShowAnnexedSize)
34}
35
36func TestSetEnableArchiveBrowsing(t *testing.T) {
37	ui := UI{
38		Analyzer: &MockedAnalyzer{},
39	}
40	ui.SetArchiveBrowsing(true)
41
42	assert.Equal(t, true, ui.Analyzer.(*MockedAnalyzer).ArchiveBrowsing)
43}
44
45func TestSetAnalyzer(t *testing.T) {
46	ui := UI{}
47	a := &MockedAnalyzer{}
48	ui.SetAnalyzer(a)
49	assert.Equal(t, a, ui.Analyzer)
50}
51
52func TestSetTimeFilter(t *testing.T) {
53	ui := UI{Analyzer: &MockedAnalyzer{}}
54	assert.NotPanics(t, func() {
55		ui.SetTimeFilter(nil)
56	})
57}
58
59type MockedAnalyzer struct {
60	FollowSymlinks  bool
61	ShowAnnexedSize bool
62	ArchiveBrowsing bool
63}
64
65// SetFileTypeFilter sets the file type filter function
66func (a *MockedAnalyzer) SetFileTypeFilter(filter ShouldFileBeIgnored) {
67	// Mock implementation - do nothing
68}
69
70// AnalyzeDir returns dir with files with different size exponents
71func (a *MockedAnalyzer) AnalyzeDir(
72	path string, ignore ShouldDirBeIgnored, fileTypeFilter ShouldFileBeIgnored,
73) fs.Item {
74	return nil
75}
76
77// GetProgress returns empty progress
78func (a *MockedAnalyzer) GetProgress() CurrentProgress {
79	return CurrentProgress{}
80}
81
82// GetDone returns always Done
83func (a *MockedAnalyzer) GetDone() SignalGroup {
84	c := make(SignalGroup)
85	defer c.Broadcast()
86	return c
87}
88
89// ResetProgress does nothing
90func (a *MockedAnalyzer) ResetProgress() {}
91
92func (a *MockedAnalyzer) Cancel() {}
93
94// SetFollowSymlinks does nothing
95func (a *MockedAnalyzer) SetFollowSymlinks(v bool) {
96	a.FollowSymlinks = v
97}
98
99// SetShowAnnexedSize does nothing
100func (a *MockedAnalyzer) SetShowAnnexedSize(v bool) {
101	a.ShowAnnexedSize = v
102}
103
104// SetTimeFilter does nothing
105func (a *MockedAnalyzer) SetTimeFilter(timeFilter TimeFilter) {}
106
107// SetArchiveBrowsing sets EnableArchiveBrowsing
108func (a *MockedAnalyzer) SetArchiveBrowsing(v bool) {
109	a.ArchiveBrowsing = v
110}
111
112func TestSetBlockSizeFromEnvironment(t *testing.T) {
113	t.Run("BLOCK_SIZE takes precedence", func(t *testing.T) {
114		t.Setenv("BLOCK_SIZE", "1K")
115		t.Setenv("BLOCKSIZE", "1MB")
116		ui := &UI{}
117		ui.SetBlockSizeFromEnvironment()
118
119		formatted, ok := ui.FormatBlockSize(1025)
120		assert.True(t, ok)
121		assert.Equal(t, "2", formatted)
122	})
123
124	t.Run("BLOCKSIZE fallback", func(t *testing.T) {
125		unsetBlockSizeEnvironment(t)
126		t.Setenv("BLOCKSIZE", "2kB")
127		ui := &UI{}
128		ui.SetBlockSizeFromEnvironment()
129
130		formatted, ok := ui.FormatBlockSize(2001)
131		assert.True(t, ok)
132		assert.Equal(t, "2", formatted)
133	})
134
135	t.Run("human readable", func(t *testing.T) {
136		t.Setenv("BLOCK_SIZE", "human-readable")
137		ui := &UI{}
138		ui.SetBlockSizeFromEnvironment()
139
140		_, ok := ui.FormatBlockSize(1)
141		assert.False(t, ok)
142		assert.False(t, ui.UseSIPrefix)
143	})
144
145	t.Run("si", func(t *testing.T) {
146		t.Setenv("BLOCK_SIZE", "si")
147		ui := &UI{}
148		ui.SetBlockSizeFromEnvironment()
149
150		assert.True(t, ui.UseSIPrefix)
151	})
152
153	t.Run("invalid value", func(t *testing.T) {
154		t.Setenv("BLOCK_SIZE", "invalid")
155		ui := &UI{}
156		ui.SetBlockSizeFromEnvironment()
157
158		_, ok := ui.FormatBlockSize(1)
159		assert.False(t, ok)
160	})
161
162	t.Run("absent", func(t *testing.T) {
163		unsetBlockSizeEnvironment(t)
164		ui := &UI{}
165		ui.SetBlockSizeFromEnvironment()
166
167		_, ok := ui.FormatBlockSize(1)
168		assert.False(t, ok)
169	})
170}
171
172func TestFormatBlockSize(t *testing.T) {
173	ui := &UI{blockSize: 1000, blockSuffix: "kB"}
174
175	tests := []struct {
176		size     int64
177		expected string
178	}{
179		{size: -1, expected: "0kB"},
180		{size: 0, expected: "0kB"},
181		{size: 1000, expected: "1kB"},
182		{size: 1001, expected: "2kB"},
183	}
184
185	for _, test := range tests {
186		formatted, ok := ui.FormatBlockSize(test.size)
187		assert.True(t, ok)
188		assert.Equal(t, test.expected, formatted)
189	}
190}
191
192func TestParseBlockSize(t *testing.T) {
193	tests := []struct {
194		value  string
195		size   int64
196		suffix string
197		valid  bool
198	}{
199		{value: "1", size: 1, valid: true},
200		{value: "k", size: 1 << 10, suffix: "k", valid: true},
201		{value: "kB", size: 1e3, suffix: "kB", valid: true},
202		{value: "1K", size: 1 << 10, valid: true},
203		{value: "1M", size: 1 << 20, valid: true},
204		{value: "1MB", size: 1e6, valid: true},
205		{value: "1G", size: 1 << 30, valid: true},
206		{value: "1GB", size: 1e9, valid: true},
207		{value: "1T", size: 1 << 40, valid: true},
208		{value: "1TB", size: 1e12, valid: true},
209		{value: "1P", size: 1 << 50, valid: true},
210		{value: "1PB", size: 1e15, valid: true},
211		{value: "1E", size: 1 << 60, valid: true},
212		{value: "1EB", size: 1e18, valid: true},
213		{value: "", valid: false},
214		{value: "0", valid: false},
215		{value: "-1", valid: false},
216		{value: "1Z", valid: false},
217		{value: "8E", valid: false},
218		{value: "999999999999999999999", valid: false},
219		{value: "'1", valid: false},
220	}
221
222	for _, test := range tests {
223		t.Run(test.value, func(t *testing.T) {
224			size, suffix, ok := parseBlockSize(test.value)
225			assert.Equal(t, test.valid, ok)
226			if ok {
227				assert.Equal(t, test.size, size)
228				assert.Equal(t, test.suffix, suffix)
229			}
230		})
231	}
232}
233
234func unsetBlockSizeEnvironment(t *testing.T) {
235	t.Helper()
236	blockSize, blockSizeSet := os.LookupEnv("BLOCK_SIZE")
237	legacyBlockSize, legacyBlockSizeSet := os.LookupEnv("BLOCKSIZE")
238	os.Unsetenv("BLOCK_SIZE")
239	os.Unsetenv("BLOCKSIZE")
240	t.Cleanup(func() {
241		if blockSizeSet {
242			os.Setenv("BLOCK_SIZE", blockSize)
243		} else {
244			os.Unsetenv("BLOCK_SIZE")
245		}
246		if legacyBlockSizeSet {
247			os.Setenv("BLOCKSIZE", legacyBlockSize)
248		} else {
249			os.Unsetenv("BLOCKSIZE")
250		}
251	})
252}