Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pause/resume, refactored code, improved command line interface, etc #514

Closed
wants to merge 21 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Prev

added test cli interface

  • Loading branch information
whitef0x0 committed Dec 5, 2015
commit 8752393ce0be6217d9ac3e69da48f25e61c7761f
@@ -6,5 +6,6 @@ node_js:
sudo: false
env:
global:
- IS_TRAVIS='true'
- secure: AJsEWFnWC5W8hcF3hJzm3PT7heazJpKg85xiSvIWVzLHZU/s0h4+WfJ6t0F9v3L4awaowm62vy8CRaxRkB4lJyJg+JK2K0QN7lNFGj2f8Jx2cFlVJ1IyY959GY4iUg66JrNj1yzS02+yQfweDngyifqzb7IlxnowiveDjUO2gyo=
- secure: hvihwLUqlPchrGFXKWFF7iKRugISU7r/gLBo6O63nPeg0OwnYqYcC2BnBWoSiOdu9oR5bM4a5u0os04XL+bP3dqt324g0uBTqvyyxD6NhBsphVFkUmdUH3HMe7IQY6JTns96KT/6UkQapKhIuW4CUDeidR+5NFKvyRdKIjSawS4=
@@ -1,18 +1,107 @@
#!/usr/bin/env node

var spawn = require('cross-spawn-async')
var minimist = require('minimist')
var fs = require('fs')
var path = require('path')
var clivas = require('clivas')

var runBrowserTests = !process.env.TRAVIS_PULL_REQUEST ||
process.env.TRAVIS_PULL_REQUEST === 'false'
var argv = minimist(process.argv.slice(2), {
alias: {
l: 'local',
b: 'browser',
s: 'standard'
},
boolean: [ // options that are always boolean
'local',
'standard',
'browser',
'help'
]
})

var runBrowserTests = (!process.env.TRAVIS_PULL_REQUEST ||
process.env.TRAVIS_PULL_REQUEST === 'false') && argv.local

var command = argv._[0]
if (argv.help || command === 'help') {
runHelp()
} else if (command === 'coverage' || command === 'test') {
runTest(command)
} else {
clivas.line('webtorrent-test: \'' + command + '\' is not a webtorrent-test command. See \'webtorrent-test --help\'')
}

