Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle a couple of edge cases #94

Merged
merged 2 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.getParentBranchName = async function getParentBranchName () {
}

exports.getTestingBranchName = function getTestingBranchName (parentBranchName) {
return `wiby-${parentBranchName}`
return parentBranchName.startsWith('wiby-') ? parentBranchName : `wiby-${parentBranchName}`
}

exports.getDependencyLink = async function getDependencyLink (owner, repo, commitish) {
Expand Down
19 changes: 17 additions & 2 deletions lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ const debug = logger('wiby:result')

// enum containing possible pipeline checks statuses
const pipelineStatusesEnum = module.exports.pipelineStatusesEnum = Object.freeze({
// statuses returned by github
FAILED: 'failure',
QUEUED: 'queued',
PENDING: 'pending',
SUCCEED: 'success'
SUCCEED: 'success',

// custom statuses
MISSING: 'test branch missing'
})

const PENDING_RESULT_EXIT_CODE = 64
Expand All @@ -36,6 +40,16 @@ module.exports = async function ({ dependents }) {

const parentBranchName = await context.getParentBranchName()
const branch = await context.getTestingBranchName(parentBranchName)
const exists = await github.getBranch(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
if (!exists) {
output.results.push({
dependent: `${dependentPkgInfo.owner}/${dependentPkgInfo.name}`,
status: pipelineStatusesEnum.MISSING,
runs: []
})
allDependentsChecks.push([undefined, pipelineStatusesEnum.MISSING])
continue
}
let resp = await github.getChecks(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
if (resp.data.check_runs.length === 0) {
resp = await github.getCommitStatusesForRef(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
Expand Down Expand Up @@ -106,7 +120,8 @@ const getOverallStatusForAllRuns = module.exports.getOverallStatusForAllRuns = f
// if includes null or pending or queued - overall status is pending
if (statuses.includes(null) ||
statuses.includes(pipelineStatusesEnum.PENDING) ||
statuses.includes(pipelineStatusesEnum.QUEUED)
statuses.includes(pipelineStatusesEnum.QUEUED) ||
statuses.includes(pipelineStatusesEnum.MISSING)
) {
return pipelineStatusesEnum.PENDING
}
Expand Down
56 changes: 51 additions & 5 deletions test/cli/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const gitFixture = require('../fixtures/git')
const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby')
const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures'))

const SUCCESS_RESULT_EXIT_CODE = 0
const FAIL_RESULT_EXIT_CODE = 1
const PENDING_RESULT_EXIT_CODE = 64

tap.test('result command', async (tap) => {
Expand Down Expand Up @@ -37,20 +39,64 @@ tap.test('result command', async (tap) => {
childProcess.execSync(`${wibyCommand} result --dependent="https://github.com/wiby-test/fakeRepo"`, {
env: {
...process.env,
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive.js`
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive-pass.js`
}
})
} catch (e) {
const result = e.output[1].toString().trim()

tap.equal(result, expected)
tap.equal(e.status, PENDING_RESULT_EXIT_CODE)
tap.equal(e.status, SUCCESS_RESULT_EXIT_CODE)
}
})

tap.test('result command should call result module with all deps from .wiby.json', async (tap) => {
const expected = fs.readFileSync(
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-multiple-dependant.md'),
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-multiple-pass.md'),
'utf-8'
)
.trim()

try {
childProcess.execSync(`${wibyCommand} result`, {
env: {
...process.env,
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive-pass.js`
}
})
} catch (e) {
const result = e.output[1].toString().trim()

tap.equal(result, expected)
tap.equal(e.status, SUCCESS_RESULT_EXIT_CODE)
}
})

tap.test('result command should call result module with all deps from .wiby.json (pending result)', async (tap) => {
const expected = fs.readFileSync(
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-multiple-pending.md'),
'utf-8'
)
.trim()

try {
childProcess.execSync(`${wibyCommand} result`, {
env: {
...process.env,
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive-pending.js`
}
})
} catch (e) {
const result = e.output[1].toString().trim()

tap.equal(result, expected)
tap.equal(e.status, PENDING_RESULT_EXIT_CODE)
}
})

tap.test('result command should call result module with all deps from .wiby.json (missing branch result)', async (tap) => {
const expected = fs.readFileSync(
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-missing-branch.md'),
'utf-8'
)
.trim()
Expand All @@ -59,7 +105,7 @@ tap.test('result command', async (tap) => {
childProcess.execSync(`${wibyCommand} result`, {
env: {
...process.env,
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive.js`
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-missing-branch.js`
}
})
} catch (e) {
Expand Down Expand Up @@ -136,7 +182,7 @@ tap.test('result command', async (tap) => {
const result = e.output[1].toString().trim()

tap.equal(result, expected)
tap.equal(e.status, 1)
tap.equal(e.status, FAIL_RESULT_EXIT_CODE)
}
})
})
23 changes: 20 additions & 3 deletions test/cli/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby')
const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures'))

tap.test('test command', async (tap) => {
tap.beforeEach(async () => {
tap.test('test command should fail when config and dependent provided', async (tap) => {
gitFixture.init()
})

tap.test('test command should fail when config and dependent provided', async (tap) => {
try {
childProcess.execSync(`${wibyCommand} test --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`).toString()
tap.fail()
Expand All @@ -24,6 +22,8 @@ tap.test('test command', async (tap) => {
})

tap.test('test command should call test module with dependent URI', async (tap) => {
gitFixture.init()

const result = childProcess.execSync(`${wibyCommand} test --dependent="https://github.com/wiby-test/fakeRepo"`, {
env: {
...process.env,
Expand All @@ -35,6 +35,23 @@ tap.test('test command', async (tap) => {
})

tap.test('test command should call test module with all deps from .wiby.json', async (tap) => {
gitFixture.init()

const result = childProcess.execSync(`${wibyCommand} test`, {
env: {
...process.env,
NODE_OPTIONS: `-r ${fixturesPath}/http/test-command-positive.js`
}
}).toString()

tap.match(result, 'Changes pushed to https://github.com/wiby-test/pass/blob/wiby-running-unit-tests/package.json')
tap.match(result, 'Changes pushed to https://github.com/wiby-test/fail/blob/wiby-running-unit-tests/package.json')
tap.match(result, 'Changes pushed to https://github.com/wiby-test/partial/blob/wiby-running-unit-tests/package.json')
})

tap.test('test command should not add `wiby-` prefix when branch already has it', async (tap) => {
gitFixture.init('wiby-running-unit-tests')

const result = childProcess.execSync(`${wibyCommand} test`, {
env: {
...process.env,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# wiby result command

Overall status - pending

## wiby-test/partial - success

Checks:

- partial_run - success
- partial_run_2 - success

## wiby-test/fail - test branch missing

Checks:

## wiby-test/pass - success

Checks:

- pass_run - success
- pass_run_2 - success
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# wiby result command

Overall status - success

## wiby-test/partial - success

Checks:

- partial_run - success
- partial_run_2 - success

## wiby-test/fail - success

Checks:

- fail_run - success
- fail_run_2 - success

## wiby-test/pass - success

Checks:

- pass_run - success
- pass_run_2 - success
6 changes: 2 additions & 4 deletions test/fixtures/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ const fs = require('fs')
const path = require('path')
const tmp = require('tmp')

exports.TEST_BRANCH_NAME = 'running-unit-tests'

exports.init = function () {
exports.init = function (initialBranch = 'running-unit-tests') {
const gitRepoPath = path.join(__dirname, '..', '..')

const { name: tmpDir } = tmp.dirSync()
process.chdir(tmpDir)

childProcess.execSync('git init --initial-branch=running-unit-tests')
childProcess.execSync(`git init --initial-branch=${initialBranch}`)
childProcess.execSync('git config user.email "[email protected]"')
childProcess.execSync('git config user.name "Wiby Bot"')

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/http/result-command-empty-branch-checks-flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ nock('https://api.github.com')
}
}
})
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
.reply(200, {})
// get check results
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/http/result-command-empty-branch-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ nock('https://api.github.com')
}
}
})
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
.reply(200, {})
// get check results
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
Expand Down
63 changes: 63 additions & 0 deletions test/fixtures/http/result-command-missing-branch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict'

/**
* Mocks of HTTP calls for "wiby result" command positive flow
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it worth to drop few additional words here about this new flow?

*/
const nock = require('nock')

nock.disableNetConnect()

nock('https://api.github.com')
// get package json
.post('/graphql')
.times(3)
.reply(200, {
data: {
repository: {
object: {
text: JSON.stringify({
dependencies: {
wiby: '*'
}
})
}
}
}
})
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/partial/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/pass/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/fail/branches/wiby-running-unit-tests')
.reply(404, {})
// get check results
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'fake_run', conclusion: 'success' },
{ status: 'done', name: 'fake_run_2', conclusion: 'success' }
]
})
.get('/repos/wiby-test/fail/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'fail_run', conclusion: 'success' },
{ status: 'done', name: 'fail_run_2', conclusion: 'success' }
]
})
.get('/repos/wiby-test/pass/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'pass_run', conclusion: 'success' },
{ status: 'done', name: 'pass_run_2', conclusion: 'success' }
]
})
.get('/repos/wiby-test/partial/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'partial_run', conclusion: 'success' },
{ status: 'done', name: 'partial_run_2', conclusion: 'success' }
]
})
2 changes: 2 additions & 0 deletions test/fixtures/http/result-command-positive-checks-failed.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ nock('https://api.github.com')
}
}
})
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
.reply(200, {})
// get check results
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
Expand Down
Loading