internal/control/marks_test.go
96 lines · 3110 bytes
5 symbols in this file
1package control
2
3import (
4 "testing"
5
6 "gitbay.org/gitbay/internal/store"
7)
8
9func TestChecksMark(t *testing.T) {
10 st := func(states ...string) []store.CommitStatus {
11 var out []store.CommitStatus
12 for _, s := range states {
13 out = append(out, store.CommitStatus{State: s})
14 }
15 return out
16 }
17 cases := []struct {
18 in []store.CommitStatus
19 text, sgr string
20 }{
21 {nil, "", ""},
22 {st("success", "success", "skipped"), "✓ 3/3", sgrGreen},
23 {st("success", "pending"), "◐ 1 pending", sgrDim},
24 {st("success", "failure", "error", "pending"), "✗ 2 failed", sgrRed},
25 }
26 for _, tc := range cases {
27 if got := checksMark(tc.in); got.s != tc.text || got.sgr != tc.sgr {
28 t.Errorf("checksMark(%v) = %q %q, want %q %q", tc.in, got.s, got.sgr, tc.text, tc.sgr)
29 }
30 }
31}
32
33func TestReviewMark(t *testing.T) {
34 counts := map[string]bool{"bob": true, "carol": true}
35 mr := store.MR{Author: "alice"}
36 rv := func(who, verdict string, stale bool) store.MRReview {
37 return store.MRReview{Reviewer: who, Verdict: verdict, Stale: stale}
38 }
39 cases := []struct {
40 name string
41 mr store.MR
42 reviews []store.MRReview
43 want string
44 }{
45 {"nothing", mr, nil, ""},
46 {"approved", mr, []store.MRReview{rv("bob", "approve", false)}, "approved"},
47 {"changes outrank", mr, []store.MRReview{rv("bob", "approve", false), rv("carol", "request_changes", false)}, "changes requested"},
48 {"latest verdict wins", mr, []store.MRReview{rv("bob", "request_changes", false), rv("bob", "approve", false)}, "approved"},
49 {"stale ignored", mr, []store.MRReview{rv("bob", "approve", true)}, ""},
50 {"non-writer ignored", mr, []store.MRReview{rv("dave", "approve", false)}, ""},
51 {"author ignored", store.MR{Author: "bob"}, []store.MRReview{rv("bob", "approve", false)}, ""},
52 {"asked of viewer", store.MR{Author: "alice", ReviewRequests: []string{"me"}}, nil, "review requested"},
53 {"asked of others", store.MR{Author: "alice", ReviewRequests: []string{"bob"}}, nil, "requested"},
54 }
55 for _, tc := range cases {
56 if got := reviewMark(tc.mr, tc.reviews, counts, "me"); got.s != tc.want {
57 t.Errorf("%s: reviewMark = %q, want %q", tc.name, got.s, tc.want)
58 }
59 }
60}
61
62func TestLabelsMark(t *testing.T) {
63 if got := labelsMark([]string{"a", "b", "c", "d"}); got != "a, b, +2" {
64 t.Errorf("labelsMark = %q", got)
65 }
66}
67
68func TestStepState(t *testing.T) {
69 cases := []struct {
70 status string
71 failed, n int
72 want string
73 }{
74 {"success", 0, 1, "success"},
75 {"running", 0, 1, ""},
76 {"failure", 0, 1, ""},
77 {"failure", 2, 1, "success"},
78 {"failure", 2, 2, "failure"},
79 {"failure", 2, 3, "skipped"},
80 }
81 for _, tc := range cases {
82 if got := stepState(tc.status, tc.failed, tc.n); got != tc.want {
83 t.Errorf("stepState(%q, %d, %d) = %q, want %q", tc.status, tc.failed, tc.n, got, tc.want)
84 }
85 }
86}
87
88func TestKeyValues(t *testing.T) {
89 got := keyValues(`{"source":"SHA256:x","argv":["--untrusted","a b"],"n":3}`)
90 if got != "argv=[--untrusted a b] n=3 source=SHA256:x" {
91 t.Errorf("keyValues = %q", got)
92 }
93 if got := keyValues("not json"); got != "not json" {
94 t.Errorf("non-JSON = %q", got)
95 }
96}