fork/gdu
git clone https://git.julia.blue/fork/gdu
daaae96
master
1package tui 2 3import ( 4"bytes" 5"strings" 6"testing" 7 8"github.com/dundee/gdu/v5/internal/testapp" 9"github.com/dundee/gdu/v5/pkg/analyze" 10"github.com/dundee/gdu/v5/pkg/fs" 11"github.com/rivo/tview" 12"github.com/stretchr/testify/assert" 13) 14 15func TestHelpMoveToTrash (t * testing.T ) { 16app ,simScreen := testapp .CreateTestAppWithSimScreen (50 ,50 ) 17defer simScreen .Fini () 18 19ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,false ,false ) 20 21helpText := ui .formatHelpTextFor () 22 23assert .True (t ,strings .Contains (helpText ,"Move file or directory to trash" )) 24assert .False (t ,strings .Contains (helpText ,"Move file or directory to trash (disabled)" )) 25} 26 27func TestHelpListsCopyPath (t * testing.T ) { 28app ,simScreen := testapp .CreateTestAppWithSimScreen (50 ,50 ) 29defer simScreen .Fini () 30 31ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,false ,false ) 32 33helpText := ui .formatHelpTextFor () 34 35assert .True (t ,strings .Contains (helpText ,"Copy path of file or directory to clipboard" )) 36} 37 38func TestHelpNoSpawnShell (t * testing.T ) { 39app ,simScreen := testapp .CreateTestAppWithSimScreen (50 ,50 ) 40defer simScreen .Fini () 41 42ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,false ,false ) 43ui .SetNoDelete () 44ui .SetNoSpawnShell () 45ui .SetNoViewFile () 46ui .showHelp () 47 48assert .True (t ,ui .pages .HasPage ("help" )) 49 50helpText := ui .formatHelpTextFor () 51 52assert .True (t ,strings .Contains (helpText ,"Delete file or directory (disabled)" )) 53assert .True (t ,strings .Contains (helpText ,"Empty file or directory (disabled)" )) 54assert .True (t ,strings .Contains (helpText ,"Move file or directory to trash (disabled)" )) 55assert .True (t ,strings .Contains (helpText ,"Spawn shell in current directory (disabled)" )) 56assert .True (t ,strings .Contains (helpText ,"Open file or directory in external program (disabled)" )) 57assert .True (t ,strings .Contains (helpText ,"Show content of file (disabled)" )) 58} 59 60func TestCollapsePathFlag (t * testing.T ) { 61app := testapp .CreateMockedApp (true ) 62simScreen := testapp .CreateSimScreen () 63defer simScreen .Fini () 64ui := CreateUI (app ,simScreen ,& bytes.Buffer {},false ,false ,false ,false ) 65 66// Create a collapsible structure 67deepestDir := & analyze.Dir { 68File :& analyze.File { 69Name :"deepest" , 70Usage :100 , 71Size :100 , 72 }, 73Files : []fs.Item {}, 74 } 75middleDir := & analyze.Dir { 76File :& analyze.File { 77Name :"middle" , 78Usage :100 , 79Size :100 , 80 }, 81Files : []fs.Item {deepestDir }, 82 } 83topDir := & analyze.Dir { 84File :& analyze.File { 85Name :"top" , 86 }, 87Files : []fs.Item {middleDir }, 88 } 89deepestDir .SetParent (middleDir ) 90middleDir .SetParent (topDir ) 91 92ui .currentDir = topDir 93ui .topDir = topDir 94ui .topDirPath = "top" 95 96// Default (flag false) -> Should NOT collapse 97ui .showDir () 98cell := ui .table .GetCell (0 ,0 ) 99assert .Contains (t ,cell .Text ,"middle" ) 100assert .NotContains (t ,cell .Text ,"deepest" ) 101 102// Enable flag -> Should collapse 103ui .SetCollapsePath (true ) 104ui .showDir () 105cell = ui .table .GetCell (0 ,0 ) 106assert .Contains (t ,cell .Text ,"middle/deepest" ) 107} 108 109// TestColumnsWidthMatchesRenderedRow pins ui.columnsWidth() to the width the 110// columns actually occupy on screen, for every combination of optional columns. 111// The "/.." row is padded with columnsWidth() spaces, so if the two drift apart 112// that row stops lining up with the listing below it. 113func TestColumnsWidthMatchesRenderedRow (t * testing.T ) { 114simScreen := testapp .CreateSimScreen () 115defer simScreen .Fini () 116 117app := testapp .CreateMockedApp (false ) 118ui := CreateUI (app ,simScreen ,& bytes.Buffer {},false ,true ,false ,false ) 119 120root := & analyze.Dir { 121File :& analyze.File {Name :"root" ,Usage :1000 ,Size :1000 }, 122ItemCount :1 , 123BasePath :"." , 124 } 125sub := & analyze.Dir { 126File :& analyze.File {Name :"subdir" ,Usage :700 ,Size :700 ,Parent :root ,Flag :' ' }, 127ItemCount :11 , 128 } 129root .Files = fs.Files {sub } 130 131maxima := rowMaxima {usage :1000 ,size :1000 ,count :10 } 132 133for _ ,tc := range []struct { 134name string 135oldBar ,percentage ,count ,countBar ,mtime ,marked bool 136 }{ 137 {name :"defaults" }, 138 {name :"old bar" ,oldBar :true }, 139 {name :"percentage" ,percentage :true }, 140 {name :"count" ,count :true }, 141 {name :"count and bar" ,count :true ,countBar :true }, 142 {name :"count bar without count column" ,countBar :true }, 143 {name :"count bar with old bar" ,count :true ,countBar :true ,oldBar :true }, 144 {name :"mtime" ,mtime :true }, 145 {name :"marked" ,marked :true }, 146 { 147name :"everything" ,oldBar :true ,percentage :true , 148count :true ,countBar :true ,mtime :true ,marked :true , 149 }, 150 } { 151t .Run (tc .name ,func (t * testing.T ) { 152ui .useOldSizeBar = tc .oldBar 153ui .showBarPercentage = tc .percentage 154ui .showItemCount = tc .count 155ui .showItemCountBar = tc .countBar 156ui .showMtime = tc .mtime 157ui .markedRows = map [int ]struct {}{} 158if tc .marked { 159ui .markedRows [0 ]= struct {}{} 160 } 161 162rendered := ui .formatColumns (sub ,maxima ,false ,false ) 163 164assert .Equal (t ,ui .columnsWidth (),tview .TaggedStringWidth (rendered )) 165 }) 166 } 167} 168 169// TestParentRowIsPaddedToColumnsWidth checks the "/.." row is indented by 170// exactly the width of the columns it has none of. 171func TestParentRowIsPaddedToColumnsWidth (t * testing.T ) { 172simScreen := testapp .CreateSimScreen () 173defer simScreen .Fini () 174 175app := testapp .CreateMockedApp (false ) 176ui := CreateUI (app ,simScreen ,& bytes.Buffer {},false ,true ,false ,false ) 177ui .showItemCount = true 178ui .showItemCountBar = true 179 180root := & analyze.Dir { 181File :& analyze.File {Name :"root" ,Usage :1000 ,Size :1000 }, 182ItemCount :1 , 183BasePath :"." , 184 } 185root .Files = fs.Files { 186& analyze.Dir { 187File :& analyze.File {Name :"subdir" ,Usage :700 ,Size :700 ,Parent :root ,Flag :' ' }, 188ItemCount :11 , 189 }, 190 } 191 192ui .currentDir = root 193ui .topDir = root 194// differs from the current dir, so the "/.." row is rendered 195ui .topDirPath = "/somewhere/else" 196 197ui .showDir () 198 199parentRow := ui .table .GetCell (0 ,0 ).Text 200 201assert .Equal (t ,strings .Repeat (" " ,ui .columnsWidth ())+ "[::b]/.." ,parentRow ) 202}