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

Extracts logic from apm commands: versions command #891

Merged
merged 4 commits into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
50 changes: 41 additions & 9 deletions packages/aragon-cli/src/commands/apm_cmds/versions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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 TaskList = require('listr')
const getApmRepoVersions = require('../../lib/apm/getApmRepoVersions')

exports.command = 'versions [apmRepo]'

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

const tasks = new TaskList([
{
title: 'Fetching published versions',
task: async (ctx, task) => {
const web3 = await ensureWeb3(network)
apmRepoName = apmRepo ? defaultAPMName(apmRepo) : module.appName

task.title = `Fetching ${chalk.bold(apmRepoName)} published versions`

const versions = await APM(web3, apmOptions).getAllVersions(repoName)
versions = await getApmRepoVersions(web3, apmRepoName, apmOptions)
},
},
])
await tasks.run()

displayVersionNumbers(apmRepoName, versions, reporter)
displayVersions(versions, reporter)
process.exit()
}

/**
* Display the number of published versions for the repository
* @param {string} apmRepoName Repo name
* @param {Object[]} versions Repo versions
* @param {Object} reporter Reporter
* @returns {void}
*/
function displayVersionNumbers(apmRepoName, versions, reporter) {
reporter.info(
`${chalk.blue(repoName)} has ${chalk.green(
`${chalk.blue(apmRepoName)} has ${chalk.green(
versions.length
)} published versions`
)
}

/**
* Display the published versions for a repository
* @param {Object[]} versions Repo versions
* @param {Object} reporter Reporter
* @returns {void}
*/
function displayVersions(versions, reporter) {
versions.map(version => {
if (version && version.content) {
reporter.success(
Expand All @@ -56,5 +89,4 @@ exports.handler = async function({
)
}
})
process.exit()
}
21 changes: 21 additions & 0 deletions packages/aragon-cli/src/lib/apm/getApmRepoVersions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const APM = require('@aragon/apm')

module.exports = async (web3, apmRepoName, apmOptions) => {
// 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)

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

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.')
})