forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipfs-exec.js
79 lines (67 loc) · 2.2 KB
/
ipfs-exec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict'
const execa = require('execa')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
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 <hash>')` and `ipfs.fail('files get <hash>')`
// 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
const config = Object.assign({}, {
stripEof: false,
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(' ')
}
const cp = exec(args)
const res = cp.then((res) => {
// We can't escape the os.tmpdir warning due to:
// https://github.com/shelljs/shelljs/blob/master/src/tempdir.js#L43
// expect(res.stderr).to.be.eql('')
return res.stdout
})
res.kill = cp.kill.bind(cp)
res.stdout = cp.stdout
res.stderr = cp.stderr
return res
}
/**
* Expect the command passed as @param arguments to fail.
* @return {Promise} Resolves if the command passed as @param arguments fails,
* rejects if it was successful.
*/
ipfs.fail = function ipfsFail () {
let args = Array.from(arguments)
let caught = false
if (args.length === 1) {
args = args[0].split(' ')
}
return exec(args)
.catch(err => {
caught = true
expect(err).to.exist()
})
.then(() => {
if (!caught) {
throw new Error(`jsipfs expected to fail during command: jsipfs ${args.join(' ')}`)
}
})
}
return ipfs
}