Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add unit tests for CLI

Fix a bug exposed by the unit tests
  • Loading branch information...
commit 449a60b23e7386ef4826bbeabfd94a775d1e4e9e 1 parent c9601fe
@curvedmark authored
Showing with 401 additions and 17 deletions.
  1. +8 −1 Makefile
  2. +17 −15 bin/roole
  3. +1 −1  package.json
  4. +91 −0 test/assert.js
  5. +284 −0 test/cli.js
View
9 Makefile
@@ -100,6 +100,13 @@ test: node_modules/.bin/mocha parser
$< -bu qunit $(TEST_FILES)
$(MAKE) lint
+cli-test: node_modules/.bin/mocha parser
+ $< -bu qunit test/cli.js
+
+all-test:
+ $(MAKE) test
+ $(MAKE) cli-test
+
coverage: coverage/index.html
COV_LIB_FILES = $(addprefix coverage/,$(LIB_FILES))
@@ -187,4 +194,4 @@ clean:
dist/roole.min.js \
test/test.js
-.PHONY: parser test coverage roole min browser-test clean
+.PHONY: parser test cli-test all-test browser-test coverage roole min clean
View
32 bin/roole
@@ -20,14 +20,14 @@ program
.option(' --skip-prefixed', 'Not generate prefixed rule that already exists')
.parse(process.argv);
-var rooleOptions = {
+var options = {
prettyError: true
};
-if (program.prefix != null) { rooleOptions.prefix = program.prefix; }
-if (program.indent != null) { rooleOptions.indent = program.indent; }
-if (program.precision != null) { rooleOptions.precision = program.precision; }
-if (program.skipPrefixed != null) { rooleOptions.skipPrefixed = program.skipPrefixed; }
+if (program.prefix != null) { options.prefix = program.prefix; }
+if (program.indent != null) { options.indent = program.indent; }
+if (program.precision != null) { options.precision = program.precision; }
+if (program.skipPrefixed != null) { options.skipPrefixed = program.skipPrefixed; }
if (!program.args.length) {
compileStdin(function(content) {
@@ -57,7 +57,7 @@ function compileStdin(callback) {
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) { stdin += chunk; });
process.stdin.on('end', function(){
- roole.compile(stdin, rooleOptions, function(error, css) {
+ roole.compile(stdin, options, function(error, css) {
if (error) {
throw error;
}
@@ -125,8 +125,8 @@ function compileFiles(files, callback) {
}
function compileFile(file, callback) {
- rooleOptions.filePath = file.path;
- roole.compile(file.content, rooleOptions, function(error, css) {
+ options.filePath = file.path;
+ roole.compile(file.content, options, function(error, css) {
if (error) {
throw error;
}
@@ -144,18 +144,20 @@ function writeFiles(files) {
}
function writeFile(file, watching) {
- var dir = path.dirname(file.outputPath);
- if (!fs.existsSync(dir)) { mkdirp.sync(dir); }
- fs.writeFileSync(file.outputPath, file.compiledContent);
-
- if (!program.force && !file.compiledContent) {
+ if (!file.compiledContent && !program.force) {
if (!program.watch) {
console.log('INFO:', file.path, 'compiles to empty string, not writing to file');
} else if (watching) {
console.log(' compiled', file.path, 'to empty string, not writing to file');
}
- } else if (program.watch && watching) {
- console.log(' compiled', file.path, 'to', file.outputPath);
+ } else {
+ var dir = path.dirname(file.outputPath);
+ if (!fs.existsSync(dir)) { mkdirp.sync(dir); }
+ fs.writeFileSync(file.outputPath, file.compiledContent);
+
+ if (program.watch && watching) {
+ console.log(' compiled', file.path, 'to', file.outputPath);
+ }
}
}
View
2  package.json
@@ -19,7 +19,7 @@
"url": "git://github.com/curvedmark/roole.git"
},
"scripts": {
- "test": "make test"
+ "test": "make all-test"
},
"dependencies": {
"mkdirp": "0.3.x",
View
91 test/assert.js
@@ -1,5 +1,9 @@
'use strict';
+var fs = require('fs');
+var path = require('path');
+var exec = require('child_process').exec;
+var mkdirp = require('mkdirp');
var roole = require('../lib/roole');
var assert = exports;
@@ -98,4 +102,91 @@ assert.failAt = function(options, input, loc) {
if (!called) {
throw new Error('input is never compiled');
}
+};
+
+assert.run = function(cmd, input, output) {
+ var dir = 'test-dir';
+ if (!fs.existsSync(dir)) {
+ mkdirp.sync(dir);
+ }
+
+ if (Array.isArray(input.stdin)) {
+ input.stdin = input.stdin.join('\n');
+ }
+
+ var done = output.done;
+ var callback = function(error) {
+ exec('rm -rf ' + dir, function() {
+ done(error);
+ });
+ };
+
+ if (input.files) {
+ for (var fileName in input.files) {
+ var fileContent = input.files[fileName];
+ fileName = path.join(dir, fileName);
+
+ if (fs.existsSync(fileName)) {
+ return callback(new Error("'" + fileName + "' already exists"));
+ }
+
+ var fileDir = path.dirname(fileName);
+ if (!fs.existsSync(fileDir)) {
+ mkdirp.sync(fileDir);
+ }
+
+ if (Array.isArray(fileContent)) {
+ fileContent = fileContent.join('\n');
+ }
+
+ fs.writeFileSync(fileName, fileContent);
+ }
+ }
+
+ var child = exec('../bin/' + cmd, {cwd: dir}, function(error, stdout) {
+ if (error) {
+ return callback(error);
+ }
+
+ if (Array.isArray(output.stdout)) {
+ output.stdout = output.stdout.join('\n');
+ }
+
+ if (output.stdout) {
+ output.stdout += '\n';
+ stdout = stdout.toString();
+ if (stdout !== output.stdout) {
+ return callback(new Error('stdout is\n"""\n' + stdout + '\n"""\n\ninstead of\n\n"""\n' + output.stdout + '\n"""'));
+ }
+ } else if (output.files) {
+ for (var fileName in output.files) {
+ var fileContent = output.files[fileName];
+ fileName = path.join(dir, fileName);
+
+ if (fileContent === null) {
+ if (fs.existsSync(fileName)) {
+ return callback(new Error('"' + fileName + '" is created, which is not supposed to be'));
+ }
+
+ continue;
+ }
+
+ var realContent = fs.readFileSync(fileName, 'utf8');
+
+ if (Array.isArray(fileContent)) {
+ fileContent = fileContent.join('\n');
+ }
+
+ if (realContent !== fileContent) {
+ return callback(new Error('"' + fileName + '" is\n"""\n' + realContent + '\n"""\n\ninstead of\n\n"""\n' + fileContent + '\n"""'));
+ }
+ }
+ }
+
+ callback();
+ });
+
+ if (input.stdin) {
+ child.stdin.end(input.stdin);
+ }
};
View
284 test/cli.js
@@ -0,0 +1,284 @@
+'use strict';
+
+var assert = require('./assert');
+
+suite('Command Line');
+
+test('roole', function(done) {
+ assert.run('roole', {
+ stdin: 'body {margin: 1px}',
+ }, {
+ stdout: [
+ 'body {',
+ ' margin: 1px;',
+ '}',
+ ],
+ done: done,
+ });
+});
+
+test('roole file', function(done) {
+ assert.run('roole foo.roo', {
+ files: {
+ 'foo.roo': 'body {margin: 1px}',
+ },
+ }, {
+ files: {
+ 'foo.css': [
+ 'body {',
+ ' margin: 1px;',
+ '}',
+ ],
+ },
+ done: done,
+ });
+});
+
+test('roole files...', function(done) {
+ assert.run('roole foo.roo bar.roo', {
+ files: {
+ 'foo.roo': 'body {margin: 1px}',
+ 'bar.roo': 'body {margin: 2px}',
+ },
+ }, {
+ files: {
+ 'foo.css': [
+ 'body {',
+ ' margin: 1px;',
+ '}',
+ ],
+ 'bar.css': [
+ 'body {',
+ ' margin: 2px;',
+ '}',
+ ],
+ },
+ done: done,
+ });
+});
+
+test('roole empty-file', function(done) {
+ assert.run('roole foo.roo', {
+ files: {
+ 'foo.roo': '',
+ },
+ }, {
+ files: {
+ 'foo.css': null,
+ },
+ done: done,
+ });
+});
+
+test('roole empty-files...', function(done) {
+ assert.run('roole foo.roo bar.roo', {
+ files: {
+ 'foo.roo': 'body {margin: 1px}',
+ 'bar.roo': '',
+ },
+ }, {
+ files: {
+ 'foo.css': [
+ 'body {',
+ ' margin: 1px;',
+ '}',
+ ],
+ 'bar.css': null,
+ },
+ done: done,
+ });
+});
+
+test('roole -f empty-file', function(done) {
+ assert.run('roole -f foo.roo', {
+ files: {
+ 'foo.roo': '',
+ },
+ }, {
+ files: {
+ 'foo.css': '',
+ },
+ done: done,
+ });
+});
+
+test('roole dir', function(done) {
+ assert.run('roole foo', {
+ files: {
+ 'foo/foo.roo': 'body {margin: 1px}',
+ 'foo/bar.roo': 'body {margin: 2px}',
+ },
+ }, {
+ files: {
+ 'foo/foo.css': [
+ 'body {',
+ ' margin: 1px;',
+ '}',
+ ],
+ 'foo/bar.css': [
+ 'body {',
+ ' margin: 2px;',
+ '}',
+ ],
+ },
+ done: done,
+ });
+});
+
+test('roole -o dir files...', function(done) {
+ assert.run('roole -o foo foo.roo bar.roo', {
+ files: {
+ 'foo.roo': 'body {margin: 1px}',
+ 'bar.roo': 'body {margin: 2px}',
+ },
+ }, {
+ files: {
+ 'foo/foo.css': [
+ 'body {',
+ ' margin: 1px;',
+ '}',
+ ],
+ 'foo/bar.css': [
+ 'body {',
+ ' margin: 2px;',
+ '}',
+ ],
+ },
+ done: done,
+ });
+});
+
+test('roole -o dir empty-files...', function(done) {
+ assert.run('roole -o foo foo.roo bar.roo', {
+ files: {
+ 'foo.roo': 'body {margin: 1px}',
+ 'bar.roo': '',
+ },
+ }, {
+ files: {
+ 'foo/foo.css': [
+ 'body {',
+ ' margin: 1px;',
+ '}',
+ ],
+ 'foo/bar.css': null,
+ },
+ done: done,
+ });
+});
+
+test('roole -o dir dir', function(done) {
+ assert.run('roole -o foo bar', {
+ files: {
+ 'bar/foo.roo': 'body {margin: 1px}',
+ 'bar/baz/baz.roo': 'body {margin: 2px}',
+ },
+ }, {
+ files: {
+ 'foo/foo.css': [
+ 'body {',
+ ' margin: 1px;',
+ '}',
+ ],
+ 'foo/baz/baz.css': [
+ 'body {',
+ ' margin: 2px;',
+ '}',
+ ],
+ },
+ done: done,
+ });
+});
+
+test('roole -p files...', function(done) {
+ assert.run('roole -p foo.roo bar.roo', {
+ files: {
+ 'foo.roo': 'body {margin: 1px}',
+ 'bar.roo': 'body {margin: 2px}',
+ },
+ }, {
+ stdout: [
+ 'body {',
+ ' margin: 1px;',
+ '}',
+ '',
+ 'body {',
+ ' margin: 2px;',
+ '}',
+ ],
+ done: done,
+ });
+});
+
+test('roole --prefix "webkit"', function(done) {
+ assert.run('roole --prefix "webkit"', {
+ stdin: 'body {box-sizing: border-box}',
+ }, {
+ stdout: [
+ 'body {',
+ ' -webkit-box-sizing: border-box;',
+ ' box-sizing: border-box;',
+ '}',
+ ],
+ done: done,
+ });
+});
+
+test('roole --prefix ""', function(done) {
+ assert.run('roole --prefix ""', {
+ stdin: 'body {box-sizing: border-box}',
+ }, {
+ stdout: [
+ 'body {',
+ ' box-sizing: border-box;',
+ '}',
+ ],
+ done: done,
+ });
+});
+
+test('roole --indent " "', function(done) {
+ assert.run('roole --indent " "', {
+ stdin: 'body {margin: 1px}',
+ }, {
+ stdout: [
+ 'body {',
+ ' margin: 1px;',
+ '}',
+ ],
+ done: done,
+ });
+});
+
+test('roole --precision 5', function(done) {
+ assert.run('roole --precision 5', {
+ stdin: 'body {margin: 1px / 3}',
+ }, {
+ stdout: [
+ 'body {',
+ ' margin: 0.33333px;',
+ '}',
+ ],
+ done: done,
+ });
+});
+
+test('roole --skip-prefixed', function(done) {
+ assert.run('roole --skip-prefixed', {
+ stdin: [
+ 'body {',
+ ' -moz-box-sizing: padding-box;',
+ ' box-sizing: border-box;',
+ '}',
+ ]
+ }, {
+ stdout: [
+ 'body {',
+ ' -moz-box-sizing: padding-box;',
+ ' -webkit-box-sizing: border-box;',
+ ' box-sizing: border-box;',
+ '}',
+ ],
+ done: done,
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.