-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathgithub.test.js
More file actions
193 lines (164 loc) · 5.11 KB
/
Copy pathgithub.test.js
File metadata and controls
193 lines (164 loc) · 5.11 KB
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
'use strict';
const core = require('@actions/core');
const fs = require('fs');
const github = require('@actions/github');
const sinon = require('sinon');
const yaml = require('yaml');
const { ContextStub } = require('./stubs/context');
const { expect } = require('chai');
const {
get_pull_request,
fetch_config,
fetch_changed_files,
assign_reviewers,
clear_cache,
get_team_members,
} = require('../src/github');
describe('github', function() {
beforeEach(function() {
clear_cache();
const context = ContextStub.build();
github.context = context;
sinon.stub(core, 'getInput');
sinon.stub(github, 'getOctokit');
});
afterEach(function() {
core.getInput.restore();
github.getOctokit.restore();
});
describe('get_pull_request()', function() {
it('returns pull request data', function() {
const pull_request = get_pull_request();
// See the default values of ContextStub
expect(pull_request.title).to.equal('Extract GitHub related functions into a github module');
expect(pull_request.author).to.equal('necojackarc');
expect(pull_request.is_draft).to.be.false;
});
});
describe('fetch_config()', function() {
const config_path = 'test/assets/reviewers.yml';
const encoding = 'utf8';
const content = fs.readFileSync(config_path, encoding);
const octokit = {
repos: {
getContent() {
return {
data: {
encoding,
content,
},
};
},
},
};
beforeEach(function() {
core.getInput.withArgs('config').returns(config_path);
github.getOctokit.returns(octokit);
});
it('returns a config object', async function() {
const expected = yaml.parse(Buffer.from(content, encoding).toString());
const actual = await fetch_config();
expect(actual).to.deep.equal(expected);
});
});
describe('fetch_changed_files()', function() {
const stub = sinon.stub();
const octokit = {
pulls: {
listFiles: stub,
},
};
beforeEach(function() {
github.getOctokit.returns(octokit);
});
it('fetch changed files', async function() {
stub.returns({
data: [
{ filename: 'super/mario/64' },
{ filename: 'paper/mario' },
],
});
const expected = [ 'super/mario/64', 'paper/mario' ];
const actual = await fetch_changed_files();
expect(actual).to.deep.equal(expected);
});
it('fetch changed files through the last page', async function() {
const filenames = [];
for (let index = 0; index < 222; index += 1) {
filenames.push(`path/to/file${index}`);
}
const page_size = 100;
const filenames_in_chunks = [];
for (let index = 0; index < filenames.length; index += page_size) {
filenames_in_chunks.push(filenames.slice(index, index + page_size));
}
// Make sure filenames are correctly split into chunks
expect(filenames_in_chunks[0].length).to.equal(100);
expect(filenames_in_chunks[1].length).to.equal(100);
expect(filenames_in_chunks[2].length).to.equal(22);
stub.onCall(1).returns({ data: filenames_in_chunks[0].map((filename) => ({ filename })) });
stub.onCall(2).returns({ data: filenames_in_chunks[1].map((filename) => ({ filename })) });
stub.onCall(3).returns({ data: filenames_in_chunks[2].map((filename) => ({ filename })) });
const changed_files = await fetch_changed_files();
expect(changed_files).to.have.members(filenames);
});
});
describe('assign_reviewers()', function() {
const spy = sinon.spy();
const octokit = {
pulls: {
requestReviewers: spy,
},
};
beforeEach(function() {
github.getOctokit.resetBehavior();
github.getOctokit.returns(octokit);
});
it('assigns reviewers', async function() {
const reviewers = [ 'mario', 'princess-peach', 'team:koopa-troop' ];
await assign_reviewers(reviewers);
expect(spy.calledOnce).to.be.true;
expect(spy.lastCall.args[0]).to.deep.equal({
owner: 'necojackarc',
pull_number: 18,
repo: 'auto-request-review',
reviewers: [
'mario',
'princess-peach',
],
team_reviewers: [
'koopa-troop',
],
});
});
});
describe('get_team_members()', function() {
const stub = sinon.stub();
const octokit = {
teams: {
listMembersInOrg: stub,
},
};
beforeEach(function() {
github.getOctokit.resetBehavior();
github.getOctokit.returns(octokit);
});
it('gets team members', async function() {
stub.returns({
data: [
{ login: 'bowser' },
{ login: 'king-boo' },
{ login: 'goomboss' },
],
});
const team = 'koopa-troop';
const actual = await get_team_members(team);
expect(stub.calledOnce).to.be.true;
expect(stub.lastCall.args[0]).to.deep.equal({
org: 'necojackarc',
team_slug: 'koopa-troop',
});
expect(actual).to.deep.equal([ 'bowser', 'king-boo', 'goomboss' ]);
});
});
});