Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 4c337c2

Browse files
committed
fix tests
1 parent b86d017 commit 4c337c2

File tree

1 file changed

+116
-102
lines changed

1 file changed

+116
-102
lines changed

src/files.js

+116-102
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,163 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
var expect = require('chai').expect
5-
var bs58 = require('bs58')
6-
var Readable = require('readable-stream')
7-
var path = require('path')
8-
var isNode = require('detect-node')
9-
var fs = require('fs')
10-
var bl = require('bl')
11-
12-
module.exports = function (common) {
13-
describe('.files', function () {
14-
var smallFile = void 0
15-
var bigFile = void 0
16-
var ipfs = void 0
17-
18-
before(function (done) {
19-
smallFile = fs.readFileSync(path.join(__dirname, './data/testfile.txt'))
20-
bigFile = fs.readFileSync(path.join(__dirname, './data/15mb.random'))
21-
22-
common.setup(function (err, _ipfs) {
4+
const expect = require('chai').expect
5+
const bs58 = require('bs58')
6+
const Readable = require('readable-stream')
7+
const path = require('path')
8+
const fs = require('fs')
9+
const isNode = require('detect-node')
10+
const bl = require('bl')
11+
12+
module.exports = (common) => {
13+
describe('.files', () => {
14+
let smallFile
15+
let bigFile
16+
let ipfs
17+
18+
before((done) => {
19+
smallFile = fs.readFileSync(path.join(__dirname, './data/testfile.txt')
20+
)
21+
bigFile = fs.readFileSync(path.join(__dirname, './data/15mb.random')
22+
)
23+
24+
common.setup((err, _ipfs) => {
2325
expect(err).to.not.exist
2426
ipfs = _ipfs
2527
done()
2628
})
2729
})
2830

29-
after(function (done) {
31+
after((done) => {
3032
common.teardown(done)
3133
})
3234

33-
describe('.add', function () {
34-
it('stream', function (done) {
35-
var buffered = new Buffer('some data')
36-
var rs = new Readable()
35+
describe('.add', () => {
36+
it('stream', (done) => {
37+
const buffered = new Buffer('some data')
38+
const rs = new Readable()
3739
rs.push(buffered)
3840
rs.push(null)
3941

40-
var arr = []
41-
var filePair = { path: 'data.txt', content: rs }
42+
const arr = []
43+
const filePair = {path: 'data.txt', content: rs}
4244
arr.push(filePair)
4345

44-
ipfs.files.add(arr, function (err, res) {
46+
ipfs.files.add(arr, (err, res) => {
4547
expect(err).to.not.exist
4648
expect(res).to.be.length(1)
4749
expect(res[0].path).to.equal('data.txt')
4850
expect(res[0].node.size()).to.equal(17)
49-
var mh = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
51+
const mh = 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS'
5052
expect(bs58.encode(res[0].node.multihash()).toString()).to.equal(mh)
5153
done()
5254
})
5355
})
5456

55-
it('buffer as tuple', function (done) {
56-
if (!isNode) return done()
57-
58-
var file = {
57+
it('buffer as tuple', (done) => {
58+
const file = {
5959
path: 'testfile.txt',
6060
content: smallFile
6161
}
6262

63-
ipfs.files.add([file], function (err, res) {
63+
ipfs.files.add([file], (err, res) => {
6464
expect(err).to.not.exist
6565

66-
var added = res[0] != null ? res[0] : res
67-
var mh = bs58.encode(added.node.multihash()).toString()
66+
const added = res[0] != null ? res[0] : res
67+
const mh = bs58.encode(added.node.multihash()).toString()
6868
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
6969
expect(added.path).to.equal('testfile.txt')
7070
expect(added.node.links).to.have.length(0)
7171
done()
7272
})
7373
})
7474

75-
it('buffer', function (done) {
76-
ipfs.files.add(smallFile, function (err, res) {
75+
it('buffer', (done) => {
76+
ipfs.files.add(smallFile, (err, res) => {
7777
expect(err).to.not.exist
7878

7979
expect(res).to.have.length(1)
80-
var mh = bs58.encode(res[0].node.multihash()).toString()
80+
const mh = bs58.encode(res[0].node.multihash()).toString()
8181
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
8282
expect(res[0].path).to.equal(mh)
8383
expect(res[0].node.links).to.have.length(0)
8484
done()
8585
})
8686
})
8787

88-
it('BIG buffer', function (done) {
89-
ipfs.files.add(bigFile, function (err, res) {
88+
it('BIG buffer', (done) => {
89+
ipfs.files.add(bigFile, (err, res) => {
9090
expect(err).to.not.exist
9191

9292
expect(res).to.have.length(1)
9393
expect(res[0].node.links).to.have.length(58)
94-
var mh = bs58.encode(res[0].node.multihash()).toString()
94+
const mh = bs58.encode(res[0].node.multihash()).toString()
9595
expect(res[0].path).to.equal(mh)
9696
expect(mh).to.equal('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq')
9797
done()
9898
})
9999
})
100100

101-
it('add a nested dir as array', function (done) {
101+
it('add a nested dir as array', (done) => {
102102
if (!isNode) {
103103
return done()
104+
// can't run this test cause browserify
105+
// can't shim readFileSync in runtime
104106
}
105-
var base = path.join(__dirname, 'data/test-folder')
106-
var content = function content (name) {
107-
return {
108-
path: 'test-folder/' + name,
109-
content: fs.readFileSync(path.join(base, name))
110-
}
111-
}
112-
var emptyDir = function emptyDir (name) {
113-
return {
114-
path: 'test-folder/' + name,
115-
dir: true
116-
}
117-
}
118-
var dirs = [content('pp.txt'), content('holmes.txt'), content('jungle.txt'), content('alice.txt'), emptyDir('empty-folder'), content('files/hello.txt'), content('files/ipfs.txt'), emptyDir('files/empty')]
107+
const base = path.join(__dirname, 'data/test-folder')
108+
const content = (name) => ({
109+
path: `test-folder/${name}`,
110+
content: fs.readFileSync(path.join(base, name))
111+
})
112+
const emptyDir = (name) => ({
113+
path: `test-folder/${name}`
114+
})
115+
const dirs = [
116+
content('pp.txt'),
117+
content('holmes.txt'),
118+
content('jungle.txt'),
119+
content('alice.txt'),
120+
emptyDir('empty-folder'),
121+
content('files/hello.txt'),
122+
content('files/ipfs.txt'),
123+
emptyDir('files/empty')
124+
]
119125

120-
ipfs.files.add(dirs, function (err, res) {
126+
ipfs.files.add(dirs, (err, res) => {
121127
expect(err).to.not.exist
122128

123-
var added = res[res.length - 1]
124-
var mh = bs58.encode(added.node.multihash()).toString()
129+
const added = res[res.length - 1]
130+
const mh = bs58.encode(added.node.multihash()).toString()
125131
expect(mh).to.equal('QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP')
126132
expect(added.path).to.equal('test-folder')
127133
expect(added.node.links).to.have.length(6)
128134
done()
129135
})
130136
})
131137

132-
describe('promise', function () {
133-
it('buffer', function () {
134-
return ipfs.files.add(smallFile).then(function (res) {
135-
var added = res[0] != null ? res[0] : res
136-
var mh = bs58.encode(added.node.multihash()).toString()
137-
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
138-
expect(added.path).to.equal(mh)
139-
expect(added.node.links).to.have.length(0)
140-
}).catch(function (err) {
141-
expect(err).to.not.exist
142-
})
138+
describe('promise', () => {
139+
it('buffer', () => {
140+
return ipfs.files.add(smallFile)
141+
.then((res) => {
142+
const added = res[0] != null ? res[0] : res
143+
const mh = bs58.encode(added.node.multihash()).toString()
144+
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
145+
expect(added.path).to.equal(mh)
146+
expect(added.node.links).to.have.length(0)
147+
})
148+
.catch((err) => {
149+
expect(err).to.not.exist
150+
})
143151
})
144152
})
145153
})
146154

147-
describe('.cat', function () {
148-
it('returns file stream', function (done) {
149-
var hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
150-
ipfs.cat(hash, function (err, file) {
155+
describe('.cat', () => {
156+
it('returns file stream', (done) => {
157+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
158+
ipfs.cat(hash, (err, file) => {
151159
expect(err).to.not.exist
152-
file.pipe(bl(function (err, bldata) {
160+
file.pipe(bl((err, bldata) => {
153161
expect(err).to.not.exist
154162
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
155163
done()
@@ -158,11 +166,11 @@ module.exports = function (common) {
158166
})
159167

160168
// This fails on js-ipfs-api
161-
it('takes a buffer input', function (done) {
162-
var mhBuf = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
163-
ipfs.cat(mhBuf, function (err, file) {
169+
it('takes a buffer input', (done) => {
170+
const mhBuf = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
171+
ipfs.cat(mhBuf, (err, file) => {
164172
expect(err).to.not.exist
165-
file.pipe(bl(function (err, bldata) {
173+
file.pipe(bl((err, bldata) => {
166174
expect(err).to.not.exist
167175
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
168176
done()
@@ -171,23 +179,23 @@ module.exports = function (common) {
171179
})
172180

173181
// You can add a large file to your ipfs repo and change the hash to the file after installing js-ipfs
174-
it('returns a large file', function (done) {
175-
var hash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
176-
ipfs.cat(hash, function (err, file) {
182+
it('returns a large file', (done) => {
183+
const hash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
184+
ipfs.cat(hash, (err, file) => {
177185
expect(err).to.not.exist
178-
file.pipe(bl(function (err, bldata) {
186+
file.pipe(bl((err, bldata) => {
179187
expect(err).to.not.exist
180188
expect(bldata).to.deep.equal(bigFile)
181189
done()
182190
}))
183191
})
184192
})
185193

186-
it('returns error on invalid key', function (done) {
187-
var hash = 'somethingNotMultihash'
188-
ipfs.cat(hash, function (err, file) {
194+
it('returns error on invalid key', (done) => {
195+
const hash = 'somethingNotMultihash'
196+
ipfs.cat(hash, (err, file) => {
189197
expect(err).to.exist
190-
var errString = err.toString()
198+
const errString = err.toString()
191199
if (errString === 'Error: invalid ipfs ref path') {
192200
expect(err.toString()).to.contain('Error: invalid ipfs ref path')
193201
}
@@ -198,25 +206,29 @@ module.exports = function (common) {
198206
})
199207
})
200208

201-
describe('promise', function () {
202-
it('files.cat', function (done) {
203-
var hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
204-
ipfs.cat(hash).then(function (stream) {
205-
stream.pipe(bl(function (err, bldata) {
209+
describe('promise', () => {
210+
it('files.cat', (done) => {
211+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
212+
ipfs.cat(hash)
213+
.then((stream) => {
214+
stream.pipe(bl((err, bldata) => {
206215
expect(err).to.not.exist
207216
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
208217
done()
209218
}))
210-
}).catch(function (err) {
219+
})
220+
.catch((err) => {
211221
expect(err).to.not.exist
212222
})
213223
})
214224

215-
it('returns error on invalid key', function (done) {
216-
var hash = 'somethingNotMultihash'
217-
ipfs.cat(hash).then(function (stream) {}).catch(function (err) {
225+
it('returns error on invalid key', (done) => {
226+
const hash = 'somethingNotMultihash'
227+
ipfs.cat(hash)
228+
.then((stream) => {})
229+
.catch((err) => {
218230
expect(err).to.exist
219-
var errString = err.toString()
231+
const errString = err.toString()
220232
if (errString === 'Error: invalid ipfs ref path') {
221233
expect(err.toString()).to.contain('Error: invalid ipfs ref path')
222234
}
@@ -227,15 +239,17 @@ module.exports = function (common) {
227239
})
228240
})
229241

230-
it('takes a buffer input', function (done) {
231-
var hash = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
232-
ipfs.cat(hash).then(function (stream) {
233-
stream.pipe(bl(function (err, bldata) {
242+
it('takes a buffer input', (done) => {
243+
const hash = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
244+
ipfs.cat(hash)
245+
.then((stream) => {
246+
stream.pipe(bl((err, bldata) => {
234247
expect(err).to.not.exist
235248
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
236249
done()
237250
}))
238-
}).catch(function (err) {
251+
})
252+
.catch((err) => {
239253
expect(err).to.not.exist
240254
})
241255
})

0 commit comments

Comments
 (0)