Skip to content

Commit

Permalink
Refactor code, add use strict
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbert Szydlik committed Dec 19, 2015
1 parent cd8f3fc commit 6b131e1
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

var gulp = require("gulp");
var mocha = require("gulp-mocha");

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

var vfs = require("./src/vfs");
var path = require("path");
var proxyquire = require("proxyquire").noCallThru();
Expand All @@ -11,7 +13,7 @@ function enclose(modulePath, options) {
}
}

var moduleUT = proxyquire(path.join(path.dirname(module.parent.filename), modulePath), requireStubs);
var moduleUT = require(path.join(path.dirname(module.parent.filename), modulePath));
vfs.prepareEnvironment(options);
return moduleUT;
}
Expand Down
17 changes: 17 additions & 0 deletions src/fsStub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

var _ = require("lodash");
var vfs;

function prepareEnvironment(options, pVfs) {
vfs = pVfs;
}

module.exports.prepareEnvironment = prepareEnvironment;

function readFileSync() {

}

module.exports.stub = {};
module.exports.stub.readFileSync = readFileSync;
18 changes: 14 additions & 4 deletions src/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

var _ = require("lodash");
var assert = require("assert");
var fsStub = require("./fsStub");

var vfs = {};
var vfs;

function getBufferFromData(data) {
if(Buffer.isBuffer(data)) {
Expand All @@ -21,13 +22,24 @@ function getBufferFromData(data) {
}

function prepareEnvironment(options) {
vfs = {};
if(_.has(options, "enabled") && options.enabled.vfs) {
var fs = require("fs");

fs.__originalFunctions = {};
_.forEach(fsStub.stub, function(stubFunction, functionName) {
fs.__originalFunctions[functionName] = fs[functionName];
fs[functionName] = stubFunction;
});
}
if(_.has(options, "vfs")) {
_.forEach(options.vfs, function(file, absolutePath) {
vfs[absolutePath] = {};
var storedFile = vfs[absolutePath];
storedFile.data = getBufferFromData(file.data);
});
}
fsStub.prepareEnvironment(options, vfs);
}

function getDataOfFile(absolutePath, encoding) {
Expand All @@ -39,7 +51,5 @@ function getDataOfFile(absolutePath, encoding) {

module.exports.prepareEnvironment = prepareEnvironment;
module.exports.getDataOfFile = getDataOfFile;
module.exports.fsStub = {
readFileSync: function() {}
};
module.exports.fsStub = fsStub.stub;

2 changes: 2 additions & 0 deletions test/fixtures/stubModule.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
"use strict";

module.exports.fs = require("fs");
2 changes: 2 additions & 0 deletions test/testDomeTest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

var testDome = require("../index");
var expect = require("chai").expect;
var sinon = require("sinon");
Expand Down
2 changes: 2 additions & 0 deletions test/vfs/prepareEnvironmentTest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

var vfs = require("../../src/vfs");
var expect = require("chai").expect;

Expand Down

0 comments on commit 6b131e1

Please sign in to comment.