UNPKG

@mihon/spacejam

Version:

Run your meteor package tinytests and mocha tests from the command line with phantomjs.

260 lines (228 loc) 8.8 kB
// Generated by CoffeeScript 1.8.0 (function() { var ChildProcess, EventEmitter, Meteor, MeteorMongodb, expect, fs, glob, path, _, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; require('./log'); expect = require('chai').expect; _ = require("underscore"); ChildProcess = require('./ChildProcess'); EventEmitter = require('events').EventEmitter; MeteorMongodb = require("./MeteorMongodb"); glob = require("glob"); fs = require("fs"); path = require("path"); Meteor = (function(_super) { __extends(Meteor, _super); function Meteor() { this.hasReadyText = __bind(this.hasReadyText, this); this.hasErrorText = __bind(this.hasErrorText, this); this.hasStartedMongoDBText = __bind(this.hasStartedMongoDBText, this); this.runTestCommand = __bind(this.runTestCommand, this); return Meteor.__super__.constructor.apply(this, arguments); } Meteor.prototype.childProcess = null; Meteor.prototype.buffer = { stdout: "", stderr: "" }; Meteor.prototype.options = null; Meteor.prototype.mongodb = null; Meteor.prototype.defaultOptions = function() { return { "dir": ".", "port": 4096, "packages": [], "driver-package": "test-in-console", "meteor-ready-text": "=> App running at:", "meteor-error-text": "Waiting for file change." }; }; Meteor.getPackageVersion = function() { var _ref; log.debug("Meteor.getPackageVersion()"); if (!fs.existsSync('package.js')) { throw new Error("Missing package.js in current working directory."); } require('./PackageJSStubs'); require("" + (process.cwd()) + "/package.js"); expect((_ref = Package.description) != null ? _ref.version : void 0).to.be.a('string').that.is.ok; return Package.description.version; }; Meteor.prototype.getTestArgs = function(command, options) { var args, packagesToTest; log.debug("Meteor.getTestArgs()", options); expect(+options.port, "options.port is not a number.").to.be.ok; expect(options.packages, "options.packages is not an array of package names").to.be.an('array'); args = [command, '--driver-package', options['driver-package']]; if (options.release) { args.push(["--release", options.release]); } args.push(["--port", options.port]); if (options.settings) { args.push(["--settings", options.settings]); } if (options.production) { args.push("--production"); } if (options.mocha != null) { options["driver-package"] = "practicalmeteor:mocha-console-runner"; } if (options["root-url"] == null) { options["root-url"] = "http://localhost:" + options.port + "/"; } if (command === 'test') { if (options["test-app-path"]) { args.push(['--test-app-path'], options["test-app-path"]); } if (options["full-app"]) { args.push(['--full-app']); } } else if (command === 'test-packages') { if (options["test-app-path"]) { args.push(['--test-app-path'], options["test-app-path"]); } if (options.packages.length > 0) { packagesToTest = this._globPackages(options.packages); expect(packagesToTest).to.have.length.above(0); args.push(packagesToTest); } } return _.flatten(args); }; Meteor.prototype.testPackages = function(options) { if (options == null) { options = {}; } log.debug("Meteor.testPackages()", arguments); return this.runTestCommand("test-packages", options); }; Meteor.prototype.testApp = function(options) { if (options == null) { options = {}; } log.debug("Meteor.testApp()", arguments); return this.runTestCommand("test", options); }; Meteor.prototype.runTestCommand = function(command, options) { var args, cwd, env; if (options == null) { options = {}; } log.debug("Meteor.runTestCommand()", arguments); expect(options, "options should be an object.").to.be.an("object"); expect(this.childProcess, "Meteor's child process is already running").to.be["null"]; this.options = _.extend(this.defaultOptions(), options); log.debug('meteor options:', this.options); cwd = path.resolve(this.options.dir); log.debug("meteor cwd=" + cwd); expect(this.options['driver-package'], "options.driver-package is missing").to.be.ok; args = this.getTestArgs(command, this.options); log.debug('meteor args=', args); env = _.clone(process.env); env.METEOR_TEST_PACKAGES = '1'; env.ROOT_URL = this.options["root-url"]; if (this.options["mongo-url"]) { env.MONGO_URL = this.options["mongo-url"]; } else { if (env.MONGO_URL != null) { delete env.MONGO_URL; } } options = { cwd: cwd, env: env, detached: false }; this.childProcess = new ChildProcess(); this.childProcess.spawn("meteor", args, options); this.childProcess.child.on("exit", (function(_this) { return function(code, signal) { return _this.emit("exit", code, signal); }; })(this)); this.childProcess.child.stdout.on("data", (function(_this) { return function(data) { _this.buffer.stdout += data; _this.hasStartedMongoDBText(data); _this.hasErrorText(data); return _this.hasReadyText(data); }; })(this)); return this.childProcess.child.stderr.on("data", (function(_this) { return function(data) { _this.buffer.stderr += data; return _this.hasErrorText(data); }; })(this)); }; Meteor.prototype._globPackages = function(packages) { var globOpts, matchedPackages, pkgsFolder; log.debug("Meteor._globPackages()", arguments); expect(packages, "@packages should be and array").to.be.an("array"); pkgsFolder = process.cwd() + '/packages'; globOpts = { cwd: pkgsFolder }; matchedPackages = []; packages.forEach((function(_this) { return function(pkgArg) { var globedPackages; if (pkgArg.indexOf(':') > 0) { return matchedPackages.push(pkgArg); } else if (pkgArg.indexOf('/') >= 0) { return matchedPackages.push(pkgArg); } else { globedPackages = glob.sync(pkgArg, globOpts); if (globedPackages.length > 0) { return globedPackages.forEach(function(pkg) { return matchedPackages.push(pkg); }); } else { log.warn("spacjam: Warning: No packages matching " + pkgArg + " have been found. Will add it to the meteor command line anyway, in case it's in PACKAGE_DIRS."); return matchedPackages.push(pkgArg); } } }; })(this)); return matchedPackages; }; Meteor.prototype.hasStartedMongoDBText = function(buffer) { if (buffer.lastIndexOf('Started MongoDB') !== -1) { this.mongodb = new MeteorMongodb(this.childProcess.child.pid); return this.emit("mongodb ready"); } }; Meteor.prototype.hasErrorText = function(buffer) { if (buffer.lastIndexOf(this.defaultOptions()["meteor-error-text"]) !== -1) { return this.emit("error"); } }; Meteor.prototype.hasReadyText = function(buffer) { if (buffer.lastIndexOf(this.defaultOptions()["meteor-ready-text"]) !== -1) { return this.emit("ready"); } }; Meteor.prototype.hasMongodb = function() { log.debug("Meteor.hasMongodb()"); if (this.mongodb) { return this.mongodb.hasMongodb(); } return false; }; Meteor.prototype.kill = function(signal) { var _ref, _ref1; if (signal == null) { signal = "SIGTERM"; } log.debug("Meteor.kill()", arguments, "@childProcess?=", this.childProcess != null, "@mongodb?=", this.mongodb != null); if ((_ref = this.childProcess) != null) { _ref.kill(signal); } return (_ref1 = this.mongodb) != null ? _ref1.kill() : void 0; }; return Meteor; })(EventEmitter); module.exports = Meteor; }).call(this);