Skip to content
This repository has been archived by the owner on Oct 22, 2018. It is now read-only.

Commit

Permalink
Fix plugin require in tests in iojs 2.2.x
Browse files Browse the repository at this point in the history
See tschaub/mock-fs#43
Either way, it's nice to have the test plugins as standalone files for
syntax highlighting purposes.
  • Loading branch information
ericlathrop committed Jul 20, 2015
1 parent b1ccef9 commit eb79b94
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 30 deletions.
26 changes: 21 additions & 5 deletions lib/unfold.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ function spread(plugin, files, callback) {
});
}

function isFunction(plugin) {
return typeof plugin === "function";
}

function unfold(config, callback) {
if (!config.sourceDirectory) {
callback(new ConfigurationError("The sourceDirectory is required and was not specified"));
Expand All @@ -44,14 +48,26 @@ function unfold(config, callback) {
callback(new ConfigurationError("The destinationDirectory is required and was not specified"));
return;
}

var userPlugins = (config.plugins || []).map(require);
var plugins = [].concat(readFile, userPlugins, moveToDestinationPath, writeFile);
plugins = plugins.map(function(plugin) {
return plugin.process;
}).map(function(fn) {
return spread.bind(this, fn.bind(this, config));

var inits = plugins.map(function(plugin) {
return plugin.init;
}).filter(isFunction);
async.seq.apply(this, inits)(config, function(err, newConfig) {
if (err) {
callback(err);
return;
}

plugins = plugins.map(function(plugin) {
return plugin.process;
}).filter(isFunction).map(function(fn) {
return spread.bind(this, fn.bind(this, newConfig));
});
walk(newConfig.sourceDirectory, process.bind(this, plugins), callback);
});
walk(config.sourceDirectory, process.bind(this, plugins), callback);
}

module.exports = unfold;
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
"mocha": "^2.2.5",
"mock-fs": "^3.0.0",
"tap-spec": "^4.0.2",
"tape": "^4.0.0",
"tape-catch": "^1.0.4"
"tape": "^4.0.0"
},
"scripts": {
"lint": "eslint bin lib test",
Expand Down
8 changes: 8 additions & 0 deletions test/plugins/backup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

module.exports = {
process: function(config, file, callback) {
file.path += ".bak";
callback(undefined, file);
}
};
12 changes: 12 additions & 0 deletions test/plugins/doubler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

module.exports = {
process: function(config, file, callback) {
var file2 = {
path: file.path + ".bak",
stats: file.stats,
data: file.data
};
callback(undefined, [file, file2]);
}
};
17 changes: 17 additions & 0 deletions test/plugins/mock-fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

var mockFs = require("mock-fs");

/*
* We need to mock the filesystem in the first plugin because iojs 2.2.x made a change
* that breaks mock-fs's mocking behavior for the "require" method.
* https://github.com/tschaub/mock-fs/issues/43
* This lets the real require work against the filesystem, and the rest of the test operate against the mock filesystem.
* It's a bit strange, but it works.
*/
module.exports = {
init: function(config, callback) {
mockFs(config.fs);
callback(undefined, config);
}
};
8 changes: 8 additions & 0 deletions test/plugins/yolo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

module.exports = {
process: function(config, file, callback) {
file.data = "yolo";
callback(undefined, file);
}
};
2 changes: 1 addition & 1 deletion test/src-to-dest-path-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

var test = require("tape-catch");
var test = require("tape");
var srcToDestPath = require("../lib/src-to-dest-path");

test("with src and dest folders should rewrite path", function(t) {
Expand Down
47 changes: 25 additions & 22 deletions test/unfold-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var fs = require("fs");
var mockFs = require("mock-fs");
var test = require("tape-catch");
var test = require("tape");
var unfold = require("../lib/unfold");

function setup(t, fakeFs) {
Expand Down Expand Up @@ -43,18 +43,19 @@ test("text file should be copied to destination", function(t) {
});
});
test("plugin can modify file path", function(t) {
setup(t, {
src: { "test.txt": "hello world" },
dest: {},
"plugins/backup.js": "module.exports = { process: function(config, file, callback) { file.path+='.bak'; callback(undefined, file); } };"
});
mockFs.restore();
t.plan(3);
unfold({
sourceDirectory: "src",
destinationDirectory: "dest",
plugins: [
"../plugins/backup"
]
"../test/plugins/mock-fs",
"../test/plugins/backup"
],
fs: {
src: { "test.txt": "hello world" },
dest: {}
}
}, function(err) {
t.notOk(err, "should not have an error");
fs.readFile("dest/test.txt.bak", "utf8", function(err2, data) {
Expand All @@ -64,18 +65,19 @@ test("plugin can modify file path", function(t) {
});
});
test("plugin can modify file data", function(t) {
setup(t, {
src: { "test.txt": "hello world" },
dest: {},
"plugins/yolo.js": "module.exports = { process: function(config, file, callback) { file.data='yolo'; callback(undefined, file); } };"
});
mockFs.restore();
t.plan(3);
unfold({
sourceDirectory: "src",
destinationDirectory: "dest",
plugins: [
"../plugins/yolo"
]
"../test/plugins/mock-fs",
"../test/plugins/yolo"
],
fs: {
src: { "test.txt": "hello world" },
dest: {}
}
}, function(err) {
t.notOk(err, "should not have an error");
fs.readFile("dest/test.txt", "utf8", function(err2, data) {
Expand All @@ -85,18 +87,19 @@ test("plugin can modify file data", function(t) {
});
});
test("plugin can make two files from one file", function(t) {
setup(t, {
src: { "test.txt": "hello world" },
dest: {},
"plugins/doubler.js": "module.exports = { process: function(config, file, callback) { var file2 = { path: file.path + '.bak', stats: file.stats, data: file.data }; callback(undefined, [file, file2]); } };"
});
mockFs.restore();
t.plan(5);
unfold({
sourceDirectory: "src",
destinationDirectory: "dest",
plugins: [
"../plugins/doubler"
]
"../test/plugins/mock-fs",
"../test/plugins/doubler"
],
fs: {
src: { "test.txt": "hello world" },
dest: {}
}
}, function(err) {
t.notOk(err, "should not have an error");
fs.readFile("dest/test.txt", "utf8", function(err2, data) {
Expand Down

0 comments on commit eb79b94

Please sign in to comment.