Skip to content

Commit fe2d2e8

Browse files
committed
feat: test dependents defined in wiby.json
1 parent 9acb856 commit fe2d2e8

File tree

5 files changed

+117
-69
lines changed

5 files changed

+117
-69
lines changed

bin/commands/test.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ exports.desc = 'Use this command to test your breaking changes against any one o
77
exports.builder = (yargs) => yargs
88
.option('dependent', {
99
desc: 'URL of a dependent',
10-
demandOption: true,
10+
type: 'string',
11+
conflicts: 'config'
12+
})
13+
.option('config', {
14+
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`',
1115
type: 'string'
1216
})
1317

14-
exports.handler = (params) => wiby.test(params.dependent)
18+
exports.handler = (params) => {
19+
const config = params.dependent
20+
? { dependents: [{ repository: params.dependent }] }
21+
: wiby.validate({ config: params.config })
22+
23+
return wiby.test(config)
24+
}

lib/test.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@ const fsPromises = require('fs').promises
22
const github = require('./github')
33
const gitURLParse = require('git-url-parse')
44

5-
module.exports = async function (url) {
5+
module.exports = async function ({ dependents }) {
66
const parentPkgJSON = await getLocalPackageJSON()
77
const parentPkgInfo = gitURLParse(parentPkgJSON.repository.url)
88
console.log(`Parent module: ${parentPkgInfo.owner}/${parentPkgJSON.name}`)
99

10-
const dependentPkgInfo = gitURLParse(url)
11-
const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name)
12-
console.log(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
13-
14-
if (!checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) {
15-
throw new Error(`${parentPkgInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
16-
}
17-
1810
const commitURL = await getCommitURL(parentPkgInfo.owner, parentPkgInfo.name)
1911
console.log('Commit URL to test:', commitURL)
2012

21-
const patchedPackageJSON = applyPatch(commitURL, parentPkgInfo.name, dependentPkgJSON)
22-
await pushPatch(patchedPackageJSON, dependentPkgInfo.owner, dependentPkgInfo.name, parentPkgJSON.name)
13+
for (const { repository: url } of dependents) {
14+
const dependentPkgInfo = gitURLParse(url)
15+
const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name)
16+
console.log(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
17+
18+
if (!checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) {
19+
throw new Error(`${parentPkgInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`)
20+
}
21+
22+
const patchedPackageJSON = applyPatch(commitURL, parentPkgInfo.name, dependentPkgJSON)
23+
await pushPatch(patchedPackageJSON, dependentPkgInfo.owner, dependentPkgInfo.name, parentPkgJSON.name)
24+
}
2325
}
2426

2527
const getCommitHash = module.exports.getCommitHash = async function getCommitHash (owner, repo) {

test/cli.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const wibyCommand = path.join(__dirname, '..', 'bin', 'wiby')
66
const cwd = path.join(__dirname, '..')
77

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

@@ -25,6 +25,21 @@ tap.test('test command', async (tap) => {
2525

2626
tap.equal(true, result.includes('Changes pushed to https://github.com/wiby-test/fakeRepo/blob/wiby-wiby/package.json'))
2727
})
28+
29+
tap.test('test command should call test module with all deps from .wiby.json', async (tap) => {
30+
const result = childProcess.execSync(`${wibyCommand} test`, {
31+
cwd: cwd,
32+
env: {
33+
NODE_OPTIONS: '-r ./test/fixtures/http/test-command-positive.js'
34+
}
35+
}).toString()
36+
37+
console.info(result)
38+
39+
tap.equal(true, result.includes('Changes pushed to https://github.com/wiby-test/pass/blob/wiby-wiby/package.json'))
40+
tap.equal(true, result.includes('Changes pushed to https://github.com/wiby-test/fail/blob/wiby-wiby/package.json'))
41+
tap.equal(true, result.includes('Changes pushed to https://github.com/wiby-test/partial/blob/wiby-wiby/package.json'))
42+
})
2843
})
2944

