diff --git a/package.json b/package.json index 8ef26feedd..6b9bf0a5a3 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "aegir": "^8.0.1", "buffer-loader": "0.0.1", "chai": "^3.5.0", + "execa": "^0.4.0", "expose-loader": "^0.7.1", "form-data": "^2.0.0", "fs-pull-blob-store": "^0.3.0", diff --git a/src/cli/commands/bitswap/stat.js b/src/cli/commands/bitswap/stat.js index 14668e0768..96727bacb4 100644 --- a/src/cli/commands/bitswap/stat.js +++ b/src/cli/commands/bitswap/stat.js @@ -23,8 +23,7 @@ module.exports = { stats.Wantlist = stats.Wantlist || [] stats.Peers = stats.Peers || [] - console.log(` -bitswap status + console.log(`bitswap status blocks received: ${stats.BlocksReceived} dup blocks received: ${stats.DupBlksReceived} dup data received: ${stats.DupDataReceived}B diff --git a/src/cli/commands/block/stat.js b/src/cli/commands/block/stat.js index 3d1b0a243c..ab7887c75b 100644 --- a/src/cli/commands/block/stat.js +++ b/src/cli/commands/block/stat.js @@ -1,7 +1,6 @@ 'use strict' const utils = require('../../utils') -const bs58 = require('bs58') const debug = require('debug') const log = debug('cli:block') log.error = debug('cli:block:error') @@ -24,7 +23,7 @@ module.exports = { throw err } - console.log('Key:', bs58.encode(stats.key).toString()) + console.log('Key:', stats.key) console.log('Size:', stats.size) }) }) diff --git a/src/cli/commands/bootstrap/add.js b/src/cli/commands/bootstrap/add.js index 0122e53fee..78750a22dd 100644 --- a/src/cli/commands/bootstrap/add.js +++ b/src/cli/commands/bootstrap/add.js @@ -17,6 +17,7 @@ module.exports = { if (err) { throw err } + ipfs.bootstrap.add(argv.peer, (err, list) => { if (err) { throw err diff --git a/src/cli/commands/files/cat.js b/src/cli/commands/files/cat.js index 7c16b9aa6e..88dd96b2ea 100644 --- a/src/cli/commands/files/cat.js +++ b/src/cli/commands/files/cat.js @@ -1,5 +1,6 @@ 'use strict' +const waterfall = require('run-waterfall') const debug = require('debug') const utils = require('../../utils') const log = debug('cli:files') @@ -14,19 +15,16 @@ module.exports = { handler (argv) { const path = argv['ipfs-path'] - utils.getIPFS((err, ipfs) => { + + waterfall([ + (cb) => utils.getIPFS(cb), + (ipfs, cb) => ipfs.files.cat(path, cb) + ], (err, file) => { if (err) { throw err } - ipfs.files.cat(path, onFile) + file.pipe(process.stdout) }) } } - -function onFile (err, file) { - if (err) { - throw (err) - } - file.pipe(process.stdout) -} diff --git a/src/cli/commands/files/get.js b/src/cli/commands/files/get.js index c5e0aa9051..dbf0a76732 100644 --- a/src/cli/commands/files/get.js +++ b/src/cli/commands/files/get.js @@ -98,12 +98,11 @@ module.exports = { if (err) { throw err } - + console.log(`Saving file(s) to ${ipfsPath}`) pull( toPull.source(stream), pull.asyncMap(fileHandler(dir)), pull.onEnd((err) => { - console.log('finished writing') if (err) { throw err } diff --git a/src/core/components/block.js b/src/core/components/block.js index 751fcf0efe..49acdddee5 100644 --- a/src/core/components/block.js +++ b/src/core/components/block.js @@ -33,7 +33,7 @@ module.exports = function block (self) { return callback(err) } callback(null, { - key: hash, + key: multihash.toB58String(hash), size: block.data.length }) }) diff --git a/src/http-api/resources/block.js b/src/http-api/resources/block.js index 7fe425a540..20cfd88106 100644 --- a/src/http-api/resources/block.js +++ b/src/http-api/resources/block.js @@ -1,6 +1,6 @@ 'use strict' -const bs58 = require('bs58') +const mh = require('multihashes') const multipart = require('ipfs-multipart') const Block = require('ipfs-block') const debug = require('debug') @@ -17,7 +17,7 @@ exports.parseKey = (request, reply) => { try { return reply({ - key: new Buffer(bs58.decode(request.query.arg)) + key: mh.fromB58String(request.query.arg) }) } catch (err) { log.error(err) @@ -93,7 +93,7 @@ exports.put = { } return reply({ - Key: bs58.encode(block.key).toString(), + Key: mh.toB58String(block.key), Size: block.data.length }) }) @@ -112,7 +112,7 @@ exports.del = { if (err) { log.error(err) return reply({ - Message: 'Failed to get block stats: ' + err, + Message: 'Failed to delete block: ' + err, Code: 0 }).code(500) } @@ -129,7 +129,7 @@ exports.stat = { // main route handler which is called after the above `parseArgs`, but only if the args were valid handler: (request, reply) => { const key = request.pre.args.key - + console.log('fetching', key) request.server.app.ipfs.block.stat(key, (err, block) => { if (err) { log.error(err) @@ -140,7 +140,7 @@ exports.stat = { } return reply({ - Key: bs58.encode(block.key).toString(), + Key: block.key, Size: block.size }) }) diff --git a/src/http-api/resources/bootstrap.js b/src/http-api/resources/bootstrap.js index 6866acf985..6492435083 100644 --- a/src/http-api/resources/bootstrap.js +++ b/src/http-api/resources/bootstrap.js @@ -1,11 +1,31 @@ 'use strict' const boom = require('boom') +const multiaddr = require('multiaddr') exports = module.exports +// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args` +exports.parseKey = (request, reply) => { + if (!request.query.arg) { + return reply("Argument 'multiaddr' is required").code(400).takeover() + } + + try { + return reply({ + addr: multiaddr(request.query.arg) + }) + } catch (err) { + return reply({ + Message: 'Not a valid multiaddr', + Code: 0 + }).code(500).takeover() + } +} + exports.list = (request, reply) => { - request.server.app.ipfs.bootstrap.list((err, list) => { + const ipfs = request.server.app.ipfs + ipfs.bootstrap.list((err, list) => { if (err) { return reply(boom.badRequest(err)) } @@ -13,16 +33,32 @@ exports.list = (request, reply) => { }) } -exports.add = (request, reply) => { -// request.server.app.ipfs.id((err, id) => { -// if (err) { return reply(boom.badRequest(err)) } -// return reply(id) -// }) +exports.add = { + parseArgs: exports.parseKey, + handler (request, reply) { + const ipfs = request.server.app.ipfs + const addr = request.pre.args.addr + + ipfs.bootstrap.add(addr.toString(), (err, list) => { + if (err) { + return reply(boom.badRequest(err)) + } + return reply() + }) + } } -exports.rm = (request, reply) => { -// request.server.app.ipfs.id((err, id) => { -// if (err) { return reply(boom.badRequest(err)) } -// return reply(id) -// }) +exports.rm = { + parseArgs: exports.parseKey, + handler (request, reply) { + const ipfs = request.server.app.ipfs + const addr = request.pre.args.addr + + ipfs.bootstrap.rm(addr.toString(), (err, list) => { + if (err) { + return reply(boom.badRequest(err)) + } + return reply() + }) + } } diff --git a/src/http-api/resources/files.js b/src/http-api/resources/files.js index ac52d19cc2..a33877f8ea 100644 --- a/src/http-api/resources/files.js +++ b/src/http-api/resources/files.js @@ -10,7 +10,7 @@ const pull = require('pull-stream') const toPull = require('stream-to-pull-stream') const pushable = require('pull-pushable') const EOL = require('os').EOL -const through = require('through2') +const toStream = require('pull-stream-to-stream') exports = module.exports @@ -55,8 +55,10 @@ exports.cat = { // - _read method // - _readableState object // are there :( - stream._read = () => {} - stream._readableState = {} + if (!stream._read) { + stream._read = () => {} + stream._readableState = {} + } return reply(stream).header('X-Stream-Output', '1') }) } @@ -72,7 +74,7 @@ exports.get = { const ipfs = request.server.app.ipfs const pack = tar.pack() - ipfs.files.get(key, (err, stream) => { + ipfs.files.getPull(key, (err, stream) => { if (err) { log.error(err) @@ -83,33 +85,37 @@ exports.get = { return } - stream.pipe(through.obj((file, enc, cb) => { - const header = {name: file.path} - - if (!file.content) { - header.type = 'directory' - pack.entry(header) - cb() - } else { - header.size = file.size - const packStream = pack.entry(header, cb) - if (!packStream) { - // this happens if the request is aborted - // we just skip things then - return cb() + pull( + stream, + pull.asyncMap((file, cb) => { + const header = {name: file.path} + if (!file.content) { + header.type = 'directory' + pack.entry(header) + cb() + } else { + header.size = file.size + const packStream = pack.entry(header, cb) + if (!packStream) { + // this happens if the request is aborted + // we just skip things then + log('other side hung up') + return cb() + } + toStream.source(file.content).pipe(packStream) + } + }), + pull.onEnd((err) => { + if (err) { + log.error(err) + pack.emit('error', err) + pack.destroy() + return } - file.content.pipe(packStream) - } - }), () => { - if (err) { - log.error(err) - pack.emit('error', err) - pack.destroy() - return - } - pack.finalize() - }) + pack.finalize() + }) + ) // the reply must read the tar stream, // to pull values through diff --git a/src/http-api/routes/bootstrap.js b/src/http-api/routes/bootstrap.js index cb7426fae6..d4ac0d37d4 100644 --- a/src/http-api/routes/bootstrap.js +++ b/src/http-api/routes/bootstrap.js @@ -1,7 +1,6 @@ 'use strict' const resources = require('./../resources') -const Joi = require('joi') module.exports = (server) => { const api = server.select('API') @@ -17,14 +16,11 @@ module.exports = (server) => { api.route({ method: '*', path: '/api/v0/bootstrap/add', - handler: resources.bootstrap.add, config: { - validate: { - query: { - arg: Joi.string().required(), // multiaddr to add - default: Joi.boolean() - } - } + pre: [ + { method: resources.bootstrap.add.parseArgs, assign: 'args' } + ], + handler: resources.bootstrap.add.handler } }) @@ -39,14 +35,11 @@ module.exports = (server) => { api.route({ method: '*', path: '/api/v0/bootstrap/rm', - handler: resources.bootstrap.rm, config: { - validate: { - query: { - arg: Joi.string().required(), // multiaddr to rm - all: Joi.boolean() - } - } + pre: [ + { method: resources.bootstrap.rm.parseArgs, assign: 'args' } + ], + handler: resources.bootstrap.rm.handler } }) } diff --git a/test/cli/test-bitswap.js b/test/cli/test-bitswap.js index a73d7d1466..425640b22c 100644 --- a/test/cli/test-bitswap.js +++ b/test/cli/test-bitswap.js @@ -2,34 +2,32 @@ 'use strict' const expect = require('chai').expect -const nexpect = require('nexpect') const Block = require('ipfs-block') -const _ = require('lodash') const bs58 = require('bs58') const HttpAPI = require('../../src/http-api') const createTempNode = require('../utils/temp-node') const repoPath = require('./index').repoPath +const ipfs = require('../utils/ipfs')(repoPath) -describe('bitswap', function () { - this.timeout(40000) - const env = _.clone(process.env) - env.IPFS_PATH = repoPath - - let ipfs +describe('bitswap', () => { + let node before((done) => { - createTempNode(38, (err, _ipfs) => { + createTempNode(38, (err, _node) => { expect(err).to.not.exist - ipfs = _ipfs - ipfs.goOnline(done) + node = _node + node.goOnline(done) }) }) + after((done) => { + node.goOffline(done) + }) + describe('api running', () => { const block = new Block('hello') const key = bs58.encode(block.key) - let httpAPI before((done) => { @@ -41,42 +39,32 @@ describe('bitswap', function () { httpAPI.stop(done) }) - it('wantlist', (done) => { + it('wantlist', () => { const api = httpAPI.server.select('API') api.inject({ method: 'GET', url: `/api/v0/block/get?arg=${key}` - }, (res) => {}) + }, () => {}) - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'bitswap', 'wantlist'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout).to.be.eql([ - key - ]) - done() - }) + return ipfs('bitswap wantlist').then((out) => { + expect(out).to.be.eql(key) + }) }) - it('stat', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'bitswap', 'stat'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout).to.be.eql([ - 'bitswap status', - ' blocks received: 0', - ' dup blocks received: 0', - ' dup data received: 0B', - ' wantlist [1 keys]', - ` ${key}`, - ' partners [0]', - ' ' - ]) - done() - }) + it('stat', () => { + return ipfs('bitswap stat').then((out) => { + expect(out).to.be.eql([ + 'bitswap status', + ' blocks received: 0', + ' dup blocks received: 0', + ' dup data received: 0B', + ' wantlist [1 keys]', + ` ${key}`, + ' partners [0]', + ' ' + ].join('\n')) + }) }) }) }) diff --git a/test/cli/test-block.js b/test/cli/test-block.js index cbd5f5a51f..c5a03eb4a1 100644 --- a/test/cli/test-block.js +++ b/test/cli/test-block.js @@ -2,135 +2,41 @@ 'use strict' const expect = require('chai').expect -const nexpect = require('nexpect') -const HttpAPI = require('../../src/http-api') -const path = require('path') const repoPath = require('./index').repoPath -const _ = require('lodash') - -const spawn = (args) => { - const env = _.clone(process.env) - env.IPFS_PATH = repoPath - return nexpect.spawn( - 'node', - [path.join(__dirname, '../../src/cli/bin.js')].concat(args), - {env} - ) -} +const describeOnlineAndOffline = require('../utils/on-and-off') +const ipfs = require('../utils/ipfs')(repoPath) describe('block', () => { - describe('api offline', () => { - it('put', (done) => { - spawn(['block', 'put', process.cwd() + '/test/test-data/hello']) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(stdout[0]) - .to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') - expect(exitcode).to.equal(0) - done() - }) - }) - - it('get', (done) => { - spawn(['block', 'get', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp']) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(stdout[0]) - .to.equal('hello world') - expect(exitcode).to.equal(0) - done() - }) - }) - - it('stat', (done) => { - spawn(['block', 'stat', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp']) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(stdout[0]) - .to.equal('Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') - expect(stdout[1]) - .to.equal('Size: 12') - expect(exitcode).to.equal(0) - - done() - }) - }) - - it.skip('rm', (done) => { - spawn(['block', 'rm', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp']) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(stdout[0]) - .to.equal('removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') - expect(exitcode).to.equal(0) - done() - }) - }) - }) - - describe('api running', () => { - let httpAPI - before((done) => { - httpAPI = new HttpAPI(repoPath) - httpAPI.start((err) => { - expect(err).to.not.exist - done() + describeOnlineAndOffline(repoPath, () => { + it('put', () => { + return ipfs('block put test/test-data/hello').then((out) => { + expect(out).to.be.eql( + 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp' + ) }) }) - after((done) => { - httpAPI.stop((err) => { - expect(err).to.not.exist - done() + it('get', () => { + return ipfs('block get QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => { + expect(out).to.be.eql('hello world\n') }) }) - it('put', (done) => { - spawn(['block', 'put', process.cwd() + '/test/test-data/hello']) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - console.log - expect(stdout[0]) - .to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') - expect(exitcode).to.equal(0) - done() - }) - }) - - it('get', (done) => { - spawn(['block', 'get', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp']) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(stdout[0]) - .to.equal('hello world') - expect(exitcode).to.equal(0) - done() - }) - }) - - // TODO: Investigate why it doesn't work as expected - it.skip('stat', (done) => { - spawn(['block', 'stat', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp']) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(stdout[0]) - .to.equal('Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') - expect(stdout[1]) - .to.equal('Size: 12') - expect(exitcode).to.equal(0) - done() - }) + it('stat', () => { + return ipfs('block stat QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => { + expect(out).to.be.eql([ + 'Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp', + 'Size: 12' + ].join('\n')) + }) }) - it.skip('rm', (done) => { - spawn(['block', 'rm', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp']) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(stdout[0]) - .to.equal('removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') - expect(exitcode).to.equal(0) - done() - }) + it.skip('rm', () => { + return ipfs('block rm QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => { + expect(out).to.be.eql( + 'removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp' + ) + }) }) }) }) diff --git a/test/cli/test-bootstrap.js b/test/cli/test-bootstrap.js index c58cb4c78a..766c3f6887 100644 --- a/test/cli/test-bootstrap.js +++ b/test/cli/test-bootstrap.js @@ -3,14 +3,12 @@ 'use strict' const expect = require('chai').expect -const nexpect = require('nexpect') -const _ = require('lodash') +const repoPath = require('./index').repoPath +const ipfs = require('../utils/ipfs')(repoPath) +const describeOnlineAndOffline = require('../utils/on-and-off') describe('bootstrap', () => { - const env = _.clone(process.env) - env.IPFS_PATH = require('./index').repoPath - - describe('api offline', () => { + describeOnlineAndOffline(repoPath, () => { const defaultList = [ '/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', @@ -33,51 +31,29 @@ describe('bootstrap', () => { '/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', '/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', '/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx', - '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT' + '/ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD' ] - it('list the bootstrap nodes', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'bootstrap', 'list'], {env}) - .run((err, stdout, exitcode) => { - expect(stdout).to.deep.equal(defaultList) - expect(err).to.not.exist - expect(exitcode).to.equal(0) - done() - }) + it('list the bootstrap nodes', () => { + return ipfs('bootstrap list').then((out) => { + expect(out).to.be.eql(defaultList.join('\n')) + }) }) - it('add another bootstrap node', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'bootstrap', 'add', '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'bootstrap', 'list'], {env}) - .run((err, stdout, exitcode) => { - expect(stdout).to.deep.equal(updatedList) - expect(err).to.not.exist - expect(exitcode).to.equal(0) - done() - }) - }) + it('add another bootstrap node', () => { + return ipfs('bootstrap add /ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD').then((out) => { + return ipfs('bootstrap list') + }).then((out) => { + expect(out).to.be.eql(updatedList.join('\n')) + }) }) - it('rm a bootstrap node', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'bootstrap', 'rm', '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'bootstrap', 'list'], {env}) - .run((err, stdout, exitcode) => { - expect(stdout).to.deep.equal(defaultList) - expect(err).to.not.exist - expect(exitcode).to.equal(0) - done() - }) - }) + it('rm a bootstrap node', () => { + return ipfs('bootstrap rm /ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD').then((out) => { + return ipfs('bootstrap list') + }).then((out) => { + expect(out).to.deep.equal(defaultList.join('\n')) + }) }) }) - - describe('api running', () => {}) }) diff --git a/test/cli/test-commands.js b/test/cli/test-commands.js index 3e24ad2ffb..539c055363 100644 --- a/test/cli/test-commands.js +++ b/test/cli/test-commands.js @@ -2,25 +2,25 @@ 'use strict' const expect = require('chai').expect -const nexpect = require('nexpect') +const repoPath = require('./index').repoPath +const ipfsBase = require('../utils/ipfs') +const ipfs = ipfsBase(repoPath) +const describeOnlineAndOffline = require('../utils/on-and-off') describe('commands', () => { - it('list the commands', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'commands']) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout.length).to.equal(56) - done() + describeOnlineAndOffline(repoPath, () => { + it('list the commands', () => { + return ipfs('commands').then((out) => { + expect(out.split('\n')).to.have.length(56) }) + }) }) - it('list the commands even if not in the same dir', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'commands'], {cwd: '/tmp'}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout.length).to.equal(56) - done() - }) + + it('list the commands even if not in the same dir', () => { + return ipfsBase(repoPath, { + cwd: '/tmp' + })('commands').then((out) => { + expect(out.split('\n').length).to.equal(56) + }) }) }) diff --git a/test/cli/test-config.js b/test/cli/test-config.js index 0490946d29..1027995335 100644 --- a/test/cli/test-config.js +++ b/test/cli/test-config.js @@ -2,12 +2,11 @@ 'use strict' const expect = require('chai').expect -const nexpect = require('nexpect') const fs = require('fs') -const HttpAPI = require('../../src/http-api') -const repoPath = require('./index').repoPath const path = require('path') -const _ = require('lodash') +const repoPath = require('./index').repoPath +const ipfs = require('../utils/ipfs')(repoPath) +const describeOnlineAndOffline = require('../utils/on-and-off') describe('config', () => { const configPath = path.join(repoPath, 'config') @@ -15,204 +14,65 @@ describe('config', () => { const updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8')) const restoreConfig = () => fs.writeFileSync(configPath, fs.readFileSync(originalConfigPath, 'utf8'), 'utf8') - const env = _.clone(process.env) - env.IPFS_PATH = repoPath - - describe('api offline', () => { + describeOnlineAndOffline(repoPath, () => { describe('get/set', () => { - it('get a config key value', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'Identity.PeerID'], {env}) - .run((err, stdout, exitcode) => { - const expected = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A' - expect(stdout[0]).to.equal(expected) - expect(err).to.not.exist - expect(exitcode).to.equal(0) - done() - }) + it('get a config key value', () => { + return ipfs('config Identity.PeerID').then((out) => { + expect(out).to.be.eql( + 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A' + ) + }) }) - it('set a config key with a string value', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', 'bar'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(updatedConfig().foo).to.equal('bar') - expect(exitcode).to.equal(0) - done() - }) + it('set a config key with a string value', () => { + return ipfs('config foo bar').then((out) => { + expect(updatedConfig().foo).to.equal('bar') + }) }) - it('set a config key with true', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', true, '--bool'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(updatedConfig().foo).to.equal(true) - done() - }) + it('set a config key with true', () => { + return ipfs('config foo true --bool').then((out) => { + expect(updatedConfig().foo).to.equal(true) + }) }) - it('set a config key with false', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', false, '--bool'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(updatedConfig().foo).to.equal(false) - done() - }) + it('set a config key with false', () => { + return ipfs('config foo false --bool').then((out) => { + expect(updatedConfig().foo).to.equal(false) + }) }) - it('set a config key with json', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar": 0}', '--json'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(updatedConfig().foo).to.deep.equal({ bar: 0 }) - done() - }) + it('set a config key with json', () => { + return ipfs('config foo {"bar":0} --json').then((out) => { + expect(updatedConfig().foo).to.deep.equal({ bar: 0 }) + }) }) - it('set a config key with invalid json', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(1) - done() - }) + it('set a config key with invalid json', () => { + return ipfs.fail('config foo {"bar:0} --json') }) - it('call config with no arguments', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(1) - done() - }) + it('call config with no arguments', () => { + return ipfs.fail('config') }) }) - describe('replace', () => { - it('replace config with file', (done) => { - const filePath = 'test/test-data/otherconfig' - const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')) - - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'replace', filePath], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - - expect(updatedConfig()).to.deep.equal(expectedConfig) - done() - }) - }) - - after(() => { - restoreConfig() - }) - }) - }) - - describe('api running', () => { - let httpAPI - - before((done) => { - httpAPI = new HttpAPI(repoPath) - httpAPI.start((err) => { - expect(err).to.not.exist - done() - }) - }) - - after((done) => { - httpAPI.stop((err) => { - expect(err).to.not.exist - done() - }) - }) - - describe('get/set', () => { - it('get a config key value', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'Identity.PeerID'], {env}) - .run((err, stdout, exitcode) => { - const expected = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A' - expect(stdout[0]).to.equal(expected) - expect(err).to.not.exist - expect(exitcode).to.equal(0) - done() - }) - }) - - it('set a config key with a string value', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', 'bar'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(updatedConfig().foo).to.equal('bar') - done() - }) - }) - - it('set a config key with true', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', true, '--bool'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(updatedConfig().foo).to.equal(true) - done() - }) - }) - - it('set a config key with false', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', false, '--bool'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(updatedConfig().foo).to.equal(false) - done() - }) - }) - - it('set a config key with json', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar": 0}', '--json'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(updatedConfig().foo).to.deep.equal({ bar: 0 }) - done() - }) - }) - - it('set a config key with invalid json', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(1) - done() - }) - }) - - it('call config with no arguments', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(1) - done() - }) + describe('show', () => { + it('returns the full config', () => { + return ipfs('config show').then((out) => { + expect(JSON.parse(out)).to.be.eql(updatedConfig()) + }) }) }) describe('replace', () => { - it('replace config with file', (done) => { + it('replace config with file', () => { const filePath = 'test/test-data/otherconfig' const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')) - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'replace', filePath], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - - expect(updatedConfig()).to.deep.equal(expectedConfig) - done() - }) + return ipfs(`config replace ${filePath}`).then((out) => { + expect(updatedConfig()).to.be.eql(expectedConfig) + }) }) after(() => { diff --git a/test/cli/test-files.js b/test/cli/test-files.js index c46724efd0..b9edc56354 100644 --- a/test/cli/test-files.js +++ b/test/cli/test-files.js @@ -2,121 +2,61 @@ 'use strict' const expect = require('chai').expect -const nexpect = require('nexpect') -const HttpAPI = require('../../src/http-api') const repoPath = require('./index').repoPath -const _ = require('lodash') +const fs = require('fs') +const path = require('path') +const describeOnlineAndOffline = require('../utils/on-and-off') +const ipfs = require('../utils/ipfs')(repoPath) describe('files', () => { - const env = _.clone(process.env) - env.IPFS_PATH = repoPath - - describe('api offline', () => { - it('cat', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'cat', 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]).to.equal('hello world') - done() - }) - }) - - it('add', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', 'src/init-files/init-docs/readme'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]).to.equal('added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB readme') - done() - }) + describeOnlineAndOffline(repoPath, () => { + it('cat', () => { + return ipfs('files cat QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o').then((out) => { + expect(out).to.be.eql('hello world') + }) }) - it('add recursively', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', '-r', 'src/init-files/init-docs'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - - const expected = [ - 'added QmYE7xo6NxbHEVEHej1yzxijYaNY51BaeKxjXxn6Ssa6Bs init-docs/tour/0.0-intro', - 'added QmciSU8hfpAXKjvK5YLUSwApomGSWN5gFbP4EpDAEzu2Te init-docs/tour', - 'added QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ init-docs/security-notes', - 'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB init-docs/readme', - 'added QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha init-docs/quick-start', - 'added QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 init-docs/help', - 'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index', - 'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs', - 'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact', - 'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about', - 'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs' - ] - expect(stdout).to.deep.equal(expected) - done() - }) - }) - }) + it('get', () => { + return ipfs('files get QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o').then((out) => { + expect(out).to.be.eql( + 'Saving file(s) to QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o' + ) - describe('api running', () => { - let httpAPI + const file = path.join(process.cwd(), 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o') + expect( + fs.readFileSync(file).toString() + ).to.be.eql( + 'hello world\n' + ) - before((done) => { - httpAPI = new HttpAPI(repoPath) - httpAPI.start((err) => { - expect(err).to.not.exist - done() + fs.unlinkSync(file) }) }) - after((done) => { - httpAPI.stop((err) => { - expect(err).to.not.exist - done() + it('add', () => { + return ipfs('files add src/init-files/init-docs/readme').then((out) => { + expect(out).to.be.eql( + 'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB readme' + ) }) }) - it('cat', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'cat', 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]).to.equal('hello world') - done() - }) - }) - - it('add', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', 'src/init-files/init-docs/readme'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]).to.equal('added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB readme') - done() - }) - }) - - it('add recursively', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', '-r', 'src/init-files/init-docs'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - - const expected = [ - 'added QmYE7xo6NxbHEVEHej1yzxijYaNY51BaeKxjXxn6Ssa6Bs init-docs/tour/0.0-intro', - 'added QmciSU8hfpAXKjvK5YLUSwApomGSWN5gFbP4EpDAEzu2Te init-docs/tour', - 'added QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ init-docs/security-notes', - 'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB init-docs/readme', - 'added QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha init-docs/quick-start', - 'added QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 init-docs/help', - 'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index', - 'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs', - 'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact', - 'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about', - 'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs' - ] - expect(stdout).to.deep.equal(expected) - done() - }) + it('add recursively', () => { + return ipfs('files add -r src/init-files/init-docs').then((out) => { + expect(out).to.be.eql([ + 'added QmYE7xo6NxbHEVEHej1yzxijYaNY51BaeKxjXxn6Ssa6Bs init-docs/tour/0.0-intro', + 'added QmciSU8hfpAXKjvK5YLUSwApomGSWN5gFbP4EpDAEzu2Te init-docs/tour', + 'added QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ init-docs/security-notes', + 'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB init-docs/readme', + 'added QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha init-docs/quick-start', + 'added QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 init-docs/help', + 'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index', + 'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs', + 'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact', + 'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about', + 'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs' + ].join('\n')) + }) }) }) }) diff --git a/test/cli/test-id.js b/test/cli/test-id.js index 84e57a0290..cfef37e5e4 100644 --- a/test/cli/test-id.js +++ b/test/cli/test-id.js @@ -2,62 +2,19 @@ 'use strict' const expect = require('chai').expect -const nexpect = require('nexpect') -const HttpAPI = require('../../src/http-api') const repoPath = require('./index').repoPath -const _ = require('lodash') +const describeOnlineAndOffline = require('../utils/on-and-off') +const ipfs = require('../utils/ipfs')(repoPath) describe('id', () => { - const env = _.clone(process.env) - env.IPFS_PATH = repoPath - - describe('api offline', () => { - it('get the id', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'id'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - - const id = JSON.parse(stdout.join('')) - expect(id).to.have.property('id') - expect(id).to.have.property('publicKey') - expect(id).to.have.property('addresses') - done() - }) - }) - }) - - describe('api running', () => { - let httpAPI - - before((done) => { - httpAPI = new HttpAPI(repoPath) - httpAPI.start((err) => { - expect(err).to.not.exist - done() + describeOnlineAndOffline(repoPath, () => { + it('get the id', () => { + return ipfs('id').then((res) => { + const id = JSON.parse(res) + expect(id).to.have.property('id') + expect(id).to.have.property('publicKey') + expect(id).to.have.property('addresses') }) }) - - after((done) => { - httpAPI.stop((err) => { - expect(err).to.not.exist - done() - }) - }) - - it('get the id', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'id'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - - const id = JSON.parse(stdout.join('')) - expect(id).to.have.property('id') - expect(id).to.have.property('publicKey') - expect(id).to.have.property('addresses') - - done() - }) - }) }) }) diff --git a/test/cli/test-init.js b/test/cli/test-init.js index 4eeb9e2e3a..99268e7a27 100644 --- a/test/cli/test-init.js +++ b/test/cli/test-init.js @@ -2,54 +2,50 @@ 'use strict' const expect = require('chai').expect -const nexpect = require('nexpect') const path = require('path') const fs = require('fs') -const _ = require('lodash') const clean = require('../utils/clean') +const ipfsCmd = require('../utils/ipfs') describe('init', function () { this.timeout(60 * 1000) - const env = _.clone(process.env) + let repoPath + let ipfs + const repoExistsSync = (p) => ( - fs.existsSync(path.join(env.IPFS_PATH, p)) + fs.existsSync(path.join(repoPath, p)) ) beforeEach(() => { - env.IPFS_PATH = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + ipfs = ipfsCmd(repoPath) }) afterEach(() => { - clean(env.IPFS_PATH) + clean(repoPath) }) - it('basic', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(repoExistsSync('blocks')).to.equal(true) - expect(repoExistsSync('config')).to.equal(true) - expect(repoExistsSync('version')).to.equal(true) - done() - }) + it('basic', () => { + return ipfs('init').then(() => { + expect(repoExistsSync('blocks')).to.equal(true) + expect(repoExistsSync('config')).to.equal(true) + expect(repoExistsSync('version')).to.equal(true) + }) }) - it('bits', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - done() - }) + it('bits', () => { + return ipfs('init --bits 512').then(() => { + expect(repoExistsSync('blocks')).to.equal(true) + expect(repoExistsSync('config')).to.equal(true) + expect(repoExistsSync('version')).to.equal(true) + }) }) - it('empty', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64', '--empty-repo', 'true'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(repoExistsSync('blocks')).to.equal(false) - expect(repoExistsSync('config')).to.equal(true) - expect(repoExistsSync('version')).to.equal(true) - done() - }) + it('empty', () => { + return ipfs('init --bits 512 --empty-repo true').then(() => { + expect(repoExistsSync('blocks')).to.equal(false) + expect(repoExistsSync('config')).to.equal(true) + expect(repoExistsSync('version')).to.equal(true) + }) }) }) diff --git a/test/cli/test-object.js b/test/cli/test-object.js index fd4972fc87..6506abb8ee 100644 --- a/test/cli/test-object.js +++ b/test/cli/test-object.js @@ -3,273 +3,93 @@ 'use strict' const expect = require('chai').expect -const nexpect = require('nexpect') -const HttpAPI = require('../../src/http-api') const repoPath = require('./index').repoPath -const _ = require('lodash') +const describeOnlineAndOffline = require('../utils/on-and-off') +const ipfs = require('../utils/ipfs')(repoPath) describe('object', () => { - const env = _.clone(process.env) - env.IPFS_PATH = repoPath - - describe('api offline', () => { - it('new', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'new'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - done() - }) - }) - - it('get', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'get', 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - - const result = JSON.parse(stdout[0]) - expect(result.Links) - .to.deep.equal([]) - expect(result.Data) - .to.equal('') - done() - }) - }) - - it('put', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'put', process.cwd() + '/test/test-data/node.json'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('added QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm') - done() - }) - }) - - it('stat', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'stat', 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('NumLinks: 1') - expect(stdout[1]) - .to.equal('BlockSize: 60') - expect(stdout[2]) - .to.equal('LinksSize: 53') - expect(stdout[3]) - .to.equal('DataSize: 7') - done() - }) - }) - - it('data', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'data', 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]).to.equal('another') - done() - }) - }) - - it('links', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'links', 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]).to.equal('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V 8 some link') - done() - }) - }) - - describe('patch', () => { - it('append-data', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'patch', 'append-data', 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n', process.cwd() + '/test/test-data/badconfig'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6') - done() - }) - }) - - it('set-data', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'patch', 'set-data', 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', process.cwd() + '/test/test-data/badconfig'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6') - done() - }) - }) - - it('add-link', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'patch', 'add-link', 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n', 'foo', 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK') - done() - }) - }) - - it('rm-link', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'patch', 'rm-link', 'QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK', 'foo'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - done() - }) + describeOnlineAndOffline(repoPath, () => { + it('new', () => { + return ipfs('object new').then((out) => { + expect(out).to.be.eql( + 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' + ) }) }) - }) - // Waiting for js-ipfs-api to be updated - describe('api running', () => { - let httpAPI - - before((done) => { - httpAPI = new HttpAPI(repoPath) - httpAPI.start((err) => { - expect(err).to.not.exist - done() + it('get', () => { + return ipfs('object get QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n').then((out) => { + const result = JSON.parse(out) + expect(result.Links).to.be.eql([]) + expect(result.Data).to.be.eql('') }) }) - after((done) => { - httpAPI.stop((err) => { - expect(err).to.not.exist - done() + it('put', () => { + return ipfs('object put test/test-data/node.json').then((out) => { + expect(out).to.be.eql( + 'added QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm' + ) }) }) - it('new', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'new'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - done() - }) - }) - - it('get', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'get', 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - const result = JSON.parse(stdout[0]) - expect(result.Links) - .to.deep.equal([]) - expect(result.Data) - .to.equal('') - done() - }) - }) - - it('put', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'put', process.cwd() + '/test/test-data/node.json'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - - expect(stdout[0]) - .to.equal('added QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm') - done() - }) - }) - - it('stat', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'stat', 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('NumLinks: 1') - expect(stdout[1]) - .to.equal('BlockSize: 60') - expect(stdout[2]) - .to.equal('LinksSize: 53') - expect(stdout[3]) - .to.equal('DataSize: 7') - done() - }) + it('stat', () => { + return ipfs('object stat QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm').then((out) => { + expect(out).to.be.eql([ + 'NumLinks: 1', + 'BlockSize: 60', + 'LinksSize: 53', + 'DataSize: 7', + 'CumulativeSize: 68' + ].join('\n')) + }) }) - it('data', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'data', 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]).to.equal('another') - done() - }) + it('data', () => { + return ipfs('object data QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm').then((out) => { + expect(out).to.be.eql('another') + }) }) - it('links', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'links', 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]).to.equal('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V 8 some link') - done() - }) + it('links', () => { + return ipfs('object links QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm').then((out) => { + expect(out).to.be.eql( + 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V 8 some link' + ) + }) }) describe('patch', () => { - it('append-data', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'patch', 'append-data', 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n', process.cwd() + '/test/test-data/badconfig'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6') - done() - }) + it('append-data', () => { + return ipfs('object patch append-data QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n test/test-data/badconfig').then((out) => { + expect(out).to.be.eql( + 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6' + ) + }) }) - it('set-data', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'patch', 'set-data', 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', process.cwd() + '/test/test-data/badconfig'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6') - done() - }) + it('set-data', () => { + return ipfs('object patch set-data QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6 test/test-data/badconfig').then((out) => { + expect(out).to.be.eql( + 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6' + ) + }) }) - it('add-link', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'patch', 'add-link', 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n', 'foo', 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK') - done() - }) + it('add-link', () => { + return ipfs('object patch add-link QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n foo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn').then((out) => { + expect(out).to.be.eql( + 'QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK' + ) + }) }) - it('rm-link', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'object', 'patch', 'rm-link', 'QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK', 'foo'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]) - .to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - done() - }) + it('rm-link', () => { + return ipfs('object patch rm-link QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK foo').then((out) => { + expect(out).to.be.eql( + 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' + ) + }) }) }) }) diff --git a/test/cli/test-swarm.js b/test/cli/test-swarm.js index edeaa3ce9b..3426f1e982 100644 --- a/test/cli/test-swarm.js +++ b/test/cli/test-swarm.js @@ -1,39 +1,37 @@ /* eslint max-nested-callbacks: ["error", 8] */ /* eslint-env mocha */ -/* eslint max-nested-callbacks: ["error", 8] */ 'use strict' const expect = require('chai').expect -const nexpect = require('nexpect') const HttpAPI = require('../../src/http-api') const createTempNode = require('../utils/temp-node') const repoPath = require('./index').repoPath -const _ = require('lodash') +const ipfs = require('../utils/ipfs')(repoPath) describe('swarm', function () { this.timeout(30 * 1000) - const env = _.clone(process.env) - env.IPFS_PATH = repoPath - - var ipfs - var ipfsAddr + let node + let nodeAddr before((done) => { - createTempNode(1, (err, _ipfs) => { + createTempNode(1, (err, _node) => { expect(err).to.not.exist - ipfs = _ipfs - ipfs.goOnline((err) => { + node = _node + node.goOnline((err) => { expect(err).to.not.exist - ipfs.id((err, identity) => { + node.id((err, identity) => { expect(err).to.not.exist - ipfsAddr = identity.addresses[0] - console.log('addr', ipfsAddr) + nodeAddr = identity.addresses[0] done() }) }) }) }) + after((done) => { + node.goOffline(done) + }) + describe('api running', () => { let httpAPI @@ -52,48 +50,30 @@ describe('swarm', function () { }) }) - it('connect', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'swarm', 'connect', ipfsAddr], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout).to.be.eql([ - `connect ${ipfsAddr} success` - ]) - done() - }) + it('connect', () => { + return ipfs('swarm', 'connect', nodeAddr).then((out) => { + expect(out).to.be.eql( + `connect ${nodeAddr} success` + ) + }) }) - it('peers', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'swarm', 'peers'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout).to.be.eql([ - ipfsAddr - ]) - done() - }) + it('peers', () => { + return ipfs('swarm peers').then((out) => { + expect(out).to.be.eql(nodeAddr) + }) }) - it('addrs', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'swarm', 'addrs'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout).to.have.length.above(0) - done() - }) + it('addrs', () => { + return ipfs('swarm addrs').then((out) => { + expect(out).to.have.length.above(0) + }) }) - it('addrs local', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'swarm', 'addrs', 'local'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout).to.have.length.above(0) - done() - }) + it('addrs local', () => { + return ipfs('swarm addrs local').then((out) => { + expect(out).to.have.length.above(0) + }) }) }) }) diff --git a/test/cli/test-version.js b/test/cli/test-version.js index 2378bd2df5..95d104fb0d 100644 --- a/test/cli/test-version.js +++ b/test/cli/test-version.js @@ -2,54 +2,19 @@ 'use strict' const expect = require('chai').expect -const nexpect = require('nexpect') -const HttpAPI = require('../../src/http-api') -const repoPath = require('./index').repoPath -const _ = require('lodash') const pkgversion = require('../../package.json').version +const repoPath = require('./index').repoPath +const describeOnlineAndOffline = require('../utils/on-and-off') +const ipfs = require('../utils/ipfs')(repoPath) describe('version', () => { - const env = _.clone(process.env) - env.IPFS_PATH = repoPath - - describe('api offline', () => { - it('get the version', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'version'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(stdout[0]).to.equal('js-ipfs version: ' + pkgversion) - expect(exitcode).to.equal(0) - done() - }) - }) - }) - - describe('api running', () => { - let httpAPI - - before((done) => { - httpAPI = new HttpAPI(repoPath) - httpAPI.start((err) => { - expect(err).to.not.exist - done() - }) - }) - - after((done) => { - httpAPI.stop((err) => { - expect(err).to.not.exist - done() + describeOnlineAndOffline(repoPath, () => { + it('get the version', () => { + return ipfs('version').then((out) => { + expect(out).to.be.eql( + `js-ipfs version: ${pkgversion}` + ) }) }) - - it('get the version', (done) => { - nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'version'], {env}) - .run((err, stdout, exitcode) => { - expect(err).to.not.exist - expect(exitcode).to.equal(0) - expect(stdout[0]).to.equal('js-ipfs version: ' + pkgversion) - done() - }) - }) }) }) diff --git a/test/utils/ipfs.js b/test/utils/ipfs.js new file mode 100644 index 0000000000..2c8c94d718 --- /dev/null +++ b/test/utils/ipfs.js @@ -0,0 +1,44 @@ +'use strict' + +const execa = require('execa') +const expect = require('chai').expect +const _ = require('lodash') + +module.exports = (repoPath, opts) => { + const env = _.clone(process.env) + env.IPFS_PATH = repoPath + + const config = Object.assign({}, { + stipEof: true, + env: env, + timeout: 60 * 1000 + }, opts) + + const exec = (args) => execa(`${process.cwd()}/src/cli/bin.js`, args, config) + + function ipfs () { + let args = Array.from(arguments) + if (args.length === 1) { + args = args[0].split(' ') + } + + return exec(args).then((res) => { + expect(res.stderr).to.be.eql('') + + return res.stdout + }) + } + + ipfs.fail = function ipfsFail () { + let args = Array.from(arguments) + if (args.length === 1) { + args = args[0].split(' ') + } + + return exec(args).catch((err) => { + expect(err).to.exist + }) + } + + return ipfs +} diff --git a/test/utils/on-and-off.js b/test/utils/on-and-off.js new file mode 100644 index 0000000000..531b109e44 --- /dev/null +++ b/test/utils/on-and-off.js @@ -0,0 +1,25 @@ +/* eslint-env mocha */ +'use strict' + +const HttpAPI = require('../../src/http-api') + +module.exports = function onlineAndOffline (repoPath, tests) { + describe('api offline', () => { + tests() + }) + + describe('api running', () => { + let httpAPI + + before((done) => { + httpAPI = new HttpAPI(repoPath) + httpAPI.start(done) + }) + + after((done) => { + httpAPI.stop(done) + }) + + tests() + }) +}