diff --git a/.gitignore b/.gitignore index 123ae94..254988d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,13 @@ +**/node_modules/ +**/*.log +test/repo-tests* + # Logs logs *.log +coverage + # Runtime data pids *.pid @@ -19,9 +25,11 @@ coverage # node-waf configuration .lock-wscript -# Compiled binary addons (http://nodejs.org/api/addons.html) -build/Release +build # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules + +lib +dist diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..59335fd --- /dev/null +++ b/.npmignore @@ -0,0 +1,34 @@ +**/node_modules/ +**/*.log +test/repo-tests* + +# Logs +logs +*.log + +coverage + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +build + +# Dependency directory +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git +node_modules + +test diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..52cd14d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +sudo: false +language: node_js +node_js: + - "stable" + +before_install: + - npm install -g npm + +script: + - npm run lint diff --git a/README.md b/README.md index 4097239..b61e09c 100644 --- a/README.md +++ b/README.md @@ -32,21 +32,29 @@ Include this badge in your readme if you make a module that is compatible with t ## Node.js -``` -var tape = require('tape') -var tests = require('interface-transport/tests') -var YourTransportHandler = require('../src') - -var common = { - setup: function (t, cb) { - cb(null, YourTransportHandler) - }, - teardown: function (t, cb) { - cb() - } -} - -tests(tape, common) +```js +/* eslint-env mocha */ +'use strict' + +const tests = require('interface-transport') +const multiaddr = require('multiaddr') +const YourTransport = require('../src') + +describe('compliance', () => { + tests({ + setup (cb) { + let t = new YourTransport() + const addrs = [ + multiaddr('valid-multiaddr-for-your-transport'), + multiaddr('valid-multiaddr2-for-your-transport') + ] + cb(null, t, addrs) + }, + teardown (cb) { + cb() + } + }) +}) ``` ## Go diff --git a/package.json b/package.json index 249aa8d..539c2da 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,21 @@ "type": "git", "url": "https://github.com/diasdavid/interface-transport.git" }, + "main": "src/index.js", + "jsnext:main": "src/index.js", + "scripts": { + "lint": "aegir-lint", + "build": "aegir-build", + "release": "aegir-release", + "release-minor": "aegir-release --type minor", + "release-major": "aegir-release --type major", + "coverage": "exit(0)", + "coverage-publish": "exit(0)" + }, + "pre-commit": [ + "lint", + "test" + ], "keywords": [ "IPFS" ], @@ -15,9 +30,13 @@ "url": "https://github.com/diasdavid/interface-transport/issues" }, "homepage": "https://github.com/diasdavid/interface-transport", - "devDependencies": {}, + "devDependencies": { + "aegir": "^6.0.1" + }, "dependencies": { + "chai": "^3.5.0", "multiaddr": "^2.0.2", - "timed-tape": "^0.1.1" + "pull-goodbye": "0.0.1", + "pull-stream": "^3.4.4" } } diff --git a/src/dial-test.js b/src/dial-test.js new file mode 100644 index 0000000..c4d1d73 --- /dev/null +++ b/src/dial-test.js @@ -0,0 +1,74 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect +const pull = require('pull-stream') +const goodbye = require('pull-goodbye') + +module.exports = (common) => { + describe('dial', () => { + let addrs + let transport + let listener + + before((done) => { + common.setup((err, _transport, _addrs) => { + if (err) return done(err) + transport = _transport + addrs = _addrs + done() + }) + }) + + after((done) => { + common.teardown(done) + }) + + beforeEach((done) => { + listener = transport.createListener((conn) => { + pull( + conn, + pull.map((x) => { + if (x.toString() !== 'GOODBYE') { + return new Buffer(x.toString() + '!') + } + return x + }), + conn + ) + }) + listener.listen(addrs[0], done) + }) + + afterEach((done) => { + listener.close(done) + }) + + it('simple', (done) => { + const s = goodbye({ + source: pull.values([new Buffer('hey')]), + sink: pull.collect((err, values) => { + expect(err).to.not.exist + expect( + values + ).to.be.eql( + [new Buffer('hey!')] + ) + done() + }) + }) + + pull(s, transport.dial(addrs[0]), s) + }) + + it('to non existent listener', (done) => { + pull( + transport.dial(addrs[1]), + pull.onEnd((err) => { + expect(err).to.exist + done() + }) + ) + }) + }) +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..e8173e2 --- /dev/null +++ b/src/index.js @@ -0,0 +1,12 @@ +/* eslint-env mocha */ +'use strict' + +const dial = require('./dial-test') +const listen = require('./listen-test') + +module.exports = (common) => { + describe('interface-transport', () => { + dial(common) + listen(common) + }) +} diff --git a/src/listen-test.js b/src/listen-test.js new file mode 100644 index 0000000..5398c59 --- /dev/null +++ b/src/listen-test.js @@ -0,0 +1,118 @@ +/* eslint max-nested-callbacks: ["error", 8] */ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect +const pull = require('pull-stream') + +module.exports = (common) => { + describe('listen', () => { + let addrs + let transport + + before((done) => { + common.setup((err, _transport, _addrs) => { + if (err) return done(err) + transport = _transport + addrs = _addrs + done() + }) + }) + + after((done) => { + common.teardown(done) + }) + + it('simple', (done) => { + const listener = transport.createListener((conn) => {}) + listener.listen(addrs[0], () => { + listener.close(done) + }) + }) + + it('close listener with connections, through timeout', (done) => { + const finish = plan(3, done) + const listener = transport.createListener((conn) => { + pull(conn, conn) + }) + + listener.listen(addrs[0], () => { + const socket1 = transport.dial(addrs[0], () => { + listener.close(finish) + }) + + pull( + transport.dial(addrs[0]), + pull.onEnd(() => { + finish() + }) + ) + + pull( + pull.values([Buffer('Some data that is never handled')]), + socket1, + pull.onEnd(() => { + finish() + }) + ) + }) + }) + + describe('events', () => { + // TODO: figure out why it fails in the full test suite + it.skip('connection', (done) => { + const finish = plan(2, done) + + const listener = transport.createListener() + + listener.on('connection', (conn) => { + expect(conn).to.exist + finish() + }) + + listener.listen(addrs[0], () => { + transport.dial(addrs[0], () => { + listener.close(finish) + }) + }) + }) + + it('listening', (done) => { + const listener = transport.createListener() + listener.on('listening', () => { + listener.close(done) + }) + listener.listen(addrs[0]) + }) + + // TODO: how to get the listener to emit an error? + it.skip('error', (done) => { + const listener = transport.createListener() + listener.on('error', (err) => { + expect(err).to.exist + listener.close(done) + }) + }) + + it('close', (done) => { + const finish = plan(2, done) + const listener = transport.createListener() + listener.on('close', finish) + + listener.listen(addrs[0], () => { + listener.close(finish) + }) + }) + }) + }) +} + +function plan (n, done) { + let i = 0 + return (err) => { + if (err) return done(err) + i++ + + if (i === n) done() + } +} diff --git a/tests/base-test.js b/tests/base-test.js deleted file mode 100644 index bd67f93..0000000 --- a/tests/base-test.js +++ /dev/null @@ -1,31 +0,0 @@ -var multiaddr = require('multiaddr') - -module.exports.all = function (test, common) { - test('a test', function (t) { - common.setup(test, function (err, transport) { - t.plan(5) - t.ifError(err) - - var maddr = multiaddr('/ip4/127.0.0.1/tcp/9050') - - var listener = transport.createListener(function (stream) { - t.pass('received incoming connection') - stream.end() - listener.close(function () { - t.pass('listener closed successfully') - t.end() - }) - }) - - listener.listen(maddr.nodeAddress().port, function () { - t.pass('started listening') - var stream = transport.dial(maddr, { - ready: function () { - t.pass('dialed successfuly') - stream.end() - } - }) - }) - }) - }) -} diff --git a/tests/index.js b/tests/index.js deleted file mode 100644 index b232406..0000000 --- a/tests/index.js +++ /dev/null @@ -1,6 +0,0 @@ -var timed = require('timed-tape') - -module.exports = function (test, common) { - test = timed(test) - require('./base-test.js').all(test, common) -}