fork/gdu
git clone https://git.julia.blue/fork/gdu
34f3996
master
1package stdout 2 3import ( 4"bytes" 5"os" 6"path/filepath" 7"regexp" 8"strings" 9"testing" 10 11 log"github.com/sirupsen/logrus" 12 13"github.com/dundee/gdu/v5/internal/testanalyze" 14"github.com/dundee/gdu/v5/internal/testdev" 15"github.com/dundee/gdu/v5/internal/testdir" 16"github.com/dundee/gdu/v5/pkg/analyze" 17"github.com/dundee/gdu/v5/pkg/device" 18"github.com/stretchr/testify/assert" 19) 20 21func init () { 22log .SetLevel (log .WarnLevel ) 23} 24 25func TestMain (m * testing.M ) { 26os .Unsetenv ("BLOCK_SIZE" ) 27os .Unsetenv ("BLOCKSIZE" ) 28os .Exit (m .Run ()) 29} 30 31func TestAnalyzePath (t * testing.T ) { 32fin := testdir .CreateTestDir () 33defer fin () 34 35buff := make ([]byte ,10 ) 36output := bytes .NewBuffer (buff ) 37 38ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 39ui .SetIgnoreDirPaths ([]string {"/xxx" }) 40err := ui .AnalyzePath ("test_dir" ,nil ) 41assert .Nil (t ,err ) 42err = ui .StartUILoop () 43 44assert .Nil (t ,err ) 45assert .Contains (t ,output .String (),"nested" ) 46} 47 48func TestShowItemCountInNonInteractiveMode (t * testing.T ) { 49tmpDir := t .TempDir () 50 51for dirName ,fileCount := range map [string ]int {"a" :5 ,"b" :10 ,"c" :15 } { 52dirPath := filepath .Join (tmpDir ,dirName ) 53err := os .Mkdir (dirPath ,0o755 ) 54assert .Nil (t ,err ) 55 56for i := 0 ;i < fileCount ;i ++ { 57filePath := filepath .Join (dirPath ,"f" + string (rune ('a' + i ))) 58err = os .WriteFile (filePath , []byte ("x" ),0o644 ) 59assert .Nil (t ,err ) 60 } 61 } 62 63output := bytes .NewBuffer (make ([]byte ,10 )) 64ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 65ui .SetShowItemCount () 66 67err := ui .AnalyzePath (tmpDir ,nil ) 68assert .Nil (t ,err ) 69 70out := output .String () 71assert .Regexp (t ,regexp .MustCompile (`(?m)\s+5\s+/a$` ),out ) 72assert .Regexp (t ,regexp .MustCompile (`(?m)\s+10\s+/b$` ),out ) 73assert .Regexp (t ,regexp .MustCompile (`(?m)\s+15\s+/c$` ),out ) 74} 75 76func TestShowItemCountInNonInteractiveModeWithColorsAndFile (t * testing.T ) { 77tmpDir := t .TempDir () 78filePath := filepath .Join (tmpDir ,"single" ) 79err := os .WriteFile (filePath , []byte ("x" ),0o644 ) 80assert .Nil (t ,err ) 81 82output := bytes .NewBuffer (make ([]byte ,10 )) 83ui := CreateStdoutUI (output ,true ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 84ui .SetShowItemCount () 85 86err = ui .AnalyzePath (tmpDir ,nil ) 87assert .Nil (t ,err ) 88 89out := output .String () 90assert .Regexp (t ,regexp .MustCompile (`(?m)\s+1\s+single$` ),out ) 91} 92 93func TestShowSummaryApparentSizeForFilePath (t * testing.T ) { 94tmpDir := t .TempDir () 95filePath := filepath .Join (tmpDir ,"small.txt" ) 96err := os .WriteFile (filePath , []byte ("hello" ),0o644 ) 97assert .Nil (t ,err ) 98 99output := bytes .NewBuffer (make ([]byte ,10 )) 100 101// showApparentSize=true, summarize=true 102ui := CreateStdoutUI (output ,false ,false ,true ,false ,true ,false ,false ,"" ,0 ,false ,0 ) 103err = ui .AnalyzePath (filePath ,nil ) 104assert .Nil (t ,err ) 105 106out := output .String () 107// With apparent size, a 5-byte file should show "5 B", not "4.0 KiB" 108assert .Contains (t ,out ,"5 B" ) 109assert .Contains (t ,out ,"small.txt" ) 110} 111 112func TestShowSummaryDiskUsageForFilePath (t * testing.T ) { 113tmpDir := t .TempDir () 114filePath := filepath .Join (tmpDir ,"small.txt" ) 115err := os .WriteFile (filePath , []byte ("hello" ),0o644 ) 116assert .Nil (t ,err ) 117 118output := bytes .NewBuffer (make ([]byte ,10 )) 119 120// showApparentSize=false, summarize=true 121ui := CreateStdoutUI (output ,false ,false ,false ,false ,true ,false ,false ,"" ,0 ,false ,0 ) 122err = ui .AnalyzePath (filePath ,nil ) 123assert .Nil (t ,err ) 124 125out := output .String () 126// Without apparent size, should show disk usage (>= 5 bytes, typically 4 KiB) 127assert .Contains (t ,out ,"small.txt" ) 128assert .NotContains (t ,out ,"5 B" ) 129} 130 131func TestShowSummary (t * testing.T ) { 132fin := testdir .CreateTestDir () 133defer fin () 134 135buff := make ([]byte ,10 ) 136output := bytes .NewBuffer (buff ) 137 138ui := CreateStdoutUI (output ,true ,false ,true ,false ,true ,false ,false ,"" ,0 ,false ,0 ) 139ui .SetIgnoreDirPaths ([]string {"/xxx" }) 140err := ui .AnalyzePath ("test_dir" ,nil ) 141assert .Nil (t ,err ) 142err = ui .StartUILoop () 143 144assert .Nil (t ,err ) 145assert .Contains (t ,output .String (),"test_dir" ) 146} 147 148func TestShowSummaryBw (t * testing.T ) { 149fin := testdir .CreateTestDir () 150defer fin () 151 152buff := make ([]byte ,10 ) 153output := bytes .NewBuffer (buff ) 154 155ui := CreateStdoutUI (output ,false ,false ,false ,false ,true ,false ,false ,"" ,0 ,false ,0 ) 156ui .SetIgnoreDirPaths ([]string {"/xxx" }) 157err := ui .AnalyzePath ("test_dir" ,nil ) 158assert .Nil (t ,err ) 159err = ui .StartUILoop () 160 161assert .Nil (t ,err ) 162assert .Contains (t ,output .String (),"test_dir" ) 163} 164 165func TestShowTop (t * testing.T ) { 166fin := testdir .CreateTestDir () 167defer fin () 168 169buff := make ([]byte ,10 ) 170output := bytes .NewBuffer (buff ) 171 172ui := CreateStdoutUI (output ,true ,false ,true ,false ,true ,false ,false ,"" ,2 ,false ,0 ) 173ui .SetIgnoreDirPaths ([]string {"/xxx" }) 174err := ui .AnalyzePath ("test_dir" ,nil ) 175assert .Nil (t ,err ) 176err = ui .StartUILoop () 177 178assert .Nil (t ,err ) 179assert .Contains (t ,output .String (),"test_dir/nested/subnested/file" ) 180assert .Contains (t ,output .String (),"test_dir/nested/file2" ) 181} 182 183func TestShowTopBw (t * testing.T ) { 184fin := testdir .CreateTestDir () 185defer fin () 186 187buff := make ([]byte ,10 ) 188output := bytes .NewBuffer (buff ) 189 190ui := CreateStdoutUI (output ,false ,false ,false ,false ,true ,false ,false ,"" ,2 ,false ,0 ) 191ui .SetIgnoreDirPaths ([]string {"/xxx" }) 192err := ui .AnalyzePath ("test_dir" ,nil ) 193assert .Nil (t ,err ) 194err = ui .StartUILoop () 195 196assert .Nil (t ,err ) 197assert .Contains (t ,output .String (),"test_dir/nested/subnested/file" ) 198assert .Contains (t ,output .String (),"test_dir/nested/file2" ) 199} 200 201func TestShowDepth (t * testing.T ) { 202fin := testdir .CreateTestDir () 203defer fin () 204 205buff := make ([]byte ,10 ) 206output := bytes .NewBuffer (buff ) 207 208ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,2 ) 209ui .SetIgnoreDirPaths ([]string {"/xxx" }) 210err := ui .AnalyzePath ("test_dir" ,nil ) 211assert .Nil (t ,err ) 212err = ui .StartUILoop () 213 214assert .Nil (t ,err ) 215assert .Contains (t ,output .String (),"test_dir" ) 216assert .Contains (t ,output .String (),"test_dir/nested" ) 217assert .Contains (t ,output .String (),"test_dir/nested/subnested" ) 218} 219 220func TestShowDepthWithColors (t * testing.T ) { 221fin := testdir .CreateTestDir () 222defer fin () 223 224buff := make ([]byte ,10 ) 225output := bytes .NewBuffer (buff ) 226 227ui := CreateStdoutUI (output ,true ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,2 ) 228ui .SetIgnoreDirPaths ([]string {"/xxx" }) 229err := ui .AnalyzePath ("test_dir" ,nil ) 230assert .Nil (t ,err ) 231err = ui .StartUILoop () 232 233assert .Nil (t ,err ) 234assert .Contains (t ,output .String (),"test_dir" ) 235assert .Contains (t ,output .String (),"test_dir/nested" ) 236} 237 238func TestShowDepthWithReverseSort (t * testing.T ) { 239fin := testdir .CreateTestDir () 240defer fin () 241 242buff := make ([]byte ,10 ) 243output := bytes .NewBuffer (buff ) 244 245ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,true ,2 ) 246ui .SetIgnoreDirPaths ([]string {"/xxx" }) 247err := ui .AnalyzePath ("test_dir" ,nil ) 248assert .Nil (t ,err ) 249err = ui .StartUILoop () 250 251assert .Nil (t ,err ) 252outputStr := output .String () 253assert .Contains (t ,outputStr ,"test_dir" ) 254assert .Contains (t ,outputStr ,"test_dir/nested" ) 255assert .Contains (t ,outputStr ,"test_dir/nested/subnested" ) 256} 257 258func TestAnalyzeSubdir (t * testing.T ) { 259fin := testdir .CreateTestDir () 260defer fin () 261 262buff := make ([]byte ,10 ) 263output := bytes .NewBuffer (buff ) 264 265ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 266ui .SetIgnoreDirPaths ([]string {"/xxx" }) 267err := ui .AnalyzePath ("test_dir/nested" ,nil ) 268assert .Nil (t ,err ) 269err = ui .StartUILoop () 270 271assert .Nil (t ,err ) 272assert .Contains (t ,output .String (),"file2" ) 273} 274 275func TestAnalyzePathWithTypeFiltering (t * testing.T ) { 276fin := testdir .CreateTestDir () 277defer fin () 278 279buff := make ([]byte ,10 ) 280output := bytes .NewBuffer (buff ) 281 282ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 283ui .SetIncludeTypes ([]string {"go" }) 284ui .SetIgnoreDirPaths ([]string {"/xxx" }) 285err := ui .AnalyzePath ("test_dir" ,nil ) 286assert .Nil (t ,err ) 287err = ui .StartUILoop () 288 289assert .Nil (t ,err ) 290assert .Contains (t ,output .String (),"nested" ) 291} 292 293func TestAnalyzePathWithColors (t * testing.T ) { 294fin := testdir .CreateTestDir () 295defer fin () 296 297buff := make ([]byte ,10 ) 298output := bytes .NewBuffer (buff ) 299 300ui := CreateStdoutUI (output ,true ,false ,true ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 301ui .SetIgnoreTypes ([]string {"go" }) 302ui .SetIgnoreDirPaths ([]string {"/xxx" }) 303err := ui .AnalyzePath ("test_dir/nested" ,nil ) 304 305assert .Nil (t ,err ) 306assert .Contains (t ,output .String (),"subnested" ) 307} 308 309func TestAnalyzePathWoUnicode (t * testing.T ) { 310fin := testdir .CreateTestDir () 311defer fin () 312 313buff := make ([]byte ,10 ) 314output := bytes .NewBuffer (buff ) 315 316ui := CreateStdoutUI (output ,false ,true ,true ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 317ui .UseOldProgressRunes () 318err := ui .AnalyzePath ("test_dir/nested" ,nil ) 319 320assert .Nil (t ,err ) 321assert .Contains (t ,output .String (),"subnested" ) 322} 323 324func TestItemRows (t * testing.T ) { 325output := bytes .NewBuffer (make ([]byte ,10 )) 326 327ui := CreateStdoutUI (output ,false ,true ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 328ui .Analyzer = & testanalyze.MockedAnalyzer {} 329err := ui .AnalyzePath ("test_dir" ,nil ) 330 331assert .Nil (t ,err ) 332assert .Contains (t ,output .String (),"B" ) 333} 334 335func TestAnalyzePathWithProgress (t * testing.T ) { 336fin := testdir .CreateTestDir () 337defer fin () 338 339output := bytes .NewBuffer (make ([]byte ,10 )) 340 341ui := CreateStdoutUI (output ,false ,true ,true ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 342ui .SetIgnoreDirPaths ([]string {"/xxx" }) 343err := ui .AnalyzePath ("test_dir" ,nil ) 344 345assert .Nil (t ,err ) 346assert .Contains (t ,output .String (),"nested" ) 347} 348 349func TestShowDevices (t * testing.T ) { 350output := bytes .NewBuffer (make ([]byte ,10 )) 351 352ui := CreateStdoutUI (output ,false ,true ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 353err := ui .ListDevices (getDevicesInfoMock ()) 354 355assert .Nil (t ,err ) 356assert .Contains (t ,output .String (),"Device" ) 357assert .Contains (t ,output .String (),"xxx" ) 358} 359 360func TestShowDevicesWithColor (t * testing.T ) { 361output := bytes .NewBuffer (make ([]byte ,10 )) 362 363ui := CreateStdoutUI (output ,true ,true ,true ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 364err := ui .ListDevices (getDevicesInfoMock ()) 365 366assert .Nil (t ,err ) 367assert .Contains (t ,output .String (),"Device" ) 368assert .Contains (t ,output .String (),"xxx" ) 369} 370 371func TestReadAnalysisWithColor (t * testing.T ) { 372input ,err := os .OpenFile ("../internal/testdata/test.json" ,os .O_RDONLY ,0o644 ) 373assert .Nil (t ,err ) 374 375output := bytes .NewBuffer (make ([]byte ,10 )) 376 377ui := CreateStdoutUI (output ,true ,true ,true ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 378err = ui .ReadAnalysis (input ) 379 380assert .Nil (t ,err ) 381assert .Contains (t ,output .String (),"main.go" ) 382} 383 384func TestReadAnalysisBw (t * testing.T ) { 385input ,err := os .OpenFile ("../internal/testdata/test.json" ,os .O_RDONLY ,0o644 ) 386assert .Nil (t ,err ) 387 388output := bytes .NewBuffer (make ([]byte ,10 )) 389 390ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 391err = ui .ReadAnalysis (input ) 392 393assert .Nil (t ,err ) 394assert .Contains (t ,output .String (),"main.go" ) 395} 396 397func TestReadAnalysisWithWrongFile (t * testing.T ) { 398input ,err := os .OpenFile ("../internal/testdata/wrong.json" ,os .O_RDONLY ,0o644 ) 399assert .Nil (t ,err ) 400 401output := bytes .NewBuffer (make ([]byte ,10 )) 402 403ui := CreateStdoutUI (output ,true ,true ,true ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 404err = ui .ReadAnalysis (input ) 405 406assert .NotNil (t ,err ) 407} 408 409func TestReadAnalysisWithSummarize (t * testing.T ) { 410input ,err := os .OpenFile ("../internal/testdata/test.json" ,os .O_RDONLY ,0o644 ) 411assert .Nil (t ,err ) 412 413output := bytes .NewBuffer (make ([]byte ,10 )) 414 415ui := CreateStdoutUI (output ,false ,false ,false ,false ,true ,false ,false ,"" ,0 ,false ,0 ) 416err = ui .ReadAnalysis (input ) 417 418assert .Nil (t ,err ) 419assert .Contains (t ,output .String ()," gdu\n" ) 420} 421 422func TestMaxInt (t * testing.T ) { 423assert .Equal (t ,5 ,maxInt (2 ,5 )) 424assert .Equal (t ,4 ,maxInt (4 ,2 )) 425} 426 427func TestFormatSize (t * testing.T ) { 428output := bytes .NewBuffer (make ([]byte ,10 )) 429 430ui := CreateStdoutUI (output ,true ,true ,true ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 431 432assert .Contains (t ,ui .formatSize (1 ),"B" ) 433assert .Contains (t ,ui .formatSize (1 << 10 + 1 ),"KiB" ) 434assert .Contains (t ,ui .formatSize (1 << 20 + 1 ),"MiB" ) 435assert .Contains (t ,ui .formatSize (1 << 30 + 1 ),"GiB" ) 436assert .Contains (t ,ui .formatSize (1 << 40 + 1 ),"TiB" ) 437assert .Contains (t ,ui .formatSize (1 << 50 + 1 ),"PiB" ) 438assert .Contains (t ,ui .formatSize (1 << 60 + 1 ),"EiB" ) 439} 440 441func TestFormatSizeDec (t * testing.T ) { 442output := bytes .NewBuffer (make ([]byte ,10 )) 443 444ui := CreateStdoutUI (output ,true ,true ,true ,false ,false ,true ,false ,"" ,0 ,false ,0 ) 445 446assert .Contains (t ,ui .formatSize (1 ),"B" ) 447assert .Contains (t ,ui .formatSize (1 << 10 + 1 ),"kB" ) 448assert .Contains (t ,ui .formatSize (1 << 20 + 1 ),"MB" ) 449assert .Contains (t ,ui .formatSize (1 << 30 + 1 ),"GB" ) 450assert .Contains (t ,ui .formatSize (1 << 40 + 1 ),"TB" ) 451assert .Contains (t ,ui .formatSize (1 << 50 + 1 ),"PB" ) 452assert .Contains (t ,ui .formatSize (1 << 60 + 1 ),"EB" ) 453} 454 455func TestFormatCount (t * testing.T ) { 456output := bytes .NewBuffer (make ([]byte ,10 )) 457ui := CreateStdoutUI (output ,true ,true ,true ,false ,false ,true ,false ,"" ,0 ,false ,0 ) 458 459assert .Equal (t ,"42" ,ui .formatCount (42 )) 460assert .Equal (t ,"1.5k" ,ui .formatCount (1500 )) 461assert .Equal (t ,"2.5M" ,ui .formatCount (2500000 )) 462assert .Equal (t ,"3.5G" ,ui .formatCount (3500000000 )) 463} 464 465func TestFormatSizeRaw (t * testing.T ) { 466output := bytes .NewBuffer (make ([]byte ,10 )) 467 468ui := CreateStdoutUI (output ,true ,true ,true ,false ,false ,true ,true ,"" ,0 ,false ,0 ) 469 470assert .Equal (t ,ui .formatSize (1 ),"1" ) 471assert .Equal (t ,ui .formatSize (1 << 10 + 1 ),"1025" ) 472assert .Equal (t ,ui .formatSize (1 << 20 + 1 ),"1048577" ) 473assert .Equal (t ,ui .formatSize (1 << 30 + 1 ),"1073741825" ) 474assert .Equal (t ,ui .formatSize (1 << 40 + 1 ),"1099511627777" ) 475assert .Equal (t ,ui .formatSize (1 << 50 + 1 ),"1125899906842625" ) 476assert .Equal (t ,ui .formatSize (1 << 60 + 1 ),"1152921504606846977" ) 477} 478func TestFormatSizeFixedUnitBinary (t * testing.T ) { 479output := bytes .NewBuffer (make ([]byte ,10 )) 480 481ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"k" ,0 ,false ,0 ) 482assert .Equal (t ,"0.1 KiB" ,ui .formatSize (100 )) 483assert .Equal (t ,"1500.0 KiB" ,ui .formatSize (1536000 )) 484 485ui = CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"m" ,0 ,false ,0 ) 486assert .Equal (t ,"0.1 MiB" ,ui .formatSize (100 * 1024 )) 487assert .Equal (t ,"1500.0 MiB" ,ui .formatSize (1536000 * 1024 )) 488 489ui = CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"g" ,0 ,false ,0 ) 490assert .Equal (t ,"0.1 GiB" ,ui .formatSize (100 * 1024 * 1024 )) 491assert .Equal (t ,"1500.0 GiB" ,ui .formatSize (1536000 * 1024 * 1024 )) 492} 493func TestFormatSizeFixedUnitSI (t * testing.T ) { 494output := bytes .NewBuffer (make ([]byte ,10 )) 495 496// -k --si 497ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,true ,false ,"k" ,0 ,false ,0 ) 498assert .Equal (t ,"0.1 kB" ,ui .formatSize (100 )) 499assert .Equal (t ,"1500.0 kB" ,ui .formatSize (15e+5 )) 500 501ui = CreateStdoutUI (output ,false ,false ,false ,false ,false ,true ,false ,"m" ,0 ,false ,0 ) 502assert .Equal (t ,"0.1 MB" ,ui .formatSize (1e+5 )) 503assert .Equal (t ,"1500.0 MB" ,ui .formatSize (1.5e+9 )) 504 505ui = CreateStdoutUI (output ,false ,false ,false ,false ,false ,true ,false ,"g" ,0 ,false ,0 ) 506assert .Equal (t ,"0.1 GB" ,ui .formatSize (1e+8 )) 507assert .Equal (t ,"1500.0 GB" ,ui .formatSize (1.5e+12 )) 508} 509 510// func printBuffer(buff *bytes.Buffer) { 511// for i, x := range buff.String() { 512// println(i, string(x)) 513// } 514// } 515 516func getDevicesInfoMock () device.DevicesInfoGetter { 517item := & device.Device { 518Name :"xxx" , 519 } 520 521mock := testdev.DevicesInfoGetterMock {} 522mock .Devices = []* device.Device {item } 523return mock 524} 525 526// New tests for reverse sort functionality 527func TestAnalyzePathWithReverseSort (t * testing.T ) { 528fin := testdir .CreateTestDir () 529defer fin () 530 531buff := make ([]byte ,10 ) 532output := bytes .NewBuffer (buff ) 533 534ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,true ,0 ) 535ui .SetIgnoreDirPaths ([]string {"/xxx" }) 536err := ui .AnalyzePath ("test_dir" ,nil ) 537assert .Nil (t ,err ) 538err = ui .StartUILoop () 539 540assert .Nil (t ,err ) 541assert .Contains (t ,output .String (),"nested" ) 542 543// Verify that smaller items appear first when reverse sort is enabled 544outputStr := output .String () 545lines := strings .Split (outputStr ,"\n" ) 546 547// Filter out empty lines and progress lines 548var fileLines []string 549for _ ,line := range lines { 550if strings .Contains (line ," " )&& ! strings .Contains (line ,"Scanning" ) { 551fileLines = append (fileLines ,line ) 552 } 553 } 554 555// With reverse sort, smaller files should appear before larger ones 556assert .True (t ,len (fileLines )> 0 ,"Should have file entries in output" ) 557} 558 559func TestAnalyzePathWithoutReverseSort (t * testing.T ) { 560fin := testdir .CreateTestDir () 561defer fin () 562 563buff := make ([]byte ,10 ) 564output := bytes .NewBuffer (buff ) 565 566ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 567ui .SetIgnoreDirPaths ([]string {"/xxx" }) 568err := ui .AnalyzePath ("test_dir" ,nil ) 569assert .Nil (t ,err ) 570err = ui .StartUILoop () 571 572assert .Nil (t ,err ) 573assert .Contains (t ,output .String (),"nested" ) 574} 575 576func TestReverseSortWithColors (t * testing.T ) { 577fin := testdir .CreateTestDir () 578defer fin () 579 580buff := make ([]byte ,10 ) 581output := bytes .NewBuffer (buff ) 582 583ui := CreateStdoutUI (output ,true ,false ,true ,false ,false ,false ,false ,"" ,0 ,true ,0 ) 584ui .SetIgnoreDirPaths ([]string {"/xxx" }) 585err := ui .AnalyzePath ("test_dir/nested" ,nil ) 586 587assert .Nil (t ,err ) 588assert .Contains (t ,output .String (),"subnested" ) 589} 590 591func TestReverseSortWithSummarize (t * testing.T ) { 592fin := testdir .CreateTestDir () 593defer fin () 594 595buff := make ([]byte ,10 ) 596output := bytes .NewBuffer (buff ) 597 598ui := CreateStdoutUI (output ,false ,false ,false ,false ,true ,false ,false ,"" ,0 ,true ,0 ) 599ui .SetIgnoreDirPaths ([]string {"/xxx" }) 600err := ui .AnalyzePath ("test_dir" ,nil ) 601assert .Nil (t ,err ) 602err = ui .StartUILoop () 603 604assert .Nil (t ,err ) 605assert .Contains (t ,output .String (),"test_dir" ) 606} 607 608func TestReverseSortWithTop (t * testing.T ) { 609fin := testdir .CreateTestDir () 610defer fin () 611 612buff := make ([]byte ,10 ) 613output := bytes .NewBuffer (buff ) 614 615ui := CreateStdoutUI (output ,true ,false ,true ,false ,true ,false ,false ,"" ,2 ,true ,0 ) 616ui .SetIgnoreDirPaths ([]string {"/xxx" }) 617err := ui .AnalyzePath ("test_dir" ,nil ) 618assert .Nil (t ,err ) 619err = ui .StartUILoop () 620 621assert .Nil (t ,err ) 622assert .Contains (t ,output .String (),"test_dir/nested/subnested/file" ) 623assert .Contains (t ,output .String (),"test_dir/nested/file2" ) 624} 625 626func TestReverseSortFromAnalysisFile (t * testing.T ) { 627input ,err := os .OpenFile ("../internal/testdata/test.json" ,os .O_RDONLY ,0o644 ) 628assert .Nil (t ,err ) 629 630output := bytes .NewBuffer (make ([]byte ,10 )) 631 632ui := CreateStdoutUI (output ,true ,true ,true ,false ,false ,false ,false ,"" ,0 ,true ,0 ) 633err = ui .ReadAnalysis (input ) 634 635assert .Nil (t ,err ) 636assert .Contains (t ,output .String (),"main.go" ) 637} 638 639func TestDefaultAnalyzerIsTopDir (t * testing.T ) { 640output := bytes .NewBuffer (make ([]byte ,10 )) 641ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 642 643_ ,ok := ui .Analyzer .(* analyze.TopDirAnalyzer ) 644assert .True (t ,ok ,"default analyzer should be TopDirAnalyzer" ) 645} 646 647func TestAnalyzerWithTopIsParallel (t * testing.T ) { 648output := bytes .NewBuffer (make ([]byte ,10 )) 649ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,5 ,false ,0 ) 650 651_ ,ok := ui .Analyzer .(* analyze.ParallelAnalyzer ) 652assert .True (t ,ok ,"analyzer with top > 0 should be ParallelAnalyzer" ) 653} 654 655func TestAnalyzerWithDepthIsParallel (t * testing.T ) { 656output := bytes .NewBuffer (make ([]byte ,10 )) 657ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,3 ) 658 659_ ,ok := ui .Analyzer .(* analyze.ParallelAnalyzer ) 660assert .True (t ,ok ,"analyzer with depth > 0 should be ParallelAnalyzer" ) 661} 662 663func TestAnalyzePathWithTopDirAnalyzer (t * testing.T ) { 664fin := testdir .CreateTestDir () 665defer fin () 666 667output := bytes .NewBuffer (make ([]byte ,10 )) 668ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 669err := ui .AnalyzePath ("test_dir" ,nil ) 670assert .Nil (t ,err ) 671err = ui .StartUILoop () 672assert .Nil (t ,err ) 673assert .Contains (t ,output .String (),"nested" ) 674} 675 676func TestBlockSizeEnvironment (t * testing.T ) { 677t .Setenv ("BLOCK_SIZE" ,"1K" ) 678t .Setenv ("BLOCKSIZE" ,"1MB" ) 679ui := CreateStdoutUI (bytes .NewBuffer (nil ),false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 680assert .Equal (t ,"2" ,ui .formatSize (1025 )) 681} 682 683func TestBlockSizeEnvironmentFormats (t * testing.T ) { 684tests := []struct { 685name string 686blockSize string 687size int64 688expected string 689 }{ 690 {name :"decimal suffix" ,blockSize :"kB" ,size :1025 ,expected :"2kB" }, 691 {name :"numeric suffix" ,blockSize :"2kB" ,size :1025 ,expected :"1" }, 692 } 693 694for _ ,test := range tests { 695t .Run (test .name ,func (t * testing.T ) { 696t .Setenv ("BLOCK_SIZE" ,test .blockSize ) 697t .Setenv ("BLOCKSIZE" ,"" ) 698ui := CreateStdoutUI (bytes .NewBuffer (nil ),false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 699assert .Equal (t ,test .expected ,ui .formatSize (test .size )) 700 }) 701 } 702} 703 704func TestBlockSizeEnvironmentHumanReadableModes (t * testing.T ) { 705t .Run ("binary" ,func (t * testing.T ) { 706t .Setenv ("BLOCK_SIZE" ,"human-readable" ) 707ui := CreateStdoutUI (bytes .NewBuffer (nil ),false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 708assert .Equal (t ,"1.0 KiB" ,ui .formatSize (1025 )) 709 }) 710t .Run ("si" ,func (t * testing.T ) { 711t .Setenv ("BLOCK_SIZE" ,"si" ) 712ui := CreateStdoutUI (bytes .NewBuffer (nil ),false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 713assert .Equal (t ,"1.0 kB" ,ui .formatSize (1025 )) 714 }) 715} 716 717func TestBlockSizeEnvironmentIsOverriddenByFlags (t * testing.T ) { 718t .Setenv ("BLOCK_SIZE" ,"1K" ) 719ui := CreateStdoutUI (bytes .NewBuffer (nil ),false ,false ,false ,false ,false ,false ,true ,"" ,0 ,false ,0 ) 720assert .Equal (t ,"1025" ,ui .formatSize (1025 )) 721} 722 723func TestBlockSizeEnvironmentSIFlagOverridesEnvironment (t * testing.T ) { 724t .Setenv ("BLOCK_SIZE" ,"1K" ) 725ui := CreateStdoutUI (bytes .NewBuffer (nil ),false ,false ,false ,false ,false ,true ,false ,"" ,0 ,false ,0 ) 726assert .Equal (t ,"1.0 kB" ,ui .formatSize (1025 )) 727} 728 729func newStdoutSymlinkFile ()* analyze.File { 730dir := & analyze.Dir {File :& analyze.File {Name :"test_dir" }} 731return & analyze.File { 732Name :"bin" , 733Parent :dir , 734Size :7 , 735Flag :'@' , 736Symlink :"usr/bin" , 737 } 738} 739 740func TestPrintItemShowsSymlinkTarget (t * testing.T ) { 741output := bytes .NewBuffer (nil ) 742ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 743ui .SetShowSymlinkTarget (true ) 744 745ui .printItem (nil ,newStdoutSymlinkFile ()) 746 747assert .Contains (t ,output .String (),"bin -> usr/bin" ) 748} 749 750func TestPrintItemHidesSymlinkTargetByDefault (t * testing.T ) { 751output := bytes .NewBuffer (nil ) 752ui := CreateStdoutUI (output ,false ,false ,false ,false ,false ,false ,false ,"" ,0 ,false ,0 ) 753 754ui .printItem (nil ,newStdoutSymlinkFile ()) 755 756out := output .String () 757assert .NotContains (t ,out ,"-> usr/bin" ) 758assert .Contains (t ,out ,"bin" ) 759}