Skip to content

Commit

Permalink
Add unit tests for CLI
Browse files Browse the repository at this point in the history
Fix a bug exposed by the unit tests
  • Loading branch information
curvedmark committed Mar 24, 2013
1 parent c9601fe commit 449a60b
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 17 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
32 changes: 17 additions & 15 deletions bin/roole
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"url": "git://github.com/curvedmark/roole.git"
},
"scripts": {
"test": "make test"
"test": "make all-test"
},
"dependencies": {
"mkdirp": "0.3.x",
Expand Down
91 changes: 91 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}
};
Loading

0 comments on commit 449a60b

Please sign in to comment.