This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
out_test.go
134 lines (120 loc) · 3.53 KB
/
out_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
package resource_test
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
resource "github.com/telia-oss/github-pr-resource"
"github.com/telia-oss/github-pr-resource/fakes"
)
func TestPut(t *testing.T) {
tests := []struct {
description string
source resource.Source
version resource.Version
parameters resource.PutParameters
pullRequest *resource.PullRequest
}{
{
description: "put with no parameters does nothing",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
},
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
},
parameters: resource.PutParameters{},
pullRequest: createTestPR(1, "master", false, false),
},
{
description: "we can set status on a commit",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
},
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
},
parameters: resource.PutParameters{
Status: "success",
},
pullRequest: createTestPR(1, "master", false, false),
},
{
description: "we can provide a custom context for the status",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
},
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
},
parameters: resource.PutParameters{
Status: "failure",
Context: "build",
},
pullRequest: createTestPR(1, "master", false, false),
},
{
description: "we can comment on the pull request",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
},
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
},
parameters: resource.PutParameters{
Comment: "comment",
},
pullRequest: createTestPR(1, "master", false, false),
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
github := new(fakes.FakeGithub)
github.GetPullRequestReturns(tc.pullRequest, nil)
git := new(fakes.FakeGit)
git.RevParseReturns("sha", nil)
dir := createTestDirectory(t)
defer os.RemoveAll(dir)
// Run get so we have version and metadata for the put request
// (This is tested in in_test.go)
getInput := resource.GetRequest{Source: tc.source, Version: tc.version, Params: resource.GetParameters{}}
_, err := resource.Get(getInput, github, git, dir)
require.NoError(t, err)
putInput := resource.PutRequest{Source: tc.source, Params: tc.parameters}
output, err := resource.Put(putInput, github, dir)
// Validate output
if assert.NoError(t, err) {
assert.Equal(t, tc.version, output.Version)
}
// Validate method calls put on Github.
if tc.parameters.Status != "" {
if assert.Equal(t, 1, github.UpdateCommitStatusCallCount()) {
commit, context, status := github.UpdateCommitStatusArgsForCall(0)
assert.Equal(t, tc.version.Commit, commit)
assert.Equal(t, tc.parameters.Context, context)
assert.Equal(t, tc.parameters.Status, status)
}
}
if tc.parameters.Comment != "" {
if assert.Equal(t, 1, github.PostCommentCallCount()) {
pr, comment := github.PostCommentArgsForCall(0)
assert.Equal(t, tc.version.PR, pr)
assert.Equal(t, tc.parameters.Comment, comment)
}
}
})
}
}