var node = spawn('npm', ['run', 'test-node'], { stdio: 'inherit' })
node.on('close', function (code) {
if (code === 0 && runBrowserTests) {
var browser = spawn('npm', ['run', 'test-browser'], { stdio: 'inherit' })
browser.on('close', function (code) {
process.exit(code)
function runHelp () {
fs.readFileSync(path.join(__dirname, 'ascii-logo.txt'), 'utf8')
.split('\n')
.forEach(function (line) {
clivas.line('{bold:' + line.substring(0, 20) + '}{red:' + line.substring(20) + '}')
})

console.log(function () {
/*
Usage:
webtorrent-test [command] <options>
Example:
webtorrent-test test --local --browser
Commands:
test Run the nodejs test suite
coverage Generate test coverage data (via istanbul)
Options:
-l, --local run test suite for local development
-b, --browser run browser test suite (along with existing test suite)
-b, --standard run js `standard` code linting tool before test suite
*/
}.toString().split(/\n/).slice(2, -2).join('\n'))
process.exit(0)
}

function runTest (testType) {
var testCommand = testType
var browserTestCommand = 'test'

if (argv.local) {
if (testType === 'coverage') {
testCommand += '-local'
} else {
testCommand += '-node'
}

if (argv.browser) {
browserTestCommand += '-browser-local'
}
} else {
process.exit(code)
browserTestCommand += '-browser'
testCommand += '-node'
}
})

if (argv.standard) {
var node = spawn('sh', ['-c', 'node_modules/.bin/standard'], { stdio: 'inherit' })
node.on('close', function (code) {
if (code === 0) {
executeTest(testCommand, browserTestCommand)
}
})
} else {
executeTest(testCommand, browserTestCommand)
}

function executeTest (_command, _browserCommand) {
var node = spawn('npm', ['run', _command], { stdio: 'inherit' })
node.on('close', function (code) {
if (code === 0 && runBrowserTests && argv.browser) {
var browser = spawn('npm', ['run', _browserCommand], { stdio: 'inherit' })
browser.on('close', function (code) {
process.exit(code)
})
} else {
process.exit(code)
}
})
}
}
@@ -232,20 +232,22 @@ WebTorrent.prototype.download = function (torrentId, opts, ontorrent) {
WebTorrent.prototype.pause = function (currentTorrent, cb) {
var self = this

if (currentTorrent instanceof Torrent) self.emit('error', new Error('input must be a valid torrent')
if (!(currentTorrent instanceof Torrent)) return self.emit('error', new Error('input for pause() must be a valid torrent'))
if (self.destroyed) return self.emit('error', new Error('client is destroyed'))

if (currentTorrent === null) return self.emit('error', new Error('torrent does not exist'))

currentTorrent.pause(cb)
}

WebTorrent.prototype.resume = function (currentTorrent, cb) {
WebTorrent.prototype.resume = function (currentTorrent) {
var self = this

if (!(currentTorrent instanceof Torrent)) return self.emit('error', new Error('input for resume() must be a valid torrent'))
if (self.destroyed) return self.emit('error', new Error('client is destroyed'))
if (currentTorrent === null) return self.emit('error', new Error('torrent does not exist'))

currentTorrent.resume(cb)
currentTorrent.resume()
}

/**
@@ -8,7 +8,8 @@
"url": "http://feross.org/"
},
"bin": {
"webtorrent": "./bin/cmd.js"
"webtorrent": "./bin/cmd.js",
"webtorrent-test": "./bin/test.js"
},
"browser": {
"./lib/server.js": false,
@@ -76,7 +77,6 @@
"browserify": "^12.0.1",
"finalhandler": "^0.4.0",
"http-server": "^0.8.5",
"istanbul-combine": "^0.3.0",
"istanbul-coveralls": "^1.0.3",
"jsdom": "^7.1.1",
"run-auto": "^1.0.0",
@@ -116,12 +116,11 @@
"build": "browserify -s WebTorrent -e ./ | uglifyjs -m > webtorrent.min.js",
"build-debug": "browserify -s WebTorrent -e ./ > webtorrent.debug.js",
"size": "npm run build && cat webtorrent.min.js | gzip | wc -c",
"test-local": "standard && node ./bin/test.js",
"coverage": "istanbul cover -- tape test/*.js && istanbul-coveralls",
"coverage-local": "istanbul cover -- tape test/*.js && http-server coverage/client/report-html/ -p 6000 -a 127.0.0.1 && echo 'Istanbul coverage report served at 127.0.0.1:6000'",
"test": "standard && node ./bin/test.js && coverage",
"test-browser": "zuul -- test/browser/*.js",
"test-browser-local": "zuul --local 4000 -- test/browser/*.js",
"test-node": "tape test/*.js"
"coverage-node": "istanbul cover -- tape test/*.js && istanbul-coveralls",
"coverage-local": "istanbul cover -- tape test/*.js && http-server coverage/client/report-html/ -p 3000 -q && echo 'Istanbul coverage report served at 127.0.0.1:3000'",
"test-browser": "tape test/*.js && zuul -- test/browser/*.js",
"test-browser-local": "tape test/*.js && zuul --local 4000 -- test/browser/*.js",
"test-node": "tape test/*.js",
"test": "bin/test.js coverage --standard"
}
}
@@ -6,7 +6,7 @@ var WebTorrent = require('../')

var leaves = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
var leavesTorrent = parseTorrent(leaves)
var leavesBook = fs.readFileSync(__dirname + 'content/Leaves of Grass by Walt Whitman.epub')
var leavesBook = fs.readFileSync(__dirname + '/content/Leaves of Grass by Walt Whitman.epub')

var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80'

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.