Skip to content

Commit

Permalink
feat: report results for dependents defined in wiby.json
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas committed Oct 21, 2020
1 parent fe2d2e8 commit 95e9ced
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 23 deletions.
14 changes: 12 additions & 2 deletions bin/commands/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ exports.desc = 'Use this command to fetch the results of your latest test agains
exports.builder = (yargs) => yargs
.option('dependent', {
desc: 'URL of a dependent',
demandOption: true,
type: 'string',
conflicts: 'config'
})
.option('config', {
desc: 'Path to the configuration file. By default it will try to load the configuration from the first file it finds in the current working directory: `.wiby.json`, `.wiby.js`',
type: 'string'
})

exports.handler = (params) => wiby.result(params.dependent)
exports.handler = (params) => {
const config = params.dependent
? { dependents: [{ repository: params.dependent }] }
: wiby.validate({ config: params.config })

return wiby.result(config)
}
33 changes: 18 additions & 15 deletions lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@ const test = require('./test')
const github = require('./github')
const gitURLParse = require('git-url-parse')

module.exports = async function (url) {
module.exports = async function ({ dependents }) {
const parentPkgJSON = await test.getLocalPackageJSON()
const parentPkgInfo = gitURLParse(parentPkgJSON.repository.url)
console.log(`Parent module: ${parentPkgInfo.owner}/${parentPkgJSON.name}`)

const dependentPkgInfo = gitURLParse(url)
const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name)
console.log(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
for (const { repository: url } of dependents) {
const dependentPkgInfo = gitURLParse(url)
const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name)
console.log(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)

if (!test.checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) {
throw new Error(`${parentPkgInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
}
if (!test.checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) {
throw new Error(`${parentPkgInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
}

const branch = await getBranchName(parentPkgJSON.name)
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)
}

const branch = await getBranchName(parentPkgJSON.name)
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)
const runs = resp.data.check_runs || resp.data
console.log(`Tests on branch "${branch}"`)
const results = getResultForEachRun(runs)
console.log(results)
}
const runs = resp.data.check_runs || resp.data
console.log(`Tests on branch "${branch}"`)
const results = getResultForEachRun(runs)
console.log(results)
}

const getBranchName = module.exports.getBranchName = async function getBranchName (dep) {
Expand Down
26 changes: 21 additions & 5 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,19 @@ tap.test('test command', async (tap) => {
}
}).toString()

console.info(result)

tap.equal(true, result.includes('Changes pushed to https://github.com/wiby-test/pass/blob/wiby-wiby/package.json'))
tap.equal(true, result.includes('Changes pushed to https://github.com/wiby-test/fail/blob/wiby-wiby/package.json'))
tap.equal(true, result.includes('Changes pushed to https://github.com/wiby-test/partial/blob/wiby-wiby/package.json'))
})
})

tap.test('result command', async (tap) => {
tap.test('result command should require dependent option', async (tap) => {
tap.test('result command should fail when config and dependent provided', async (tap) => {
try {
childProcess.execSync(`${wibyCommand} result`, { cwd: cwd }).toString()
childProcess.execSync(`${wibyCommand} result --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`, { cwd: cwd }).toString()
tap.fail()
} catch (err) {
tap.equal(true, err.message.includes('Missing required argument: dependent'))
tap.equal(true, err.message.includes('Arguments dependent and config are mutually exclusive'))
}
})

Expand All @@ -64,6 +62,24 @@ tap.test('result command', async (tap) => {
tap.equal(true, result.includes("[ 'fake_run_2', 'fake_conclusion' ]"))
})

tap.test('test command should call test module with all deps from .wiby.json', async (tap) => {
const result = childProcess.execSync(`${wibyCommand} result`, {
cwd: cwd,
env: {
NODE_OPTIONS: '-r ./test/fixtures/http/result-command-positive.js'
}
}).toString()

tap.equal(true, result.includes("[ 'fail_run', 'queued' ]"))
tap.equal(true, result.includes("[ 'fail_run_2', 'fake_conclusion' ]"))

tap.equal(true, result.includes("[ 'pass_run', 'queued' ]"))
tap.equal(true, result.includes("[ 'pass_run_2', 'fake_conclusion' ]"))

tap.equal(true, result.includes("[ 'partial_run', 'queued' ]"))
tap.equal(true, result.includes("[ 'partial_run_2', 'fake_conclusion' ]"))
})

tap.test('result command handles empty response from github.getChecks()', tap => {
const result = childProcess.execSync(`${wibyCommand} result --dependent="https://github.com/wiby-test/fakeRepo"`, {
cwd: cwd,
Expand Down
22 changes: 22 additions & 0 deletions test/fixtures/http/result-command-positive.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const nock = require('nock')
nock('https://api.github.com', { allowUnmocked: false })
// get package json
.post('/graphql')
.times(3)
.reply(200, {
data: {
repository: {
Expand All @@ -27,3 +28,24 @@ nock('https://api.github.com', { allowUnmocked: false })
{ status: 'done', name: 'fake_run_2', conclusion: 'fake_conclusion' }
]
})
.get('/repos/wiby-test/fail/commits/wiby-wiby/check-runs')
.reply(200, {
check_runs: [
{ status: 'queued', name: 'fail_run' },
{ status: 'done', name: 'fail_run_2', conclusion: 'fake_conclusion' }
]
})
.get('/repos/wiby-test/pass/commits/wiby-wiby/check-runs')
.reply(200, {
check_runs: [
{ status: 'queued', name: 'pass_run' },
{ status: 'done', name: 'pass_run_2', conclusion: 'fake_conclusion' }
]
})
.get('/repos/wiby-test/partial/commits/wiby-wiby/check-runs')
.reply(200, {
check_runs: [
{ status: 'queued', name: 'partial_run' },
{ status: 'done', name: 'partial_run_2', conclusion: 'fake_conclusion' }
]
})
2 changes: 1 addition & 1 deletion test/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tap.test('result command checks package exists in dependant package.json', tap =
})

tap.rejects(
result(`https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}`),
result({ dependents: [{ repository: `https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}` }] }),
new Error(`pkgjs/wiby not found in the package.json of ${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}`)
)
tap.end()
Expand Down

0 comments on commit 95e9ced

Please sign in to comment.