From a2a646534637a653082ba40c9f262628d719b546 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 24 Apr 2024 15:25:19 +0200 Subject: [PATCH] add test --- lib/ci/run_ci.js | 1 - test/unit/ci_start.test.js | 44 +++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/ci/run_ci.js b/lib/ci/run_ci.js index e14ddb41..29725da8 100644 --- a/lib/ci/run_ci.js +++ b/lib/ci/run_ci.js @@ -85,7 +85,6 @@ export class RunPRJob { try { cli.startSpinner('Starting PR CI job'); - throw 'NOOO' const response = await this.request.fetch(CI_PR_URL, { method: 'POST', headers: { diff --git a/test/unit/ci_start.test.js b/test/unit/ci_start.test.js index f2285d7d..2628d796 100644 --- a/test/unit/ci_start.test.js +++ b/test/unit/ci_start.test.js @@ -1,4 +1,4 @@ -import { describe, it, before } from 'node:test'; +import { describe, it, before, afterEach } from 'node:test'; import assert from 'assert'; import sinon from 'sinon'; @@ -9,6 +9,7 @@ import { CI_CRUMB_URL, CI_PR_URL } from '../../lib/ci/run_ci.js'; +import PRChecker from '../../lib/pr_checker.js'; import TestCLI from '../fixtures/test_cli.js'; @@ -111,4 +112,45 @@ describe('Jenkins', () => { const jobRunner = new RunPRJob(cli, request, owner, repo, prid, true); assert.strictEqual(await jobRunner.start(), false); }); + + describe('without --certify-safe flag', () => { + afterEach(() => { + sinon.restore(); + }); + for (const certifySafe of [false, true]) { + it(`should return ${certifySafe} if PR checker reports it as ${ + certifySafe ? '' : 'potentially un' + }safe`, async() => { + const cli = new TestCLI(); + + sinon.replace(PRChecker.prototype, 'checkCommitsAfterReview', + sinon.fake.returns(certifySafe)); + + const request = { + gql: sinon.stub().returns({ + repository: { + pullRequest: { + labels: { + nodes: [] + } + } + } + }), + fetch: sinon.stub() + .callsFake((url, { method, headers, body }) => { + assert.strictEqual(url, CI_PR_URL); + assert.strictEqual(method, 'POST'); + assert.deepStrictEqual(headers, { 'Jenkins-Crumb': crumb }); + assert.ok(body._validated); + return Promise.resolve({ status: 201 }); + }), + json: sinon.stub().withArgs(CI_CRUMB_URL) + .returns(Promise.resolve({ crumb })) + }; + + const jobRunner = new RunPRJob(cli, request, owner, repo, prid, false); + assert.strictEqual(await jobRunner.start(), certifySafe); + }); + } + }); });