fork/gdu
git clone https://git.julia.blue/fork/gdu
34f3996
master
1package tui 2 3import ( 4"bytes" 5"os" 6"path/filepath" 7"testing" 8 9"github.com/dundee/gdu/v5/internal/testapp" 10"github.com/dundee/gdu/v5/pkg/analyze" 11"github.com/gdamore/tcell/v2" 12"github.com/stretchr/testify/assert" 13"github.com/stretchr/testify/require" 14) 15 16// createMultiTestDirs creates two sibling directories to be scanned together. 17func createMultiTestDirs (t * testing.T ) (first ,second string ) { 18t .Helper () 19 20require .NoError (t ,os .MkdirAll ("multi_a/nested" ,os .ModePerm )) 21require .NoError (t ,os .MkdirAll ("multi_b/other" ,os .ModePerm )) 22require .NoError (t ,os .WriteFile ("multi_a/nested/file" , []byte ("hello" ),0o600 )) 23require .NoError (t ,os .WriteFile ("multi_b/other/file2" , []byte ("go" ),0o600 )) 24 25t .Cleanup (func () { 26assert .NoError (t ,os .RemoveAll ("multi_a" )) 27assert .NoError (t ,os .RemoveAll ("multi_b" )) 28 }) 29 30return "multi_a" ,"multi_b" 31} 32 33// createNestedTestDirs creates two directories that share a base name but live 34// under different parents. 35func createNestedTestDirs (t * testing.T ) (first ,second string ) { 36first = filepath .Join ("outer_p" ,"data" ) 37second = filepath .Join ("outer_q" ,"data" ) 38 39for _ ,dir := range []string {first ,second } { 40require .NoError (t ,os .MkdirAll (dir ,os .ModePerm )) 41require .NoError (t ,os .WriteFile (filepath .Join (dir ,"file" ), []byte ("hello" ),0o600 )) 42 } 43 44t .Cleanup (func () { 45assert .NoError (t ,os .RemoveAll ("outer_p" )) 46assert .NoError (t ,os .RemoveAll ("outer_q" )) 47 }) 48 49return first ,second 50} 51 52// analyzeMultiplePaths runs a multi-root scan to completion and flushes the 53// queued UI updates, as the TUI tests do for single-path scans. 54func analyzeMultiplePaths (t * testing.T ,paths []string )* UI { 55t .Helper () 56 57simScreen := testapp .CreateSimScreen () 58t .Cleanup (simScreen .Fini ) 59 60app := testapp .CreateMockedApp (true ) 61ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,false ,false ) 62ui .done = make (chan struct {}) 63 64require .NoError (t ,ui .AnalyzePaths (paths )) 65 66<- ui .done 67for _ ,draw := range ui .app .(* testapp.MockedApp ).GetUpdateDraws () { 68draw () 69 } 70 71return ui 72} 73 74func TestAnalyzePathsGroupsRootsUnderVirtualDir (t * testing.T ) { 75first ,second := createMultiTestDirs (t ) 76 77ui := analyzeMultiplePaths (t , []string {first ,second }) 78 79assert .True (t ,analyze .IsVirtualRootDir (ui .currentDir )) 80assert .Equal (t ,analyze .VirtualRootName ,ui .topDirPath ) 81// no "/.." row is shown at the top, so the rows are exactly the two roots 82assert .Equal (t ,2 ,ui .table .GetRowCount ()) 83 84names := []string {ui .table .GetCell (0 ,0 ).Text ,ui .table .GetCell (1 ,0 ).Text } 85assert .Contains (t ,names [0 ]+ names [1 ],first ) 86assert .Contains (t ,names [0 ]+ names [1 ],second ) 87} 88 89// Roots from different parents can share a base name, so rows under the virtual 90// top level dir are labelled with the absolute path instead. 91func TestRootsAreLabelledWithTheirAbsolutePath (t * testing.T ) { 92first ,second := createNestedTestDirs (t ) 93 94ui := analyzeMultiplePaths (t , []string {first ,second }) 95 96rows := ui .table .GetCell (0 ,0 ).Text + "\n" + ui .table .GetCell (1 ,0 ).Text 97assert .Contains (t ,rows ,first ) 98assert .Contains (t ,rows ,second ) 99// the path is not given a second leading separator by the directory marker 100assert .NotContains (t ,rows ,string (filepath .Separator )+ first ) 101} 102 103func TestRootsKeepBaseNamesBelowTheVirtualDir (t * testing.T ) { 104first ,second := createMultiTestDirs (t ) 105ui := analyzeMultiplePaths (t , []string {first ,second }) 106 107// descend into the first root; its children are ordinary rows again 108ui .table .Select (0 ,0 ) 109ui .handleRight () 110 111assert .False (t ,analyze .IsVirtualRootDir (ui .currentDir )) 112assert .Contains (t ,ui .table .GetCell (1 ,0 ).Text ,"/nested" ) 113} 114 115func TestFilteringAtVirtualRootMatchesThePathShown (t * testing.T ) { 116first ,second := createNestedTestDirs (t ) 117ui := analyzeMultiplePaths (t , []string {first ,second }) 118 119// "outer_p" appears only in the path, never in the base name 120ui .filterValue = "outer_p" 121ui .showDir () 122 123assert .Equal (t ,1 ,ui .table .GetRowCount ()) 124assert .Contains (t ,ui .table .GetCell (0 ,0 ).Text ,first ) 125} 126 127func TestAnalyzePathsKeepsRealPathsOfRoots (t * testing.T ) { 128first ,second := createMultiTestDirs (t ) 129 130ui := analyzeMultiplePaths (t , []string {first ,second }) 131 132paths := ui .scannedRootPaths () 133assert .ElementsMatch (t , []string {first ,second },paths ) 134} 135 136func TestAnalyzePathsSumsRootStats (t * testing.T ) { 137first ,second := createMultiTestDirs (t ) 138 139ui := analyzeMultiplePaths (t , []string {first ,second }) 140 141var rootSize int64 142for item := range ui .currentDir .GetFiles (0 ,0 ) { 143rootSize += item .GetSize () 144 } 145assert .Equal (t ,rootSize ,ui .currentDir .GetSize ()) 146} 147 148func TestAnalyzePathsWithSinglePathHasNoVirtualDir (t * testing.T ) { 149first ,_ := createMultiTestDirs (t ) 150 151ui := analyzeMultiplePaths (t , []string {first }) 152 153assert .False (t ,analyze .IsVirtualRootDir (ui .currentDir )) 154assert .Equal (t ,first ,ui .currentDir .GetName ()) 155} 156 157func TestEnteringAndLeavingARootUnderTheVirtualDir (t * testing.T ) { 158first ,second := createMultiTestDirs (t ) 159ui := analyzeMultiplePaths (t , []string {first ,second }) 160 161// enter the first root 162ui .table .Select (0 ,0 ) 163enteredName := ui .currentDir .GetName () 164ui .handleRight () 165assert .NotEqual (t ,enteredName ,ui .currentDir .GetName ()) 166assert .False (t ,analyze .IsVirtualRootDir (ui .currentDir )) 167 168// and back out to the virtual root via the "/.." row 169ui .handleLeft () 170assert .True (t ,analyze .IsVirtualRootDir (ui .currentDir )) 171} 172 173func TestHandleLeftAtVirtualRootDoesNotBrowseParent (t * testing.T ) { 174first ,second := createMultiTestDirs (t ) 175ui := analyzeMultiplePaths (t , []string {first ,second }) 176ui .SetBrowseParentDirs () 177 178ui .handleLeft () 179 180// the virtual root has no parent on disk, so nothing may be rescanned 181assert .True (t ,analyze .IsVirtualRootDir (ui .currentDir )) 182assert .Equal (t ,analyze .VirtualRootName ,ui .topDirPath ) 183} 184 185func TestRescanAtVirtualRootRescansAllRoots (t * testing.T ) { 186first ,second := createMultiTestDirs (t ) 187ui := analyzeMultiplePaths (t , []string {first ,second }) 188 189ui .done = make (chan struct {}) 190ui .rescanDir () 191<- ui .done 192for _ ,draw := range ui .app .(* testapp.MockedApp ).GetUpdateDraws () { 193draw () 194 } 195 196assert .True (t ,analyze .IsVirtualRootDir (ui .currentDir )) 197assert .ElementsMatch (t , []string {first ,second },ui .scannedRootPaths ()) 198} 199 200func TestExportAtVirtualRootIsRejected (t * testing.T ) { 201first ,second := createMultiTestDirs (t ) 202ui := analyzeMultiplePaths (t , []string {first ,second }) 203 204ui .exportAnalysis () 205 206assert .Contains (t ,ui .header .GetText (false ),"Export is not supported" ) 207assert .False (t ,ui .pages .HasPage ("exporting" )) 208} 209 210func TestSpawnShellAtVirtualRootIsRejected (t * testing.T ) { 211first ,second := createMultiTestDirs (t ) 212ui := analyzeMultiplePaths (t , []string {first ,second }) 213called := false 214ui .exec = func (argv0 string ,argv ,envv []string )error { 215called = true 216return nil 217 } 218 219assert .Nil (t ,ui .keyPressed (tcell .NewEventKey (tcell .KeyRune ,'b' ,0 ))) 220 221assert .False (t ,called ) 222assert .Contains (t ,ui .header .GetText (false ),"Shell cannot be spawned" ) 223}