Permalink
Browse files

* Fixed tests (no assert.length).

* Added exceptions in case of unknown group/type.
  • Loading branch information...
1 parent 7059ded commit 5bf48ac7a56cebbfa440636d1d6b124629036be7 @jakubpawlowicz committed Jun 17, 2012
Showing with 69 additions and 37 deletions.
  1. +1 −0 .gitignore
  2. +2 −3 Makefile
  3. +40 −22 lib/assets.js
  4. +26 −12 test/assets-test.js
View
@@ -0,0 +1 @@
+node_modules
View
@@ -1,9 +1,8 @@
TEST_DIR = test
all: test
-
+
test:
- @@echo "Running all tests via vows"
@@vows ${TEST_DIR}/*-test.js
-
+
.PHONY: all test
View
@@ -14,18 +14,18 @@ AssetsExpander.prototype = {
allTypes: function() {
return Object.keys(this.yamlSource || {});
},
-
+
groupsFor: function(type) {
return Object.keys(this.yamlSource[type] || {});
},
-
+
processList: function(mainGroup, options) {
options = options || this.options || {};
var self = this;
-
+
var expandAsset = function(root, asset) {
var matches = [];
-
+
if (asset.includes('**/*')) {
var prefix = asset.substring(0, asset.indexOf('**/*'));
matches.push(self._scanDir(path.join(root, prefix), '*', true, options.type));
@@ -40,40 +40,46 @@ AssetsExpander.prototype = {
var expanded = path.join(root, asset) + '.' + options.type;
if (path.existsSync(expanded)) matches.push(expanded);
}
-
+
return matches;
};
-
+
var expandGroup = function(root, group) {
var groupAssetsExpander = [];
-
+
if (group.includes('[')) {
var tokens = group.split(/[\[\]]/);
root = path.join(root, tokens[0]);
group = tokens[1];
}
-
+
group.split(',').forEach(function(asset) {
var expanded = expandAsset(root, asset);
if (expanded.length == 0) return;
-
+
groupAssetsExpander.push(expanded);
});
-
+
return groupAssetsExpander;
};
-
+
return expandGroup(options.root, mainGroup).flatten().uniq();
},
-
+
processGroup: function(type, name, localOptions) {
+ if (!this.yamlSource[type])
+ throw new AssetsExpander.UnknownTypeError("Unknown type: '" + type + "'");
+
+ if (!this.yamlSource[type][name])
+ throw new AssetsExpander.UnknownGroupError("Unknown group: '" + name + "' for type '" + type + "'");
+
var definition = this.yamlSource[type][name],
assets = [],
options = Object.merge(Object.clone(this.options), localOptions || {}),
self = this;
-
+
options.root = path.join(options.root, type);
-
+
var processLevel = function(levelDefinition, levelOptions) {
if (rightjs.isString(levelDefinition)) {
assets.push(self.processList(levelDefinition, levelOptions));
@@ -87,39 +93,51 @@ AssetsExpander.prototype = {
});
}
};
-
+
processLevel(definition, options);
-
+
return assets.flatten().uniq();
},
-
+
// private
-
+
_scanDir: function(root, pathToDir, recursive, extension) {
var pattern = new RegExp('^' + pathToDir.replace(/\*/, '.*'), 'g'),
extPattern = new RegExp(extension + '$'),
dirs = [],
matches = [],
self = this;
-
+
fs.readdirSync(path.join(root, path.dirname(pathToDir))).forEach(function(match) {
var fullPath = path.join(root, match),
stat = fs.lstatSync(fullPath);
-
+
if (match.match(pattern) && match.match(extPattern) && !stat.isDirectory())
matches.push(fullPath);
if (stat.isDirectory())
dirs.push(fullPath);
});
-
+
if (recursive) {
dirs.forEach(function(dir) {
matches.push(self._scanDir(dir, '*', true, extension));
});
}
-
+
return matches;
}
};
+AssetsExpander.UnknownGroupError = function(message) {
+ this.type = "UnknownGroupError";
+ this.message = message;
+};
+AssetsExpander.UnknownGroupError.prototype = new Error();
+
+AssetsExpander.UnknownTypeError = function(message) {
+ this.type = "UnknownTypeError";
+ this.message = message;
+};
+AssetsExpander.UnknownTypeError.prototype = new Error();
+
module.exports = AssetsExpander;
View
@@ -35,7 +35,7 @@ exports.listsSuite = vows.describe('expanding assets').addBatch({
'expand single file': {
topic: expand('asset1'),
'should get array with one element': function(expanded) {
- assert.length(expanded, 1);
+ assert.equal(expanded.length, 1);
},
'should get full path to 1.css': function(expanded) {
assert.equal(expanded[0], fullPathTo('asset1'));
@@ -44,7 +44,7 @@ exports.listsSuite = vows.describe('expanding assets').addBatch({
'expanding three files': {
topic: expand('asset1,asset3,asset2'),
'should give array of three files': function(expanded) {
- assert.length(expanded, 3);
+ assert.equal(expanded.length, 3);
},
'should give three files in proper order': function(expanded) {
assert.equal(expanded[0], fullPathTo('asset1'));
@@ -61,7 +61,7 @@ exports.listsSuite = vows.describe('expanding assets').addBatch({
'expanding single level wildcard arguments': {
topic: expand('*'),
'should give three assets': function(expanded) {
- assert.length(expanded, 4);
+ assert.equal(expanded.length, 4);
},
'should give files in natural order': function(expanded) {
assert.equal(expanded[0], fullPathTo('asset1'));
@@ -73,7 +73,7 @@ exports.listsSuite = vows.describe('expanding assets').addBatch({
'expanding single level custom wildcard arguments': {
topic: expand('asset*'),
'should give three assets': function(expanded) {
- assert.length(expanded, 3);
+ assert.equal(expanded.length, 3);
},
'should give files in natural order': function(expanded) {
assert.equal(expanded[0], fullPathTo('asset1'));
@@ -84,7 +84,7 @@ exports.listsSuite = vows.describe('expanding assets').addBatch({
'expanding single level list with wildcard': {
topic: expand('asset2,other-asset,*'),
'should give 4 assets': function(expanded) {
- assert.length(expanded, 4);
+ assert.equal(expanded.length, 4);
},
'should give assets in proper order': function(expanded) {
assert.equal(expanded[0], fullPathTo('asset2'));
@@ -96,7 +96,7 @@ exports.listsSuite = vows.describe('expanding assets').addBatch({
'expanding multi level wildcard argument': {
topic: expand('**/*'),
'should give 8 assets': function(expanded) {
- assert.length(expanded, 8);
+ assert.equal(expanded.length, 8);
},
'should give files in proper order': function(expanded) {
assert.equal(expanded[0], fullPathTo('asset1'));
@@ -112,7 +112,7 @@ exports.listsSuite = vows.describe('expanding assets').addBatch({
'expanding multi level wildcard with prefix': {
topic: expand('folder1/**/*'),
'should give 4 assets': function(expanded) {
- assert.length(expanded, 4);
+ assert.equal(expanded.length, 4);
},
'should give files in proper order': function(expanded) {
assert.equal(expanded[0], fullPathTo('folder1/asset4'));
@@ -124,7 +124,7 @@ exports.listsSuite = vows.describe('expanding assets').addBatch({
'expanding sublevel list': {
topic: expand('folder1/[asset5,subfolder1/*,*]'),
'should give 4 assets': function(expanded) {
- assert.length(expanded, 4);
+ assert.equal(expanded.length, 4);
},
'should give files in proper order': function(expanded) {
assert.equal(expanded[0], fullPathTo('folder1/asset5'));
@@ -136,10 +136,24 @@ exports.listsSuite = vows.describe('expanding assets').addBatch({
});
exports.groupsSuite = vows.describe('expanding assets groups').addBatch({
+ 'expand unknown type': {
+ 'should not fail': function(expanded) {
+ assert.throws(function() {
+ expanderFor('assets.yml').processGroup('unknown', 'type1', { type: 'css' });
+ }, AssetsExpander.UnknownTypeError);
+ }
+ },
+ 'expand unknown group': {
+ 'should not fail': function(expanded) {
+ assert.throws(function() {
+ expanderFor('assets.yml').processGroup('stylesheets', 'type1', { type: 'css' });
+ }, AssetsExpander.UnknownGroupError);
+ }
+ },
'expanding group #1 from assets.yml': {
topic: group('desktop/public1'),
'should give 5 assets': function(expanded) {
- assert.length(expanded, 5);
+ assert.equal(expanded.length, 5);
},
'should give in proper order': function(expanded) {
assert.equal(expanded[0], fullPathTo('asset3'));
@@ -152,7 +166,7 @@ exports.groupsSuite = vows.describe('expanding assets groups').addBatch({
'expanding group #2 from assets.yml': {
topic: group('desktop/public2'),
'should give 2 assets': function(expanded) {
- assert.length(expanded, 2);
+ assert.equal(expanded.length, 2);
},
'should give in proper order': function(expanded) {
assert.equal(expanded[0], fullPathTo('folder1/asset5'));
@@ -162,7 +176,7 @@ exports.groupsSuite = vows.describe('expanding assets groups').addBatch({
'expanding group #3 from assets.yml': {
topic: group('desktop/public3'),
'should give 4 assets': function(expanded) {
- assert.length(expanded, 4);
+ assert.equal(expanded.length, 4);
},
'should give in proper order': function(expanded) {
assert.equal(expanded[0], fullPathTo('asset3'));
@@ -174,7 +188,7 @@ exports.groupsSuite = vows.describe('expanding assets groups').addBatch({
'expanding group #4 from assets.yml': {
topic: group('desktop/public4'),
'should give 4 assets': function(expanded) {
- assert.length(expanded, 4);
+ assert.equal(expanded.length, 4);
},
'should give in proper order': function(expanded) {
assert.equal(expanded[0], fullPathTo('folder1/asset4'));

0 comments on commit 5bf48ac

Please sign in to comment.