Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 7ff6c5223aaead95002fa1444821eaa74903ef7c @ForbesLindesay committed Dec 30, 2013
Showing with 214 additions and 0 deletions.
  1. +13 −0 .gitignore
  2. +19 −0 LICENSE
  3. +19 −0 README.md
  4. +42 −0 bin/cli.js
  5. +47 −0 index.js
  6. +39 −0 lib/override-log.js
  7. +13 −0 lib/test-page.html
  8. +22 −0 package.json
@@ -0,0 +1,13 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+pids
+logs
+results
+npm-debug.log
+node_modules
@@ -0,0 +1,19 @@
+Copyright (c) 2013 Forbes Lindesay
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,19 @@
+# run-browser
+
+The simplest way to run testling type tests in the browser
+
+[![Dependency Status](https://gemnasium.com/ForbesLindesay/run-browser.png)](https://gemnasium.com/ForbesLindesay/run-browser)
+[![NPM version](https://badge.fury.io/js/run-browser.png)](http://badge.fury.io/js/run-browser)
+
+## Installation
+
+ npm install run-browser -g
+
+
+## Usage
+
+ run-browser tests/test.js
+
+## License
+
+ MIT
@@ -0,0 +1,42 @@
+#!/usr/bin/env node
+
+'use strict'
+
+var path = require('path');
+var runbrowser = require('../');
+
+var args = process.argv.slice(2);
+
+var filename;
+var port = 3000;
+var help = false;
+
+if (args.length === 1 && args[0] !== '--help' && args[0] !== '-h') {
+ filename = args[0];
+} else if (args.length === 3) {
+ if (args[0] === '-p') {
+ port = args[1];
+ filename = args[2];
+ } else if (args[1] === '-p') {
+ port = args[2];
+ filename = args[0];
+ } else {
+ help = true;
+ }
+} else {
+ help = true;
+}
+
+if (help) {
+ console.log('Usage:');
+ console.log();
+ console.log(' run-browser test-file.js');
+ console.log();
+ console.log(' run-browser test-file.js -p 3000');
+ console.log();
+ process.exit(args.length === 1 ? 0 : 1);
+}
+
+var server = runbrowser(path.resolve(filename));
+server.listen(port);
+console.log('Open a browser and navigate to "http://localhost:' + port + '"');
@@ -0,0 +1,47 @@
+'use strict';
+
+var http = require('http');
+var fs = require('fs');
+var browserify = require('browserify');
+
+var html = fs.readFileSync(__dirname + '/lib/test-page.html', 'utf8');
+var overrideLog = fs.readFileSync(__dirname + '/lib/override-log.js', 'utf8');
+
+module.exports = createServer;
+function createServer(filename) {
+ return http.createServer(function (req, res) {
+ if (req.url === '/') {
+ res.setHeader('Content-Type', 'text/html');
+ return res.end(html);
+ }
+ if (req.url === '/logging.js') {
+ res.setHeader('Content-Type', 'application/javascript');
+ return res.end(overrideLog);
+ }
+ var match;
+ if (match = /^\/tests.js$/.test(req.url)) {
+ var sent = false;
+ return fs.stat(filename, function (err) {
+ if (err) {
+ res.setHeader('Content-Type', 'application/javascript');
+ res.end('console.error(' + JSON.stringify(err.toString()) + ');');
+ console.error(err.stack || err.message || err);
+ return;
+ }
+ return browserify(filename).bundle({debug: true}, function (err, src) {
+ if (sent) return;
+ sent = true;
+ res.setHeader('Content-Type', 'application/javascript');
+ if (err) {
+ res.end('console.error(' + JSON.stringify(err.toString()) + ');');
+ console.error(err.stack || err.message || err);
+ } else {
+ res.end(src);
+ }
+ });
+ });
+ }
+ res.statusCode = 404;
+ res.end('404: Path not found');
+ });
+}
@@ -0,0 +1,39 @@
+;(function overrideLog() {
+ 'use strict';
+ function proxy(original, color) {
+ return function (msg) {
+ var index = 1;
+ var args = arguments;
+
+ if (typeof msg === 'string') {
+ msg = msg.replace(/(^|[^%])%[sd]/g, function (_, s) {
+ return s + args[index++];
+ });
+ }
+ else msg = inspect(msg);
+
+ for (var i = index; i < args.length; i++) {
+ msg += ' ' + inspect(args[i]);
+ }
+
+ var elem = document.getElementById('__testling_output');
+ if (elem) {
+ var span = document.createElement('span');
+ var txt = document.createTextNode(msg + '\n');
+ span.setAttribute('style', 'color: ' + color);
+ span.appendChild(txt);
+ elem.appendChild(span);
+ }
+
+ if (typeof original === 'function') {
+ return original.apply(this, arguments);
+ }
+ else if (original) return original(arguments[0]);
+ }
+ }
+
+ if (typeof console === 'undefined') window.console = {};
+ console.log = proxy(console.log, 'black');
+ console.warn = proxy(console.warn, 'orange');
+ console.error = proxy(console.error, 'red');
+}());
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
+ <title>Tests</title>
+ </head>
+ <body>
+ <pre id="__testling_output"></pre>
+ <script src="/logging.js"></script>
+ <script src="/tests.js"></script>
+ </body>
+</html>
@@ -0,0 +1,22 @@
+{
+ "name": "run-browser",
+ "version": "0.0.0",
+ "description": "The simplest way to run testling type tests in the browser",
+ "bin": "./bin/cli.js",
+ "keywords": [],
+ "dependencies": {
+ "browserify": "~3.18.0"
+ },
+ "devDependencies": {
+ "linify": "*"
+ },
+ "scripts": {
+ "prepublish": "linify transform ./bin"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/ForbesLindesay/run-browser.git"
+ },
+ "author": "ForbesLindesay",
+ "license": "MIT"
+}

0 comments on commit 7ff6c52

Please sign in to comment.