From 7015586c96f36ef1873395740765ddc25b954616 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 16 Sep 2016 18:30:54 +0200 Subject: [PATCH 1/4] fix(files.get): fix the command --- src/cli/commands/files/get.js | 52 +++++++++++++++++++++--------- src/http-api/resources/files.js | 56 ++++++++++++++++----------------- 2 files changed, 64 insertions(+), 44 deletions(-) diff --git a/src/cli/commands/files/get.js b/src/cli/commands/files/get.js index b8d746c03f..c5e0aa9051 100644 --- a/src/cli/commands/files/get.js +++ b/src/cli/commands/files/get.js @@ -7,6 +7,8 @@ log.error = debug('cli:files:error') var fs = require('fs') const path = require('path') const pathExists = require('path-exists') +const pull = require('pull-stream') +const toPull = require('stream-to-pull-stream') function checkArgs (hash, outPath) { // format the output directory @@ -33,30 +35,38 @@ function ensureDir (dir, cb) { .catch(cb) } -function fileHandler (result, dir) { - return function onFile (file) { +function fileHandler (dir) { + return function onFile (file, cb) { + const lastSlash = file.path.lastIndexOf('/') // Check to see if the result is in a directory - if (file.path.lastIndexOf('/') === -1) { + if (lastSlash === -1) { const dirPath = path.join(dir, file.path) // Check to see if the result is a directory - if (file.dir === false) { + if (file.content) { file.content.pipe(fs.createWriteStream(dirPath)) + .once('error', cb) + .once('end', cb) } else { - ensureDir(dirPath, (err) => { - if (err) { - throw err - } - }) + ensureDir(dirPath, cb) } } else { - const filePath = file.path.substring(0, file.path.lastIndexOf('/') + 1) + const filePath = file.path.substring(0, lastSlash + 1) const dirPath = path.join(dir, filePath) + ensureDir(dirPath, (err) => { if (err) { - throw err + return cb(err) } - file.content.pipe(fs.createWriteStream(dirPath)) + if (file.content) { + const filename = file.path.substring(lastSlash) + const target = path.join(dirPath, filename) + + file.content.pipe(fs.createWriteStream(target)) + .once('error', cb) + .once('end', cb) + } + cb() }) } } @@ -76,17 +86,29 @@ module.exports = { }, handler (argv) { - const dir = checkArgs(argv.ipfsPath, argv.output) + const ipfsPath = argv['ipfs-path'] + const dir = checkArgs(ipfsPath, argv.output) utils.getIPFS((err, ipfs) => { if (err) { throw err } - ipfs.files.get(argv.ipfsPath, (err, result) => { + + ipfs.files.get(ipfsPath, (err, stream) => { if (err) { throw err } - result.on('data', fileHandler(result, dir)) + + pull( + toPull.source(stream), + pull.asyncMap(fileHandler(dir)), + pull.onEnd((err) => { + console.log('finished writing') + if (err) { + throw err + } + }) + ) }) }) } diff --git a/src/http-api/resources/files.js b/src/http-api/resources/files.js index 3c3cbd1e5e..ac52d19cc2 100644 --- a/src/http-api/resources/files.js +++ b/src/http-api/resources/files.js @@ -7,10 +7,10 @@ const tar = require('tar-stream') const log = debug('http-api:files') log.error = debug('http-api:files:error') const pull = require('pull-stream') -const toStream = require('pull-stream-to-stream') const toPull = require('stream-to-pull-stream') const pushable = require('pull-pushable') const EOL = require('os').EOL +const through = require('through2') exports = module.exports @@ -72,7 +72,7 @@ exports.get = { const ipfs = request.server.app.ipfs const pack = tar.pack() - ipfs.files.getPull(key, (err, stream) => { + ipfs.files.get(key, (err, stream) => { if (err) { log.error(err) @@ -83,35 +83,33 @@ exports.get = { return } - 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 - toStream.source(file.content) - .pipe(pack.entry(header, cb)) - } - }), - pull.onEnd((err) => { - if (err) { - log.error(err) - - reply({ - Message: 'Failed to get file: ' + err, - Code: 0 - }).code(500) - 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() } + 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 From 5fe4674cc8d90e8bdd82eadb439dd63f5ad0f138 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 16 Sep 2016 21:45:37 +0200 Subject: [PATCH 2/4] test: fix current tests, add more cli tests --- package.json | 1 + src/cli/commands/bitswap/stat.js | 3 +- src/cli/commands/block/stat.js | 3 +- src/cli/commands/bootstrap/add.js | 1 + src/cli/commands/files/cat.js | 16 +- src/cli/commands/files/get.js | 3 +- src/core/components/block.js | 2 +- src/http-api/resources/block.js | 12 +- src/http-api/resources/bootstrap.js | 58 +++++- src/http-api/resources/files.js | 64 +++--- src/http-api/routes/bootstrap.js | 23 +-- test/cli/test-bitswap.js | 68 +++---- test/cli/test-block.js | 142 +++---------- test/cli/test-bootstrap.js | 66 ++---- test/cli/test-commands.js | 32 +-- test/cli/test-config.js | 218 ++++---------------- test/cli/test-files.js | 144 ++++--------- test/cli/test-id.js | 61 +----- test/cli/test-init.js | 56 +++-- test/cli/test-object.js | 304 ++++++---------------------- test/cli/test-swarm.js | 80 +++----- test/cli/test-version.js | 53 +---- test/utils/ipfs.js | 44 ++++ test/utils/on-and-off.js | 25 +++ 24 files changed, 484 insertions(+), 995 deletions(-) create mode 100644 test/utils/ipfs.js create mode 100644 test/utils/on-and-off.js 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() + }) +} From 85091999b4f5856f98c6f646d4e79c71149359dd Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Sun, 25 Sep 2016 20:45:39 +0200 Subject: [PATCH 3/4] refactor: replace run-* with async, ipfs->ipfs-exec --- gulpfile.js | 4 ++-- package.json | 1 + src/cli/commands/config/edit.js | 2 +- src/cli/commands/files/cat.js | 2 +- src/cli/commands/files/get.js | 1 + src/core/components/go-online.js | 2 +- src/core/components/object.js | 9 ++------- src/http-api/index.js | 2 +- test/cli/test-bitswap.js | 2 +- test/cli/test-block.js | 2 +- test/cli/test-bootstrap.js | 2 +- test/cli/test-commands.js | 2 +- test/cli/test-config.js | 2 +- test/cli/test-files.js | 2 +- test/cli/test-id.js | 2 +- test/cli/test-init.js | 2 +- test/cli/test-object.js | 2 +- test/cli/test-swarm.js | 2 +- test/cli/test-version.js | 2 +- test/core/both/test-bitswap.js | 6 +++--- test/core/browser.js | 2 +- test/utils/factory-core/index.js | 2 +- test/utils/factory-http/index.js | 2 +- test/utils/{ipfs.js => ipfs-exec.js} | 11 +++++++++++ test/utils/temp-node.js | 2 +- 25 files changed, 39 insertions(+), 31 deletions(-) rename test/utils/{ipfs.js => ipfs-exec.js} (58%) diff --git a/gulpfile.js b/gulpfile.js index 2fdf17efa6..3d53274daf 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,8 +1,8 @@ 'use strict' const gulp = require('gulp') -const parallel = require('run-parallel') -const series = require('run-series') +const parallel = require('async/parallel') +const series = require('async/series') const createTempNode = require('./test/utils/temp-node') const API = require('./src/http-api') diff --git a/package.json b/package.json index 6b9bf0a5a3..ae6ec3ef80 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "transform-loader": "^0.2.3" }, "dependencies": { + "async": "^2.0.1", "babel-runtime": "^6.11.6", "bl": "^1.1.2", "boom": "^4.0.0", diff --git a/src/cli/commands/config/edit.js b/src/cli/commands/config/edit.js index 18d6fda014..91900578ce 100644 --- a/src/cli/commands/config/edit.js +++ b/src/cli/commands/config/edit.js @@ -3,7 +3,7 @@ const spawn = require('child_process').spawn const fs = require('fs') const temp = require('temp') -const waterfall = require('run-waterfall') +const waterfall = require('async/waterfall') const debug = require('debug') const log = debug('cli:config') log.error = debug('cli:config:error') diff --git a/src/cli/commands/files/cat.js b/src/cli/commands/files/cat.js index 88dd96b2ea..2b804d4e34 100644 --- a/src/cli/commands/files/cat.js +++ b/src/cli/commands/files/cat.js @@ -1,6 +1,6 @@ 'use strict' -const waterfall = require('run-waterfall') +const waterfall = require('async/waterfall') const debug = require('debug') const utils = require('../../utils') const log = debug('cli:files') diff --git a/src/cli/commands/files/get.js b/src/cli/commands/files/get.js index dbf0a76732..bc011ff68b 100644 --- a/src/cli/commands/files/get.js +++ b/src/cli/commands/files/get.js @@ -65,6 +65,7 @@ function fileHandler (dir) { file.content.pipe(fs.createWriteStream(target)) .once('error', cb) .once('end', cb) + return } cb() }) diff --git a/src/core/components/go-online.js b/src/core/components/go-online.js index 985f307e27..403226a8e8 100644 --- a/src/core/components/go-online.js +++ b/src/core/components/go-online.js @@ -1,6 +1,6 @@ 'use strict' -const series = require('run-series') +const series = require('async/series') const Bitswap = require('ipfs-bitswap') module.exports = function goOnline (self) { diff --git a/src/core/components/object.js b/src/core/components/object.js index 8e987b4d07..87239bb6c9 100644 --- a/src/core/components/object.js +++ b/src/core/components/object.js @@ -1,7 +1,7 @@ 'use strict' const mDAG = require('ipfs-merkle-dag') -const waterfall = require('run-waterfall') +const waterfall = require('async/waterfall') const promisify = require('promisify-es6') const bs58 = require('bs58') const DAGNode = mDAG.DAGNode @@ -69,12 +69,7 @@ module.exports = function object (self) { cb(err, node) }) } - ], (err, node) => { - if (err) { - return cb(err) - } - cb(null, node) - }) + ], cb) } } diff --git a/src/http-api/index.js b/src/http-api/index.js index 848d3a00ce..0e46e0f33b 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -1,6 +1,6 @@ 'use strict' -const parallel = require('run-parallel') +const parallel = require('async/parallel') const Hapi = require('hapi') const debug = require('debug') const fs = require('fs') diff --git a/test/cli/test-bitswap.js b/test/cli/test-bitswap.js index 425640b22c..d1230c53d7 100644 --- a/test/cli/test-bitswap.js +++ b/test/cli/test-bitswap.js @@ -8,7 +8,7 @@ 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) +const ipfs = require('../utils/ipfs-exec')(repoPath) describe('bitswap', () => { let node diff --git a/test/cli/test-block.js b/test/cli/test-block.js index c5a03eb4a1..6beb8b3076 100644 --- a/test/cli/test-block.js +++ b/test/cli/test-block.js @@ -4,7 +4,7 @@ const expect = require('chai').expect const repoPath = require('./index').repoPath const describeOnlineAndOffline = require('../utils/on-and-off') -const ipfs = require('../utils/ipfs')(repoPath) +const ipfs = require('../utils/ipfs-exec')(repoPath) describe('block', () => { describeOnlineAndOffline(repoPath, () => { diff --git a/test/cli/test-bootstrap.js b/test/cli/test-bootstrap.js index 766c3f6887..8c69c8486e 100644 --- a/test/cli/test-bootstrap.js +++ b/test/cli/test-bootstrap.js @@ -4,7 +4,7 @@ const expect = require('chai').expect const repoPath = require('./index').repoPath -const ipfs = require('../utils/ipfs')(repoPath) +const ipfs = require('../utils/ipfs-exec')(repoPath) const describeOnlineAndOffline = require('../utils/on-and-off') describe('bootstrap', () => { diff --git a/test/cli/test-commands.js b/test/cli/test-commands.js index 539c055363..da484580d1 100644 --- a/test/cli/test-commands.js +++ b/test/cli/test-commands.js @@ -3,7 +3,7 @@ const expect = require('chai').expect const repoPath = require('./index').repoPath -const ipfsBase = require('../utils/ipfs') +const ipfsBase = require('../utils/ipfs-exec') const ipfs = ipfsBase(repoPath) const describeOnlineAndOffline = require('../utils/on-and-off') diff --git a/test/cli/test-config.js b/test/cli/test-config.js index 1027995335..fecdeb3a2e 100644 --- a/test/cli/test-config.js +++ b/test/cli/test-config.js @@ -5,7 +5,7 @@ const expect = require('chai').expect const fs = require('fs') const path = require('path') const repoPath = require('./index').repoPath -const ipfs = require('../utils/ipfs')(repoPath) +const ipfs = require('../utils/ipfs-exec')(repoPath) const describeOnlineAndOffline = require('../utils/on-and-off') describe('config', () => { diff --git a/test/cli/test-files.js b/test/cli/test-files.js index b9edc56354..0e7aca6f7e 100644 --- a/test/cli/test-files.js +++ b/test/cli/test-files.js @@ -6,7 +6,7 @@ const repoPath = require('./index').repoPath const fs = require('fs') const path = require('path') const describeOnlineAndOffline = require('../utils/on-and-off') -const ipfs = require('../utils/ipfs')(repoPath) +const ipfs = require('../utils/ipfs-exec')(repoPath) describe('files', () => { describeOnlineAndOffline(repoPath, () => { diff --git a/test/cli/test-id.js b/test/cli/test-id.js index cfef37e5e4..6a76170e41 100644 --- a/test/cli/test-id.js +++ b/test/cli/test-id.js @@ -4,7 +4,7 @@ const expect = require('chai').expect const repoPath = require('./index').repoPath const describeOnlineAndOffline = require('../utils/on-and-off') -const ipfs = require('../utils/ipfs')(repoPath) +const ipfs = require('../utils/ipfs-exec')(repoPath) describe('id', () => { describeOnlineAndOffline(repoPath, () => { diff --git a/test/cli/test-init.js b/test/cli/test-init.js index 99268e7a27..27e20bc191 100644 --- a/test/cli/test-init.js +++ b/test/cli/test-init.js @@ -5,7 +5,7 @@ const expect = require('chai').expect const path = require('path') const fs = require('fs') const clean = require('../utils/clean') -const ipfsCmd = require('../utils/ipfs') +const ipfsCmd = require('../utils/ipfs-exec') describe('init', function () { this.timeout(60 * 1000) diff --git a/test/cli/test-object.js b/test/cli/test-object.js index 6506abb8ee..383b32457b 100644 --- a/test/cli/test-object.js +++ b/test/cli/test-object.js @@ -5,7 +5,7 @@ const expect = require('chai').expect const repoPath = require('./index').repoPath const describeOnlineAndOffline = require('../utils/on-and-off') -const ipfs = require('../utils/ipfs')(repoPath) +const ipfs = require('../utils/ipfs-exec')(repoPath) describe('object', () => { describeOnlineAndOffline(repoPath, () => { diff --git a/test/cli/test-swarm.js b/test/cli/test-swarm.js index 3426f1e982..d846337024 100644 --- a/test/cli/test-swarm.js +++ b/test/cli/test-swarm.js @@ -6,7 +6,7 @@ const expect = require('chai').expect const HttpAPI = require('../../src/http-api') const createTempNode = require('../utils/temp-node') const repoPath = require('./index').repoPath -const ipfs = require('../utils/ipfs')(repoPath) +const ipfs = require('../utils/ipfs-exec')(repoPath) describe('swarm', function () { this.timeout(30 * 1000) diff --git a/test/cli/test-version.js b/test/cli/test-version.js index 95d104fb0d..c4e9113a29 100644 --- a/test/cli/test-version.js +++ b/test/cli/test-version.js @@ -5,7 +5,7 @@ const expect = require('chai').expect const pkgversion = require('../../package.json').version const repoPath = require('./index').repoPath const describeOnlineAndOffline = require('../utils/on-and-off') -const ipfs = require('../utils/ipfs')(repoPath) +const ipfs = require('../utils/ipfs-exec')(repoPath) describe('version', () => { describeOnlineAndOffline(repoPath, () => { diff --git a/test/core/both/test-bitswap.js b/test/core/both/test-bitswap.js index 208b2cfca1..5ccb186d00 100644 --- a/test/core/both/test-bitswap.js +++ b/test/core/both/test-bitswap.js @@ -4,9 +4,9 @@ const expect = require('chai').expect const _ = require('lodash') -const series = require('run-series') -const waterfall = require('run-waterfall') -const parallel = require('run-parallel') +const series = require('async/series') +const waterfall = require('async/waterfall') +const parallel = require('async/parallel') const leftPad = require('left-pad') const Block = require('ipfs-block') const bs58 = require('bs58') diff --git a/test/core/browser.js b/test/core/browser.js index 02eced8342..54731912c9 100644 --- a/test/core/browser.js +++ b/test/core/browser.js @@ -1,7 +1,7 @@ /* eslint-env mocha */ 'use strict' -const series = require('run-series') +const series = require('async/series') const Store = require('idb-pull-blob-store') const _ = require('lodash') const pull = require('pull-stream') diff --git a/test/utils/factory-core/index.js b/test/utils/factory-core/index.js index f334536795..c23b628473 100644 --- a/test/utils/factory-core/index.js +++ b/test/utils/factory-core/index.js @@ -5,7 +5,7 @@ const isNode = require('detect-node') const IPFSRepo = require('ipfs-repo') const cleanRepo = require('../clean') const IPFS = require('../../../src/core') -const series = require('run-series') +const series = require('async/series') const defaultConfig = require('./default-config.json') module.exports = Factory diff --git a/test/utils/factory-http/index.js b/test/utils/factory-http/index.js index 6582184d25..290330aa29 100644 --- a/test/utils/factory-http/index.js +++ b/test/utils/factory-http/index.js @@ -6,7 +6,7 @@ const IPFSAPI = require('ipfs-api') const IPFS = require('../../../src/core') const cleanRepo = require('../clean') const HTTPAPI = require('../../../src/http-api') -const series = require('run-series') +const series = require('async/series') const defaultConfig = require('./default-config.json') module.exports = Factory diff --git a/test/utils/ipfs.js b/test/utils/ipfs-exec.js similarity index 58% rename from test/utils/ipfs.js rename to test/utils/ipfs-exec.js index 2c8c94d718..5867e4c5d6 100644 --- a/test/utils/ipfs.js +++ b/test/utils/ipfs-exec.js @@ -4,6 +4,17 @@ const execa = require('execa') const expect = require('chai').expect const _ = require('lodash') +// This is our new test utility to easily check and execute +// ipfs cli commands. +// +// The top level export is a function that can be passed a `repoPath` +// and optional `opts` to customize the execution of the commands. +// This function returns the actual executer, which consists of +// `ipfs('files get ')` and `ipfs.fail('files get ')` +// The first one executes and asserts that the command ran successfully +// and returns a promise which is resolved to `stdout` of the command. +// The `.fail` variation asserts that the command exited with `Code > 0` +// and returns a promise that resolves to `stderr`. module.exports = (repoPath, opts) => { const env = _.clone(process.env) env.IPFS_PATH = repoPath diff --git a/test/utils/temp-node.js b/test/utils/temp-node.js index 7375d9e60b..8fbc90f86c 100644 --- a/test/utils/temp-node.js +++ b/test/utils/temp-node.js @@ -3,7 +3,7 @@ const expect = require('chai').expect const leftPad = require('left-pad') -const series = require('run-series') +const series = require('async/series') const IPFS = require('../../src/core') const createTempRepo = require('./temp-repo') From 028a98c50037d75a7f2a2bc9a127ef489471730e Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 30 Sep 2016 10:59:48 +0200 Subject: [PATCH 4/4] feat(http-api): add joi validation to bootstrap --- src/http-api/routes/bootstrap.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/http-api/routes/bootstrap.js b/src/http-api/routes/bootstrap.js index d4ac0d37d4..8f1c46af42 100644 --- a/src/http-api/routes/bootstrap.js +++ b/src/http-api/routes/bootstrap.js @@ -1,5 +1,6 @@ 'use strict' +const Joi = require('joi') const resources = require('./../resources') module.exports = (server) => { @@ -20,7 +21,14 @@ module.exports = (server) => { pre: [ { method: resources.bootstrap.add.parseArgs, assign: 'args' } ], - handler: resources.bootstrap.add.handler + handler: resources.bootstrap.add.handler, + validate: { + query: { + arg: Joi.string().required(), + default: Joi.boolean(), + 'stream-channels': Joi.boolean() + } + } } }) @@ -39,7 +47,14 @@ module.exports = (server) => { pre: [ { method: resources.bootstrap.rm.parseArgs, assign: 'args' } ], - handler: resources.bootstrap.rm.handler + handler: resources.bootstrap.rm.handler, + validate: { + query: { + arg: Joi.string().required(), + default: Joi.boolean(), + 'stream-channels': Joi.boolean() + } + } } }) }