Skip to content

Commit

Permalink
Merge branch 'release-0.4.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
apendua committed Mar 5, 2015
2 parents 15326c4 + c427437 commit 1326ae6
Show file tree
Hide file tree
Showing 25 changed files with 685 additions and 276 deletions.
1 change: 1 addition & 0 deletions .npm/package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions .npm/package/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This directory and the files immediately inside it are automatically generated
when you change this package's NPM dependencies. Commit the files in this
directory (npm-shrinkwrap.json, .gitignore, and this README) to source control
so that others run the same versions of sub-dependencies.

You should NOT check in the node_modules directory that Meteor automatically
creates; if you are using git, the .gitignore file tells git to ignore it.
23 changes: 23 additions & 0 deletions .npm/package/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .versions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
anti:[email protected].2
anti:[email protected].3
[email protected]
[email protected]
[email protected]
Expand Down
7 changes: 4 additions & 3 deletions bin/gagarin
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var program = require('commander');
var Gagarin = require('../lib/mocha/gagarin');
var path = require('path');
var fs = require('fs');
var coffee = require('coffee-script/register');

//TODO check why we can simply pass parseInt as argument ?

Expand Down Expand Up @@ -60,10 +61,10 @@ if (fs.lstatSync(pathToTests).isDirectory()) {
return addFiles(pathToFile);
}

