Skip to content

Commit

Permalink
list remote browsers in a new cli
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed May 16, 2012
1 parent cdf7efb commit 7b81b93
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 107 deletions.
136 changes: 32 additions & 104 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,42 @@
#!/usr/bin/env node
var http = require('http');
var request = require('request');
var archy = require('archy');
var argv = require('optimist').argv;

var testling = require('testling'); // sets up the test output

var runner = require('../lib/run');
var testFiles = require('../lib/test_files');
var streamFiles = require('../lib/stream_files');
var withConfig = require('../lib/account/with_config');

var parse = require('optimist')
.usage('Usage: testling [test files] {OPTIONS}')
.option('browsers', {
alias : 'b',
desc : 'Run your tests remotely in real browsers on testling.com.'
})
.option('output', {
alias : 'o',
desc : 'The output format to use in remote mode.\r\n'
+ ' http://testling.com/docs/#output-parameter\r\n'
})
.option('noinstrument', {
desc : 'Turn off instrumentation for particular files in remote mode.\r\n'
+ ' http://testling.com/docs/#noinstrument\r\n'
})
.option('main', {
desc : 'Use a filename besides "test.js" to start multifile bundles.\r\n'
+ ' http://testling.com/docs/#main\r\n'
})
.option('browserlist', {
alias : 'l',
desc : 'Show the available browsers on testling.com.'
})
.option('config', {
default : process.env.HOME + '/.config/testling.json',
desc : 'Read configuration information from this file.'
})
;
var argv = parse.argv;

if (argv.browserlist) {
if (argv['list-browsers']) {
var opts = {
host : 'testling.com',
path : '/browsers.json'
uri : 'http://testling.com/browsers.json',
json : true,
};
http.get(opts, function (res) {
var body = '';
res.on('data', function (buf) {
body += buf.toString();
});
request(opts, function (err, res, browsers) {
var remotes = browsers.reduce(function (acc, name) {
var s = name.split('/');
var b = s[0], v = s[1];

if (!acc[b]) acc[b] = [];
acc[b].push(v);

return acc;
}, {});

res.on('end', function () {
if (res.statusCode !== 200) console.error(err)
else {
var xs = JSON.parse(body);
var browsers = xs.reduce(function (acc, x) {
var s = x.split('/');
var b = s[0], v = s[1];
acc[b] = (acc[b] || []).concat(v);
return acc;
}, {});

Object.keys(browsers).forEach(function (name) {
console.log(name);
var vs = browsers[name].sort();
console.log(' [ ' + vs.join(', ') + ' ]');
});
}
var remoteNodes = Object.keys(remotes).map(function (key) {
var bs = remotes[key].sort(function (a,b) {
if (isNaN(Number(a))) return 1;
if (isNaN(Number(b))) return -1;
return Number(a) - Number(b);
});
return { label : key, nodes : bs };
});
});
}
else if (argv.browsers) {
withConfig(argv.config, function (err, config) {
if (err) {
console.error('\r\nError: ' + err);
return;
}

var auth = 'basic ' + new Buffer(
[ config.email, config.password ].join(':')
).toString('base64');

var params = [ 'output', 'browsers', 'noinstrument', 'main' ]
.reduce(function (acc, key) {
if (typeof argv[key] === 'string') {
acc[key] = argv[key];
}
return acc;
}, {})
;
console.log(archy({
label : 'remote browsers',
nodes : remoteNodes
}));

var query = Object.keys(params).map(function (key) {
return escape(key) + '=' + escape(params[key]);
}).join('&');
var localNodes = [];

var opts = {
method : 'PUT',
host : 'testling.com',
port : 80,
path : '/?' + query,
headers : { authorization : auth },
};
var req = http.request(opts, function (res) {
res.pipe(process.stdout);
});
streamFiles(testFiles(argv._)).pipe(req);
console.log(archy({
label : 'local browsers',
nodes : localNodes
}));
});
}
else if (argv._.length) {
runner(testFiles(argv._));
}
else {
parse.showHelp();
}
114 changes: 114 additions & 0 deletions oldness/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env node
var http = require('http');

var testling = require('testling'); // sets up the test output

var runner = require('../lib/run');
var testFiles = require('../lib/test_files');
var streamFiles = require('../lib/stream_files');
var withConfig = require('../lib/account/with_config');

var parse = require('optimist')
.usage('Usage: testling [test files] {OPTIONS}')
.option('browsers', {
alias : 'b',
desc : 'Run your tests remotely in real browsers on testling.com.'
})
.option('output', {
alias : 'o',
desc : 'The output format to use in remote mode.\r\n'
+ ' http://testling.com/docs/#output-parameter\r\n'
})
.option('noinstrument', {
desc : 'Turn off instrumentation for particular files in remote mode.\r\n'
+ ' http://testling.com/docs/#noinstrument\r\n'
})
.option('main', {
desc : 'Use a filename besides "test.js" to start multifile bundles.\r\n'
+ ' http://testling.com/docs/#main\r\n'
})
.option('browserlist', {
alias : 'l',
desc : 'Show the available browsers on testling.com.'
})
.option('config', {
default : process.env.HOME + '/.config/testling.json',
desc : 'Read configuration information from this file.'
})
;
var argv = parse.argv;

if (argv.browserlist) {
var opts = {
host : 'testling.com',
path : '/browsers.json'
};
http.get(opts, function (res) {
var body = '';
res.on('data', function (buf) {
body += buf.toString();
});

res.on('end', function () {
if (res.statusCode !== 200) console.error(err)
else {
var xs = JSON.parse(body);
var browsers = xs.reduce(function (acc, x) {
var s = x.split('/');
var b = s[0], v = s[1];
acc[b] = (acc[b] || []).concat(v);
return acc;
}, {});

Object.keys(browsers).forEach(function (name) {
console.log(name);
var vs = browsers[name].sort();
console.log(' [ ' + vs.join(', ') + ' ]');
});
}
});
});
}
else if (argv.browsers) {
withConfig(argv.config, function (err, config) {
if (err) {
console.error('\r\nError: ' + err);
return;
}

var auth = 'basic ' + new Buffer(
[ config.email, config.password ].join(':')
).toString('base64');

var params = [ 'output', 'browsers', 'noinstrument', 'main' ]
.reduce(function (acc, key) {
if (typeof argv[key] === 'string') {
acc[key] = argv[key];
}
return acc;
}, {})
;

var query = Object.keys(params).map(function (key) {
return escape(key) + '=' + escape(params[key]);
}).join('&');

var opts = {
method : 'PUT',
host : 'testling.com',
port : 80,
path : '/?' + query,
headers : { authorization : auth },
};
var req = http.request(opts, function (res) {
res.pipe(process.stdout);
});
streamFiles(testFiles(argv._)).pipe(req);
});
}
else if (argv._.length) {
runner(testFiles(argv._));
}
else {
parse.showHelp();
}
File renamed without changes.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@
"mkdirp" : "0.0.x",
"pw" : "0.0.x",
"request" : "2.1.x",
"optimist" : "0.2.x",
"jsdom" : "https://github.com/substack/jsdom/tarball/master"
"optimist" : "0.2.x"
},
"devDependencies" : {
"tap" : "0.0.x"
},
"engines" : {
"node" : ">=0.4.0"
"node" : "*"
},
"license" : "MIT/X11",
"author" : {
Expand Down

0 comments on commit 7b81b93

Please sign in to comment.