From 091340410e58ea84183d7edbaec29f5a1b96f061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominykas=20Blyz=CC=8Ce=CC=87?= Date: Sun, 8 Nov 2015 19:43:21 +0200 Subject: [PATCH] use proxyquire instead of a custom mocking solution --- package.json | 62 +++++++++++++++++--------------- test/multi-glob-test.js | 80 ++++++++++++++++------------------------- 2 files changed, 63 insertions(+), 79 deletions(-) diff --git a/package.json b/package.json index 1f6f6ef..3a39754 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/test/multi-glob-test.js b/test/multi-glob-test.js index a91f720..5f90a9e 100644 --- a/test/multi-glob-test.js +++ b/test/multi-glob-test.js @@ -1,67 +1,48 @@ 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(); @@ -69,11 +50,11 @@ buster.testCase("Multi-glob", { }, "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(); @@ -81,22 +62,22 @@ buster.testCase("Multi-glob", { }, "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(); @@ -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" });