You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by cs...@apache.org on 2013/10/23 01:57:54 UTC

[1/2] git commit: CB-5125: replace child process exec with spawn

Updated Branches:
  refs/heads/master 51306462e -> dedc29a81


CB-5125: replace child process exec with spawn


Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/01c7ecec
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/01c7ecec
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/01c7ecec

Branch: refs/heads/master
Commit: 01c7ecec7ccf4a3c1423ddf3844e125d24965025
Parents: 5130646
Author: Carlos Santana <cs...@gmail.com>
Authored: Mon Oct 21 14:30:27 2013 -0400
Committer: Carlos Santana <cs...@gmail.com>
Committed: Mon Oct 21 23:11:22 2013 -0400

----------------------------------------------------------------------
 src/compile.js | 45 ++++++++++++++++++++++++++++++++++-----------
 src/emulate.js | 50 +++++++++++++++++++++++++++++++++++++-------------
 src/run.js     | 50 +++++++++++++++++++++++++++++++++++++-------------
 3 files changed, 108 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/01c7ecec/src/compile.js
----------------------------------------------------------------------
diff --git a/src/compile.js b/src/compile.js
index 34c24fe..97f7f21 100644
--- a/src/compile.js
+++ b/src/compile.js
@@ -16,6 +16,10 @@
     specific language governing permissions and limitations
     under the License.
 */
+
+/*global require: true, module: true, process: true*/
+/*jslint sloppy: true, white: true, newcap: true */
+
 var cordova_util      = require('./util'),
     path              = require('path'),
     child_process     = require('child_process'),
