...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package predicate
16
17 import (
18 "context"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22
23 "edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
24 "edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull/pulltest"
25
26 "edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common"
27 )
28
29 func TestHasLabels(t *testing.T) {
30 p := HasLabels([]string{"foo", "bar"})
31
32 runLabelsTestCase(t, p, []HasLabelsTestCase{
33 {
34 "all labels",
35 &pulltest.Context{
36 LabelsValue: []string{"foo", "bar"},
37 },
38 &common.PredicateResult{
39 Satisfied: true,
40 Values: []string{"foo", "bar"},
41 ConditionValues: []string{"foo", "bar"},
42 },
43 },
44 {
45 "missing a label",
46 &pulltest.Context{
47 LabelsValue: []string{"foo"},
48 },
49 &common.PredicateResult{
50 Satisfied: false,
51 Values: []string{"foo"},
52 ConditionValues: []string{"bar"},
53 },
54 },
55 {
56 "no labels",
57 &pulltest.Context{
58 LabelsValue: []string{},
59 },
60 &common.PredicateResult{
61 Satisfied: false,
62 Values: []string{},
63 ConditionValues: []string{"foo"},
64 },
65 },
66 })
67 }
68
69 type HasLabelsTestCase struct {
70 name string
71 context pull.Context
72 ExpectedPredicateResult *common.PredicateResult
73 }
74
75 func runLabelsTestCase(t *testing.T, p Predicate, cases []HasLabelsTestCase) {
76 ctx := context.Background()
77
78 for _, tc := range cases {
79 t.Run(tc.name, func(t *testing.T) {
80 predicateResult, err := p.Evaluate(ctx, tc.context)
81 if assert.NoError(t, err, "evaluation failed") {
82 assertPredicateResult(t, tc.ExpectedPredicateResult, predicateResult)
83 }
84 })
85 }
86 }
87
View as plain text