[Pkg-javascript-commits] [node-coveralls] 237/332: Add command line option to write output to stdout

Bastien Roucariès rouca at moszumanska.debian.org
Thu Nov 9 13:54:06 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-coveralls.

commit 7d9d9a4514c12c80b4c9475d57d674bf775c9149
Author: Anna Henningsen <sqrt at entless.org>
Date:   Wed Feb 4 01:46:30 2015 +0100

    Add command line option to write output to stdout
    
    Adds the command line option pair -w/--write.
    These options indicate that the output should be written to standard output,
    rather than being posted to coveralls.io, which may be useful for debugging,
    mixed-language codebases (e.g. node addons with C++ code) etc.
---
 lib/getOptions.js       | 14 +++++---------
 lib/logger.js           |  3 ++-
 lib/sendToCoveralls.js  | 18 +++++++++++++++---
 test/sendToCoveralls.js | 25 +++++++++++++++++++++++--
 4 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/lib/getOptions.js b/lib/getOptions.js
index 144d0ad..78f1bf3 100644
--- a/lib/getOptions.js
+++ b/lib/getOptions.js
@@ -129,15 +129,11 @@ var getOptions = function(cb, _userOptions){
 
   getBaseOptions(function(err, options){
     // try to get filepath from the command-line
-    if (process.argv[2]) {
-      if (~['-v', '--verbose'].indexOf(process.argv[2])) {
-        if (process.argv[3]) {
-          options.filepath = process.argv[3];
-        }
-      } else {
-        options.filepath = process.argv[2];
-      }
-    }
+    // look into all command line arguments from index 2 which don't start with -
+    var firstNonOptionArgument = process.argv.slice(2).filter(RegExp.prototype.test.bind(/^[^-]/))[0];
+    
+    if (firstNonOptionArgument)
+      options.filepath = firstNonOptionArgument;
 
     // lodash or else would be better, but no need for the extra dependency
     for (var option in userOptions) {
diff --git a/lib/logger.js b/lib/logger.js
index 9c27e0b..a847a41 100644
--- a/lib/logger.js
+++ b/lib/logger.js
@@ -10,7 +10,8 @@ function getLogLevel(){
 }
 
 function hasVerboseCommandLineOption(){
-    return process.argv[2] && ~['-v', '--verbose'].indexOf(process.argv[2]);
+    // look into command line arguments starting from index 2
+    return process.argv.slice(2).filter(RegExp.prototype.test.bind(/^(-v|--verbose)$/)).length > 0;
 }
 
 function hasDebugEnvVariable(){
diff --git a/lib/sendToCoveralls.js b/lib/sendToCoveralls.js
index 38b37f7..f574997 100644
--- a/lib/sendToCoveralls.js
+++ b/lib/sendToCoveralls.js
@@ -8,9 +8,21 @@ var sendToCoveralls = function(obj, cb){
 
   var str = JSON.stringify(obj);
   var url = urlBase + '/api/v1/jobs';
-  request.post({url : url, form : { json : str}}, function(err, response, body){
-    cb(err, response, body);
-  });
+  
+  if (hasWriteToStdoutOption()) {
+    process.stdout.write(str);
+    cb(null, { statusCode: 200 }, '');
+  } else {
+    request.post({url : url, form : { json : str}}, function(err, response, body){
+      cb(err, response, body);
+    });
+  }
 };
 
+function hasWriteToStdoutOption(){
+    // look into command line arguments starting from index 2
+    return process.argv.slice(2).filter(RegExp.prototype.test.bind(/^(-w|--write)$/)).length > 0;
+}
+
+
 module.exports = sendToCoveralls;
diff --git a/test/sendToCoveralls.js b/test/sendToCoveralls.js
index f809319..eca6bef 100644
--- a/test/sendToCoveralls.js
+++ b/test/sendToCoveralls.js
@@ -1,6 +1,7 @@
 var should = require('should');
 var request = require('request');
 var sinon = require('sinon-restore');
+var stream = require('stream');
 var index = require('../index');
 logger = require('log-driver')({level : false});
 
@@ -27,7 +28,7 @@ describe("sendToCoveralls", function(){
     });
 
     var obj = {"some":"obj"};
-	index.sendToCoveralls(obj, function(err, response, body){
+    index.sendToCoveralls(obj, function(err, response, body){
       err.should.equal('err');
       response.should.equal('response');
       body.should.equal('body');
@@ -44,11 +45,31 @@ describe("sendToCoveralls", function(){
     });
 
     var obj = {"some":"obj"};
-	index.sendToCoveralls(obj, function(err, response, body){
+    index.sendToCoveralls(obj, function(err, response, body){
       err.should.equal('err');
       response.should.equal('response');
       body.should.equal('body');
       done();
     });
   });
+  it ("writes output to stdout when --write is passed", function(done) {
+    var obj = {"some":"obj"};
+    
+    // set up mock process.stdout.write temporarily
+    var origStdoutWrite = process.stdout.write;
+    process.stdout.write = function(string) {
+      if (string == JSON.stringify(obj)) {
+        process.stdout.write = origStdoutWrite;
+        return done();
+      }
+      
+      origStdoutWrite.apply(this, arguments);
+    };
+    
+    process.argv[2] = '--write';
+    
+    index.sendToCoveralls(obj, function(err, response, body) {
+      response.statusCode.should.equal(200);
+    });
+  });
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-coveralls.git



More information about the Pkg-javascript-commits mailing list