diff --git a/bin/commands/close-pr.js b/bin/commands/close-pr.js index cecca88..920f051 100644 --- a/bin/commands/close-pr.js +++ b/bin/commands/close-pr.js @@ -10,8 +10,8 @@ exports.builder = (yargs) => yargs type: 'string', conflicts: 'config' }) - .option('pull-request', { - desc: 'Raise a draft PR in addition to creating a branch', + .option('close-pr', { + desc: 'Close a PR of a dependent raised in test', alias: 'pr', type: 'boolean', conflicts: 'config' @@ -21,12 +21,15 @@ exports.builder = (yargs) => yargs type: 'string' }) -exports.handler = (params) => { +exports.handler = async (params) => { const config = params.dependent ? { - dependents: [{ repository: params.dependent, pullRequest: !!params['pull-request'] }] + dependents: [{ repository: params.dependent, pullRequest: true }] } : wiby.validate({ config: params.config }) - return wiby.closePR(config) + const result = await wiby.closePR(config) + const output = `${result.length} PRs closed` + console.log(output) + return output } diff --git a/test/cli/closePR.js b/test/cli/closePR.js index ade5157..d725a3a 100644 --- a/test/cli/closePR.js +++ b/test/cli/closePR.js @@ -1,5 +1,4 @@ 'use strict' -/* const tap = require('tap') const gitFixture = require('../fixtures/git') const childProcess = require('child_process') @@ -10,18 +9,36 @@ const fs = require('fs') const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby') const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures')) -tap.test('closePRs command', async (tap) => { - tap.beforeEach(async () => { +tap.only('closePRs command', async (t) => { + t.beforeEach(async () => { nock.disableNetConnect() gitFixture.init() }) - tap.afterEach(async () => { - nock.cleanAll() - nock.enableNetConnect() - }) - tap.test('close PRs should result in a successful outcome', async (t) => { + t.test('close-pr should fail if config and dependent provided', async (t) => { + try { + childProcess.execSync(`${wibyCommand} close-pr --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`) + tap.fail() + } catch (err) { + tap.equal(true, err.message.includes('Arguments dependent and config are mutually exclusive')) + } + }) + t.test('close-pr should call and close the PR on command with dependent argument', async (t) => { + const result = childProcess.execSync(`${wibyCommand} close-pr --dependent="https://github.com/wiby-test/fakeRepo"`, { + env: { + ...process.env, + NODE_OPTIONS: `-r ${fixturesPath}/http/close-pr-command-positive.js` + } + }).toString() + t.match(result, '1 PRs closed\n') + }) + t.only('close-pr should call and close the PR on command using wiby.json settings', async (t) => { + const result = childProcess.execSync(`${wibyCommand} close-pr`, { + env: { + ...process.env, + NODE_OPTIONS: `-r ${fixturesPath}/http/close-pr-command-positive.js` + } + }).toString() + t.match(result, '1 PRs closed\n') }) - tap.end() }) -*/ diff --git a/test/fixtures/http/close-pr-command-positive.js b/test/fixtures/http/close-pr-command-positive.js index 6a82be1..6ce7b06 100644 --- a/test/fixtures/http/close-pr-command-positive.js +++ b/test/fixtures/http/close-pr-command-positive.js @@ -10,7 +10,7 @@ nock.disableNetConnect() function nockRepo (nockInstance, owner, repo, branch) { return nockInstance // /repos/{owner}/{repo}/commits/{ref}/check-runs - .get(`repos/${owner}/${repo}/commits/${branch}/check-runs`) + .get(`/repos/${owner}/${repo}/commits/${branch}/check-runs`) .reply(200, { check_runs: [ { @@ -32,7 +32,8 @@ function nockRepo (nockInstance, owner, repo, branch) { } ] }) - .patch(/repos.*pulls\/1/) + // /repos/{owner}/{repo}/pulls/{pull_number} + .patch(`/repos/${owner}/${repo}/pulls/1`) .reply(200, { state: 'closed', title: 'wiby test pr' @@ -42,8 +43,8 @@ function nockRepo (nockInstance, owner, repo, branch) { function buildNock () { let nockInstance = nock('https://api.github.com') - nockInstance = nockRepo(nockInstance, 'fakeRepo') - + nockInstance = nockRepo(nockInstance, 'wiby-test', 'fakeRepo', 'wiby-running-unit-tests') + nockInstance = nockRepo(nockInstance, 'wiby-test', 'pass', 'wiby-running-unit-tests') return nockInstance }