1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
package analyze
import (
"os"
"testing"
"time"
"github.com/dundee/gdu/v5/internal/testdir"
"github.com/stretchr/testify/assert"
)
func TestParallelAnalyzerSetFollowSymlinks(t *testing.T) {
analyzer := CreateAnalyzer()
analyzer.SetFollowSymlinks(true)
assert.True(t, analyzer.followSymlinks)
analyzer.SetFollowSymlinks(false)
assert.False(t, analyzer.followSymlinks)
}
func TestParallelAnalyzerSetShowAnnexedSize(t *testing.T) {
analyzer := CreateAnalyzer()
analyzer.SetShowAnnexedSize(true)
assert.True(t, analyzer.gitAnnexedSize)
analyzer.SetShowAnnexedSize(false)
assert.False(t, analyzer.gitAnnexedSize)
}
func TestGetDirFlagWithError(t *testing.T) {
flag := getDirFlag(os.ErrNotExist, 5)
assert.Equal(t, '!', flag)
}
func TestGetDirFlagWithEmptyDir(t *testing.T) {
flag := getDirFlag(nil, 0)
assert.Equal(t, 'e', flag)
}
func TestGetDirFlagWithNormalDir(t *testing.T) {
flag := getDirFlag(nil, 5)
assert.Equal(t, ' ', flag)
}
func TestGetFlagWithSymlink(t *testing.T) {
// Create a temporary symlink
symlinkPath := "/tmp/test_symlink"
defer os.Remove(symlinkPath)
err := os.Symlink("/tmp", symlinkPath)
assert.NoError(t, err)
info, err := os.Lstat(symlinkPath)
assert.NoError(t, err)
flag := getFlag(info)
assert.Equal(t, '@', flag)
}
func TestGetFlagWithRegularFile(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
info, err := os.Stat("test_dir/nested/file2")
assert.NoError(t, err)
flag := getFlag(info)
assert.Equal(t, ' ', flag)
}
func TestParallelAnalyzerUpdateProgress(t *testing.T) {
analyzer := CreateAnalyzer()
// Start the progress updater
go analyzer.updateProgress()
// Send some progress updates
analyzer.progressChan <- struct {
CurrentItemName string
ItemCount int64
TotalSize int64
}{
CurrentItemName: "test",
ItemCount: 5,
TotalSize: 100,
}
// Wait a bit for the progress to be processed
time.Sleep(10 * time.Millisecond)
// Send done signal
analyzer.progressDoneChan <- struct{}{}
// Wait for the updater to finish
time.Sleep(10 * time.Millisecond)
}
func TestParallelAnalyzerUpdateProgressWithDefaultCase(t *testing.T) {
analyzer := CreateAnalyzer()
// Start the progress updater
go analyzer.updateProgress()
// Send some progress updates
analyzer.progressChan <- struct {
CurrentItemName string
ItemCount int64
TotalSize int64
}{
CurrentItemName: "test",
ItemCount: 5,
TotalSize: 100,
}
// Wait a bit for the progress to be processed
time.Sleep(10 * time.Millisecond)
// Send another progress update to trigger the default case
analyzer.progressChan <- struct {
CurrentItemName string
ItemCount int64
TotalSize int64
}{
CurrentItemName: "test2",
ItemCount: 3,
TotalSize: 50,
}
// Wait a bit for the progress to be processed
time.Sleep(10 * time.Millisecond)
// Send done signal
analyzer.progressDoneChan <- struct{}{}
// Wait for the updater to finish
time.Sleep(10 * time.Millisecond)
}
func TestParallelAnalyzerAnalyzeDirWithIgnoreDir(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
analyzer := CreateAnalyzer()
dir := analyzer.AnalyzeDir(
"test_dir", func(name, _ string) bool { return name == "nested" }, func(_ string) bool { return false },
).(*Dir)
analyzer.GetDone().Wait()
assert.NotNil(t, dir)
assert.Equal(t, "test_dir", dir.Name)
// Should have fewer items since nested directory was ignored
assert.Less(t, dir.ItemCount, int64(5))
}
|