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

Commit a631a21

Browse files
committedSep 17, 2019
fix: fix electron renderer tests and a couple more bugs (#1105)
temp fix for electron renderer until ky-universal is fixed Blob can accept Buffer directly Buffer.buffer triggered a super weird bug in electron renderer enable a bunch of tests in the browser xvfb-run makes electron go crazy detect-node needs to be removed everywhere and ipfs-utils/src/env.js should be used If you mix promises and callbacks inside, call the callback with setImmediate
1 parent af6a5ed commit a631a21

10 files changed

+98
-113
lines changed
 

‎.aegir.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
'use strict'
22

3+
const promisify = require('promisify-es6')
34
const createServer = require('ipfsd-ctl').createServer
4-
5+
const EchoServer = require('interface-ipfs-core/src/utils/echo-http-server')
56
const server = createServer()
7+
const echoServer = EchoServer.createServer()
68

9+
const echoServerStart = promisify(echoServer.start)
10+
const echoServerStop = promisify(echoServer.stop)
711
module.exports = {
812
bundlesize: { maxSize: '245kB' },
913
webpack: {
@@ -22,9 +26,23 @@ module.exports = {
2226
singleRun: true
2327
},
2428
hooks: {
29+
node: {
30+
pre: () => echoServerStart(),
31+
post: () => echoServerStop()
32+
},
2533
browser: {
26-
pre: () => server.start(),
27-
post: () => server.stop()
34+
pre: () => {
35+
return Promise.all([
36+
server.start(),
37+
echoServerStart()
38+
])
39+
},
40+
post: () => {
41+
return Promise.all([
42+
server.stop(),
43+
echoServerStop()
44+
])
45+
}
2846
}
2947
}
3048
}

‎.travis.yml

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
language: node_js
22
cache: npm
3+
34
stages:
45
- check
56
- test
67
- cov
78

89
node_js:
9-
- '10'
1010
- '12'
11+
- '10'
1112

1213
os:
1314
- linux
@@ -18,11 +19,6 @@ script: npx nyc -s npm run test:node -- --bail
1819
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
1920

2021
jobs:
21-
allow_failures:
22-
- name: electron-renderer
23-
24-
fast_finish: true
25-
2622
include:
2723
- stage: check
2824
script:
@@ -44,13 +40,15 @@ jobs:
4440

4541
- stage: test
4642
name: electron-main
43+
os: osx
4744
script:
48-
- xvfb-run npx aegir test -t electron-main -- --bail
45+
- npx aegir test -t electron-main --bail
4946

5047
- stage: test
5148
name: electron-renderer
49+
os: osx
5250
script:
53-
- xvfb-run npx aegir test -t electron-renderer -- --bail
51+
- npx aegir test -t electron-renderer --bail
5452

5553
notifications:
5654
email: false

‎package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"cids": "~0.7.1",
5252
"concat-stream": "github:hugomrdias/concat-stream#feat/smaller",
5353
"debug": "^4.1.0",
54+
"delay": "^4.3.0",
5455
"detect-node": "^2.0.4",
5556
"end-of-stream": "^1.4.1",
5657
"err-code": "^2.0.0",
@@ -77,6 +78,7 @@
7778
"ky": "^0.13.0",
7879
"ky-universal": "^0.3.0",
7980
"lru-cache": "^5.1.1",
81+
"merge-options": "^1.0.1",
8082
"multiaddr": "^7.1.0",
8183
"multibase": "~0.6.0",
8284
"multicodec": "~0.5.1",
@@ -99,15 +101,15 @@
99101
"through2": "^3.0.1"
100102
},
101103
"devDependencies": {
102-
"aegir": "^20.0.0",
104+
"aegir": "^20.2.0",
103105
"browser-process-platform": "~0.1.1",
104106
"chai": "^4.2.0",
105107
"chai-as-promised": "^7.1.1",
106-
"cross-env": "^5.2.0",
108+
"cross-env": "^5.2.1",
107109
"dirty-chai": "^2.0.1",
108110
"go-ipfs-dep": "^0.4.22",
109-
"interface-ipfs-core": "^0.112.0",
110-
"ipfsd-ctl": "~0.45.0",
111+
"interface-ipfs-core": "~0.114.0",
112+
"ipfsd-ctl": "^0.46.2",
111113
"nock": "^11.3.2",
112114
"stream-equal": "^1.1.1"
113115
},

‎src/add/form-data.browser.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22
/* eslint-env browser */
33

4-
const { Buffer } = require('buffer')
54
const normaliseInput = require('ipfs-utils/src/files/normalise-input')
65

76
exports.toFormData = async input => {
@@ -16,12 +15,12 @@ exports.toFormData = async input => {
1615
// One day, this will be browser streams
1716
const bufs = []
1817
for await (const chunk of file.content) {
19-
bufs.push(Buffer.isBuffer(chunk) ? chunk.buffer : chunk)
18+
bufs.push(chunk)
2019
}
2120

22-
formData.append(`file-${i}`, new Blob(bufs, { type: 'application/octet-stream' }), file.path)
21+
formData.append(`file-${i}`, new Blob(bufs, { type: 'application/octet-stream' }), encodeURIComponent(file.path))
2322
} else {
24-
formData.append(`dir-${i}`, new Blob([], { type: 'application/x-directory' }), file.path)
23+
formData.append(`dir-${i}`, new Blob([], { type: 'application/x-directory' }), encodeURIComponent(file.path))
2524
}
2625

2726
i++

‎src/add/form-data.js

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const FormData = require('form-data')
44
const { Buffer } = require('buffer')
55
const toStream = require('it-to-stream')
66
const normaliseInput = require('ipfs-utils/src/files/normalise-input')
7+
const { isElectronRenderer } = require('ipfs-utils/src/env')
78

89
exports.toFormData = async input => {
910
const files = normaliseInput(input)
@@ -40,3 +41,10 @@ exports.toFormData = async input => {
4041

4142
return formData
4243
}
44+
45+
// TODO remove this when upstream fix for ky-universal is merged
46+
// https://github.com/sindresorhus/ky-universal/issues/9
47+
// also this should only be necessary when nodeIntegration is false in electron renderer
48+
if (isElectronRenderer) {
49+
exports.toFormData = require('./form-data.browser').toFormData
50+
}

‎src/dht/findpeer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = (send) => {
3232
// 2 = FinalPeer
3333
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
3434
if (!res || res.Type !== 2) {
35-
const errMsg = `key was not found (type 4)`
35+
const errMsg = 'key was not found (type 4)'
3636
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
3737
}
3838

‎src/id.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = (arg) => {
1111
callback = opts
1212
opts = undefined
1313
}
14+
1415
send({
1516
path: 'id',
1617
args: opts

‎test/get.spec.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const chaiAsPromised = require('chai-as-promised')
99
const expect = chai.expect
1010
chai.use(dirtyChai)
1111
chai.use(chaiAsPromised)
12-
const isNode = require('detect-node')
1312
const loadFixture = require('aegir/fixtures')
1413

1514
const ipfsClient = require('../src')
@@ -75,26 +74,19 @@ describe('.get (specific go-ipfs features)', function () {
7574
})
7675

7776
it('add path containing "+"s (for testing get)', async () => {
78-
if (!isNode) {
79-
return
80-
}
81-
8277
const filename = 'ti,c64x+mega++mod-pic.txt'
8378
const subdir = 'tmp/c++files'
8479
const expectedCid = 'QmPkmARcqjo5fqK1V1o8cFsuaXxWYsnwCNLJUYS4KeZyff'
80+
const path = `${subdir}/${filename}`
8581
const files = await ipfs.add([{
86-
path: subdir + '/' + filename,
87-
content: Buffer.from(subdir + '/' + filename, 'utf-8')
82+
path,
83+
content: Buffer.from(path)
8884
}])
8985

9086
expect(files[2].hash).to.equal(expectedCid)
9187
})
9288

9389
it('get path containing "+"s', async () => {
94-
if (!isNode) {
95-
return
96-
}
97-
9890
const cid = 'QmPkmARcqjo5fqK1V1o8cFsuaXxWYsnwCNLJUYS4KeZyff'
9991
let count = 0
10092
const files = await ipfs.get(cid)

‎test/interface.spec.js

+1-77
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
const tests = require('interface-ipfs-core')
55
const isNode = require('detect-node')
66
const CommonFactory = require('./utils/interface-common-factory')
7-
const ipfsClient = require('../src')
87
const isWindows = process.platform && process.platform === 'win32'
98

109
describe('interface-ipfs-core tests', () => {
@@ -102,40 +101,11 @@ describe('interface-ipfs-core tests', () => {
102101

103102
tests.filesRegular(defaultCommonFactory, {
104103
skip: [
105-
// .add
106-
isNode ? null : {
107-
name: 'should add a nested directory as array of tupples',
108-
reason: 'FIXME https://github.com/ipfs/js-ipfs-http-client/issues/339'
109-
},
110-
isNode ? null : {
111-
name: 'should add a nested directory as array of tupples with progress',
112-
reason: 'FIXME https://github.com/ipfs/js-ipfs-http-client/issues/339'
113-
},
114-
// .addPullStream
115-
isNode ? null : {
116-
name: 'should add pull stream of valid files and dirs',
117-
reason: 'FIXME https://github.com/ipfs/js-ipfs-http-client/issues/339'
118-
},
119-
// .addReadableStream
120-
isNode ? null : {
121-
name: 'should add readable stream of valid files and dirs',
122-
reason: 'FIXME https://github.com/ipfs/js-ipfs-http-client/issues/339'
123-
},
124104
// .addFromFs
125105
isNode ? null : {
126106
name: 'addFromFs',
127107
reason: 'Not designed to run in the browser'
128108
},
129-
// .addFromURL
130-
isNode ? null : {
131-
name: 'addFromURL',
132-
reason: 'Not designed to run in the browser'
133-
},
134-
// TODO: remove when interface-ipfs-core updated
135-
isNode ? null : {
136-
name: 'addFromUrl',
137-
reason: 'Not designed to run in the browser'
138-
},
139109
// .catPullStream
140110
{
141111
name: 'should export a chunk of a file',
@@ -148,26 +118,6 @@ describe('interface-ipfs-core tests', () => {
148118
{
149119
name: 'should export a chunk of a file in a Readable Stream',
150120
reason: 'TODO not implemented in go-ipfs yet'
151-
},
152-
// .get
153-
isNode ? null : {
154-
name: 'should get a directory',
155-
reason: 'FIXME https://github.com/ipfs/js-ipfs-http-client/issues/339'
156-
},
157-
// .ls
158-
isNode ? null : {
159-
name: 'should ls with a base58 encoded CID',
160-
reason: 'FIXME https://github.com/ipfs/js-ipfs-http-client/issues/339'
161-
},
162-
// .lsPullStream
163-
isNode ? null : {
164-
name: 'should pull stream ls with a base58 encoded CID',
165-
reason: 'FIXME https://github.com/ipfs/js-ipfs-http-client/issues/339'
166-
},
167-
// .lsReadableStream
168-
isNode ? null : {
169-
name: 'should readable stream ls with a base58 encoded CID',
170-
reason: 'FIXME https://github.com/ipfs/js-ipfs-http-client/issues/339'
171121
}
172122
]
173123
})
@@ -277,31 +227,5 @@ describe('interface-ipfs-core tests', () => {
277227

278228
tests.stats(defaultCommonFactory)
279229

280-
tests.swarm(CommonFactory.create({
281-
createSetup ({ ipfsFactory, nodes }) {
282-
return callback => {
283-
callback(null, {
284-
spawnNode (repoPath, config, cb) {
285-
if (typeof repoPath === 'function') {
286-
cb = repoPath
287-
repoPath = undefined
288-
}
289-
290-
if (typeof config === 'function') {
291-
cb = config
292-
config = undefined
293-
}
294-
295-
const spawnOptions = { repoPath, config, initOptions: { bits: 1024, profile: 'test' } }
296-
297-
ipfsFactory.spawn(spawnOptions)
298-
.then(ipfsd => {
299-
nodes.push(ipfsd)
300-
cb(null, ipfsClient(ipfsd.apiAddr))
301-
}, cb)
302-
}
303-
})
304-
}
305-
}
306-
}))
230+
tests.swarm(CommonFactory.createAsync())
307231
})

‎test/utils/interface-common-factory.js

+48-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
const each = require('async/each')
55
const IPFSFactory = require('ipfsd-ctl')
66
const ipfsClient = require('../../src')
7+
const merge = require('merge-options')
78

89
function createFactory (options) {
910
options = options || {}
1011

11-
options.factoryOptions = options.factoryOptions || {}
12+
options.factoryOptions = options.factoryOptions || { IpfsClient: ipfsClient }
1213
options.spawnOptions = options.spawnOptions || { initOptions: { bits: 1024, profile: 'test' } }
1314

1415
const ipfsFactory = IPFSFactory.create(options.factoryOptions)
@@ -26,8 +27,11 @@ function createFactory (options) {
2627
ipfsFactory.spawn(options.spawnOptions)
2728
.then((ipfsd) => {
2829
nodes.push(ipfsd)
29-
cb(null, ipfsClient(ipfsd.apiAddr))
30-
}, cb)
30+
setImmediate(() => cb(null, ipfsd.api))
31+
})
32+
.catch(err => {
33+
setImmediate(() => cb(err))
34+
})
3135
}
3236
})
3337
}
@@ -36,11 +40,50 @@ function createFactory (options) {
3640
if (options.createTeardown) {
3741
teardown = options.createTeardown({ ipfsFactory, nodes }, options)
3842
} else {
39-
teardown = callback => each(nodes, (node, cb) => node.stop().then(cb, cb), callback)
43+
teardown = callback => each(nodes, (node, cb) => {
44+
node
45+
.stop()
46+
.then(() => setImmediate(() => cb()))
47+
.catch(err => setImmediate(() => cb(err)))
48+
}, callback)
4049
}
4150

4251
return { setup, teardown }
4352
}
4453
}
4554

46-
exports.create = createFactory
55+
function createAsync (createFactoryOptions, createSpawnOptions) {
56+
return () => {
57+
const nodes = []
58+
const setup = async (factoryOptions = {}, spawnOptions) => {
59+
const ipfsFactory = IPFSFactory.create(merge(
60+
{ IpfsClient: ipfsClient },
61+
factoryOptions,
62+
createFactoryOptions
63+
))
64+
const node = await ipfsFactory.spawn(merge(
65+
{ initOptions: { profile: 'test' } },
66+
spawnOptions,
67+
createSpawnOptions
68+
))
69+
nodes.push(node)
70+
71+
const id = await node.api.id()
72+
node.api.peerId = id
73+
74+
return node.api
75+
}
76+
77+
const teardown = () => {
78+
return Promise.all(nodes.map(n => n.stop()))
79+
}
80+
return {
81+
setup,
82+
teardown
83+
}
84+
}
85+
}
86+
module.exports = {
87+
createAsync,
88+
create: createFactory
89+
}

0 commit comments

Comments
 (0)
This repository has been archived.