forked from openshift-pipelines/pipelines-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository_test.go
137 lines (131 loc) · 3.7 KB
/
repository_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package formatting
import (
"testing"
"time"
"github.com/google/go-github/v42/github"
"github.com/jonboulle/clockwork"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knativeapis "knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck/v1beta1"
)
func makeRepoStatus(prname, sha, conditionReason string, cw clockwork.FakeClock, started, completed time.Duration) v1alpha1.RepositoryRunStatus {
return v1alpha1.RepositoryRunStatus{
Status: v1beta1.Status{
Conditions: []knativeapis.Condition{
{
Reason: conditionReason,
},
},
},
PipelineRunName: prname,
StartTime: &metav1.Time{Time: cw.Now().Add(started)},
CompletionTime: &metav1.Time{Time: cw.Now().Add(completed)},
SHA: github.String(sha),
SHAURL: github.String("https://anurl.com/repo/owner/commit/SHA"),
Title: github.String("A title"),
EventType: github.String("pull_request"),
TargetBranch: github.String("TargetBranch"),
LogURL: github.String("https://help.me.obiwan.kenobi"),
}
}
func TestShowLastAge(t *testing.T) {
cw := clockwork.NewFakeClock()
tests := []struct {
name string
want string
repository v1alpha1.Repository
}{
{
name: "show last age",
repository: v1alpha1.Repository{
Status: []v1alpha1.RepositoryRunStatus{
makeRepoStatus("firstfinished", "sha1", "Success", cw, -20*time.Minute, -25*time.Minute),
makeRepoStatus("lastfinished", "sha2", "Success", cw, -10*time.Minute, -15*time.Minute),
},
},
want: "15 minutes ago",
},
{
name: "non status",
repository: v1alpha1.Repository{
Status: []v1alpha1.RepositoryRunStatus{},
},
want: nonAttributedStr,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ShowLastAge(tt.repository, cw); got != tt.want {
t.Errorf("ShowLastAge() = %v, want %v", got, tt.want)
}
})
}
}
func TestShowLastSHA(t *testing.T) {
cw := clockwork.NewFakeClock()
tests := []struct {
repository v1alpha1.Repository
name string
want string
}{
{
name: "show last sha",
repository: v1alpha1.Repository{
Status: []v1alpha1.RepositoryRunStatus{
makeRepoStatus("firstfinished", "sha1", "Success", cw, -20*time.Minute, -25*time.Minute),
makeRepoStatus("lastfinished", "shalast", "Success", cw, -10*time.Minute, -15*time.Minute),
},
},
want: "shalast",
},
{
name: "non status",
repository: v1alpha1.Repository{
Status: []v1alpha1.RepositoryRunStatus{},
},
want: nonAttributedStr,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ShowLastSHA(tt.repository); got != tt.want {
t.Errorf("ShowLastSHA() = %v, want %v", got, tt.want)
}
})
}
}
func TestShowStatus(t *testing.T) {
cw := clockwork.NewFakeClock()
tests := []struct {
name string
repository v1alpha1.Repository
want string
}{
{
name: "show status",
repository: v1alpha1.Repository{
Status: []v1alpha1.RepositoryRunStatus{
makeRepoStatus("firstfinished", "sha1", "Success", cw, -20*time.Minute, -25*time.Minute),
makeRepoStatus("lastfinished", "shalast", "LastSuccess", cw, -10*time.Minute, -15*time.Minute),
},
},
want: "LastSuccess",
},
{
name: "non status",
repository: v1alpha1.Repository{
Status: []v1alpha1.RepositoryRunStatus{},
},
want: "NoRun",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ShowStatus(tt.repository, cli.NewColorScheme(false, false)); got != tt.want {
t.Errorf("ShowStatus() = %v, want %v", got, tt.want)
}
})
}
}