Skip to content

Commit 8f10a81

Browse files
committed
Extract persistence fs mocks into a module
1 parent 1a8f6df commit 8f10a81

File tree

3 files changed

+69
-32
lines changed

3 files changed

+69
-32
lines changed

test/lib/persistence.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
3+
Copyright 2017 AJ Jordan <[email protected]>.
4+
5+
This file is part of lazymention.
6+
7+
lazymention is free software: you can redistribute it and/or modify it
8+
under the terms of the GNU Affero General Public License as published
9+
by the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
lazymention is distributed in the hope that it will be useful, but
13+
WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Affero General Public License for more details.
16+
17+
You should have received a copy of the GNU Affero General Public
18+
License along with lazymention. If not, see
19+
<https://www.gnu.org/licenses/>.
20+
21+
*/
22+
23+
'use strict';
24+
25+
// TODO test this file
26+
27+
var assert = require('perjury').assert,
28+
_ = require('lodash'),
29+
mockFs = require('mock-fs');
30+
31+
function buildMockSetup(configurePath) {
32+
return {
33+
topic: function(passthrough) {
34+
if (configurePath) require('../../lib/persistence').configure('/tmp');
35+
36+
mockFs({
37+
'/tmp': mockFs.directory()
38+
});
39+
return passthrough;
40+
},
41+
teardown: function(app) {
42+
mockFs.restore();
43+
return true;
44+
},
45+
'it works': function(err, app) {
46+
assert.ifError(err);
47+
}
48+
};
49+
};
50+
51+
function wrapFsMocks(configurePath, obj) {
52+
if (!obj) {
53+
obj = configurePath;
54+
configurePath = true;
55+
}
56+
57+
return _.assign({}, buildMockSetup(configurePath), obj);
58+
}
59+
60+
module.exports.wrapFsMocks = wrapFsMocks;

test/persistence-test.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ License along with lazymention. If not, see
2525
var vows = require('perjury'),
2626
assert = vows.assert,
2727
_ = require('lodash'),
28-
mockFs = require('mock-fs');
28+
persistenceutil = require('./lib/persistence'),
29+
wrapFsMocks = persistenceutil.wrapFsMocks;
2930

3031
vows.describe('persistence module').addBatch({
3132
'When we require the module': {
@@ -40,20 +41,7 @@ vows.describe('persistence module').addBatch({
4041
assert.isFunction(db.get);
4142
assert.isFunction(db.set);
4243
},
43-
'and we mock out the `fs` module': {
44-
topic: function(db) {
45-
mockFs({
46-
'/tmp': mockFs.directory()
47-
});
48-
return db;
49-
},
50-
teardown: function() {
51-
mockFs.restore();
52-
return true;
53-
},
54-
'it works': function(err) {
55-
assert.ifError(err);
56-
},
44+
'and we mock out the `fs` module': wrapFsMocks(false, {
5745
'and we configure the module with a path': {
5846
topic: function(db) {
5947
db.configure('/tmp');
@@ -118,6 +106,6 @@ vows.describe('persistence module').addBatch({
118106
}
119107
}
120108
}
121-
}
109+
})
122110
}
123111
}).export(module);

test/webmention-test.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var vows = require('perjury'),
2727
mockFs = require('mock-fs'),
2828
proxyquire = require('proxyquire'),
2929
sinon = require('sinon'),
30+
persistenceutil = require('./lib/persistence'),
31+
wrapFsMocks = persistenceutil.wrapFsMocks,
3032
data = {
3133
singleLink: '<a href="http://nicenice.website/blag/new-puppy">So cute!</a>',
3234
multipleLinks: '<a href="http://magic.geek/pics/another-doggo">Even cuter!</a> I love <a href="http://catscatscats.org/">cats</a> too!'
@@ -58,21 +60,7 @@ vows.describe('Webmention module').addBatch({
5860
'it exports a function': function(err, webmention) {
5961
assert.isFunction(webmention[1]);
6062
},
61-
'and we set up persistence mocks': {
62-
topic: function(webmention) {
63-
require('../lib/persistence').configure('/tmp');
64-
mockFs({
65-
'/tmp': mockFs.directory()
66-
});
67-
return webmention;
68-
},
69-
teardown: function() {
70-
mockFs.restore();
71-
return true;
72-
},
73-
'it works': function(err) {
74-
assert.ifError(err);
75-
},
63+
'and we set up persistence mocks': wrapFsMocks({
7664
'and we call the module with a post': {
7765
topic: function(fns) {
7866
var webmention = fns[1],
@@ -86,6 +74,7 @@ vows.describe('Webmention module').addBatch({
8674
});
8775
},
8876
'it works': function(err) {
77+
console.log(err);
8978
assert.ifError(err);
9079
},
9180
'the spy was called': function(err, fns) {
@@ -167,6 +156,6 @@ vows.describe('Webmention module').addBatch({
167156
}
168157
}
169158
}
170-
}
159+
})
171160
}
172161
}).export(module);

0 commit comments

Comments
 (0)