Skip to content

Commit 7af507d

Browse files
Copilotjustaugustus
authored andcommitted
scorecard: fix tests — add t.Cleanup and strengthen skip assertions
Agent-Logs-Url: https://github.com/ossf/allstar/sessions/db2b87bf-95ff-4444-8743-d64b627d1244 Co-authored-by: justaugustus <567897+justaugustus@users.noreply.github.com>
1 parent 228a7d1 commit 7af507d

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

pkg/policies/scorecard/scorecard_test.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,19 @@ func TestCheck(t *testing.T) {
301301
}
302302

303303
func TestCheckUnknownCheckSkipped(t *testing.T) {
304+
origConfigFetchConfig := configFetchConfig
305+
origConfigIsEnabled := configIsEnabled
306+
origScorecardGet := scorecardGet
307+
origChecksAllChecks := checksAllChecks
308+
origScRun := scRun
309+
t.Cleanup(func() {
310+
configFetchConfig = origConfigFetchConfig
311+
configIsEnabled = origConfigIsEnabled
312+
scorecardGet = origScorecardGet
313+
checksAllChecks = origChecksAllChecks
314+
scRun = origScRun
315+
})
316+
304317
configFetchConfig = func(_ context.Context, _ *github.Client, _, _, _ string,
305318
ol config.ConfigLevel, out interface{},
306319
) error {
@@ -324,9 +337,12 @@ func TestCheckUnknownCheckSkipped(t *testing.T) {
324337
return &scorecard.ScClient{}, nil
325338
}
326339
checksAllChecks = checker.CheckNameToFnMap{"test": {}}
340+
// "test" returns a score below threshold so that if the implementation
341+
// regresses to break-on-unknown and never runs "test", pass stays true
342+
// and the assertion below catches the regression.
327343
scRun = func(_ context.Context, _ clients.Repo, _ ...sc.Option) (sc.Result, error) {
328344
return sc.Result{
329-
Checks: []checker.CheckResult{{Name: "test", Score: 10}},
345+
Checks: []checker.CheckResult{{Name: "test", Score: 5}},
330346
}, nil
331347
}
332348

@@ -335,19 +351,34 @@ func TestCheckUnknownCheckSkipped(t *testing.T) {
335351
if err != nil {
336352
t.Fatalf("Unexpected error: %v", err)
337353
}
338-
if !res.Pass {
339-
t.Error("Expected pass — unknown check should be skipped, valid check should still run")
354+
// The unknown check is skipped; "test" still runs and scores 5 < threshold 8,
355+
// so the policy should not pass.
356+
if res.Pass {
357+
t.Error("Expected fail — unknown check should be skipped, valid check with low score should still run and fail")
340358
}
341359
}
342360

343361
func TestCheckPerCheckErrorSkipped(t *testing.T) {
362+
origConfigFetchConfig := configFetchConfig
363+
origConfigIsEnabled := configIsEnabled
364+
origScorecardGet := scorecardGet
365+
origChecksAllChecks := checksAllChecks
366+
origScRun := scRun
367+
t.Cleanup(func() {
368+
configFetchConfig = origConfigFetchConfig
369+
configIsEnabled = origConfigIsEnabled
370+
scorecardGet = origScorecardGet
371+
checksAllChecks = origChecksAllChecks
372+
scRun = origScRun
373+
})
374+
344375
configFetchConfig = func(_ context.Context, _ *github.Client, _, _, _ string,
345376
ol config.ConfigLevel, out interface{},
346377
) error {
347378
if ol == config.OrgLevel {
348379
oc := out.(*OrgConfig)
349380
*oc = OrgConfig{
350-
Checks: []string{"erroring", "passing"},
381+
Checks: []string{"erroring", "failing"},
351382
Threshold: 8,
352383
}
353384
}
@@ -363,12 +394,15 @@ func TestCheckPerCheckErrorSkipped(t *testing.T) {
363394
) (*scorecard.ScClient, error) {
364395
return &scorecard.ScClient{}, nil
365396
}
366-
checksAllChecks = checker.CheckNameToFnMap{"erroring": {}, "passing": {}}
397+
checksAllChecks = checker.CheckNameToFnMap{"erroring": {}, "failing": {}}
398+
// "failing" returns a score below threshold so that if the implementation
399+
// regresses to break-on-error and never processes "failing", pass stays true
400+
// and the assertion below catches the regression.
367401
scRun = func(_ context.Context, _ clients.Repo, _ ...sc.Option) (sc.Result, error) {
368402
return sc.Result{
369403
Checks: []checker.CheckResult{
370404
{Name: "erroring", Error: fmt.Errorf("unsupported check")},
371-
{Name: "passing", Score: 10},
405+
{Name: "failing", Score: 5},
372406
},
373407
}, nil
374408
}
@@ -378,7 +412,9 @@ func TestCheckPerCheckErrorSkipped(t *testing.T) {
378412
if err != nil {
379413
t.Fatalf("Unexpected error: %v", err)
380414
}
381-
if !res.Pass {
382-
t.Error("Expected pass — errored check should be skipped, passing check should still count")
415+
// The errored check is skipped; "failing" still runs and scores 5 < threshold 8,
416+
// so the policy should not pass.
417+
if res.Pass {
418+
t.Error("Expected fail — errored check should be skipped, failing check with low score should still run and fail")
383419
}
384420
}

0 commit comments

Comments
 (0)