Binary updates.
authorGoalSmashers <jakub@goalsmashers.com>
Thu, 28 Jun 2012 20:47:42 +0000 (22:47 +0200)
committerGoalSmashers <jakub@goalsmashers.com>
Thu, 28 Jun 2012 20:47:42 +0000 (22:47 +0200)
* Updated binary to give version (-v) and help when no options (but not in piped mode).
* Added binary tests.

bin/cleancss
package.json
test/binary-test.js [new file with mode: 0644]

index e5f12f5..1a0b017 100755 (executable)
@@ -3,20 +3,28 @@
 global.util = require("util");
 var argv = require('optimist').argv,
   cleanCss = require('../index'),
-  fs = require('fs');
+  fs = require('fs'),
+  path = require('path');
 
 var options = {
   source: null,
   target: null
 };
 var cleanOptions = {};
+var fromStdin = !process.env['__DIRECT__'] && process.stdin.readable;
 
 if (argv.o) options.target = argv.o;
 if (argv._) options.source = argv._[0];
 if (argv.e) cleanOptions.removeEmpty = true;
 
-if (argv.h || argv.help) {
-  global.util.print('Usage: cleancss [-e] -o <output-file> <input-file>\n');
+if (argv.v) {
+  var packageConfig = fs.readFileSync(path.join(path.dirname(fs.realpathSync(process.argv[1])), '../package.json'));
+  util.puts(JSON.parse(packageConfig).version);
+  process.exit(0);
+}
+
+if (argv.h || argv.help || (!fromStdin && argv._.length == 0)) {
+  global.util.print('usage: cleancss [-e] -o <output-file> <input-file>\n');
   process.exit(0);
 }
 
index a181b37..2495d65 100644 (file)
@@ -14,7 +14,7 @@
     "cleancss": "./bin/cleancss"
   },
   "dependencies": {
-    "optimist": "0.1.x"
+    "optimist": "0.3.x"
   },
   "devDependencies": {
     "vows": "0.6.x"
diff --git a/test/binary-test.js b/test/binary-test.js
new file mode 100644 (file)
index 0000000..3653009
--- /dev/null
@@ -0,0 +1,72 @@
+var vows = require('vows'),
+  assert = require('assert'),
+  exec = require('child_process').exec,
+  fs = require('fs');
+
+var binaryContext = function(options, context) {
+  context.topic = function() {
+    // We add __DIRECT__=1 to switch binary into 'non-piped' mode
+    exec("__DIRECT__=1 ./bin/cleancss " + options, this.callback);
+  };
+  return context;
+};
+
+var pipedContext = function(css, options, context) {
+  context.topic = function() {
+    exec("echo \"" + css + "\" | ./bin/cleancss " + options, this.callback);
+  };
+  return context;
+};
+
+exports.commandsSuite = vows.describe('binary commands').addBatch({
+  'no options': binaryContext('', {
+    'should output help': function(stdout) {
+      assert.equal(/usage:/.test(stdout), true);
+    }
+  }),
+  'help': binaryContext('-h', {
+    'should output help': function(error, stdout) {
+      assert.equal(/usage:/.test(stdout), true);
+    }
+  }),
+  'version': binaryContext('-v', {
+    'should output help': function(error, stdout) {
+      var version = JSON.parse(fs.readFileSync('./package.json')).version;
+      assert.equal(stdout, version + "\n");
+    }
+  }),
+  'stdin': pipedContext("a{color: #f00}", '', {
+    'should output data': function(error, stdout) {
+      assert.equal(stdout, "a{color:red}");
+    }
+  }),
+  'no empty by default': pipedContext('a{}', '', {
+    'should preserve content': function(error, stdout) {
+      assert.equal(stdout, "a{}");
+    }
+  }),
+  'empty': pipedContext('a{}', '-e', {
+    'should preserve content': function(error, stdout) {
+      assert.equal(stdout, "");
+    }
+  }),
+  'from source': binaryContext('./test/data/reset.css', {
+    'should minimize': function(error, stdout) {
+      var minimized = fs.readFileSync('./test/data/reset-min.css', 'utf-8').replace(/\n/g, '');
+      assert.equal(stdout, minimized);
+    }
+  }),
+  'to file': binaryContext('-o reset-min.css ./test/data/reset.css', {
+    'should give no output': function(error, stdout) {
+      assert.equal(stdout, '');
+    },
+    'should minimize': function(stdout) {
+      var minimized = fs.readFileSync('./test/data/reset-min.css', 'utf-8').replace(/\n/g, '');
+      var target = fs.readFileSync('./reset-min.css', 'utf-8').replace(/\n/g, '');
+      assert.equal(minimized, target);
+    },
+    teardown: function() {
+      exec('rm reset-min.css');
+    }
+  })
+});
\ No newline at end of file