Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from busterjs/resource-exclude
Browse files Browse the repository at this point in the history
initial version to exclude resources
  • Loading branch information
cjohansen committed Mar 24, 2014
2 parents 1680e7e + 22bdf92 commit 6745fc6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
43 changes: 34 additions & 9 deletions lib/resource-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,30 @@ exports.create = function (rootPath) {
* Add all resources in array resources. Returns a promise.
*/
addResources: function (resources) {
return when.all(resources.map(_.bind(this, "addResource")));

var globResources = [];

var nonGlobResources = resources.filter(function (resource) {
if (typeof resource === "string") {
if (bResource.isQualified(resource)) {
return true;
} else {
globResources.push(resource);
return false;
}
}
return true;
});

var promises = [];
if (globResources.length > 0) {
promises.push(this.addGlobResources(globResources));
}
promises = promises.concat(
nonGlobResources.map(_.bind(this, "addResource"))
);

return when.all(promises);
},

/**
Expand All @@ -142,7 +165,7 @@ exports.create = function (rootPath) {
* Resource may be a resource object, a string, or an object of
* properties supported by resource.create();
*
* When the resource is a string, it is passed on to addGlobResource.
* When the resource is a string, it is passed on to addGlobResources.
*
* When the resource is an object of properties to pass on to
* resource.create(), a couple of additional properties are supported:
Expand All @@ -160,7 +183,7 @@ exports.create = function (rootPath) {
if (bResource.isQualified(resource)) {
return this.addResource({ path: resource });
} else {
return this.addGlobResource(resource);
return this.addGlobResources([resource]);
}
}
var err = exports.validate(resource);
Expand Down Expand Up @@ -193,15 +216,17 @@ exports.create = function (rootPath) {
* Returns a promise. Promise is rejected if glob pattern matches
* no files, or if adding any resource fails.
*/
addGlobResource: function (path) {
addGlobResources: function (paths) {
var d = deferredAdder();
fr.resolvePaths(this, [path], function (e, paths) {
if (e || paths.length === 0) {
var err = e || { message: path + " matched no files" };
fr.resolvePaths(this, paths, function (e, resolvedPaths) {
if (e || resolvedPaths.length === 0) {
var err = e || {
message: "'" + paths + "' matched no files"
};
return d.resolver.reject(err);
}
this.addFileResources(paths).then(d.resolve, d.reject);
}, { strict: true });
this.addFileResources(resolvedPaths).then(d.resolve, d.reject);
}, { strict: false });
return d.promise;
},

Expand Down
38 changes: 11 additions & 27 deletions test/resource-set-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var FIXTURE_DIR = Path.join(__dirname, "fixtures");
var noop = function () {};
var logStack = function (err) {
var message = (err && err.stack) || err.message;
if (message) { buster.log(message); }
if (message) { console.log(message); }
};

function countdown(num, done) {
Expand Down Expand Up @@ -227,31 +227,6 @@ buster.testCase("Resource sets", {
}), done(logStack));
},

"uses strict globbing": function (done) {
this.rs.addResource("zyng.js").then(done(function () {
assert(false, "Should produce error");
}), done(function (err) {
assert.match(err.message, "zyng.js");
}));
},

"uses strict globbing with multiple patterns": function (done) {
this.rs.addResources(["zyng.js"]).then(done(function () {
assert(false, "Should produce error");
}), done(function (err) {
assert.match(err.message, "zyng.js");
}));
},

"uses strict globbing to catch non-matching pattern": function (done) {
var patterns = ["foo.js", "zyng/*.js"];
this.rs.addResources(patterns).then(done(function () {
assert(false, "Should produce error");
}), done(function (err) {
assert.match(err.message, "zyng/*.js");
}));
},

"adds resource from glob pattern and file path": function (done) {
this.rs.rootPath = Path.join(FIXTURE_DIR, "other-test");
var patterns = ["some-test.js", "*-test.js"];
Expand Down Expand Up @@ -279,6 +254,15 @@ buster.testCase("Resource sets", {
"should mention actual root path");
};
rs.addResource("../resource-test.js").then(noop, done(verify));
},

"exclude resource": function (done) {
this.rs.rootPath = Path.join(FIXTURE_DIR, "other-test");
var patterns = ["*.js", "!other.js"];
this.rs.addResources(patterns).then(done(function (rs) {
assert.equals(this.rs.length, 1);
assert.equals(this.rs[0].path, "/some-test.js");
}.bind(this)), done(logStack));
}
},

Expand Down Expand Up @@ -345,7 +329,7 @@ buster.testCase("Resource sets", {
{ path: "baz.js", combine: ["/foo.js", "/bar.js"] }
]).then(function (resources) {
var concat = "var thisIsTheFoo = 5;var helloFromBar = 1;";
assert.content(resources[2], concat, done);
assert.content(resources[1], concat, done);
}, done(logStack));
},

Expand Down

0 comments on commit 6745fc6

Please sign in to comment.