|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const proxyquire = require('proxyquire') |
4 | | -const sinon = require('sinon') |
5 | 3 | const tap = require('tap') |
6 | 4 | const lolex = require('lolex') |
| 5 | +const nock = require('nock') |
| 6 | + |
| 7 | +const nodeRepo = require('../../lib/node-repo') |
7 | 8 |
|
8 | 9 | const logger = require('../../lib/logger') |
9 | | -const githubClient = require('../../lib/github-client') |
10 | 10 | const readFixture = require('../read-fixture') |
| 11 | +const { ignoreQueryParams } = require('../common') |
11 | 12 |
|
12 | 13 | tap.test('fetchExistingLabels(): caches existing repository labels', async (t) => { |
13 | | - sinon.stub(githubClient.issues, 'listLabelsForRepo', () => Promise.resolve([])) |
14 | | - sinon.stub(githubClient, 'hasNextPage', () => false) |
15 | | - const nodeRepo = proxyquire('../../lib/node-repo', { |
16 | | - './github-client': githubClient |
17 | | - }) |
18 | | - |
19 | | - t.plan(1) |
20 | | - t.tearDown(() => { |
21 | | - githubClient.issues.listLabelsForRepo.restore() |
22 | | - githubClient.hasNextPage.restore() |
23 | | - }) |
24 | | - |
25 | | - await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }) |
26 | | - await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }) |
27 | | - t.ok(githubClient.issues.listLabelsForRepo.calledOnce) |
| 14 | + const owner = 'nodejs' |
| 15 | + const repo = 'node1' |
| 16 | + // Test passes if nock is only called once, no other checks to run |
| 17 | + const scope = nock('https://api.github.com') |
| 18 | + .filteringPath(ignoreQueryParams) |
| 19 | + .get(`/repos/${owner}/${repo}/labels`) |
| 20 | + .once() // should only be called once |
| 21 | + .reply(200, []) |
| 22 | + |
| 23 | + await nodeRepo._fetchExistingLabels({ owner, repo, logger }) |
| 24 | + await nodeRepo._fetchExistingLabels({ owner, repo, logger }) |
| 25 | + scope.done() |
28 | 26 | }) |
29 | 27 |
|
30 | 28 | tap.test('fetchExistingLabels(): cache expires after one hour', async (t) => { |
| 29 | + const owner = 'nodejs' |
| 30 | + const repo = 'node2' |
31 | 31 | const clock = lolex.install() |
32 | | - sinon.stub(githubClient.issues, 'listLabelsForRepo', () => Promise.resolve([])) |
33 | | - sinon.stub(githubClient, 'hasNextPage', () => false) |
34 | | - const nodeRepo = proxyquire('../../lib/node-repo', { |
35 | | - './github-client': githubClient |
36 | | - }) |
37 | 32 |
|
38 | | - t.plan(1) |
| 33 | + // Test passes if nock is only called once, no other checks to run |
| 34 | + const scope = nock('https://api.github.com') |
| 35 | + .filteringPath(ignoreQueryParams) |
| 36 | + .get(`/repos/${owner}/${repo}/labels`) |
| 37 | + .twice() // should be called twice |
| 38 | + .reply(200, []) |
| 39 | + |
39 | 40 | t.tearDown(() => { |
40 | | - githubClient.issues.listLabelsForRepo.restore() |
41 | | - githubClient.hasNextPage.restore() |
42 | 41 | clock.uninstall() |
43 | 42 | }) |
44 | 43 |
|
45 | | - await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }) |
| 44 | + await nodeRepo._fetchExistingLabels({ owner, repo, logger }) |
46 | 45 |
|
47 | 46 | // fetch labels again after 1 hour and 1 minute |
48 | 47 | clock.tick(1000 * 60 * 61) |
49 | 48 |
|
50 | | - await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }) |
51 | | - t.equal(githubClient.issues.listLabelsForRepo.callCount, 2) |
| 49 | + await nodeRepo._fetchExistingLabels({ owner, repo, logger }) |
| 50 | + scope.done() |
52 | 51 | }) |
53 | 52 |
|
54 | 53 | tap.test('fetchExistingLabels(): yields an array of existing label names', async (t) => { |
55 | 54 | const labelsFixture = readFixture('repo-labels.json') |
56 | | - sinon.stub(githubClient.issues, 'listLabelsForRepo', () => Promise.resolve(labelsFixture)) |
57 | | - sinon.stub(githubClient, 'hasNextPage', () => false) |
58 | | - const nodeRepo = proxyquire('../../lib/node-repo', { |
59 | | - './github-client': githubClient |
60 | | - }) |
| 55 | + const owner = 'nodejs' |
| 56 | + const repo = 'node3' |
| 57 | + |
| 58 | + const scope = nock('https://api.github.com') |
| 59 | + .filteringPath(ignoreQueryParams) |
| 60 | + .get(`/repos/${owner}/${repo}/labels`) |
| 61 | + .reply(200, labelsFixture.data) |
61 | 62 |
|
62 | 63 | t.plan(1) |
63 | | - t.tearDown(() => { |
64 | | - githubClient.issues.listLabelsForRepo.restore() |
65 | | - githubClient.hasNextPage.restore() |
66 | | - }) |
67 | 64 |
|
68 | | - const existingLabels = await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }) |
| 65 | + const existingLabels = await nodeRepo._fetchExistingLabels({ owner, repo, logger }) |
69 | 66 | t.ok(existingLabels.includes('cluster')) |
| 67 | + scope.done() |
70 | 68 | }) |
71 | 69 |
|
72 | 70 | tap.test('fetchExistingLabels(): can retrieve more than 100 labels', async (t) => { |
73 | 71 | const labelsFixturePage1 = readFixture('repo-labels.json') |
74 | 72 | const labelsFixturePage2 = readFixture('repo-labels-page-2.json') |
75 | | - sinon.stub(githubClient.issues, 'listLabelsForRepo', (options) => Promise.resolve(options.page === 1 ? labelsFixturePage1 : labelsFixturePage2)) |
76 | | - sinon.stub(githubClient, 'hasNextPage', (listing) => listing === labelsFixturePage1) |
77 | | - const nodeRepo = proxyquire('../../lib/node-repo', { './github-client': githubClient }) |
| 73 | + const owner = 'nodejs' |
| 74 | + const repo = 'node4' |
| 75 | + const headers = { |
| 76 | + 'Link': `<https://api.github.com/repos/${owner}/${repo}/labels?page=2>; rel="next"` |
| 77 | + } |
| 78 | + |
| 79 | + const firstPageScope = nock('https://api.github.com') |
| 80 | + .filteringPath(ignoreQueryParams) |
| 81 | + .get(`/repos/${owner}/${repo}/labels`) |
| 82 | + .reply(200, labelsFixturePage1.data, headers) |
| 83 | + |
| 84 | + const secondPageScope = nock('https://api.github.com') |
| 85 | + .get(`/repos/${owner}/${repo}/labels`) |
| 86 | + .query({ page: 2, per_page: 100, access_token: 'invalid-placeholder-token' }) |
| 87 | + .reply(200, labelsFixturePage2.data) |
78 | 88 |
|
79 | 89 | t.plan(2) |
80 | | - t.tearDown(() => { |
81 | | - githubClient.issues.listLabelsForRepo.restore() |
82 | | - githubClient.hasNextPage.restore() |
83 | | - }) |
84 | 90 |
|
85 | | - const existingLabels = await nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }) |
| 91 | + const existingLabels = await nodeRepo._fetchExistingLabels({ owner, repo, logger }) |
86 | 92 | t.ok(existingLabels.includes('cluster')) |
87 | 93 | t.ok(existingLabels.includes('windows')) |
| 94 | + firstPageScope.done() |
| 95 | + secondPageScope.done() |
88 | 96 | }) |
89 | 97 |
|
90 | 98 | tap.test('getBotPrLabels(): returns labels added by nodejs-github-bot', (t) => { |
91 | 99 | const events = readFixture('pull-request-events.json') |
92 | | - sinon.stub(githubClient.issues, 'getEvents', (options, cb) => { cb(null, events) }) |
93 | | - const nodeRepo = proxyquire('../../lib/node-repo', { './github-client': githubClient }) |
| 100 | + |
| 101 | + const owner = 'nodejs' |
| 102 | + const repo = 'node5' |
| 103 | + const prId = '1' |
| 104 | + |
| 105 | + const scope = nock('https://api.github.com') |
| 106 | + .filteringPath(ignoreQueryParams) |
| 107 | + .get(`/repos/${owner}/${repo}/issues/${prId}/events`) |
| 108 | + .reply(200, events.data) |
94 | 109 |
|
95 | 110 | t.plan(1) |
96 | | - t.tearDown(() => { |
97 | | - githubClient.issues.getEvents.restore() |
98 | | - }) |
99 | 111 |
|
100 | | - nodeRepo.getBotPrLabels({ owner: 'nodejs', repo: 'node', prId: '1' }, (_, labels) => { |
| 112 | + nodeRepo.getBotPrLabels({ owner, repo, prId }, (_, labels) => { |
101 | 113 | t.same(labels, ['testlabel']) |
| 114 | + scope.done() |
102 | 115 | }) |
103 | 116 | }) |
104 | 117 |
|
105 | 118 | tap.test('getBotPrLabels(): returns net labels added/removed by nodejs-github-bot', (t) => { |
106 | 119 | const events = readFixture('pull-request-events-2.json') |
107 | | - sinon.stub(githubClient.issues, 'getEvents', (options, cb) => { cb(null, events) }) |
108 | | - const nodeRepo = proxyquire('../../lib/node-repo', { './github-client': githubClient }) |
109 | 120 |
|
| 121 | + const owner = 'nodejs' |
| 122 | + const repo = 'node6' |
| 123 | + const prId = '2' |
| 124 | + |
| 125 | + const scope = nock('https://api.github.com') |
| 126 | + .filteringPath(ignoreQueryParams) |
| 127 | + .get(`/repos/${owner}/${repo}/issues/${prId}/events`) |
| 128 | + .reply(200, events.data) |
110 | 129 | t.plan(1) |
111 | | - t.tearDown(() => { |
112 | | - githubClient.issues.getEvents.restore() |
113 | | - }) |
114 | 130 |
|
115 | | - nodeRepo.getBotPrLabels({ owner: 'nodejs', repo: 'node', prId: '1' }, (_, labels) => { |
| 131 | + nodeRepo.getBotPrLabels({ owner, repo, prId }, (_, labels) => { |
116 | 132 | t.same(labels, []) |
| 133 | + scope.done() |
117 | 134 | }) |
118 | 135 | }) |
0 commit comments