Skip to content

Commit

Permalink
Update test utils (#933)
Browse files Browse the repository at this point in the history
Co-authored-by: Azeem Shaikh <azeems@google.com>
  • Loading branch information
azeemshaikh38 and azeemsgoogle committed Aug 30, 2021
1 parent dbb2345 commit d9f5209
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 157 deletions.
44 changes: 25 additions & 19 deletions checks/branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
{
name: "Only development branch",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 1,
NumberOfWarn: 6,
NumberOfInfo: 2,
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
{
name: "Take worst of release and development",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 5,
NumberOfWarn: 8,
NumberOfInfo: 9,
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
{
name: "Both release and development are OK",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 9,
NumberOfWarn: 4,
NumberOfInfo: 14,
Expand Down Expand Up @@ -300,7 +300,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
{
name: "Ignore a non-branch targetcommitish",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 1,
NumberOfWarn: 6,
NumberOfInfo: 2,
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
{
name: "TargetCommittish nil",
expected: scut.TestReturn{
Errors: []error{sce.ErrScorecardInternal},
Error: sce.ErrScorecardInternal,
Score: checker.InconclusiveResultScore,
NumberOfWarn: 0,
NumberOfInfo: 0,
Expand Down Expand Up @@ -396,7 +396,7 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
{
name: "Non-admin check with protected release and development",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 1,
NumberOfWarn: 2,
NumberOfInfo: 0,
Expand Down Expand Up @@ -442,7 +442,9 @@ func TestReleaseAndDevBranchProtected(t *testing.T) {
dl := scut.TestDetailLogger{}
r := checkReleaseAndDevBranchProtection(context.Background(), mockRepoClient, m,
&dl, "testowner", "testrepo")
scut.ValidateTestReturn(t, tt.name, &tt.expected, &r, &dl)
if !scut.ValidateTestReturn(t, tt.name, &tt.expected, &r, &dl) {
t.Fail()
}
ctrl.Finish()
})
}
Expand All @@ -459,7 +461,7 @@ func TestIsBranchProtected(t *testing.T) {
{
name: "Nothing is enabled",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 1,
NumberOfWarn: 6,
NumberOfInfo: 2,
Expand Down Expand Up @@ -502,7 +504,7 @@ func TestIsBranchProtected(t *testing.T) {
{
name: "Nothing is enabled and values in github.Protection are nil",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 1,
NumberOfWarn: 4,
NumberOfInfo: 2,
Expand All @@ -513,7 +515,7 @@ func TestIsBranchProtected(t *testing.T) {
{
name: "Required status check enabled",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 2,
NumberOfWarn: 6,
NumberOfInfo: 3,
Expand Down Expand Up @@ -556,7 +558,7 @@ func TestIsBranchProtected(t *testing.T) {
{
name: "Required status check enabled without checking for status string",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 2,
NumberOfWarn: 6,
NumberOfInfo: 3,
Expand Down Expand Up @@ -599,7 +601,7 @@ func TestIsBranchProtected(t *testing.T) {
{
name: "Required pull request enabled",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 2,
NumberOfWarn: 5,
NumberOfInfo: 3,
Expand Down Expand Up @@ -642,7 +644,7 @@ func TestIsBranchProtected(t *testing.T) {
{
name: "Required admin enforcement enabled",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 3,
NumberOfWarn: 5,
NumberOfInfo: 3,
Expand Down Expand Up @@ -685,7 +687,7 @@ func TestIsBranchProtected(t *testing.T) {
{
name: "Required linear history enabled",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 2,
NumberOfWarn: 5,
NumberOfInfo: 3,
Expand Down Expand Up @@ -728,7 +730,7 @@ func TestIsBranchProtected(t *testing.T) {
{
name: "Allow force push enabled",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 0,
NumberOfWarn: 7,
NumberOfInfo: 1,
Expand Down Expand Up @@ -771,7 +773,7 @@ func TestIsBranchProtected(t *testing.T) {
{
name: "Allow deletions enabled",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 0,
NumberOfWarn: 7,
NumberOfInfo: 1,
Expand Down Expand Up @@ -814,7 +816,7 @@ func TestIsBranchProtected(t *testing.T) {
{
name: "Branches are protected",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: 9,
NumberOfWarn: 2,
NumberOfInfo: 7,
Expand Down Expand Up @@ -860,8 +862,12 @@ func TestIsBranchProtected(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
dl := scut.TestDetailLogger{}
score := IsBranchProtected(tt.protection, "test", &dl)
scut.ValidateTestValues(t, tt.name, &tt.expected, score, nil, &dl)
actual := &checker.CheckResult{
Score: IsBranchProtected(tt.protection, "test", &dl),
}
if !scut.ValidateTestReturn(t, tt.name, &tt.expected, actual, &dl) {
t.Fail()
}
})
}
}
36 changes: 18 additions & 18 deletions checks/permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "run workflow codeql write test",
filename: "./testdata/github-workflow-permissions-run-codeql-write.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MaxResultScore,
NumberOfWarn: 0,
NumberOfInfo: 1,
Expand All @@ -47,7 +47,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "run workflow no codeql write test",
filename: "./testdata/github-workflow-permissions-run-no-codeql-write.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MaxResultScore - 1,
NumberOfWarn: 1,
NumberOfInfo: 1,
Expand All @@ -58,7 +58,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "run workflow write test",
filename: "./testdata/github-workflow-permissions-run-writes-2.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MinResultScore,
NumberOfWarn: 3,
NumberOfInfo: 2,
Expand All @@ -69,7 +69,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "run package workflow write test",
filename: "./testdata/github-workflow-permissions-run-package-workflow-write.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MaxResultScore,
NumberOfWarn: 0,
NumberOfInfo: 2,
Expand All @@ -80,7 +80,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "run package write test",
filename: "./testdata/github-workflow-permissions-run-package-write.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MinResultScore,
NumberOfWarn: 1,
NumberOfInfo: 1,
Expand All @@ -91,7 +91,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "run writes test",
filename: "./testdata/github-workflow-permissions-run-writes.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MaxResultScore,
NumberOfWarn: 0,
NumberOfInfo: 1,
Expand All @@ -102,7 +102,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "write all test",
filename: "./testdata/github-workflow-permissions-writeall.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MinResultScore,
NumberOfWarn: 1,
NumberOfInfo: 0,
Expand All @@ -113,7 +113,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "read all test",
filename: "./testdata/github-workflow-permissions-readall.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MaxResultScore,
NumberOfWarn: 0,
NumberOfInfo: 1,
Expand All @@ -124,7 +124,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "no permission test",
filename: "./testdata/github-workflow-permissions-absent.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MinResultScore,
NumberOfWarn: 1,
NumberOfInfo: 0,
Expand All @@ -135,7 +135,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "writes test",
filename: "./testdata/github-workflow-permissions-writes.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MaxResultScore,
NumberOfWarn: 0,
NumberOfInfo: 1,
Expand All @@ -146,7 +146,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "reads test",
filename: "./testdata/github-workflow-permissions-reads.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MaxResultScore,
NumberOfWarn: 0,
NumberOfInfo: 10,
Expand All @@ -157,7 +157,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "nones test",
filename: "./testdata/github-workflow-permissions-nones.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MaxResultScore,
NumberOfWarn: 0,
NumberOfInfo: 10,
Expand All @@ -168,7 +168,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "none test",
filename: "./testdata/github-workflow-permissions-none.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MaxResultScore,
NumberOfWarn: 0,
NumberOfInfo: 1,
Expand All @@ -179,7 +179,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "status/checks write",
filename: "./testdata/github-workflow-permissions-status-checks.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MaxResultScore - 1,
NumberOfWarn: 2,
NumberOfInfo: 2,
Expand All @@ -190,7 +190,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "sec-events/deployments write",
filename: "./testdata/github-workflow-permissions-secevent-deployments.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MaxResultScore - 2,
NumberOfWarn: 2,
NumberOfInfo: 3,
Expand All @@ -201,7 +201,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "contents write",
filename: "./testdata/github-workflow-permissions-contents.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MinResultScore,
NumberOfWarn: 1,
NumberOfInfo: 2,
Expand All @@ -212,7 +212,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "actions write",
filename: "./testdata/github-workflow-permissions-actions.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MinResultScore,
NumberOfWarn: 1,
NumberOfInfo: 2,
Expand All @@ -223,7 +223,7 @@ func TestGithubTokenPermissions(t *testing.T) {
name: "packages write",
filename: "./testdata/github-workflow-permissions-packages.yaml",
expected: scut.TestReturn{
Errors: nil,
Error: nil,
Score: checker.MinResultScore,
NumberOfWarn: 1,
NumberOfInfo: 1,
Expand Down
Loading

0 comments on commit d9f5209

Please sign in to comment.