3045
tap.test('result command', async (tap) => {

test/fixtures/http/test-command-positive.js

+73-52
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,82 @@
33
*/
44
const nock = require('nock')
55

6-
nock('https://api.github.com', { allowUnmocked: false })
7-
// get package json
8-
.post('/graphql')
9-
.reply(200, {
10-
data: {
11-
repository: {
12-
object: {
13-
text: JSON.stringify({
14-
dependencies: {
15-
wiby: '*'
16-
}
17-
})
6+
function nockPkgjsWiby (nockInstance) {
7+
return nockInstance
8+
// get package json
9+
.post('/graphql')
10+
.times(3)
11+
.reply(200, {
12+
data: {
13+
repository: {
14+
object: {
15+
text: JSON.stringify({
16+
dependencies: {
17+
wiby: '*'
18+
}
19+
})
20+
}
1821
}
1922
}
20-
}
21-
})
22-
// get commit sha
23-
.get('/repos/pkgjs/wiby/commits?per_page=1')
24-
.reply(200, [
25-
{
26-
sha: 'fake_sha',
27-
commit: {
28-
tree: {
29-
sha: 'fake_sha'
23+
})
24+
// get commit sha
25+
.get('/repos/pkgjs/wiby/commits?per_page=1')
26+
.reply(200, [
27+
{
28+
sha: 'fake_sha',
29+
commit: {
30+
tree: {
31+
sha: 'fake_sha'
32+
}
3033
}
3134
}
32-
}
33-
])
34-
// get dependent commit sha
35-
.get('/repos/wiby-test/fakeRepo/commits?per_page=1')
36-
.reply(200, [
37-
{
38-
sha: 'fake_sha',
39-
commit: {
40-
tree: {
41-
sha: 'fake_sha'
35+
])
36+
}
37+
38+
function nockRepo (nockInstance, repo) {
39+
return nockInstance
40+
// get dependent commit sha
41+
.get(`/repos/wiby-test/${repo}/commits?per_page=1`)
42+
.reply(200, [
43+
{
44+
sha: 'fake_sha',
45+
commit: {
46+
tree: {
47+
sha: 'fake_sha'
48+
}
4249
}
4350
}
44-
}
45-
])
46-
// create blob
47-
.post('/repos/wiby-test/fakeRepo/git/blobs')
48-
.reply(200, {
49-
sha: 'fake_sha'
50-
})
51-
// create tree
52-
.post('/repos/wiby-test/fakeRepo/git/trees')
53-
.reply(200, {
54-
sha: 'fake_sha'
55-
})
56-
// create commit in dependent
57-
.post('/repos/wiby-test/fakeRepo/git/commits')
58-
.reply(200, {
59-
sha: 'fake_sha'
60-
})
61-
// create branch in dependent
62-
.post('/repos/wiby-test/fakeRepo/git/refs')
63-
.reply(200, {})
51+
])
52+
// create blob
53+
.post(`/repos/wiby-test/${repo}/git/blobs`)
54+
.reply(200, {
55+
sha: 'fake_sha'
56+
})
57+
// create tree
58+
.post(`/repos/wiby-test/${repo}/git/trees`)
59+
.reply(200, {
60+
sha: 'fake_sha'
61+
})
62+
// create commit in dependent
63+
.post(`/repos/wiby-test/${repo}/git/commits`)
64+
.reply(200, {
65+
sha: 'fake_sha'
66+
})
67+
// create branch in dependent
68+
.post(`/repos/wiby-test/${repo}/git/refs`)
69+
.reply(200, {})
70+
}
71+
72+
function buildNock () {
73+
let nockInstance = nock('https://api.github.com', { allowUnmocked: false })
74+
75+
nockInstance = nockPkgjsWiby(nockInstance)
76+
nockInstance = nockRepo(nockInstance, 'fakeRepo')
77+
nockInstance = nockRepo(nockInstance, 'fail')
78+
nockInstance = nockRepo(nockInstance, 'pass')
79+
nockInstance = nockRepo(nockInstance, 'partial')
80+
81+
return nockInstance
82+
}
83+
84+
buildNock()

test/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ tap.test('test command checks package exists in dependant package.json', tap =>
6868
})
6969

7070
tap.rejects(
71-
pkgTest(`https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}`),
71+
pkgTest({ dependents: [{ repository: `https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}` }] }),
7272
new Error(`pkgjs/wiby not found in the package.json of ${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}`)
7373
)
7474
tap.end()

0 commit comments

Comments
 (0)