@@ -25,25 +29,44 @@ var cordova_util      = require('./util'),
 
 // Returns a promise.
 function shell_out_to_build(projectRoot, platform, options) {
-    var cmd = '"' + path.join(projectRoot, 'platforms', platform, 'cordova', 'build') + (options.length ? '" ' + options.join(" ") : '"');
-    events.emit('log', 'Compiling ' + platform + ' project.');
-    var d = Q.defer();
-    child_process.exec(cmd, function(err, stdout, stderr) {
-        events.emit('verbose', stdout);
-        events.emit('verbose', stderr);
-        if (err) {
-            d.reject(new Error('An error occurred while building the ' + platform + ' project. ' + stderr));
-        } else {
+    var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'build'),
+        args = options.length ? options : [],
+        d = Q.defer(),
+        errors = "",
+        child;
+
+    events.emit('log', 'Compiling app on platform "' + platform + '" via command "' + cmd + '" ' + args.join(" "));
+
+    //CB-5125 using spawn instead of exec to avoid errors with stdout on maxBuffer
+    child = child_process.spawn(cmd, args);
+    child.stdout.setEncoding('utf8');
+    child.stdout.on('data', function (data) {
+        events.emit('verbose', data);
+    });
+
+    child.stderr.setEncoding('utf8');
+    child.stderr.on('data', function (data) {
+        events.emit('verbose', data);
+        errors = errors + data;
+    });
+
+    child.on('close', function (code) {
+        events.emit('verbose', "child_process.spawn(" + cmd + "," + args.join(" ") + ") = " + code);
+        if (code === 0) {
             events.emit('log', 'Platform "' + platform + '" compiled successfully.');
             d.resolve();
+        } else {
+            d.reject(new Error('An error occurred while building the ' + platform + ' project.' + errors));
         }
     });
+
     return d.promise;
 }
 
 // Returns a promise.
 module.exports = function compile(options) {
-    var projectRoot = cordova_util.isCordova(process.cwd());
+    var projectRoot = cordova_util.isCordova(process.cwd()),
+        hooks;
 
     if (!options) {
         options = {
@@ -58,7 +81,7 @@ module.exports = function compile(options) {
         return Q.reject(options);
     }
 
-    var hooks = new hooker(projectRoot);
+    hooks = new hooker(projectRoot);
     return hooks.fire('before_compile', options)
     .then(function() {
         // Iterate over each added platform

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/01c7ecec/src/emulate.js
----------------------------------------------------------------------
diff --git a/src/emulate.js b/src/emulate.js
index cd4c038..808812f 100644
--- a/src/emulate.js
+++ b/src/emulate.js
@@ -16,6 +16,10 @@
     specific language governing permissions and limitations
     under the License.
 */
+
+/*global require: true, module: true, process: true*/
+/*jslint sloppy: true, white: true, newcap: true */
+
 var cordova_util      = require('./util'),
     path              = require('path'),
     child_process     = require('child_process'),
@@ -25,26 +29,46 @@ var cordova_util      = require('./util'),
     DEFAULT_OPTIONS   = ["--emulator"];
 
 // Returns a promise.
-function shell_out_to_emulate(root, platform, options) {
-    options = options.length ? DEFAULT_OPTIONS.concat(options) : DEFAULT_OPTIONS;
-    var cmd = '"' + path.join(root, 'platforms', platform, 'cordova', 'run') + '" ' + options.join(" ");
-    events.emit('log', 'Running on emulator for platform "' + platform + '" via command "' + cmd + '"');
-    var d = Q.defer();
-    child_process.exec(cmd, function(err, stdout, stderr) {
-        events.emit('verbose', stdout + stderr);
-        if (err) {
-            d.reject(new Error('An error occurred while emulating/deploying the ' + platform + ' project.' + stdout + stderr));
-        } else {
+function shell_out_to_emulate(projectRoot, platform, options) {
+    var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'run'),
+        args = options.length ? DEFAULT_OPTIONS.concat(options) : DEFAULT_OPTIONS,
+        d = Q.defer(),
+        errors = "",
+        child;
+
+    events.emit('log', 'Running on emulator for platform "' + platform + '" via command "' + cmd + '" ' + args.join(" "));
+
+    //CB-5125 using spawn instead of exec to avoid errors with stdout on maxBuffer
+    child = child_process.spawn(cmd, args);
+
+    child.stdout.setEncoding('utf8');
+    child.stdout.on('data', function (data) {
+        events.emit('verbose', data);
+    });
+
+    child.stderr.setEncoding('utf8');
+    child.stderr.on('data', function (data) {
+        events.emit('verbose', data);
+        errors = errors + data;
+    });
+
+    child.on('close', function (code) {
+        events.emit('verbose', "child_process.spawn(" + cmd + "," + args.join(" ") + ") = " + code);
+        if (code === 0) {
             events.emit('log', 'Platform "' + platform + '" deployed to emulator.');
             d.resolve();
+        } else {
+            d.reject(new Error('An error occurred while emulating/deploying the ' + platform + ' project.' + errors));
         }
     });
+
     return d.promise;
 }
 
 // Returns a promise.
-module.exports = function emulate (options) {
-    var projectRoot = cordova_util.isCordova(process.cwd());
+module.exports = function emulate(options) {
+    var projectRoot = cordova_util.isCordova(process.cwd()),
+        hooks;
 
     if (!options) {
         options = {
@@ -59,7 +83,7 @@ module.exports = function emulate (options) {
         return Q.reject(options);
     }
 
-    var hooks = new hooker(projectRoot);
+    hooks = new hooker(projectRoot);
     return hooks.fire('before_emulate', options)
     .then(function() {
         // Run a prepare first!

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/01c7ecec/src/run.js
----------------------------------------------------------------------
diff --git a/src/run.js b/src/run.js
index dddc1fc..d5487b3 100644
--- a/src/run.js
+++ b/src/run.js
@@ -16,6 +16,10 @@
     specific language governing permissions and limitations
     under the License.
 */
+
+/*global require: true, module: true, process: true*/
+/*jslint sloppy: true, white: true, newcap: true */
+
 var cordova_util      = require('./util'),
     path              = require('path'),
     hooker            = require('./hooker'),
@@ -26,10 +30,16 @@ var cordova_util      = require('./util'),
 
 // Returns a promise.
 function shell_out_to_run(projectRoot, platform, options) {
-    options = options.length ? DEFAULT_OPTIONS.concat(options) : DEFAULT_OPTIONS;
-    var cmd = '"' + path.join(projectRoot, 'platforms', platform, 'cordova', 'run') + '" ' + options.join(" ");
+    var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'run'),
+        args = options.length ? DEFAULT_OPTIONS.concat(options) : DEFAULT_OPTIONS,
+        d = Q.defer(),
+        errors = "",
+        child;
+
+    events.emit('log', 'Running app on platform "' + platform + '" via command "' + cmd + '" ' + args.join(" "));
+
     // TODO: inconsistent API for BB10 run command
-/*    if (platform == 'blackberry') {
+/*  if (platform == 'blackberry') {
         var bb_project = path.join(projectRoot, 'platforms', 'blackberry')
         var project = new platforms.blackberry.parser(bb_project);
         if (project.has_device_target()) {
@@ -43,24 +53,38 @@ function shell_out_to_run(projectRoot, platform, options) {
         }
     }
 */
-    events.emit('log', 'Running app on ' + platform);
-    events.emit('verbose', 'Run command: "' + cmd + '" (output to follow)');
-    var d = Q.defer();
-    child_process.exec(cmd, function(err, output, stderr) {
-        events.emit('verbose', output);
-        if (err) {
-            d.reject(new Error('An error occurred while running the ' + platform + ' project. ' + output + stderr));
-        } else {
+
+    //CB-5125 using spawn instead of exec to avoid errors with stdout on maxBuffer
+    child = child_process.spawn(cmd, args);
+
+    child.stdout.setEncoding('utf8');
+    child.stdout.on('data', function (data) {
+        events.emit('verbose', data);
+    });
+
+    child.stderr.setEncoding('utf8');
+    child.stderr.on('data', function (data) {
+        events.emit('verbose', data);
+        errors = errors + data;
+    });
+
+    child.on('close', function (code) {
+        events.emit('verbose', "child_process.spawn(" + cmd + "," + args.join(" ") + ") = " + code);
+        if (code === 0) {
             events.emit('log', 'Platform "' + platform + '" ran successfully.');
             d.resolve();
+        } else {
+            d.reject(new Error('n error occurred while running the ' + platform + ' project.' + errors));
         }
     });
+
     return d.promise;
 }
 
 // Returns a promise.
 module.exports = function run(options) {
-    var projectRoot = cordova_util.isCordova(process.cwd());
+    var projectRoot = cordova_util.isCordova(process.cwd()),
+        hooks;
 
     if (!options) {
         options = {
@@ -75,7 +99,7 @@ module.exports = function run(options) {
         return Q.reject(options);
     }
 
-    var hooks = new hooker(projectRoot);
+    hooks = new hooker(projectRoot);
     return hooks.fire('before_run', options)
     .then(function() {
         // Run a prepare first, then shell out to run


[2/2] git commit: CB-5125 add tests for chil process spawn

Posted by cs...@apache.org.
CB-5125 add tests for chil process spawn


Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/dedc29a8
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/dedc29a8
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/dedc29a8

Branch: refs/heads/master
Commit: dedc29a81b1bc57ce25d88bbad51478d0d1091c1
Parents: 01c7ece
Author: Carlos Santana <cs...@gmail.com>
Authored: Mon Oct 21 23:14:36 2013 -0400
Committer: Carlos Santana <cs...@gmail.com>
Committed: Mon Oct 21 23:14:36 2013 -0400

----------------------------------------------------------------------
 spec/compile.spec.js | 29 +++++++++++++++++++++--------
 spec/emulate.spec.js | 28 ++++++++++++++++++++--------
 spec/run.spec.js     | 30 ++++++++++++++++++++++--------
 3 files changed, 63 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/dedc29a8/spec/compile.spec.js
----------------------------------------------------------------------
diff --git a/spec/compile.spec.js b/spec/compile.spec.js
index 3b0fe0d..4b9ee43 100644
--- a/spec/compile.spec.js
+++ b/spec/compile.spec.js
@@ -28,8 +28,23 @@ var cordova = require('../cordova'),
 var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www'; });
 
 describe('compile command', function() {
-    var is_cordova, list_platforms, fire, exec, result;
+    var is_cordova, list_platforms, fire, result, child;
     var project_dir = '/some/path';
+    child = {
+        on: function(child_event,cb){
+            if(child_event === 'close'){
+                cb(0);
+            }
+        },
+        stdout: {
+            setEncoding: function(){},
+            on: function(){}
+        },
+        stderr: {
+            setEncoding: function(){},
+            on: function(){}
+        }
+    };
 
     function wrapper(f, post) {
         runs(function() {
@@ -42,10 +57,7 @@ describe('compile command', function() {
         is_cordova = spyOn(util, 'isCordova').andReturn(project_dir);
         list_platforms = spyOn(util, 'listPlatforms').andReturn(supported_platforms);
         fire = spyOn(hooker.prototype, 'fire').andReturn(Q());
-        exec = spyOn(child_process, 'exec').andCallFake(function(cmd, opts, cb) {
-            if (!cb) cb = opts;
-            cb(null, '', '');
-        });
+        spyOn(child_process, 'spawn').andReturn(child);
     });
     describe('failure', function() {
         it('should not run inside a Cordova-based project with no added platforms by calling util.listPlatforms', function() {
@@ -65,14 +77,15 @@ describe('compile command', function() {
     describe('success', function() {
         it('should run inside a Cordova-based project with at least one added platform and shell out to build', function(done) {
             cordova.raw.compile(['android','ios']).then(function() {
-                expect(exec).toHaveBeenCalledWith('"' + path.join(project_dir, 'platforms', 'android', 'cordova', 'build') + '"', jasmine.any(Function));
+                expect(child_process.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms', 'android', 'cordova', 'build'),[]);
+                expect(child_process.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms', 'ios', 'cordova', 'build'),[]);
                 done();
             });
+
         });
         it('should pass down optional parameters', function (done) {
             cordova.raw.compile({platforms:["blackberry10"], options:["--release"]}).then(function () {
-                var buildCommand = path.join(project_dir, 'platforms', 'blackberry10', 'cordova', 'build');
-                expect(exec).toHaveBeenCalledWith('"' + buildCommand + '" --release', jasmine.any(Function));
+                expect(child_process.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms', 'blackberry10', 'cordova', 'build'),['--release']);
                 done();
             });
         });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/dedc29a8/spec/emulate.spec.js
----------------------------------------------------------------------
diff --git a/spec/emulate.spec.js b/spec/emulate.spec.js
index 79b5176..8f3303e 100644
--- a/spec/emulate.spec.js
+++ b/spec/emulate.spec.js
@@ -28,9 +28,24 @@ var cordova = require('../cordova'),
 var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www'; });
 
 describe('emulate command', function() {
-    var is_cordova, list_platforms, fire, exec, result;
+    var is_cordova, list_platforms, fire, result, child;
     var project_dir = '/some/path';
     var prepare_spy;
+    child = {
+        on: function(child_event,cb){
+            if(child_event === 'close'){
+                cb(0);
+            }
+        },
+        stdout: {
+            setEncoding: function(){},
+            on: function(){}
+        },
+        stderr: {
+            setEncoding: function(){},
+            on: function(){}
+        }
+    };
 
     function wrapper(f, post) {
         runs(function() {
@@ -45,10 +60,7 @@ describe('emulate command', function() {
         list_platforms = spyOn(util, 'listPlatforms').andReturn(supported_platforms);
         fire = spyOn(hooker.prototype, 'fire').andReturn(Q());
         prepare_spy = spyOn(cordova.raw, 'prepare').andReturn(Q());
-        exec = spyOn(child_process, 'exec').andCallFake(function(cmd, opts, cb) {
-            if (!cb) cb = opts;
-            cb(null, '', '');
-        });
+        spyOn(child_process, 'spawn').andReturn(child);
     });
     describe('failure', function() {
         it('should not run inside a Cordova-based project with no added platforms by calling util.listPlatforms', function() {
@@ -69,15 +81,15 @@ describe('emulate command', function() {
         it('should run inside a Cordova-based project with at least one added platform and call prepare and shell out to the emulate script', function(done) {
             cordova.raw.emulate(['android','ios']).then(function(err) {
                 expect(prepare_spy).toHaveBeenCalledWith(['android', 'ios']);
-                expect(exec).toHaveBeenCalledWith('"' + path.join(project_dir, 'platforms', 'android', 'cordova', 'run') + '" --emulator', jasmine.any(Function));
-                expect(exec).toHaveBeenCalledWith('"' + path.join(project_dir, 'platforms', 'ios', 'cordova', 'run') + '" --emulator', jasmine.any(Function));
+                expect(child_process.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms', 'android', 'cordova', 'run'), ['--emulator']);
+                expect(child_process.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms', 'ios', 'cordova', 'run'), ['--emulator']);
                 done();
             });
         });
         it('should pass down options', function(done) {
             cordova.raw.emulate({platforms: ['ios'], options:["--optionTastic"]}).then(function(err) {
                 expect(prepare_spy).toHaveBeenCalledWith(['ios']);
-                expect(exec).toHaveBeenCalledWith('"' + path.join(project_dir, 'platforms', 'ios', 'cordova', 'run') + '" --emulator --optionTastic', jasmine.any(Function));
+                expect(child_process.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms', 'ios', 'cordova', 'run'), ['--emulator', '--optionTastic']);
                 done();
             });
         });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/dedc29a8/spec/run.spec.js
----------------------------------------------------------------------
diff --git a/spec/run.spec.js b/spec/run.spec.js
index 7d13a96..65e6bbe 100644
--- a/spec/run.spec.js
+++ b/spec/run.spec.js
@@ -28,18 +28,31 @@ var cordova = require('../cordova'),
 var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www'; });
 
 describe('run command', function() {
-    var is_cordova, list_platforms, fire, exec;
+    var is_cordova, list_platforms, fire, child;
     var project_dir = '/some/path';
     var prepare_spy;
+    child = {
+        on: function(child_event,cb){
+            if(child_event === 'close'){
+                cb(0);
+            }
+        },
+        stdout: {
+            setEncoding: function(){},
+            on: function(){}
+        },
+        stderr: {
+            setEncoding: function(){},
+            on: function(){}
+        }
+    };
+
     beforeEach(function() {
         is_cordova = spyOn(util, 'isCordova').andReturn(project_dir);
         list_platforms = spyOn(util, 'listPlatforms').andReturn(supported_platforms);
         fire = spyOn(hooker.prototype, 'fire').andReturn(Q());
         prepare_spy = spyOn(cordova.raw, 'prepare').andReturn(Q());
-        exec = spyOn(child_process, 'exec').andCallFake(function(cmd, opts, cb) {
-            if (!cb) cb = opts;
-            cb(0, '', '');
-        });
+        spyOn(child_process, 'spawn').andReturn(child);
     });
     describe('failure', function() {
         it('should not run inside a Cordova-based project with no added platforms by calling util.listPlatforms', function(done) {
@@ -64,8 +77,9 @@ describe('run command', function() {
         it('should run inside a Cordova-based project with at least one added platform and call prepare and shell out to the run script', function(done) {
             cordova.raw.run(['android','ios']).then(function() {
                 expect(prepare_spy).toHaveBeenCalledWith(['android', 'ios']);
-                expect(exec).toHaveBeenCalledWith('"' + path.join(project_dir, 'platforms', 'android', 'cordova', 'run') + '" --device', jasmine.any(Function));
-                expect(exec).toHaveBeenCalledWith('"' + path.join(project_dir, 'platforms', 'ios', 'cordova', 'run') + '" --device', jasmine.any(Function));
+                expect(child_process.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms', 'android', 'cordova', 'run'), ['--device']);
+                expect(child_process.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms', 'ios', 'cordova', 'run'), ['--device']);
+
             }, function(err) {
                 console.log(err);
                 expect(err).toBeUndefined();
@@ -74,7 +88,7 @@ describe('run command', function() {
         it('should pass down parameters', function(done) {
             cordova.raw.run({platforms: ['blackberry10'], options:['--password', '1q1q']}).then(function() {
                 expect(prepare_spy).toHaveBeenCalledWith(['blackberry10']);
-                expect(exec).toHaveBeenCalledWith('"' + path.join(project_dir, 'platforms', 'blackberry10', 'cordova', 'run') + '" --device --password 1q1q', jasmine.any(Function));
+                expect(child_process.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms', 'blackberry10', 'cordova', 'run'), ['--device', '--password', '1q1q']);
             }, function(err) {
                 expect(err).toBeUndefined();
             }).fin(done);