diff --git a/lib/resource-set.js b/lib/resource-set.js index c506a18..e8b587c 100644 --- a/lib/resource-set.js +++ b/lib/resource-set.js @@ -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); }, /** @@ -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: @@ -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); @@ -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; }, diff --git a/test/resource-set-test.js b/test/resource-set-test.js index c20e38a..913dc47 100644 --- a/test/resource-set-test.js +++ b/test/resource-set-test.js @@ -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) { @@ -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"]; @@ -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)); } }, @@ -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)); },