Skip to content

Commit

Permalink
feat: test 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 9acb856 commit fe2d2e8
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 69 deletions.
14 changes: 12 additions & 2 deletions bin/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ exports.desc = 'Use this command to test your breaking changes against any one o
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.test(params.dependent)
exports.handler = (params) => {
const config = params.dependent
? { dependents: [{ repository: params.dependent }] }
: wiby.validate({ config: params.config })

return wiby.test(config)
}
24 changes: 13 additions & 11 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ const fsPromises = require('fs').promises
const github = require('./github')
const gitURLParse = require('git-url-parse')

module.exports = async function (url) {
module.exports = async function ({ dependents }) {
const parentPkgJSON = await 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}`)

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

const commitURL = await getCommitURL(parentPkgInfo.owner, parentPkgInfo.name)
console.log('Commit URL to test:', commitURL)

const patchedPackageJSON = applyPatch(commitURL, parentPkgInfo.name, dependentPkgJSON)
await pushPatch(patchedPackageJSON, dependentPkgInfo.owner, dependentPkgInfo.name, parentPkgJSON.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 (!checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) {
throw new Error(`${parentPkgInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
}

const patchedPackageJSON = applyPatch(commitURL, parentPkgInfo.name, dependentPkgJSON)
await pushPatch(patchedPackageJSON, dependentPkgInfo.owner, dependentPkgInfo.name, parentPkgJSON.name)
}
}

const getCommitHash = module.exports.getCommitHash = async function getCommitHash (owner, repo) {
Expand Down
21 changes: 18 additions & 3 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const wibyCommand = path.join(__dirname, '..', 'bin', 'wiby')
const cwd = path.join(__dirname, '..')

tap.test('test command', async (tap) => {
tap.test('test command should require dependent option', async (tap) => {
tap.test('test command should fail when config and dependent provided', async (tap) => {
try {
childProcess.execSync(`${wibyCommand} test`, { cwd: cwd }).toString()
childProcess.execSync(`${wibyCommand} test --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 @@ -25,6 +25,21 @@ tap.test('test command', async (tap) => {

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

tap.test('test command should call test module with all deps from .wiby.json', async (tap) => {
const result = childProcess.execSync(`${wibyCommand} test`, {
cwd: cwd,
env: {
NODE_OPTIONS: '-r ./test/fixtures/http/test-command-positive.js'
}
}).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) => {
Expand Down
125 changes: 73 additions & 52 deletions test/fixtures/http/test-command-positive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,82 @@
*/
const nock = require('nock')

nock('https://api.github.com', { allowUnmocked: false })
// get package json
.post('/graphql')
.reply(200, {
data: {
repository: {
object: {
text: JSON.stringify({
dependencies: {
wiby: '*'
}
})
function nockPkgjsWiby (nockInstance) {
return nockInstance
// get package json
.post('/graphql')
.times(3)
.reply(200, {
data: {
repository: {
object: {
text: JSON.stringify({
dependencies: {
wiby: '*'
}
})
}
}
}
}
})
// get commit sha
.get('/repos/pkgjs/wiby/commits?per_page=1')
.reply(200, [
{
sha: 'fake_sha',
commit: {
tree: {
sha: 'fake_sha'
})
// get commit sha
.get('/repos/pkgjs/wiby/commits?per_page=1')
.reply(200, [
{
sha: 'fake_sha',
commit: {
tree: {
sha: 'fake_sha'
}
}
}
}
])
// get dependent commit sha
.get('/repos/wiby-test/fakeRepo/commits?per_page=1')
.reply(200, [
{
sha: 'fake_sha',
commit: {
tree: {
sha: 'fake_sha'
])
}

function nockRepo (nockInstance, repo) {
return nockInstance
// get dependent commit sha
.get(`/repos/wiby-test/${repo}/commits?per_page=1`)
.reply(200, [
{
sha: 'fake_sha',
commit: {
tree: {
sha: 'fake_sha'
}
}
}
}
])
// create blob
.post('/repos/wiby-test/fakeRepo/git/blobs')
.reply(200, {
sha: 'fake_sha'
})
// create tree
.post('/repos/wiby-test/fakeRepo/git/trees')
.reply(200, {
sha: 'fake_sha'
})
// create commit in dependent
.post('/repos/wiby-test/fakeRepo/git/commits')
.reply(200, {
sha: 'fake_sha'
})
// create branch in dependent
.post('/repos/wiby-test/fakeRepo/git/refs')
.reply(200, {})
])
// create blob
.post(`/repos/wiby-test/${repo}/git/blobs`)
.reply(200, {
sha: 'fake_sha'
})
// create tree
.post(`/repos/wiby-test/${repo}/git/trees`)
.reply(200, {
sha: 'fake_sha'
})
// create commit in dependent
.post(`/repos/wiby-test/${repo}/git/commits`)
.reply(200, {
sha: 'fake_sha'
})
// create branch in dependent
.post(`/repos/wiby-test/${repo}/git/refs`)
.reply(200, {})
}

function buildNock () {
let nockInstance = nock('https://api.github.com', { allowUnmocked: false })

nockInstance = nockPkgjsWiby(nockInstance)
nockInstance = nockRepo(nockInstance, 'fakeRepo')
nockInstance = nockRepo(nockInstance, 'fail')
nockInstance = nockRepo(nockInstance, 'pass')
nockInstance = nockRepo(nockInstance, 'partial')

return nockInstance
}

buildNock()
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ tap.test('test command checks package exists in dependant package.json', tap =>
})

tap.rejects(
pkgTest(`https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}`),
pkgTest({ 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 fe2d2e8

Please sign in to comment.