Skip to content

Commit

Permalink
Extracted apm versions command logic
Browse files Browse the repository at this point in the history
  • Loading branch information
theethernaut committed Nov 8, 2019
1 parent 20ab9c0 commit 49f42ce
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 8 deletions.
12 changes: 12 additions & 0 deletions packages/aragon-cli/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/aragon-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@
],
"env": {
"test": {
"plugins": [ "istanbul" ]
"plugins": [
"istanbul"
]
}
}
},
Expand Down
21 changes: 14 additions & 7 deletions packages/aragon-cli/src/commands/apm_cmds/versions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { ensureWeb3 } = require('../../helpers/web3-fallback')
const APM = require('@aragon/apm')
const defaultAPMName = require('@aragon/cli-utils/src/helpers/default-apm')
const chalk = require('chalk')
const defaultAPMName = require('@aragon/cli-utils/src/helpers/default-apm')
const { ensureWeb3 } = require('../../helpers/web3-fallback')
const getApmRepoVersions = require('../../lib/apm/getApmRepoVersions')

exports.command = 'versions [apmRepo]'

Expand All @@ -24,13 +24,20 @@ exports.handler = async function({
apm: apmOptions,
}) {
const web3 = await ensureWeb3(network)
const repoName = apmRepo ? defaultAPMName(apmRepo) : module.appName
apmOptions.ensRegistryAddress = apmOptions['ens-registry']
const apmRepoName = apmRepo ? defaultAPMName(apmRepo) : module.appName

const progressHandler = (step) => {

This comment has been minimized.

Copy link
@dapplion

dapplion Nov 9, 2019

Contributor

The cases handled don't match with 49f42ce#diff-8dec29eb941e7ae1e45cf9dd83abd092R3, where it also calls progressHandler for 2 and 3

switch(step) {
case 1:
console.log(`Fetching ${chalk.bold(apmRepoName)} published versions`)
break
}
}

const versions = await APM(web3, apmOptions).getAllVersions(repoName)
const versions = await getApmRepoVersions(web3, apmRepoName, apmOptions, progressHandler)

reporter.info(
`${chalk.blue(repoName)} has ${chalk.green(
`${chalk.blue(apmRepoName)} has ${chalk.green(
versions.length
)} published versions`
)
Expand Down
33 changes: 33 additions & 0 deletions packages/aragon-cli/src/lib/apm/getApmRepoVersions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const APM = require('@aragon/apm')

module.exports = async (web3, apmRepoName, apmOptions, progressHandler) => {
if (progressHandler) {
progressHandler(1)
}

// Ensure the ens-registry property is present,
// and available with the name "ensRegistryAddress".
if (!apmOptions.ensRegistryAddress) {
if (apmOptions['ens-registry']) {
apmOptions.ensRegistryAddress = apmOptions['ens-registry']
} else {
throw new Error('ens-registry not found in given apm options.')
}
}

// Prepare APM object that can comunicate with the apm contracts.
const apm = await APM(web3, apmOptions)

if (progressHandler) {
progressHandler(2)
}

// Query all versions for this repo.
const versions = await apm.getAllVersions(apmRepoName)

if (progressHandler) {
progressHandler(3)
}

return versions
}
128 changes: 128 additions & 0 deletions packages/aragon-cli/test/lib/apm/getApmRepoVersions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import test from 'ava'
import sinon from 'sinon'
import proxyquire from 'proxyquire'

// Default values used to call getApmVersions(...).
const web3 = { name: 'web3' }
const apmRepoName = 'test.aragonpm.eth'
const apmOptions = {}
apmOptions['ens-registry'] = '0x1234512345123451234512345123451234512345'
const progressHandler = (step) => {}

/* Setup and cleanup */

test.beforeEach('setup', t => {
const apmStub = sinon.stub()
apmStub.returns({
getAllVersions: async () => {}
})

const getApmRepoVersions = proxyquire.noCallThru().load('../../../src/lib/apm/getApmRepoVersions', {
'@aragon/apm': apmStub,
})

t.context = {
getApmRepoVersions,
apmStub
}
})

test.afterEach('cleanup', t => {
sinon.restore()
})

/* Tests */

test('properly calls the progressHandler', async t => {
const { getApmRepoVersions } = t.context

const progressHandlerSpy = sinon.spy()

await getApmRepoVersions(
web3,
apmRepoName,
apmOptions,
progressHandlerSpy
)

t.true(progressHandlerSpy.calledThrice)
t.true(progressHandlerSpy.getCall(0).calledWith(1))
t.true(progressHandlerSpy.getCall(1).calledWith(2))
t.true(progressHandlerSpy.getCall(2).calledWith(3))
})

test('tolerates a progressHandler not being specified', async t => {
const { getApmRepoVersions } = t.context

await getApmRepoVersions(
web3,
apmRepoName,
apmOptions,
undefined
)

t.pass()
})

test('calls apm.getAllVersions() with the correct parameters and returns the expected object', async t => {
const { getApmRepoVersions, apmStub } = t.context

const getAllVersionsResponse = { name: 'allVersions' }
const getAllVersions = sinon.stub()
getAllVersions.returns(getAllVersionsResponse)
apmStub.returns({
getAllVersions
})

const versions = await getApmRepoVersions(
web3,
apmRepoName,
apmOptions,
progressHandler
)

t.true(
getAllVersions.calledOnceWith(
apmRepoName
)
)

t.is(versions, getAllVersionsResponse)
})

test('APM constructor gets called with the appropriate parameters', async t => {
const { getApmRepoVersions, apmStub } = t.context

await getApmRepoVersions(
web3,
apmRepoName,
apmOptions,
progressHandler
)

t.true(
apmStub.calledOnceWith(
web3,
apmOptions
)
)
})

test('fails if apmOptions does not contain an ens-registry property', async t => {
const { getApmRepoVersions } = t.context

const emptyApmOptions = {}

const error = await t.throwsAsync(
async () => {
await getApmRepoVersions(
web3,
apmRepoName,
emptyApmOptions,
progressHandler
)
}
)

t.is(error.message, 'ens-registry not found in given apm options.')
})

0 comments on commit 49f42ce

Please sign in to comment.