|
| 1 | +package junit |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/zegl/kube-score/domain" |
| 9 | + "github.com/zegl/kube-score/scorecard" |
| 10 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | +) |
| 12 | + |
| 13 | +func getTestCard() *scorecard.Scorecard { |
| 14 | + checks := []scorecard.TestScore{ |
| 15 | + { |
| 16 | + Check: domain.Check{ |
| 17 | + Name: "test-warning-two-comments", |
| 18 | + }, |
| 19 | + Grade: scorecard.GradeWarning, |
| 20 | + Comments: []scorecard.TestScoreComment{ |
| 21 | + { |
| 22 | + Path: "a", |
| 23 | + Summary: "summary", |
| 24 | + Description: "description", |
| 25 | + }, |
| 26 | + { |
| 27 | + // No path |
| 28 | + Summary: "summary", |
| 29 | + Description: "description", |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + Check: domain.Check{ |
| 35 | + Name: "test-ok-comment", |
| 36 | + }, |
| 37 | + Grade: scorecard.GradeAllOK, |
| 38 | + Comments: []scorecard.TestScoreComment{ |
| 39 | + { |
| 40 | + Path: "a", |
| 41 | + Summary: "summary", |
| 42 | + Description: "description", |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + Check: domain.Check{ |
| 48 | + Name: "test-skipped-comment", |
| 49 | + }, |
| 50 | + Skipped: true, |
| 51 | + Comments: []scorecard.TestScoreComment{ |
| 52 | + { |
| 53 | + Path: "a", |
| 54 | + Summary: "skipped sum", |
| 55 | + Description: "skipped description", |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + Check: domain.Check{ |
| 61 | + Name: "test-skipped-no-comment", |
| 62 | + }, |
| 63 | + Skipped: true, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + return &scorecard.Scorecard{ |
| 68 | + "a": &scorecard.ScoredObject{ |
| 69 | + TypeMeta: v1.TypeMeta{ |
| 70 | + Kind: "Testing", |
| 71 | + APIVersion: "v1", |
| 72 | + }, |
| 73 | + ObjectMeta: v1.ObjectMeta{ |
| 74 | + Name: "foo", |
| 75 | + Namespace: "foofoo", |
| 76 | + }, |
| 77 | + Checks: checks, |
| 78 | + }, |
| 79 | + |
| 80 | + // No namespace |
| 81 | + "b": &scorecard.ScoredObject{ |
| 82 | + TypeMeta: v1.TypeMeta{ |
| 83 | + Kind: "Testing", |
| 84 | + APIVersion: "v1", |
| 85 | + }, |
| 86 | + ObjectMeta: v1.ObjectMeta{ |
| 87 | + Name: "bar-no-namespace", |
| 88 | + }, |
| 89 | + Checks: checks, |
| 90 | + }, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestJUnitOutput(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + r := JUnit(getTestCard()) |
| 97 | + all, err := io.ReadAll(r) |
| 98 | + assert.Nil(t, err) |
| 99 | + assert.Equal(t, `<testsuites name="kube-score" tests="10" failures="4" skipped="4"> |
| 100 | + <testsuite name="foo/foofoo v1/Testing" tests="5" failures="2" errors="0" id="0" skipped="2" time=""> |
| 101 | + <testcase name="test-warning-two-comments" classname="foo/foofoo v1/Testing"> |
| 102 | + <failure message="(a) summary"></failure> |
| 103 | + </testcase> |
| 104 | + <testcase name="test-warning-two-comments" classname="foo/foofoo v1/Testing"> |
| 105 | + <failure message="summary"></failure> |
| 106 | + </testcase> |
| 107 | + <testcase name="test-ok-comment" classname="foo/foofoo v1/Testing"></testcase> |
| 108 | + <testcase name="test-skipped-comment" classname="foo/foofoo v1/Testing"> |
| 109 | + <skipped message="(a) skipped sum"></skipped> |
| 110 | + </testcase> |
| 111 | + <testcase name="test-skipped-no-comment" classname="foo/foofoo v1/Testing"> |
| 112 | + <skipped message=""></skipped> |
| 113 | + </testcase> |
| 114 | + </testsuite> |
| 115 | + <testsuite name="bar-no-namespace v1/Testing" tests="5" failures="2" errors="0" id="0" skipped="2" time=""> |
| 116 | + <testcase name="test-warning-two-comments" classname="bar-no-namespace v1/Testing"> |
| 117 | + <failure message="(a) summary"></failure> |
| 118 | + </testcase> |
| 119 | + <testcase name="test-warning-two-comments" classname="bar-no-namespace v1/Testing"> |
| 120 | + <failure message="summary"></failure> |
| 121 | + </testcase> |
| 122 | + <testcase name="test-ok-comment" classname="bar-no-namespace v1/Testing"></testcase> |
| 123 | + <testcase name="test-skipped-comment" classname="bar-no-namespace v1/Testing"> |
| 124 | + <skipped message="(a) skipped sum"></skipped> |
| 125 | + </testcase> |
| 126 | + <testcase name="test-skipped-no-comment" classname="bar-no-namespace v1/Testing"> |
| 127 | + <skipped message=""></skipped> |
| 128 | + </testcase> |
| 129 | + </testsuite> |
| 130 | +</testsuites> |
| 131 | +`, string(all)) |
| 132 | +} |
0 commit comments