if (path.extname(file) !== '.js') {
return;
var fileType = path.extname(file);
if (fileType === '.js' || fileType === '.coffee') {
return gagarin.addFile(pathToFile);
}
gagarin.addFile(pathToFile);
});
}
} else {
Expand Down
182 changes: 110 additions & 72 deletions lib/browser/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,18 @@ module.exports.execute = function (code, args) {
}

return self.__custom__(function (operand, done) {

var closure = operand.closure ? operand.closure() : {};

if (this.lastError) {
if (this.retryCount <= 1 && /chai not found/.test(this.lastError.message)) {
operand.browser.execute(fs.readFileSync('./node_modules/chai/chai.js', "utf8"), either(done).or(doAction));
} else {
done(this.lastError);
}
} else {
doAction();
}

function doAction() {
var closure = operand.closure ? operand.closure() : {};
operand.browser.execute("return (" + wrapSourceCode(codeToString(code), args, closure) + ").apply(null, arguments)",
values(closure), feedbackProcessor(operand.closure.bind(operand), done));

operand.browser.execute("return (" + wrapSourceCode(codeToString(code), args, closure) + ").apply(null, arguments)",
values(closure), feedbackProcessor(operand.closure.bind(operand), done));
}, function (err, retry) {
if (err.message.match(/chai not found/) && retry.count === 0) {
// retry after reparing the problem
return retry(function (operand, done) { loadChai(operand.browser, done) });
}

return true; // retry on first attempt
throw err;
});
};

Expand All @@ -66,6 +59,12 @@ module.exports.promise = function (code, args) {
throw new Error('`args` has to be an array');
}

// stringify arguments
args = args.map(stringify);

args.unshift("function ($) { setTimeout(function () { cb({ closure: closure(), error: ($ && typeof $ === 'object') ? $.message : $.toString() }) }) }");
args.unshift("function ($) { setTimeout(function () { cb({ closure: closure(), value: $ }) }) }");

// we could set this 5000 globally, right?

return self.__custom__(function (operand, done) {
Expand All @@ -81,18 +80,11 @@ module.exports.promise = function (code, args) {
return stringify(key) + ": " + key;
}).join(',');

// stringify arguments
args = args.map(stringify);

args.unshift("(function (cb) {\n return function ($) {\n setTimeout( function () { cb({ error : ($ && typeof $ === 'object') ? $.message : $.toString()," +
" closure: {" + keys + "}}); });\n };\n })(arguments[arguments.length-1])");

args.unshift("(function (cb) {\n return function ($) {\n setTimeout( function () { cb({ value : $, closure: {" +
keys + "}}); });\n };\n })(arguments[arguments.length-1])");

chunks.push(
"function (" + Object.keys(closure).join(', ') + ") {",
" 'use strict';",
" var expect;",
" var assert;",
" var either = function (first) {",
" return {",
" or: function (second) {",
Expand All @@ -102,25 +94,31 @@ module.exports.promise = function (code, args) {
" }",
" };",
" };",
" try {",
" (" + codeToString(code) + ")(",
" " + args.join(', ') + ");",
" } catch ($) {",
" arguments[arguments.length-1]({",
" error : $.message,",
" closure : { " + keys + " }",
" });",
" }",
" (function (action, closure, cb) {",
" try {",
" if (!window.chai) throw new Error('chai not found');",
" expect = window.chai.expect;",
" assert = window.chai.assert;",
" action(" + args.join(", ") + ");",
" } catch (err) {",
" cb({ error: err.message, closure: closure() });",
" }",
" })(" + codeToString(code) + ", function () {",
" return { " + keys + " };",
" }, arguments[arguments.length-1]);",
"}"
);

//console.log(chunks.join('\n'))

code = chunks.join('\n');

// TODO: how come "args" instead of values(closure) was passing as well?
operand.browser.executeAsync("(" + code + ").apply(null, arguments)",
// TODO: how come "args" instead of values(closure) was working fine as well?
operand.browser.executeAsync("(" + chunks.join('\n') + ").apply(null, arguments)",
values(closure), feedbackProcessor(operand.closure.bind(operand), done));

}, function (err, retry) {
if (err.message.match(/chai not found/) && retry.count === 0) {
// retry after reparing the problem
return retry(function (operand, done) { loadChai(operand.browser, done) });
}
throw err;
});

};
Expand Down Expand Up @@ -148,6 +146,8 @@ module.exports.wait = function (timeout, message, code, args) {
throw new Error('`args` has to be an array');
}

args = args.map(stringify);

var self = this;

return self.__custom__(function (operand, done) {
Expand All @@ -163,41 +163,54 @@ module.exports.wait = function (timeout, message, code, args) {
return stringify(key) + ": " + key;
}).join(',');

args = args.map(stringify);

chunks.push("function (" + Object.keys(closure).join(', ') + ") {");

chunks.push(
' "use strict";',
' var cb = arguments[arguments.length - 1];',
' var handle1 = null;',
' var handle2 = window.setTimeout(function () {',
' window.clearTimeout(handle1);',
' cb({ closure: {' + keys + '}, error: ' + JSON.stringify('I have been waiting for ' + timeout + ' ms ' + message + ', but it did not happen.') + ' });',
' }, ' + JSON.stringify(timeout) + ');',
' (function test() {',
' var value;',
' try {',
' value = (' + codeToString(code) + ')(' + args.join(', ') + ');',
' if (value) {',
"function (" + Object.keys(closure).join(', ') + ") {",
" 'use strict';",
" var expect;",
" var assert;",
" (function (action, closure, cb) {",

" if (!window.chai) return cb({ closure: closure(), error: 'chai not found' });",
" expect = window.chai.expect;",
" assert = window.chai.assert;",

' var handle1 = null;',
' var handle2 = window.setTimeout(function () {',
' window.clearTimeout(handle1);',
' cb({ closure: closure(), error: ' + JSON.stringify('I have been waiting for ' + timeout + ' ms ' + message + ', but it did not happen.') + ' });',
' }, ' + JSON.stringify(timeout) + ');',

' (function test() {',
' var value;',
' try {',
' value = action(' + args.join(', ') + ');',
' if (value) {',
' window.clearTimeout(handle2);',
' cb({ value: value, closure: closure() });',
' } else {',
' handle1 = window.setTimeout(test, 50);', // repeat after 1/20 sec.
' }',
' } catch (err) {',
' window.clearTimeout(handle2);',
' cb({ value: value, closure: {' + keys + '} });',
' } else {',
' handle1 = window.setTimeout(test, 50);', // repeat after 1/20 sec.
' cb({ error: err.message, closure: closure() });',
' }',
' } catch (err) {',
' window.clearTimeout(handle2);',
' cb({ error: err.message, closure: {' + keys + '} });',
' }',
' }());'
);

chunks.push('}');
' }());',

code = chunks.join('\n');
" })(" + codeToString(code) + ", function () {",
" return { " + keys + " };",
" }, arguments[arguments.length-1]);",
"}"
);

operand.browser.executeAsync("(" + code + ").apply(null, arguments)", values(closure),
operand.browser.executeAsync("(" + chunks.join('\n') + ").apply(null, arguments)", values(closure),
feedbackProcessor(operand.closure.bind(operand), done));

}, function (err, retry) {
if (err.message.match(/chai not found/) && retry.count === 0) {
// retry after reparing the problem
return retry(function (operand, done) { loadChai(operand.browser, done) });
}
throw err;
});

};
Expand Down Expand Up @@ -268,10 +281,13 @@ function wrapSourceCode(code, args, closure) {

chunks.push(
" 'use strict';",
// " var expect;",
" var expect, assert;",
" try {",
// " if (!window.chai) { throw new Error('chai not found'); }",
// " expect = window.chai.expect;",

// here is a good place for user-defined plugins if we ever need them

" if (!window.chai) { throw new Error('chai not found'); }",
" expect = window.chai.expect; assert = window.chai.assert;",
" return (function ($) {",
" return {",
" closure: {"
Expand Down Expand Up @@ -305,3 +321,25 @@ function wrapSourceCode(code, args, closure) {

return chunks.join('\n');
}

function loadChai (browser, done) {

var path = require('path');
var chai = fs.readFileSync(path.resolve(__dirname, '..', '..', 'node_modules', 'chai', 'chai.js'),'utf8');
var chaiThings = fs.readFileSync(path.resolve(__dirname, '..', '..', 'node_modules', 'chai-things', 'lib', 'chai-things.js'), 'utf8');

browser.execute(function (chaiSrc, chaiThingsSrc) {

var chaiScript = document.createElement('script');
chaiScript.text = chaiSrc;
window.document.head.appendChild(chaiScript);

var chaiThingsScript = document.createElement('script');
chaiThingsScript.text = chaiThingsSrc;
window.document.head.appendChild(chaiThingsScript);
chai.should(); // initialize chai things

}, [ chai, chaiThings ] , done);

}

17 changes: 11 additions & 6 deletions lib/meteor/build.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var MongoServerAsPromise = require('../mongo/mongoDBProcess');
var findAvailablePort = require('../tools').findAvailablePort;
var Promise = require('es6-promise').Promise;
var chalk = require('chalk');
var spawn = require('child_process').spawn;
Expand Down Expand Up @@ -63,14 +64,13 @@ function BuildPromise(options) {

var pathToMain = path.join(pathToApp, '.meteor', 'local', 'build', 'main.js');
var env = Object.create(process.env);
var port = 4000 + Math.floor(Math.random() * 1000);

var spawnMe = verbose ? pty.spawn : spawn;

// TODO: in the end, drop this database
env.MONGO_URL = mongoUrl + '/' + 'gagarin_build';

return new Promise(function (resolve, reject) {
return findAvailablePort().then(function (port) { return new Promise(function (resolve, reject) {

var meteor = spawnMe('meteor', [
'--production',
Expand All @@ -90,6 +90,10 @@ function BuildPromise(options) {

data.toString().split('\n').forEach(function (line) {

if (!line) {
return;
}

var hasMatch = [
{
regExp: /App running at:/,
Expand Down Expand Up @@ -123,9 +127,10 @@ function BuildPromise(options) {
//},

{
regExp: /Errors prevented startup:/,
regExp: /(Errors prevented startup:|\(STDERR\) .*Error:)/,
action: function () {
hasErrors = true;
linesOfMessage = [ line ];
//if (lastError) {
// reject(new Error(chalk.red(lastError) + chalk.magenta(' => ') + chalk.magenta(lastErrorAt)));
//} else {
Expand All @@ -135,10 +140,10 @@ function BuildPromise(options) {
},

{
regExp: /Your application has errors. Waiting for file change./,
regExp: /Your application .+\. Waiting for file change\./,
action: function () {
if (linesOfMessage.length) {
reject(new Error(chalk.red(linesOfMessage.join('\n'))));
reject(new Error(chalk.red(linesOfMessage.map(chalk.stripColor).join('\n'))));
} else {
reject(new Error('Your app does not compile, but I do not know the reason.'));
}
Expand Down Expand Up @@ -173,7 +178,7 @@ function BuildPromise(options) {
meteor.kill('SIGINT')
}, timeout);

}); // new Promise
}); });

function logMeteorOutput(data) {
if (!verbose) {
Expand Down
3 changes: 3 additions & 0 deletions lib/meteor/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ function callDDPMethod (ddpClient, name, args, closure, cb) {
if (err) {
return cb(err);
}
if (!feedback) {
return cb(new Error('no feedback provided'));
}
if (feedback.error) {
return cb(new Error(feedback.error));
}
Expand Down
Loading

0 comments on commit 1326ae6

Please sign in to comment.