|
15 | 15 | package fileparser |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "errors" |
18 | 19 | "testing" |
| 20 | + |
| 21 | + "github.com/golang/mock/gomock" |
| 22 | + |
| 23 | + "github.com/ossf/scorecard/v4/checker" |
| 24 | + mockrepo "github.com/ossf/scorecard/v4/clients/mockclients" |
19 | 25 | ) |
20 | 26 |
|
21 | 27 | func TestIsTemplateFile(t *testing.T) { |
@@ -372,3 +378,244 @@ func Test_isTestdataFile(t *testing.T) { |
372 | 378 | }) |
373 | 379 | } |
374 | 380 | } |
| 381 | + |
| 382 | +// TestFileGetCbDataAsBoolPointer tests the FileGetCbDataAsBoolPointer function. |
| 383 | +func TestFileGetCbDataAsBoolPointer(t *testing.T) { |
| 384 | + t.Parallel() |
| 385 | + type args struct { |
| 386 | + data FileCbData |
| 387 | + } |
| 388 | + b := true |
| 389 | + //nolint |
| 390 | + tests := []struct { |
| 391 | + name string |
| 392 | + args args |
| 393 | + want *bool |
| 394 | + wantPanic bool |
| 395 | + }{ |
| 396 | + { |
| 397 | + name: "true", |
| 398 | + args: args{ |
| 399 | + data: &b, |
| 400 | + }, |
| 401 | + want: &b, |
| 402 | + }, |
| 403 | + { |
| 404 | + name: "nil", |
| 405 | + args: args{}, |
| 406 | + want: &b, |
| 407 | + wantPanic: true, |
| 408 | + }, |
| 409 | + } |
| 410 | + for _, tt := range tests { |
| 411 | + tt := tt // Re-initializing variable so it is not changed while executing the closure below |
| 412 | + t.Run(tt.name, func(t *testing.T) { |
| 413 | + t.Parallel() |
| 414 | + if tt.wantPanic { |
| 415 | + defer func() { |
| 416 | + if r := recover(); r == nil { |
| 417 | + t.Errorf("FileGetCbDataAsBoolPointer() did not panic") |
| 418 | + } |
| 419 | + }() |
| 420 | + FileGetCbDataAsBoolPointer(tt.args.data) |
| 421 | + return |
| 422 | + } |
| 423 | + if got := FileGetCbDataAsBoolPointer(tt.args.data); got != tt.want { |
| 424 | + t.Errorf("FileGetCbDataAsBoolPointer() = %v, want %v", got, tt.want) |
| 425 | + } |
| 426 | + }) |
| 427 | + } |
| 428 | +} |
| 429 | + |
| 430 | +// TestCheckIfFileExistsV6 tests the CheckIfFileExistsV6 function. |
| 431 | +func TestCheckIfFileExistsV6(t *testing.T) { |
| 432 | + t.Parallel() |
| 433 | + //nolint |
| 434 | + tests := []struct { |
| 435 | + name string |
| 436 | + wantErr bool |
| 437 | + shellPattern string |
| 438 | + caseSensitive bool |
| 439 | + shouldFuncFail bool |
| 440 | + shouldGetPredicateFail bool |
| 441 | + files []string |
| 442 | + }{ |
| 443 | + { |
| 444 | + name: "no files", |
| 445 | + shellPattern: "Dockerfile", |
| 446 | + caseSensitive: true, |
| 447 | + files: []string{ |
| 448 | + "Dockerfile", |
| 449 | + "Dockerfile.template", |
| 450 | + "Dockerfile.template.template", |
| 451 | + "Dockerfile.template.template.template", |
| 452 | + "Dockerfile.template.template.template.template", |
| 453 | + }, |
| 454 | + }, |
| 455 | + { |
| 456 | + name: "no files", |
| 457 | + shellPattern: "Dockerfile", |
| 458 | + caseSensitive: false, |
| 459 | + files: []string{}, |
| 460 | + }, |
| 461 | + { |
| 462 | + name: "no files", |
| 463 | + shellPattern: "Dockerfile", |
| 464 | + caseSensitive: true, |
| 465 | + files: []string{}, |
| 466 | + }, |
| 467 | + { |
| 468 | + name: "no files", |
| 469 | + shellPattern: "Dockerfile", |
| 470 | + caseSensitive: false, |
| 471 | + files: []string{ |
| 472 | + "Dockerfile", |
| 473 | + "Dockerfile.template", |
| 474 | + "Dockerfile.template.template", |
| 475 | + "Dockerfile.template.template.template", |
| 476 | + "Dockerfile.template.template.template.template", |
| 477 | + }, |
| 478 | + shouldFuncFail: true, |
| 479 | + }, |
| 480 | + { |
| 481 | + name: "no files", |
| 482 | + shellPattern: "Dockerfile", |
| 483 | + caseSensitive: false, |
| 484 | + shouldGetPredicateFail: true, |
| 485 | + files: []string{ |
| 486 | + "Dockerfile", |
| 487 | + "Dockerfile.template", |
| 488 | + "Dockerfile.template.template", |
| 489 | + "Dockerfile.template.template.template", |
| 490 | + "Dockerfile.template.template.template.template", |
| 491 | + }, |
| 492 | + }, |
| 493 | + } |
| 494 | + |
| 495 | + for _, tt := range tests { |
| 496 | + tt := tt // Re-initializing variable so it is not changed while executing the closure below |
| 497 | + t.Run(tt.name, func(t *testing.T) { |
| 498 | + t.Parallel() |
| 499 | + x := func(path string, content []byte, data FileCbData) (bool, error) { |
| 500 | + if tt.shouldFuncFail { |
| 501 | + //nolint |
| 502 | + return false, errors.New("test error") |
| 503 | + } |
| 504 | + if tt.shouldGetPredicateFail { |
| 505 | + return false, nil |
| 506 | + } |
| 507 | + return true, nil |
| 508 | + } |
| 509 | + |
| 510 | + ctrl := gomock.NewController(t) |
| 511 | + mockRepo := mockrepo.NewMockRepoClient(ctrl) |
| 512 | + mockRepo.EXPECT().ListFiles(gomock.Any()).Return(tt.files, nil).AnyTimes() |
| 513 | + mockRepo.EXPECT().GetFileContent(gomock.Any()).Return(nil, nil).AnyTimes() |
| 514 | + |
| 515 | + result := CheckFilesContentV6(tt.shellPattern, tt.caseSensitive, mockRepo, x, x) |
| 516 | + |
| 517 | + if tt.wantErr && result == nil { |
| 518 | + t.Errorf("CheckFilesContentV6() = %v, want %v test name %v", result, tt.wantErr, tt.name) |
| 519 | + } |
| 520 | + }) |
| 521 | + } |
| 522 | +} |
| 523 | + |
| 524 | +// TestCheckIfFileExistsV6 tests the CheckIfFileExistsV6 function. |
| 525 | +func TestCheckIfFileExists(t *testing.T) { |
| 526 | + t.Parallel() |
| 527 | + //nolint |
| 528 | + tests := []struct { |
| 529 | + name string |
| 530 | + wantErr bool |
| 531 | + shellPattern string |
| 532 | + caseSensitive bool |
| 533 | + shouldFuncFail bool |
| 534 | + shouldGetPredicateFail bool |
| 535 | + files []string |
| 536 | + }{ |
| 537 | + { |
| 538 | + name: "no files", |
| 539 | + shellPattern: "Dockerfile", |
| 540 | + caseSensitive: true, |
| 541 | + files: []string{ |
| 542 | + "Dockerfile", |
| 543 | + "Dockerfile.template", |
| 544 | + "Dockerfile.template.template", |
| 545 | + "Dockerfile.template.template.template", |
| 546 | + "Dockerfile.template.template.template.template", |
| 547 | + }, |
| 548 | + }, |
| 549 | + { |
| 550 | + name: "no files", |
| 551 | + shellPattern: "Dockerfile", |
| 552 | + caseSensitive: false, |
| 553 | + files: []string{}, |
| 554 | + }, |
| 555 | + { |
| 556 | + name: "no files", |
| 557 | + shellPattern: "Dockerfile", |
| 558 | + caseSensitive: true, |
| 559 | + files: []string{}, |
| 560 | + }, |
| 561 | + { |
| 562 | + name: "no files", |
| 563 | + shellPattern: "Dockerfile", |
| 564 | + caseSensitive: false, |
| 565 | + files: []string{ |
| 566 | + "Dockerfile", |
| 567 | + "Dockerfile.template", |
| 568 | + "Dockerfile.template.template", |
| 569 | + "Dockerfile.template.template.template", |
| 570 | + "Dockerfile.template.template.template.template", |
| 571 | + }, |
| 572 | + shouldFuncFail: true, |
| 573 | + }, |
| 574 | + { |
| 575 | + name: "no files", |
| 576 | + shellPattern: "Dockerfile", |
| 577 | + caseSensitive: false, |
| 578 | + shouldGetPredicateFail: true, |
| 579 | + files: []string{ |
| 580 | + "Dockerfile", |
| 581 | + "Dockerfile.template", |
| 582 | + "Dockerfile.template.template", |
| 583 | + "Dockerfile.template.template.template", |
| 584 | + "Dockerfile.template.template.template.template", |
| 585 | + }, |
| 586 | + }, |
| 587 | + } |
| 588 | + |
| 589 | + for _, tt := range tests { |
| 590 | + tt := tt // Re-initializing variable so it is not changed while executing the closure below |
| 591 | + t.Run(tt.name, func(t *testing.T) { |
| 592 | + t.Parallel() |
| 593 | + x := func(path string, content []byte, |
| 594 | + dl checker.DetailLogger, data FileCbData) (bool, error) { |
| 595 | + if tt.shouldFuncFail { |
| 596 | + //nolint |
| 597 | + return false, errors.New("test error") |
| 598 | + } |
| 599 | + if tt.shouldGetPredicateFail { |
| 600 | + return false, nil |
| 601 | + } |
| 602 | + return true, nil |
| 603 | + } |
| 604 | + |
| 605 | + ctrl := gomock.NewController(t) |
| 606 | + mockRepo := mockrepo.NewMockRepoClient(ctrl) |
| 607 | + mockRepo.EXPECT().ListFiles(gomock.Any()).Return(tt.files, nil).AnyTimes() |
| 608 | + mockRepo.EXPECT().GetFileContent(gomock.Any()).Return(nil, nil).AnyTimes() |
| 609 | + |
| 610 | + c := checker.CheckRequest{ |
| 611 | + RepoClient: mockRepo, |
| 612 | + } |
| 613 | + |
| 614 | + result := CheckFilesContent(tt.shellPattern, tt.caseSensitive, &c, x, x) |
| 615 | + |
| 616 | + if tt.wantErr && result == nil { |
| 617 | + t.Errorf("CheckFilesContentV6() = %v, want %v test name %v", result, tt.wantErr, tt.name) |
| 618 | + } |
| 619 | + }) |
| 620 | + } |
| 621 | +} |
0 commit comments