Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add tests
  • Loading branch information
aduh95 committed Apr 6, 2025
commit 37696bd8512f56e2aac8dc9a11a7417b911e36ac
2 changes: 1 addition & 1 deletion lib/ci/run_ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const CI_PR_URL = `https://${CI_DOMAIN}/job/${CI_PR_NAME}/build`;
const CI_V8_NAME = CI_TYPES.get(CI_TYPES_KEYS.V8).jobName;
export const CI_V8_URL = `https://${CI_DOMAIN}/job/${CI_V8_NAME}/build`;

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

async function findSafeFullCommitSHA(cli, prData, request) {
// First, let's check if the head was approved.
Expand Down
67 changes: 49 additions & 18 deletions test/unit/ci_start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ describe('Jenkins', () => {
const repo = 'node-auto-test';
const prid = 123456;
const crumb = 'asdf1234';
const dummySHA = '51ce389dc1d539216d30bba0986a8c270801d65f';

before(() => {
sinon.stub(FormData.prototype, 'append').callsFake(function(key, value) {
assert.strictEqual(key, 'json');
const { parameter } = JSON.parse(value);
const expectedParameters = {
CERTIFY_SAFE: 'on',
COMMIT_SHA_CHECK: 'deadbeef',
COMMIT_SHA_CHECK: dummySHA,
TARGET_GITHUB_ORG: owner,
TARGET_REPO_NAME: repo,
PR_ID: prid,
Expand All @@ -42,7 +43,7 @@ describe('Jenkins', () => {

this._validated = true;

return FormData.prototype.append.wrappedMethod.bind(this)(key, value);
return Reflect.apply(FormData.prototype.append.wrappedMethod, this, arguments);
});
});

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

const jobRunner = new RunPRJob(cli, request, owner, repo, prid, true);
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, dummySHA);
assert.strictEqual(await jobRunner.start(), false);
});

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

const jobRunner = new RunPRJob(cli, request, owner, repo, prid, true);
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, dummySHA);
assert.strictEqual(await jobRunner.start(), false);
});

Expand Down Expand Up @@ -93,7 +94,7 @@ describe('Jenkins', () => {
json: sinon.stub().withArgs(CI_CRUMB_URL)
.returns(Promise.resolve({ crumb }))
};
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, 'deadbeef');
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, dummySHA);
assert.ok(await jobRunner.start());
});

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

describe('without --certify-safe flag', { concurrency: false }, () => {
before(() => {
sinon.replace(PRData.prototype, 'getReviews', function() {});
sinon.replace(PRData.prototype, 'getCommits', function() {});
});
afterEach(() => {
sinon.restore();
PRData.prototype.getCollaborators.restore();
PRData.prototype.getComments.restore();
PRChecker.prototype.getApprovedTipOfHead.restore();
});
for (const certifySafe of [true, false]) {
it(`should return ${certifySafe} if PR checker reports it as ${
certifySafe ? '' : 'potentially un'
}safe`, async() => {
for (const { headIsApproved = false, collaborators = [], comments = [], expected } of [{
headIsApproved: true,
expected: true,
}, {
headIsApproved: false,
expected: false,
}, {
collaborators: ['foo'],
comments: [{ login: 'foo' }],
expected: true,
}, {
// Validates that passing full commit URL also works.
collaborators: ['foo'],
comments: [{ login: 'foo', body: `@nodejs-github-bot test https://github.com/nodejs/node/commit/${dummySHA}.\n` }],
expected: true,
}, {
// Validates that non-collaborator commenting should have no effect.
collaborators: ['foo'],
comments: [{ login: 'bar' }],
expected: false,
}]) {
it(`should return ${expected} with ${
JSON.stringify({ headIsApproved, collaborators, comments })}`, async() => {
const cli = new TestCLI();

sinon.replace(PRData.prototype, 'getCollaborators',
function() { this.collaborators = []; });
sinon.replace(PRData.prototype, 'getComments',
function() { this.comments = []; });
sinon.replace(PRChecker.prototype, 'getApprovedTipOfHead',
sinon.fake.returns(certifySafe && 'deadbeef'));
sinon.stub(PRData.prototype, 'getCollaborators').callsFake(function() {
this.collaborators = collaborators.map(login => ({ login }));
});
sinon.stub(PRData.prototype, 'getComments').callsFake(function() {
this.comments = comments.map(({ body, login }) => ({
body: body ?? `@nodejs-github-bot test ${dummySHA}`,
author: { login }
}));
});
sinon.stub(PRChecker.prototype, 'getApprovedTipOfHead').callsFake(
sinon.fake.returns(headIsApproved && dummySHA));

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

const jobRunner = new RunPRJob(cli, request, owner, repo, prid, false);
assert.strictEqual(await jobRunner.start(), certifySafe);
assert.strictEqual(await jobRunner.start(), expected);
});
}
});
Expand Down
Loading