File: shared_test.go

package info (click to toggle)
gh 2.46.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,548 kB
  • sloc: sh: 227; makefile: 117
file content (203 lines) | stat: -rw-r--r-- 5,884 bytes parent folder | download | duplicates (2)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package shared

import (
	"bytes"
	"encoding/json"
	"net/http"
	"strings"
	"testing"
	"time"

	"github.com/MakeNowJust/heredoc"
	"github.com/cli/cli/v2/api"
	"github.com/cli/cli/v2/internal/ghrepo"
	"github.com/cli/cli/v2/pkg/httpmock"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestPreciseAgo(t *testing.T) {
	const form = "2006-Jan-02 15:04:05"
	now, _ := time.Parse(form, "2021-Apr-12 14:00:00")

	cases := map[string]string{
		"2021-Apr-12 14:00:00": "0s ago",
		"2021-Apr-12 13:59:30": "30s ago",
		"2021-Apr-12 13:59:00": "1m0s ago",
		"2021-Apr-12 13:30:15": "29m45s ago",
		"2021-Apr-12 13:00:00": "1h0m0s ago",
		"2021-Apr-12 02:30:45": "11h29m15s ago",
		"2021-Apr-11 14:00:00": "24h0m0s ago",
		"2021-Apr-01 14:00:00": "264h0m0s ago",
		"2021-Mar-12 14:00:00": "Mar 12, 2021",
	}

	for createdAt, expected := range cases {
		d, _ := time.Parse(form, createdAt)
		got := preciseAgo(now, d)
		if got != expected {
			t.Errorf("expected %s but got %s for %s", expected, got, createdAt)
		}
	}
}

func TestGetAnnotations404(t *testing.T) {
	reg := &httpmock.Registry{}
	defer reg.Verify(t)

	reg.Register(
		httpmock.REST("GET", "repos/OWNER/REPO/check-runs/123456/annotations"),
		httpmock.StatusStringResponse(404, "not found"))

	httpClient := &http.Client{Transport: reg}
	apiClient := api.NewClientFromHTTP(httpClient)
	repo := ghrepo.New("OWNER", "REPO")

	result, err := GetAnnotations(apiClient, repo, Job{ID: 123456, Name: "a job"})
	assert.NoError(t, err)
	assert.Equal(t, result, []Annotation{})
}

func TestRun_Duration(t *testing.T) {
	now, _ := time.Parse(time.RFC3339, "2022-07-20T11:22:58Z")

	tests := []struct {
		name  string
		json  string
		wants string
	}{
		{
			name: "no run_started_at",
			json: heredoc.Doc(`
				{
					"created_at": "2022-07-20T11:20:13Z",
					"updated_at": "2022-07-20T11:21:16Z",
					"status": "completed"
				}`),
			wants: "1m3s",
		},
		{
			name: "with run_started_at",
			json: heredoc.Doc(`
				{
					"created_at": "2022-07-20T11:20:13Z",
					"run_started_at": "2022-07-20T11:20:55Z",
					"updated_at": "2022-07-20T11:21:16Z",
					"status": "completed"
				}`),
			wants: "21s",
		},
		{
			name: "in_progress",
			json: heredoc.Doc(`
				{
					"created_at": "2022-07-20T11:20:13Z",
					"run_started_at": "2022-07-20T11:20:55Z",
					"updated_at": "2022-07-20T11:21:16Z",
					"status": "in_progress"
				}`),
			wants: "2m3s",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var r Run
			assert.NoError(t, json.Unmarshal([]byte(tt.json), &r))
			assert.Equal(t, tt.wants, r.Duration(now).String())
		})
	}
}

func TestRunExportData(t *testing.T) {
	oldestStartedAt, _ := time.Parse(time.RFC3339, "2022-07-20T11:20:13Z")
	oldestCompletedAt, _ := time.Parse(time.RFC3339, "2022-07-20T11:21:16Z")
	newestStartedAt, _ := time.Parse(time.RFC3339, "2022-07-20T11:20:55Z")
	newestCompletedAt, _ := time.Parse(time.RFC3339, "2022-07-20T11:23:16Z")

	tests := []struct {
		name   string
		fields []string
		run    Run
		output string
	}{
		{
			name:   "exports workflow run's single job",
			fields: []string{"jobs"},
			run: Run{
				Jobs: []Job{
					{
						ID:         123456,
						Status:     "completed",
						Conclusion: "success",
						Name:       "macos",
						Steps: []Step{
							{
								Name:       "Checkout",
								Status:     "completed",
								Conclusion: "success",
								Number:     1,
							},
						},
						StartedAt:   oldestStartedAt,
						CompletedAt: oldestCompletedAt,
						URL:         "https://example.com/OWNER/REPO/actions/runs/123456",
					},
				},
			},
			output: `{"jobs":[{"completedAt":"2022-07-20T11:21:16Z","conclusion":"success","databaseId":123456,"name":"macos","startedAt":"2022-07-20T11:20:13Z","status":"completed","steps":[{"conclusion":"success","name":"Checkout","number":1,"status":"completed"}],"url":"https://example.com/OWNER/REPO/actions/runs/123456"}]}`,
		},
		{
			name:   "exports workflow run's multiple jobs",
			fields: []string{"jobs"},
			run: Run{
				Jobs: []Job{
					{
						ID:         123456,
						Status:     "completed",
						Conclusion: "success",
						Name:       "macos",
						Steps: []Step{
							{
								Name:       "Checkout",
								Status:     "completed",
								Conclusion: "success",
								Number:     1,
							},
						},
						StartedAt:   oldestStartedAt,
						CompletedAt: oldestCompletedAt,
						URL:         "https://example.com/OWNER/REPO/actions/runs/123456",
					},
					{
						ID:         234567,
						Status:     "completed",
						Conclusion: "error",
						Name:       "windows",
						Steps: []Step{
							{
								Name:       "Checkout",
								Status:     "completed",
								Conclusion: "error",
								Number:     2,
							},
						},
						StartedAt:   newestStartedAt,
						CompletedAt: newestCompletedAt,
						URL:         "https://example.com/OWNER/REPO/actions/runs/234567",
					},
				},
			},
			output: `{"jobs":[{"completedAt":"2022-07-20T11:21:16Z","conclusion":"success","databaseId":123456,"name":"macos","startedAt":"2022-07-20T11:20:13Z","status":"completed","steps":[{"conclusion":"success","name":"Checkout","number":1,"status":"completed"}],"url":"https://example.com/OWNER/REPO/actions/runs/123456"},{"completedAt":"2022-07-20T11:23:16Z","conclusion":"error","databaseId":234567,"name":"windows","startedAt":"2022-07-20T11:20:55Z","status":"completed","steps":[{"conclusion":"error","name":"Checkout","number":2,"status":"completed"}],"url":"https://example.com/OWNER/REPO/actions/runs/234567"}]}`,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			exported := tt.run.ExportData(tt.fields)
			buf := bytes.Buffer{}
			enc := json.NewEncoder(&buf)
			require.NoError(t, enc.Encode(exported))
			assert.Equal(t, tt.output, strings.TrimSpace(buf.String()))
		})
	}
}