fork/gdu
git clone https://git.julia.blue/fork/gdu
5782be9
ignore
1package common_test 2 3import ( 4"os" 5"path/filepath" 6"testing" 7 8 log"github.com/sirupsen/logrus" 9 10"github.com/dundee/gdu/v5/internal/common" 11"github.com/stretchr/testify/assert" 12) 13 14func init () { 15log .SetLevel (log .WarnLevel ) 16} 17 18func TestCreateIgnorePattern (t * testing.T ) { 19re ,err := common .CreateIgnorePattern ([]string {"[abc]+" }) 20 21assert .Nil (t ,err ) 22assert .True (t ,re .MatchString ("aa" )) 23} 24 25func TestCreateIgnorePatternAnchoredSingleRelative (t * testing.T ) { 26re ,err := common .CreateIgnorePattern ([]string {"test_dir/abc" }) 27 28assert .Nil (t ,err ) 29assert .True (t ,re .MatchString ("test_dir/abc" )) 30assert .False (t ,re .MatchString ("test_dir/abcdef" )) 31} 32 33func TestCreateIgnorePatternAnchoredMiddleAlternative (t * testing.T ) { 34re ,err := common .CreateIgnorePattern ([]string {"aaa" ,`foo\.bak` ,"ccc" }) 35 36assert .Nil (t ,err ) 37assert .True (t ,re .MatchString ("foo.bak" )) 38assert .False (t ,re .MatchString ("xfoo.baky" )) 39} 40 41func TestCreateIgnorePatternWithErr (t * testing.T ) { 42re ,err := common .CreateIgnorePattern ([]string {"[[[" }) 43 44assert .NotNil (t ,err ) 45assert .Nil (t ,re ) 46} 47 48func TestEmptyIgnore (t * testing.T ) { 49ui := & common.UI {} 50shouldBeIgnored := ui .CreateIgnoreFunc () 51 52assert .False (t ,shouldBeIgnored ("abc" ,"/abc" )) 53assert .False (t ,shouldBeIgnored ("xxx" ,"/xxx" )) 54} 55 56func TestIgnoreByAbsPath (t * testing.T ) { 57ui := & common.UI {} 58ui .SetIgnoreDirPaths ([]string {"/abc" }) 59shouldBeIgnored := ui .CreateIgnoreFunc () 60 61assert .True (t ,shouldBeIgnored ("abc" ,"/abc" )) 62assert .False (t ,shouldBeIgnored ("xxx" ,"/xxx" )) 63} 64 65func TestIgnoreByPattern (t * testing.T ) { 66ui := & common.UI {} 67err := ui .SetIgnoreDirPatterns ([]string {"/[abc]+" }) 68assert .Nil (t ,err ) 69shouldBeIgnored := ui .CreateIgnoreFunc () 70 71assert .True (t ,shouldBeIgnored ("aaa" ,"/aaa" )) 72assert .True (t ,shouldBeIgnored ("aaa" ,"/aaabc" )) 73assert .False (t ,shouldBeIgnored ("xxx" ,"/xxx" )) 74} 75 76func TestIgnoreFromFile (t * testing.T ) { 77file ,err := os .OpenFile ("ignore" ,os .O_WRONLY | os .O_CREATE | os .O_TRUNC ,0o600 ) 78if err != nil { 79panic (err ) 80 } 81defer file .Close () 82 83if _ ,err := file .WriteString ("/aaa\n" );err != nil { 84panic (err ) 85 } 86if _ ,err := file .WriteString ("/aaabc\n" );err != nil { 87panic (err ) 88 } 89if _ ,err := file .WriteString ("/[abd]+\n" );err != nil { 90panic (err ) 91 } 92 93ui := & common.UI {} 94err = ui .SetIgnoreFromFile ("ignore" ) 95assert .Nil (t ,err ) 96shouldBeIgnored := ui .CreateIgnoreFunc () 97 98assert .True (t ,shouldBeIgnored ("aaa" ,"/aaa" )) 99assert .True (t ,shouldBeIgnored ("aaabc" ,"/aaabc" )) 100assert .True (t ,shouldBeIgnored ("aaabd" ,"/aaabd" )) 101assert .False (t ,shouldBeIgnored ("xxx" ,"/xxx" )) 102} 103 104func TestIgnoreFromNotExistingFile (t * testing.T ) { 105ui := & common.UI {} 106err := ui .SetIgnoreFromFile ("xxx" ) 107assert .NotNil (t ,err ) 108} 109 110func TestIgnoreHidden (t * testing.T ) { 111ui := & common.UI {} 112ui .SetIgnoreHidden (true ) 113shouldBeIgnored := ui .CreateIgnoreFunc () 114 115assert .True (t ,shouldBeIgnored (".git" ,"/aaa/.git" )) 116assert .True (t ,shouldBeIgnored (".bbb" ,"/aaa/.bbb" )) 117assert .False (t ,shouldBeIgnored ("xxx" ,"/xxx" )) 118} 119 120func TestIgnoreByAbsPathAndHidden (t * testing.T ) { 121ui := & common.UI {} 122ui .SetIgnoreDirPaths ([]string {"/abc" }) 123ui .SetIgnoreHidden (true ) 124shouldBeIgnored := ui .CreateIgnoreFunc () 125 126assert .True (t ,shouldBeIgnored ("abc" ,"/abc" )) 127assert .True (t ,shouldBeIgnored (".git" ,"/aaa/.git" )) 128assert .True (t ,shouldBeIgnored (".bbb" ,"/aaa/.bbb" )) 129assert .False (t ,shouldBeIgnored ("xxx" ,"/xxx" )) 130} 131 132func TestIgnoreByAbsPathAndPattern (t * testing.T ) { 133ui := & common.UI {} 134ui .SetIgnoreDirPaths ([]string {"/abc" }) 135err := ui .SetIgnoreDirPatterns ([]string {"/[abc]+" }) 136assert .Nil (t ,err ) 137shouldBeIgnored := ui .CreateIgnoreFunc () 138 139assert .True (t ,shouldBeIgnored ("abc" ,"/abc" )) 140assert .True (t ,shouldBeIgnored ("aabc" ,"/aabc" )) 141assert .True (t ,shouldBeIgnored ("ccc" ,"/ccc" )) 142assert .False (t ,shouldBeIgnored ("xxx" ,"/xxx" )) 143} 144 145func TestIgnoreByPatternAndHidden (t * testing.T ) { 146ui := & common.UI {} 147err := ui .SetIgnoreDirPatterns ([]string {"/[abc]+" }) 148assert .Nil (t ,err ) 149ui .SetIgnoreHidden (true ) 150shouldBeIgnored := ui .CreateIgnoreFunc () 151 152assert .True (t ,shouldBeIgnored ("abbc" ,"/abbc" )) 153assert .True (t ,shouldBeIgnored (".git" ,"/aaa/.git" )) 154assert .True (t ,shouldBeIgnored (".bbb" ,"/aaa/.bbb" )) 155assert .False (t ,shouldBeIgnored ("xxx" ,"/xxx" )) 156} 157 158func TestIgnoreByAll (t * testing.T ) { 159ui := & common.UI {} 160ui .SetIgnoreDirPaths ([]string {"/abc" }) 161err := ui .SetIgnoreDirPatterns ([]string {"/[abc]+" }) 162assert .Nil (t ,err ) 163ui .SetIgnoreHidden (true ) 164shouldBeIgnored := ui .CreateIgnoreFunc () 165 166assert .True (t ,shouldBeIgnored ("abc" ,"/abc" )) 167assert .True (t ,shouldBeIgnored ("aabc" ,"/aabc" )) 168assert .True (t ,shouldBeIgnored (".git" ,"/aaa/.git" )) 169assert .True (t ,shouldBeIgnored (".bbb" ,"/aaa/.bbb" )) 170assert .False (t ,shouldBeIgnored ("xxx" ,"/xxx" )) 171} 172 173func TestIgnoreByRelativePath (t * testing.T ) { 174ui := & common.UI {} 175ui .SetIgnoreDirPaths ([]string {"test_dir/abc" }) 176shouldBeIgnored := ui .CreateIgnoreFunc () 177 178assert .True (t ,shouldBeIgnored ("abc" ,"test_dir/abc" )) 179absPath ,err := filepath .Abs ("test_dir/abc" ) 180assert .Nil (t ,err ) 181assert .True (t ,shouldBeIgnored ("abc" ,absPath )) 182assert .False (t ,shouldBeIgnored ("xxx" ,"test_dir/xxx" )) 183} 184 185func TestIgnoreByRelativePattern (t * testing.T ) { 186ui := & common.UI {} 187err := ui .SetIgnoreDirPatterns ([]string {"test_dir/[abc]+" }) 188assert .Nil (t ,err ) 189shouldBeIgnored := ui .CreateIgnoreFunc () 190 191assert .True (t ,shouldBeIgnored ("abc" ,"test_dir/abc" )) 192absPath ,err := filepath .Abs ("test_dir/abc" ) 193assert .Nil (t ,err ) 194assert .True (t ,shouldBeIgnored ("abc" ,absPath )) 195assert .False (t ,shouldBeIgnored ("xxx" ,"test_dir/xxx" )) 196} 197 198func TestIgnoreFromFileWithRelativePaths (t * testing.T ) { 199file ,err := os .OpenFile ("ignore" ,os .O_WRONLY | os .O_CREATE | os .O_TRUNC ,0o600 ) 200if err != nil { 201panic (err ) 202 } 203defer file .Close () 204defer os .Remove ("ignore" ) 205 206if _ ,err := file .WriteString ("test_dir/aaa\n" );err != nil { 207panic (err ) 208 } 209if _ ,err := file .WriteString ("node_modules/[^/]+\n" );err != nil { 210panic (err ) 211 } 212 213ui := & common.UI {} 214err = ui .SetIgnoreFromFile ("ignore" ) 215assert .Nil (t ,err ) 216shouldBeIgnored := ui .CreateIgnoreFunc () 217 218assert .True (t ,shouldBeIgnored ("aaa" ,"test_dir/aaa" )) 219absPath ,err := filepath .Abs ("test_dir/aaa" ) 220assert .Nil (t ,err ) 221assert .True (t ,shouldBeIgnored ("aaa" ,absPath )) 222assert .False (t ,shouldBeIgnored ("xxx" ,"test_dir/xxx" )) 223} 224 225func TestShouldFileBeIgnoredByType (t * testing.T ) { 226tests := []struct { 227name string 228ignoreTypes []string 229filename string 230expectedIgnored bool 231 }{ 232 { 233name :"no ignore types" , 234ignoreTypes : []string {}, 235filename :"test.yaml" , 236expectedIgnored :false , 237 }, 238 { 239name :"ignore yaml" , 240ignoreTypes : []string {"yaml" }, 241filename :"test.yaml" , 242expectedIgnored :true , 243 }, 244 { 245name :"ignore json" , 246ignoreTypes : []string {"json" }, 247filename :"test.json" , 248expectedIgnored :true , 249 }, 250 { 251name :"ignore multiple types" , 252ignoreTypes : []string {"yaml" ,"json" }, 253filename :"test.yaml" , 254expectedIgnored :true , 255 }, 256 { 257name :"ignore multiple types - not matched" , 258ignoreTypes : []string {"yaml" ,"json" }, 259filename :"test.txt" , 260expectedIgnored :false , 261 }, 262 263 { 264name :"ignore with uppercase" , 265ignoreTypes : []string {"YAML" }, 266filename :"test.yaml" , 267expectedIgnored :true , 268 }, 269 { 270name :"ignore file without extension" , 271ignoreTypes : []string {"yaml" }, 272filename :"test" , 273expectedIgnored :false , 274 }, 275 { 276name :"ignore with dot in extension" , 277ignoreTypes : []string {".yaml" }, 278filename :"test.yaml" , 279expectedIgnored :true , 280 }, 281 } 282 283for _ ,tt := range tests { 284t .Run (tt .name ,func (t * testing.T ) { 285ui := & common.UI {} 286ui .SetIgnoreTypes (tt .ignoreTypes ) 287if len (tt .ignoreTypes )> 0 { 288assert .True (t ,ui .IsFilteringFiles ()) 289 }else { 290assert .False (t ,ui .IsFilteringFiles ()) 291 } 292 293actual := ui .ShouldFileBeIgnoredByType (tt .filename ) 294assert .Equal (t ,tt .expectedIgnored ,actual ) 295 }) 296 } 297} 298 299func TestShouldFileBeIncludedByType (t * testing.T ) { 300tests := []struct { 301name string 302includeTypes []string 303filename string 304expectedIncluded bool 305 }{ 306 { 307name :"no include types" , 308includeTypes : []string {}, 309filename :"test.yaml" , 310expectedIncluded :true , 311 }, 312 { 313name :"include yaml" , 314includeTypes : []string {"yaml" }, 315filename :"test.yaml" , 316expectedIncluded :true , 317 }, 318 { 319name :"include json" , 320includeTypes : []string {"json" }, 321filename :"test.json" , 322expectedIncluded :true , 323 }, 324 { 325name :"include multiple types" , 326includeTypes : []string {"yaml" ,"json" }, 327filename :"test.yaml" , 328expectedIncluded :true , 329 }, 330 { 331name :"include multiple types - not matched" , 332includeTypes : []string {"yaml" ,"json" }, 333filename :"test.txt" , 334expectedIncluded :false , 335 }, 336 337 { 338name :"include with uppercase" , 339includeTypes : []string {"YAML" }, 340filename :"test.yaml" , 341expectedIncluded :true , 342 }, 343 { 344name :"include file without extension" , 345includeTypes : []string {"yaml" }, 346filename :"test" , 347expectedIncluded :false , 348 }, 349 { 350name :"include with dot in extension" , 351includeTypes : []string {".yaml" }, 352filename :"test.yaml" , 353expectedIncluded :true , 354 }, 355 } 356 357for _ ,tt := range tests { 358t .Run (tt .name ,func (t * testing.T ) { 359ui := & common.UI {} 360ui .SetIncludeTypes (tt .includeTypes ) 361if len (tt .includeTypes )> 0 { 362assert .True (t ,ui .IsFilteringFiles ()) 363 }else { 364assert .False (t ,ui .IsFilteringFiles ()) 365 } 366 367actual := ui .ShouldFileBeIncludedByType (tt .filename ) 368assert .Equal (t ,tt .expectedIncluded ,actual ) 369 }) 370 } 371} 372 373func TestCreateFileTypeFilter (t * testing.T ) { 374tests := []struct { 375name string 376includeTypes []string 377ignoreTypes []string 378filename string 379expectedFiltered bool 380 }{ 381 { 382name :"no filters" , 383includeTypes : []string {}, 384ignoreTypes : []string {}, 385filename :"test.yaml" , 386expectedFiltered :false , 387 }, 388 { 389name :"include filter - matched" , 390includeTypes : []string {"yaml" }, 391ignoreTypes : []string {}, 392filename :"test.yaml" , 393expectedFiltered :false , 394 }, 395 { 396name :"include filter - not matched" , 397includeTypes : []string {"json" }, 398ignoreTypes : []string {}, 399filename :"test.yaml" , 400expectedFiltered :true , 401 }, 402 { 403name :"ignore filter - matched" , 404includeTypes : []string {}, 405ignoreTypes : []string {"yaml" }, 406filename :"test.yaml" , 407expectedFiltered :true , 408 }, 409 { 410name :"ignore filter - not matched" , 411includeTypes : []string {}, 412ignoreTypes : []string {"json" }, 413filename :"test.yaml" , 414expectedFiltered :false , 415 }, 416 { 417name :"include filter takes precedence" , 418includeTypes : []string {"yaml" }, 419ignoreTypes : []string {"yaml" }, 420filename :"test.yaml" , 421expectedFiltered :false , 422 }, 423 } 424 425for _ ,tt := range tests { 426t .Run (tt .name ,func (t * testing.T ) { 427ui := & common.UI {} 428ui .SetIncludeTypes (tt .includeTypes ) 429ui .SetIgnoreTypes (tt .ignoreTypes ) 430 431filter := ui .CreateFileTypeFilter () 432var actual bool 433if filter == nil { 434// When filter is nil, no filtering is applied, so file should not be filtered 435actual = false 436 }else { 437actual = filter (tt .filename ) 438 } 439assert .Equal (t ,tt .expectedFiltered ,actual ) 440 }) 441 } 442} 443 444func TestFileTypeFilterWithRealFiles (t * testing.T ) { 445// Create a temporary directory with test files 446tmpDir := t .TempDir () 447 448// Create test files 449testFiles := []struct { 450name string 451content string 452expected bool // expected to be included 453 }{ 454 {"test.yaml" ,"key: value" ,true }, 455 {"test.json" ,"{\"key\": \"value\"}" ,true }, 456 {"test.txt" ,"plain text" ,false }, 457 {"test.go" ,"package main" ,false }, 458 {"noextension" ,"no extension" ,false }, 459 } 460 461for _ ,tf := range testFiles { 462filePath := filepath .Join (tmpDir ,tf .name ) 463err := os .WriteFile (filePath , []byte (tf .content ),0644 ) 464assert .NoError (t ,err ) 465 } 466 467// Test include filter 468ui := & common.UI {} 469ui .SetIncludeTypes ([]string {"yaml" ,"json" }) 470filter := ui .CreateFileTypeFilter () 471 472for _ ,tf := range testFiles { 473actual := filter (tf .name ) 474expected := ! tf .expected // filter returns true if file should be filtered out 475assert .Equal (t ,expected ,actual ,"Failed for file: %s" ,tf .name ) 476 } 477 478// Test ignore filter 479ui2 := & common.UI {} 480ui2 .SetIgnoreTypes ([]string {"txt" ,"go" }) 481filter2 := ui2 .CreateFileTypeFilter () 482 483for _ ,tf := range testFiles { 484actual := filter2 (tf .name ) 485// For ignore filter, yaml and json should not be filtered, txt and go should be filtered 486expected := tf .name == "test.txt" || tf .name == "test.go" 487assert .Equal (t ,expected ,actual ,"Failed for file: %s" ,tf .name ) 488 } 489} 490 491func TestCreateFileTypeFilterReturnsNilWhenNoFiltering (t * testing.T ) { 492ui := & common.UI {} 493// No include or ignore types set 494filter := ui .CreateFileTypeFilter () 495assert .Nil (t ,filter ,"CreateFileTypeFilter should return nil when no filtering is configured" ) 496}