Skip to content

Commit 37696bd

Browse files
committed
add tests
1 parent 51ce389 commit 37696bd

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

lib/ci/run_ci.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const CI_PR_URL = `https://${CI_DOMAIN}/job/${CI_PR_NAME}/build`;
1616
const CI_V8_NAME = CI_TYPES.get(CI_TYPES_KEYS.V8).jobName;
1717
export const CI_V8_URL = `https://${CI_DOMAIN}/job/${CI_V8_NAME}/build`;
1818

19-
const REQUEST_CI_COMMENT = /^@nodejs-github-bot .+([0-9a-f]{40})$/;
19+
const REQUEST_CI_COMMENT = /^@nodejs-github-bot .+([0-9a-f]{40})\.*\n*$/;
2020

2121
async function findSafeFullCommitSHA(cli, prData, request) {
2222
// First, let's check if the head was approved.

test/unit/ci_start.test.js

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ describe('Jenkins', () => {
2020
const repo = 'node-auto-test';
2121
const prid = 123456;
2222
const crumb = 'asdf1234';
23+
const dummySHA = '51ce389dc1d539216d30bba0986a8c270801d65f';
2324

2425
before(() => {
2526
sinon.stub(FormData.prototype, 'append').callsFake(function(key, value) {
2627
assert.strictEqual(key, 'json');
2728
const { parameter } = JSON.parse(value);
2829
const expectedParameters = {
2930
CERTIFY_SAFE: 'on',
30-
COMMIT_SHA_CHECK: 'deadbeef',
31+
COMMIT_SHA_CHECK: dummySHA,
3132
TARGET_GITHUB_ORG: owner,
3233
TARGET_REPO_NAME: repo,
3334
PR_ID: prid,
@@ -42,7 +43,7 @@ describe('Jenkins', () => {
4243

4344
this._validated = true;
4445

45-
return FormData.prototype.append.wrappedMethod.bind(this)(key, value);
46+
return Reflect.apply(FormData.prototype.append.wrappedMethod, this, arguments);
4647
});
4748
});
4849

@@ -55,7 +56,7 @@ describe('Jenkins', () => {
5556
.returns(Promise.resolve({ crumb }))
5657
};
5758

58-
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, true);
59+
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, dummySHA);
5960
assert.strictEqual(await jobRunner.start(), false);
6061
});
6162

@@ -65,7 +66,7 @@ describe('Jenkins', () => {
6566
json: sinon.stub().throws()
6667
};
6768

68-
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, true);
69+
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, dummySHA);
6970
assert.strictEqual(await jobRunner.start(), false);
7071
});
7172

@@ -93,7 +94,7 @@ describe('Jenkins', () => {
9394
json: sinon.stub().withArgs(CI_CRUMB_URL)
9495
.returns(Promise.resolve({ crumb }))
9596
};
96-
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, 'deadbeef');
97+
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, dummySHA);
9798
assert.ok(await jobRunner.start());
9899
});
99100

@@ -112,26 +113,56 @@ describe('Jenkins', () => {
112113
json: sinon.stub().withArgs(CI_CRUMB_URL)
113114
.returns(Promise.resolve({ crumb }))
114115
};
115-
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, true);
116+
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, dummySHA);
116117
assert.strictEqual(await jobRunner.start(), false);
117118
});
118119

119120
describe('without --certify-safe flag', { concurrency: false }, () => {
121+
before(() => {
122+
sinon.replace(PRData.prototype, 'getReviews', function() {});
123+
sinon.replace(PRData.prototype, 'getCommits', function() {});
124+
});
120125
afterEach(() => {
121-
sinon.restore();
126+
PRData.prototype.getCollaborators.restore();
127+
PRData.prototype.getComments.restore();
128+
PRChecker.prototype.getApprovedTipOfHead.restore();
122129
});
123-
for (const certifySafe of [true, false]) {
124-
it(`should return ${certifySafe} if PR checker reports it as ${
125-
certifySafe ? '' : 'potentially un'
126-
}safe`, async() => {
130+
for (const { headIsApproved = false, collaborators = [], comments = [], expected } of [{
131+
headIsApproved: true,
132+
expected: true,
133+
}, {
134+
headIsApproved: false,
135+
expected: false,
136+
}, {
137+
collaborators: ['foo'],
138+
comments: [{ login: 'foo' }],
139+
expected: true,
140+
}, {
141+
// Validates that passing full commit URL also works.
142+
collaborators: ['foo'],
143+
comments: [{ login: 'foo', body: `@nodejs-github-bot test https://github.com/nodejs/node/commit/${dummySHA}.\n` }],
144+
expected: true,
145+
}, {
146+
// Validates that non-collaborator commenting should have no effect.
147+
collaborators: ['foo'],
148+
comments: [{ login: 'bar' }],
149+
expected: false,
150+
}]) {
151+
it(`should return ${expected} with ${
152+
JSON.stringify({ headIsApproved, collaborators, comments })}`, async() => {
127153
const cli = new TestCLI();
128154

129-
sinon.replace(PRData.prototype, 'getCollaborators',
130-
function() { this.collaborators = []; });
131-
sinon.replace(PRData.prototype, 'getComments',
132-
function() { this.comments = []; });
133-
sinon.replace(PRChecker.prototype, 'getApprovedTipOfHead',
134-
sinon.fake.returns(certifySafe && 'deadbeef'));
155+
sinon.stub(PRData.prototype, 'getCollaborators').callsFake(function() {
156+
this.collaborators = collaborators.map(login => ({ login }));
157+
});
158+
sinon.stub(PRData.prototype, 'getComments').callsFake(function() {
159+
this.comments = comments.map(({ body, login }) => ({
160+
body: body ?? `@nodejs-github-bot test ${dummySHA}`,
161+
author: { login }
162+
}));
163+
});
164+
sinon.stub(PRChecker.prototype, 'getApprovedTipOfHead').callsFake(
165+
sinon.fake.returns(headIsApproved && dummySHA));
135166

136167
const request = {
137168
gql: sinon.stub().returns({
@@ -156,7 +187,7 @@ describe('Jenkins', () => {
156187
};
157188

158189
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, false);
159-
assert.strictEqual(await jobRunner.start(), certifySafe);
190+
assert.strictEqual(await jobRunner.start(), expected);
160191
});
161192
}
162193
});

0 commit comments

Comments
 (0)