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

Commit

Permalink
use proxyquire instead of a custom mocking solution
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas committed Nov 8, 2015
1 parent 4c8ebd4 commit 0913404
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 79 deletions.
62 changes: 33 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
{
"name": "multi-glob",
"version": "0.4.0",
"description": "Small wrapper around the glob module that allows globbing for multiple patterns at once",
"homepage": "http://busterjs.org/docs/modules/multi-glob",
"author": "Christian Johansen",
"contributors": [{
"name": "Christian Johansen",
"email": "christian@cjohansen.no",
"url": "http://cjohansen.no"
}, {
"name": "Stein Magnus Jodal",
"email": "stein.magnus@jodal.no",
"url": "http://jodal.no"
}],
"main": "./lib/multi-glob",
"repository": {
"type": "git",
"url": "https://github.com/busterjs/multi-glob.git"
"name": "multi-glob",
"version": "0.4.0",
"description": "Small wrapper around the glob module that allows globbing for multiple patterns at once",
"homepage": "http://busterjs.org/docs/modules/multi-glob",
"author": "Christian Johansen",
"contributors": [
{
"name": "Christian Johansen",
"email": "christian@cjohansen.no",
"url": "http://cjohansen.no"
},
"scripts": {
"test": "node node_modules/buster/bin/buster-test",
"test-debug": "node --debug-brk node_modules/buster/bin/buster-test"
},
"dependencies": {
"glob": "5.x",
"async": "1.x",
"lodash": "3.x"
},
"devDependencies": {
"buster": "*"
{
"name": "Stein Magnus Jodal",
"email": "stein.magnus@jodal.no",
"url": "http://jodal.no"
}
],
"main": "./lib/multi-glob",
"repository": {
"type": "git",
"url": "https://github.com/busterjs/multi-glob.git"
},
"scripts": {
"test": "node node_modules/buster/bin/buster-test",
"test-debug": "node --debug-brk node_modules/buster/bin/buster-test"
},
"dependencies": {
"glob": "5.x",
"async": "1.x",
"lodash": "3.x"
},
"devDependencies": {
"buster": "*",
"proxyquire": "1.x"
}
}
80 changes: 30 additions & 50 deletions test/multi-glob-test.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,83 @@
var buster = require("buster");
var async = require("async");
var _ = require("lodash");
var glob = {};
var vm = require("vm");
var fs = require("fs");

var proxyquire = require("proxyquire");
var assert = buster.referee.assert;

var sandbox = {
buster: buster,
require: function (name) {
if (name === "async") { return async; }
if (name === "lodash") { return _; }
return function () {
return glob.glob.apply(glob, arguments);
};
},
module: {}
};

var lib = require("path").join(__dirname, "../lib/multi-glob.js");
var code = fs.readFileSync(lib, "utf-8");
vm.runInNewContext(code, sandbox);
var g = sandbox.module.exports;

buster.testCase("Multi-glob", {
setUp: function () {
glob.glob = this.stub();
"setUp": function () {
this.nodeGlobStub = this.stub();
this.multiGlob = proxyquire("../lib/multi-glob.js", {
glob: this.nodeGlobStub
});
},

"calls glob with pattern": function () {
g.glob("lib/buster.js");
this.multiGlob.glob("lib/buster.js");

assert.calledOnceWith(glob.glob, "lib/buster.js");
assert.calledOnceWith(this.nodeGlobStub, "lib/buster.js");
},

"calls glob with provided options": function () {
var args = { silent: true };
g.glob("lib/buster.js", args);
this.multiGlob.glob("lib/buster.js", args);

assert.calledOnceWith(glob.glob, "lib/buster.js", args);
assert.calledOnceWith(this.nodeGlobStub, "lib/buster.js", args);
},

"calls glob with empty options when none are provided": function () {
g.glob("lib/buster.js");
this.multiGlob.glob("lib/buster.js");

assert.equals(glob.glob.args[0].length, 3);
assert.isFunction(glob.glob.args[0][2]);
assert.equals(this.nodeGlobStub.args[0].length, 3);
assert.isFunction(this.nodeGlobStub.args[0][2]);
},

"calls glob once with each pattern": function () {
g.glob(["lib/buster.js", "src/buster.js"]);
this.multiGlob.glob(["lib/buster.js", "src/buster.js"]);

assert.calledTwice(glob.glob);
assert.calledWith(glob.glob, "lib/buster.js");
assert.calledWith(glob.glob, "src/buster.js");
assert.calledTwice(this.nodeGlobStub);
assert.calledWith(this.nodeGlobStub, "lib/buster.js");
assert.calledWith(this.nodeGlobStub, "src/buster.js");
},

"calls callback with result from glob": function (done) {
var callback = this.spy();
glob.glob.yields(null, ["lib/buster.js"]);
this.nodeGlobStub.yields(null, ["lib/buster.js"]);

g.glob("lib/buster.js", function (err, res) {
this.multiGlob.glob("lib/buster.js", function (err, res) {
assert.isNull(err);
assert.equals(res, ["lib/buster.js"]);
done();
});
},

"calls callback with combined results from glob": function (done) {
glob.glob.withArgs("lib/buster.js").yields(null, ["lib/buster.js"]);
this.nodeGlobStub.withArgs("lib/buster.js").yields(null, ["lib/buster.js"]);
var files = ["src/buster.js", "src/stuff.js"];
glob.glob.withArgs("src/*.js").yields(null, files);
this.nodeGlobStub.withArgs("src/*.js").yields(null, files);

g.glob(["lib/buster.js", "src/*.js"], function (err, res) {
this.multiGlob.glob(["lib/buster.js", "src/*.js"], function (err, res) {
assert.isNull(err);
assert.equals(res, ["lib/buster.js", "src/buster.js", "src/stuff.js"]);
done();
});
},

"calls callback once with glob error": function (done) {
glob.glob.withArgs("lib/buster.js").yields({ message: "Oh no" });
this.nodeGlobStub.withArgs("lib/buster.js").yields({ message: "Oh no" });
var files = ["src/buster.js", "src/stuff.js"];
glob.glob.withArgs("src/*.js").yields(null, files);
this.nodeGlobStub.withArgs("src/*.js").yields(null, files);

g.glob(["lib/buster.js", "src/*.js"], function (err) {
this.multiGlob.glob(["lib/buster.js", "src/*.js"], function (err) {
assert.equals(err, { message: "Oh no" });
done();
});
},

"ignore duplicated items from glob": function (done) {
glob.glob.withArgs("src/foo.js").yields(null, ["src/foo.js"]);
this.nodeGlobStub.withArgs("src/foo.js").yields(null, ["src/foo.js"]);
var files = ["src/foo.js", "src/bar.js"];
glob.glob.withArgs("src/*.js").yields(null, files);
this.nodeGlobStub.withArgs("src/*.js").yields(null, files);

g.glob(["src/foo.js", "src/*.js"], function (err, res) {
this.multiGlob.glob(["src/foo.js", "src/*.js"], function (err, res) {
assert.isNull(err);
assert.equals(res, ["src/foo.js", "src/bar.js"]);
done();
Expand All @@ -105,10 +86,9 @@ buster.testCase("Multi-glob", {

"strict": {
"fails on glob that matches no patterns": function (done) {
var callback = this.spy();
glob.glob.withArgs("src/foo.js").yields(null, []);
this.nodeGlobStub.withArgs("src/foo.js").yields(null, []);

g.glob(["src/foo.js"], { strict: true }, function (err) {
this.multiGlob.glob(["src/foo.js"], { strict: true }, function (err) {
assert.match(err, {
message: "'src/foo.js' matched no files"
});
Expand Down

0 comments on commit 0913404

Please sign in to comment.