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

Commit 50dab2b

Browse files
committed
Move "add" tests to interface-ipfs-core.
1 parent 8ae764e commit 50dab2b

File tree

2 files changed

+12
-199
lines changed

2 files changed

+12
-199
lines changed

test/api/add.spec.js

Lines changed: 10 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -2,202 +2,15 @@
22
/* globals apiClients */
33
'use strict'
44

5-
const expect = require('chai').expect
6-
const Readable = require('stream').Readable
7-
const path = require('path')
8-
const isNode = require('detect-node')
9-
const fs = require('fs')
10-
const bs58 = require('bs58')
11-
12-
let testfile
13-
let testfileBig
14-
const testfilePath = path.join(__dirname, '/../testfile.txt')
15-
16-
if (isNode) {
17-
testfile = fs.readFileSync(testfilePath)
18-
testfileBig = fs.createReadStream(path.join(__dirname, '/../15mb.random'), { bufferSize: 128 })
19-
// testfileBig = fs.createReadStream(path.join(__dirname, '/../100mb.random'), { bufferSize: 128 })
20-
} else {
21-
testfile = require('raw!../testfile.txt')
22-
// browser goes nuts with a 100mb in memory
23-
// testfileBig = require('raw!../100mb.random')
5+
const test = require('interface-ipfs-core')
6+
7+
const common = {
8+
setup: function (cb) {
9+
cb(null, apiClients.a)
10+
},
11+
teardown: function (cb) {
12+
cb()
13+
}
2414
}
2515

26-
describe('.add', () => {
27-
it('add buffer as tuple', (done) => {
28-
if (!isNode) {
29-
return done()
30-
}
31-
32-
const file = {
33-
path: 'testfile.txt',
34-
content: new Buffer(testfile)
35-
}
36-
37-
apiClients.a.files.add([file], (err, res) => {
38-
expect(err).to.not.exist
39-
40-
const added = res[0] != null ? res[0] : res
41-
const mh = bs58.encode(added.multihash()).toString()
42-
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
43-
expect(added.links).to.have.length(0)
44-
done()
45-
})
46-
})
47-
48-
it('add buffer', (done) => {
49-
let buf = new Buffer(testfile)
50-
apiClients.a.files.add(buf, (err, res) => {
51-
expect(err).to.not.exist
52-
53-
expect(res).to.have.length(1)
54-
const mh = bs58.encode(res[0].multihash()).toString()
55-
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
56-
expect(res[0].links).to.have.length(0)
57-
done()
58-
})
59-
})
60-
61-
it('add BIG buffer', (done) => {
62-
if (!isNode) {
63-
return done()
64-
}
65-
66-
apiClients.a.files.add(testfileBig, (err, res) => {
67-
expect(err).to.not.exist
68-
69-
expect(res).to.have.length(1)
70-
const mh = bs58.encode(res[0].multihash()).toString()
71-
expect(mh).to.equal('Qmcx5werSWQPdrGVap7LARHB4QUSPRPJwxhFuHvdoXqQXT')
72-
expect(res[0].links).to.have.length(58)
73-
done()
74-
})
75-
})
76-
77-
it('local fs: add file', (done) => {
78-
if (!isNode) {
79-
return done()
80-
}
81-
82-
apiClients.a.util.addFiles(testfilePath, (err, res) => {
83-
expect(err).to.not.exist
84-
85-
const added = res[0] != null ? res[0] : res
86-
const mh = bs58.encode(added.multihash()).toString()
87-
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
88-
expect(added.links).to.have.length(0)
89-
done()
90-
})
91-
})
92-
93-
it('local fs: add nested dir (follow symlinks)', (done) => {
94-
apiClients.a.util.addFiles(path.join(__dirname, '/../test-folder'), { recursive: true }, (err, res) => {
95-
if (isNode) {
96-
expect(err).to.not.exist
97-
98-
const added = res[res.length - 1]
99-
const mh = bs58.encode(added.multihash()).toString()
100-
expect(mh).to.equal('QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6')
101-
expect(added.links).to.have.length(7)
102-
103-
done()
104-
} else {
105-
expect(err.message).to.be.equal('Recursive uploads are not supported in the browser')
106-
done()
107-
}
108-
})
109-
})
110-
111-
it('local fs: add nested dir (don\'t follow symlinks)', (done) => {
112-
apiClients.a.util.addFiles(path.join(__dirname, '/../test-folder'), { recursive: true, followSymlinks: false }, (err, res) => {
113-
if (isNode) {
114-
expect(err).to.not.exist
115-
116-
const added = res[res.length - 1]
117-
// same hash as the result from the cli (ipfs add test/test-folder -r)
118-
const mh = bs58.encode(added.multihash()).toString()
119-
expect(mh).to.equal('QmRArDYd8Rk7Zb7K2699KqmQM1uUoejn1chtEAcqkvjzGg')
120-
expect(added.links).to.have.length(7)
121-
done()
122-
} else {
123-
expect(err.message).to.be.equal('Recursive uploads are not supported in the browser')
124-
done()
125-
}
126-
})
127-
})
128-
129-
it('add a nested dir as array', (done) => {
130-
if (!isNode) return done()
131-
const fs = require('fs')
132-
const base = path.join(__dirname, '../test-folder')
133-
const content = (name) => ({
134-
path: `test-folder/${name}`,
135-
content: fs.readFileSync(path.join(base, name))
136-
})
137-
const dirs = [
138-
content('add.js'),
139-
content('cat.js'),
140-
content('ls.js'),
141-
content('ipfs-add.js'),
142-
content('version.js'),
143-
content('files/hello.txt'),
144-
content('files/ipfs.txt'),
145-
{
146-
path: 'test-folder',
147-
dir: true
148-
}
149-
]
150-
151-
apiClients.a.files.add(dirs, { recursive: true }, (err, res) => {
152-
expect(err).to.not.exist
153-
154-
const added = res[res.length - 1]
155-
const mh = bs58.encode(added.multihash()).toString()
156-
expect(mh).to.equal('QmTDH2RXGn8XyDAo9YyfbZAUXwL1FCr44YJCN9HBZmL9Gj')
157-
expect(added.links).to.have.length(6)
158-
done()
159-
})
160-
})
161-
162-
it('add stream', (done) => {
163-
const stream = new Readable()
164-
stream.push('Hello world')
165-
stream.push(null)
166-
167-
apiClients.a.files.add(stream, (err, res) => {
168-
expect(err).to.not.exist
169-
170-
const added = res[0] != null ? res[0] : res
171-
const mh = bs58.encode(added.multihash()).toString()
172-
expect(mh).to.equal('QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve')
173-
expect(added.links).to.have.length(0)
174-
done()
175-
})
176-
})
177-
178-
it('add url', (done) => {
179-
const url = 'https://raw.githubusercontent.com/ipfs/js-ipfs-api/2a9cc63d7427353f2145af6b1a768a69e67c0588/README.md'
180-
apiClients.a.util.addUrl(url, (err, res) => {
181-
expect(err).to.not.exist
182-
183-
const added = res[0] != null ? res[0] : res
184-
const mh = bs58.encode(added.multihash()).toString()
185-
expect(mh).to.equal('QmRzvSX35JpzQ2Lyn55r3YwWqdVP6PPxYHFpiWpwQTff8A')
186-
expect(added.links).to.have.length(0)
187-
done()
188-
})
189-
})
190-
191-
describe('promise', () => {
192-
it('add buffer', () => {
193-
let buf = new Buffer(testfile)
194-
return apiClients.a.files.add(buf)
195-
.then((res) => {
196-
const added = res[0] != null ? res[0] : res
197-
const mh = bs58.encode(added.multihash()).toString()
198-
expect(mh).to.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
199-
expect(added.links).to.have.length(0)
200-
})
201-
})
202-
})
203-
})
16+
test.files(common)

test/api/ls.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('ls', function () {
3030
it('should correctly handle a nonexisting path', function (done) {
3131
if (!isNode) return done()
3232

33-
apiClients.a.ls('QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6/folder_that_isnt_there', (err, res) => {
33+
apiClients.a.ls('QmRNjDeKStKGTQXnJ2NFqeQ9oW/folder_that_isnt_there', (err, res) => {
3434
expect(err).to.exist
3535
expect(res).to.not.exist
3636
done()
@@ -59,7 +59,7 @@ describe('ls', function () {
5959
it('should correctly handle a nonexisting path', () => {
6060
if (!isNode) return
6161

62-
return apiClients.a.ls('QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6/folder_that_isnt_there')
62+
return apiClients.a.ls('QmRNjDeKStKGTQXnJ3NFqeQ9oW/folder_that_isnt_there')
6363
.catch((err) => {
6464
expect(err).to.exist
6565
})

0 commit comments

Comments
 (0)