jenkins-bot has submitted this change and it was merged.

Change subject: Add --color=auto option, and make it the default.
......................................................................


Add --color=auto option, and make it the default.

Like git, --color=auto turns on color output if the output device is a TTY,
and disables it otherwise.  You can still force color output with
--color or --no-color (or --color=false).  Use '--color' in runtests.sh
to make current (eccentric) behavior.

Change-Id: I1b2638b1f8973a4f8d92f5e1dca91a9ce8220c46
---
M js/lib/mediawiki.Util.js
M js/lib/mediawiki.tokenizer.peg.js
M js/tests/dumpGrepper.js
M js/tests/parserTests.js
M js/tests/roundtrip-test.js
M js/tests/runtests.sh
6 files changed, 44 insertions(+), 36 deletions(-)

Approvals:
  Subramanya Sastry: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/js/lib/mediawiki.Util.js b/js/lib/mediawiki.Util.js
index 9e8e1a5..7b67257 100644
--- a/js/lib/mediawiki.Util.js
+++ b/js/lib/mediawiki.Util.js
@@ -66,6 +66,25 @@
        /**
         * @method
         *
+        * Set the color flags, based on an options object.
+        *
+        * @param {Options} options The options object to use for setting
+        *        the mode of the 'color' package.
+        */
+       setColorFlags: function(options) {
+               var colors = require('colors');
+               if( options.color === 'auto' ) {
+                       if (!process.stdout.isTTY) {
+                               colors.mode = 'none';
+                       }
+               } else if( !Util.booleanOption( options.color ) ) {
+                       colors.mode = 'none';
+               }
+       },
+
+       /**
+        * @method
+        *
         * Update only those properties that are undefined or null
         * $.extend updates properties that are falsy (which means false gets 
updated as well)
         *
@@ -1254,12 +1273,12 @@
                        if ( change.added ) {
                                diffs++;
                                return change.value.split( '\n' ).map( function 
( line ) {
-                                       return line.green;
+                                       return line.green + '';//add '' to 
workaround color bug
                                } ).join( '\n' );
                        } else if ( change.removed ) {
                                diffs++;
                                return change.value.split( '\n' ).map( function 
( line ) {
-                                       return line.red;
+                                       return line.red + '';//add '' to 
workaround color bug
                                } ).join( '\n' );
                        } else {
                                return change.value;
diff --git a/js/lib/mediawiki.tokenizer.peg.js 
b/js/lib/mediawiki.tokenizer.peg.js
index d9b22a7..7d2ceec 100644
--- a/js/lib/mediawiki.tokenizer.peg.js
+++ b/js/lib/mediawiki.tokenizer.peg.js
@@ -13,7 +13,6 @@
        LRU = require("lru-cache"),
        fs = require('fs'),
        events = require('events'),
-       //colors = require('colors'),
        Util = require('./mediawiki.Util.js').Util,
        defines = require('./mediawiki.parser.defines.js');
 
diff --git a/js/tests/dumpGrepper.js b/js/tests/dumpGrepper.js
index f1c3de3..e66fc71 100755
--- a/js/tests/dumpGrepper.js
+++ b/js/tests/dumpGrepper.js
@@ -6,7 +6,6 @@
 var dumpReader = require('./dumpReader.js'),
        events = require('events'),
        optimist = require('optimist'),
-       colors = require('colors'),
        Util = require( '../lib/mediawiki.Util.js' ).Util;
 
 function DumpGrepper ( regexp ) {
@@ -40,9 +39,9 @@
                        'default': false
                },
                'color': {
-                       description: 'Highlight matched substring using color. 
Use --no-color to disable.',
+                       description: 'Highlight matched substring using color. 
Use --no-color to disable.  Default is "auto".',
                        'boolean': true,
-                       'default': true
+                       'default': 'auto'
                }
        } ).argv;
 
@@ -50,6 +49,7 @@
                optimist.showHelp();
                process.exit( 0 );
        }
+       Util.setColorFlags( argv );
 
        var flags = 'g';
        if( Util.booleanOption( argv.i ) ) {
@@ -76,17 +76,10 @@
                        console.log( '== Match: [[' + revision.page.title + ']] 
==' );
                        var m = matches[i];
                        //console.warn( JSON.stringify( m.index, null, 2 ) );
-                       if ( argv.color ) {
-                               console.log(
+                       console.log(
                                        revision.text.substr( m.index - 40, 40 
) +
                                        m[0].green +
                                        revision.text.substr( m.index + 
m[0].length, 40 ) );
-                       } else {
-                               console.log(
-                                       revision.text.substr( m.index, -40 ) +
-                                       m[0] +
-                                       revision.text.substr( m.index + 
m[0].length, 40 ) );
-                       }
                }
        } );
 
diff --git a/js/tests/parserTests.js b/js/tests/parserTests.js
index 14ab488..b75fac2 100755
--- a/js/tests/parserTests.js
+++ b/js/tests/parserTests.js
@@ -150,7 +150,7 @@
  */
 ParserTests.prototype.getOpts = function () {
        var default_args = ["Default tests-file: " + this.parser_tests_file,
-                           "Default options   : --wt2html --wt2wt --html2html 
--whitelist --color"];
+                           "Default options   : --wt2html --wt2wt --html2html 
--whitelist --color=auto"];
 
        return optimist.usage( 'Usage: $0 [options] [tests-file]\n\n' + 
default_args.join("\n"), {
                'help': {
@@ -254,7 +254,7 @@
                'color': {
                        description: 'Enable color output Ex: --no-color',
                        'boolean': true,
-                       'default': true
+                       'default': 'auto'
                },
                'debug': {
                        description: 'Print debugging information',
@@ -904,7 +904,7 @@
                console.log( 'INPUT'.cyan + ':' );
                console.log( actual.input + '\n' );
 
-               console.log( options.getActualExpected( actual, expected, 
options.getDiff, booleanOption( options.color ) ) );
+               console.log( options.getActualExpected( actual, expected, 
options.getDiff ) );
 
                if ( booleanOption( options.printwhitelist )  ) {
                        this.printWhitelistEntry( title, actual.raw );
@@ -969,30 +969,28 @@
  * @param {Function} getDiff Returns a string showing the diff(s) for the test.
  * @param {Object} getDiff.actual
  * @param {Object} getDiff.expected
- * @param {string} getDiff.color
- * @param {boolean} color Whether we should output colorful strings or not.
  * @returns {string}
  */
-ParserTests.prototype.getActualExpected = function ( actual, expected, 
getDiff, color ) {
+ParserTests.prototype.getActualExpected = function ( actual, expected, getDiff 
) {
        var returnStr = '';
        expected.formattedRaw = expected.isWT ? expected.raw : Util.formatHTML( 
expected.raw );
-       returnStr += ( color ? 'RAW EXPECTED'.cyan : 'RAW EXPECTED' ) + ':';
+       returnStr += 'RAW EXPECTED'.cyan + ':';
        returnStr += expected.formattedRaw + '\n';
 
        actual.formattedRaw = actual.isWT ? actual.raw : Util.formatHTML( 
actual.raw );
-       returnStr += ( color ? 'RAW RENDERED'.cyan : 'RAW RENDERED' ) + ':';
+       returnStr += 'RAW RENDERED'.cyan + ':';
        returnStr += actual.formattedRaw + '\n';
 
        expected.formattedNormal = expected.isWT ? expected.normal : 
Util.formatHTML( expected.normal );
-       returnStr += ( color ? 'NORMALIZED EXPECTED'.magenta : 'NORMALIZED 
EXPECTED' ) + ':';
+       returnStr += 'NORMALIZED EXPECTED'.magenta + ':';
        returnStr += expected.formattedNormal + '\n';
 
        actual.formattedNormal = actual.isWT ? actual.normal : Util.formatHTML( 
actual.normal );
-       returnStr += ( color ? 'NORMALIZED RENDERED'.magenta : 'NORMALIZED 
RENDERED' ) + ':';
+       returnStr += 'NORMALIZED RENDERED'.magenta + ':';
        returnStr += actual.formattedNormal + '\n';
 
-       returnStr += ( color ? 'DIFF'.cyan : 'DIFF' ) + ': \n';
-       returnStr += getDiff( actual, expected, color );
+       returnStr += 'DIFF'.cyan + ': \n';
+       returnStr += getDiff( actual, expected );
 
        return returnStr;
 };
@@ -1002,10 +1000,11 @@
  * @param {string} actual.formattedNormal
  * @param {Object} expected
  * @param {string} expected.formattedNormal
- * @param {boolean} color Whether you want color in the diff output
  */
-ParserTests.prototype.getDiff = function ( actual, expected, color ) {
-       return Util.diff( expected.formattedNormal, actual.formattedNormal, 
color );
+ParserTests.prototype.getDiff = function ( actual, expected ) {
+       // safe to always request color diff, because we set color mode='none'
+       // if colors are turned off.
+       return Util.diff( expected.formattedNormal, actual.formattedNormal, 
true );
 };
 
 /**
@@ -1172,6 +1171,7 @@
                optimist.showHelp();
                process.exit( 0 );
        }
+       Util.setColorFlags( options );
 
        if ( !( options.wt2wt || options.wt2html || options.html2wt || 
options.html2html || options.selser ) ) {
                options.wt2wt = true;
@@ -1234,9 +1234,6 @@
                        console.error( 'ERROR> See below for JS engine 
error:\n' + e + '\n' );
                        process.exit( 1 );
                }
-       }
-       if( !booleanOption( options.color ) ) {
-               colors.mode = 'none';
        }
 
        // Identify tests file
@@ -1508,7 +1505,7 @@
         *
         * @returns {string} The XML representation of the actual and expected 
outputs
         */
-       getActualExpectedXML = function ( actual, expected, getDiff, color ) {
+       getActualExpectedXML = function ( actual, expected, getDiff ) {
                var returnStr = '';
 
                expected.formattedRaw = Util.formatHTML( expected.raw );
@@ -1577,7 +1574,7 @@
                        failEle += '\n</error>\n';
                } else {
                        failEle = '<failure 
type="parserTestsDifferenceInOutputFailure">\n';
-                       failEle += getActualExpectedXML( actual, expected, 
options.getDiff, false );
+                       failEle += getActualExpectedXML( actual, expected, 
options.getDiff );
                        failEle += '\n</failure>\n';
                }
 
@@ -1657,6 +1654,7 @@
        popts.reportStart = xmlFuncs.reportStart;
        popts.reportSummary = xmlFuncs.reportSummary;
        popts.reportFailure = xmlFuncs.reportFailure;
+       colors.mode = 'none';
 }
 
 ptests.main( popts );
diff --git a/js/tests/roundtrip-test.js b/js/tests/roundtrip-test.js
index e356b07..db6c2f6 100755
--- a/js/tests/roundtrip-test.js
+++ b/js/tests/roundtrip-test.js
@@ -1,7 +1,6 @@
 #!/usr/bin/env node
 var fs = require( 'fs' ),
        path = require( 'path' ),
-       colors = require( 'colors' ),
        http = require( 'http' ),
        jsDiff = require( 'diff' ),
        optimist = require( 'optimist' ),
diff --git a/js/tests/runtests.sh b/js/tests/runtests.sh
index 8a53e20..0f09c51 100755
--- a/js/tests/runtests.sh
+++ b/js/tests/runtests.sh
@@ -12,7 +12,7 @@
 }
 cd $(dirname $0) # allow running this script from other dirs
 
-OPTS="--cache"
+OPTS="--cache --color"
 CHANGES="--changesin selser.changes.json"
 if [ ! -d results ];then
     git init results

-- 
To view, visit https://gerrit.wikimedia.org/r/60121
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I1b2638b1f8973a4f8d92f5e1dca91a9ce8220c46
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Parsoid
Gerrit-Branch: master
Gerrit-Owner: Cscott <wikime...@cscott.net>
Gerrit-Reviewer: Cscott <wikime...@cscott.net>
Gerrit-Reviewer: GWicke <gwi...@wikimedia.org>
Gerrit-Reviewer: MarkTraceur <mtrac...@member.fsf.org>
Gerrit-Reviewer: Subramanya Sastry <ssas...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to