Skip to content

Commit de77937

Browse files
committed
Abstracted away req/res from VFS calls in favor of options (#34)
Instead of: ``` const adapter = core => ({ readdir: vfs => file => { console.log(vfs.req.session.user.username) } }) ``` Is now this: ``` const adapter = core => ({ readdir: vfs => (file, options) => { console.log(options.session.user.username) } }) ```
1 parent 41f1cad commit de77937

File tree

5 files changed

+80
-67
lines changed

5 files changed

+80
-67
lines changed

__tests__/adapters/vfs/system.js

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ describe('VFS System adapter', () => {
1515
afterAll(() => core.destroy());
1616

1717
const vfs = {
18-
req: {
19-
session: {
20-
user: {
21-
username: 'jest'
22-
}
23-
}
24-
},
25-
res: {
26-
27-
},
2818
mount: {
2919
name: 'home',
3020
root: 'home:/',
@@ -34,18 +24,27 @@ describe('VFS System adapter', () => {
3424
}
3525
};
3626

27+
const createOptions = (options = {}) => ({
28+
...options,
29+
session: {
30+
user: {
31+
username: 'jest'
32+
}
33+
}
34+
});
35+
3736
const request = (name, ...args) => adapter[name](vfs, vfs)(...args);
3837

3938
test('#touch', () => {
40-
return expect(request('touch', 'home:/test'))
39+
return expect(request('touch', 'home:/test', createOptions()))
4140
.resolves
4241
.toBe(true);
4342
});
4443

4544
test('#stat', () => {
4645
const realPath = path.join(core.configuration.tempPath, 'jest/test');
4746

48-
return expect(request('stat', 'home:/test'))
47+
return expect(request('stat', 'home:/test', createOptions()))
4948
.resolves
5049
.toMatchObject({
5150
filename: 'test',
@@ -58,37 +57,37 @@ describe('VFS System adapter', () => {
5857
});
5958

6059
test('#copy', () => {
61-
return expect(request('copy', 'home:/test', 'home:/test-copy'))
60+
return expect(request('copy', 'home:/test', 'home:/test-copy', createOptions()))
6261
.resolves
6362
.toBe(true);
6463
});
6564

6665
test('#rename', () => {
67-
return expect(request('rename', 'home:/test-copy', 'home:/test-rename'))
66+
return expect(request('rename', 'home:/test-copy', 'home:/test-rename', createOptions()))
6867
.resolves
6968
.toBe(true);
7069
});
7170

7271
test('#mkdir', () => {
73-
return expect(request('mkdir', 'home:/test-directory'))
72+
return expect(request('mkdir', 'home:/test-directory', createOptions()))
7473
.resolves
7574
.toBe(true);
7675
});
7776

7877
test('#mkdir - existing directory', () => {
79-
return expect(request('mkdir', 'home:/test-directory'))
78+
return expect(request('mkdir', 'home:/test-directory', createOptions()))
8079
.rejects
8180
.toThrowError();
8281
});
8382

8483
test('#mkdir - ensure', () => {
85-
return expect(request('mkdir', 'home:/test-directory', {ensure: true}))
84+
return expect(request('mkdir', 'home:/test-directory', createOptions({ensure: true})))
8685
.resolves
8786
.toBe(true);
8887
});
8988

9089
test('#readfile', () => {
91-
return expect(request('readfile', 'home:/test'))
90+
return expect(request('readfile', 'home:/test', createOptions()))
9291
.resolves
9392
.toBeInstanceOf(stream.Readable);
9493
});
@@ -99,31 +98,31 @@ describe('VFS System adapter', () => {
9998
s.push('jest');
10099
s.push(null);
101100

102-
return expect(request('writefile', 'home:/test', s))
101+
return expect(request('writefile', 'home:/test', s, createOptions()))
103102
.resolves
104103
.toBe(true);
105104
});
106105

107106
test('#exists - existing file', () => {
108-
return expect(request('exists', 'home:/test-rename'))
107+
return expect(request('exists', 'home:/test-rename', createOptions()))
109108
.resolves
110109
.toBe(true);
111110
});
112111

113112
test('#exists - existing directory', () => {
114-
return expect(request('exists', 'home:/test-directory'))
113+
return expect(request('exists', 'home:/test-directory', createOptions()))
115114
.resolves
116115
.toBe(true);
117116
});
118117

119118
test('#exists - non existing file', () => {
120-
return expect(request('exists', 'home:/test-copy'))
119+
return expect(request('exists', 'home:/test-copy', createOptions()))
121120
.resolves
122121
.toBe(false);
123122
});
124123

125124
test('#search', () => {
126-
return expect(request('search', 'home:/', '*'))
125+
return expect(request('search', 'home:/', '*', createOptions()))
127126
.resolves
128127
.toEqual(
129128
expect.arrayContaining([
@@ -140,7 +139,7 @@ describe('VFS System adapter', () => {
140139
});
141140

142141
test('#readdir', () => {
143-
return expect(request('readdir', 'home:/'))
142+
return expect(request('readdir', 'home:/', createOptions()))
144143
.resolves
145144
.toEqual(
146145
expect.arrayContaining([
@@ -164,22 +163,22 @@ describe('VFS System adapter', () => {
164163
const files = ['home:/test', 'home:/test-directory', 'home:/test-rename'];
165164

166165
return Promise.all(files.map(f => {
167-
return expect(request('unlink', f))
166+
return expect(request('unlink', f, createOptions()))
168167
.resolves
169168
.toBe(true);
170169
}));
171170
});
172171

173172
test('#unlink', () => {
174-
return expect(request('unlink', 'home:/test-directory'))
173+
return expect(request('unlink', 'home:/test-directory', createOptions()))
175174
.resolves
176175
.toBe(true);
177176
});
178177

179178
test('#realpath', () => {
180179
const realPath = path.join(core.configuration.tempPath, 'jest/test');
181180

182-
return expect(request('realpath', 'home:/test'))
181+
return expect(request('realpath', 'home:/test', createOptions()))
183182
.resolves
184183
.toBe(realPath);
185184
});

__tests__/filesystem.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ describe('Filesystem', () => {
4646
.toBe('test/jest');
4747
});
4848

49-
test('#mount', () => {
50-
mountpoint = filesystem.mount({
49+
test('#mount', async () => {
50+
mountpoint = await filesystem.mount({
5151
name: 'jest',
5252
attributes: {
5353
root: '/tmp'

src/adapters/vfs/system.js

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ const segments = {
8080

8181
username: {
8282
dynamic: true,
83-
fn: (core, req) => req.session.user.username
83+
fn: (core, session) => session.user.username
8484
}
8585
};
8686

8787
/*
8888
* Gets a segment value
8989
*/
90-
const getSegment = (core, req, seg) => segments[seg] ? segments[seg].fn(core, req) : '';
90+
const getSegment = (core, session, seg) => segments[seg] ? segments[seg].fn(core, session) : '';
9191

9292
/*
9393
* Matches a string for segments
@@ -97,16 +97,16 @@ const matchSegments = str => (str.match(/(\{\w+\})/g) || []);
9797
/*
9898
* Resolves a string with segments
9999
*/
100-
const resolveSegments = (core, req, str) => matchSegments(str)
101-
.reduce((result, current) => result.replace(current, getSegment(core, req, current.replace(/(\{|\})/g, ''))), str);
100+
const resolveSegments = (core, session, str) => matchSegments(str)
101+
.reduce((result, current) => result.replace(current, getSegment(core, session, current.replace(/(\{|\})/g, ''))), str);
102102

103103
/*
104104
* Resolves a given file path based on a request
105105
* Will take out segments from the resulting string
106106
* and replace them with a list of defined variables
107107
*/
108-
const getRealPath = (core, req, mount, file) => {
109-
const root = resolveSegments(core, req, mount.attributes.root);
108+
const getRealPath = (core, session, mount, file) => {
109+
const root = resolveSegments(core, session, mount.attributes.root);
110110
const str = file.substr(mount.root.length - 1);
111111
return path.join(root, str);
112112
};
@@ -118,7 +118,7 @@ const getRealPath = (core, req, mount, file) => {
118118
*/
119119
module.exports = (core) => {
120120
const wrapper = (method, cb, ...args) => vfs => (file, options = {}) => {
121-
const promise = Promise.resolve(getRealPath(core, vfs.req, vfs.mount, file))
121+
const promise = Promise.resolve(getRealPath(core, options.session, vfs.mount, file))
122122
.then(realPath => fs[method](realPath, ...args));
123123

124124
return typeof cb === 'function'
@@ -127,19 +127,17 @@ module.exports = (core) => {
127127
};
128128

129129
const crossWrapper = method => (srcVfs, destVfs) => (src, dest, options = {}) => Promise.resolve({
130-
realSource: getRealPath(core, srcVfs.req, srcVfs.mount, src),
131-
realDest: getRealPath(core, destVfs.req, destVfs.mount, dest)
130+
realSource: getRealPath(core, options.session, srcVfs.mount, src),
131+
realDest: getRealPath(core, options.session, destVfs.mount, dest)
132132
})
133133
.then(({realSource, realDest}) => fs[method](realSource, realDest))
134134
.then(() => true);
135135

136136
return {
137137
watch: (mount, callback) => {
138138
const dest = resolveSegments(core, {
139-
session: {
140-
user: {
141-
username: '**'
142-
}
139+
user: {
140+
username: '**'
143141
}
144142
}, mount.attributes.root);
145143

@@ -168,6 +166,7 @@ module.exports = (core) => {
168166
/**
169167
* Checks if file exists
170168
* @param {String} file The file path from client
169+
* @param {Object} [options={}] Options
171170
* @return {Promise<boolean, Error>}
172171
*/
173172
exists: wrapper('access', promise => {
@@ -178,10 +177,11 @@ module.exports = (core) => {
178177
/**
179178
* Get file statistics
180179
* @param {String} file The file path from client
180+
* @param {Object} [options={}] Options
181181
* @return {Object}
182182
*/
183-
stat: vfs => file =>
184-
Promise.resolve(getRealPath(core, vfs.req, vfs.mount, file))
183+
stat: vfs => (file, options = {}) =>
184+
Promise.resolve(getRealPath(core, options.session, vfs.mount, file))
185185
.then(realPath => {
186186
return fs.access(realPath, fs.F_OK)
187187
.then(() => createFileIter(core, path.dirname(realPath), realPath));
@@ -190,10 +190,11 @@ module.exports = (core) => {
190190
/**
191191
* Reads directory
192192
* @param {String} root The file path from client
193+
* @param {Object} [options={}] Options
193194
* @return {Object[]}
194195
*/
195-
readdir: vfs => root =>
196-
Promise.resolve(getRealPath(core, vfs.req, vfs.mount, root))
196+
readdir: vfs => (root, options) =>
197+
Promise.resolve(getRealPath(core, options.session, vfs.mount, root))
197198
.then(realPath => fs.readdir(realPath).then(files => ({realPath, files})))
198199
.then(({realPath, files}) => {
199200
const promises = files.map(f => createFileIter(core, realPath, root.replace(/\/?$/, '/') + f));
@@ -203,10 +204,11 @@ module.exports = (core) => {
203204
/**
204205
* Reads file stream
205206
* @param {String} file The file path from client
207+
* @param {Object} [options={}] Options
206208
* @return {stream.Readable}
207209
*/
208210
readfile: vfs => (file, options = {}) =>
209-
Promise.resolve(getRealPath(core, vfs.req, vfs.mount, file))
211+
Promise.resolve(getRealPath(core, options.session, vfs.mount, file))
210212
.then(realPath => fs.stat(realPath).then(stat => ({realPath, stat})))
211213
.then(({realPath, stat}) => {
212214
if (!stat.isFile()) {
@@ -224,9 +226,10 @@ module.exports = (core) => {
224226
/**
225227
* Creates directory
226228
* @param {String} file The file path from client
229+
* @param {Object} [options={}] Options
227230
* @return {boolean}
228231
*/
229-
mkdir: wrapper('mkdir', (promise, options) => {
232+
mkdir: wrapper('mkdir', (promise, options = {}) => {
230233
return promise
231234
.then(() => true)
232235
.catch(e => {
@@ -242,13 +245,14 @@ module.exports = (core) => {
242245
* Writes file stream
243246
* @param {String} file The file path from client
244247
* @param {stream.Readable} data The stream
248+
* @param {Object} [options={}] Options
245249
* @return {Promise<boolean, Error>}
246250
*/
247-
writefile: vfs => (file, data) => new Promise((resolve, reject) => {
251+
writefile: vfs => (file, data, options = {}) => new Promise((resolve, reject) => {
248252
// FIXME: Currently this actually copies the file because
249253
// formidable will put this in a temporary directory.
250254
// It would probably be better to do a "rename()" on local filesystems
251-
const realPath = getRealPath(core, vfs.req, vfs.mount, file);
255+
const realPath = getRealPath(core, options.session, vfs.mount, file);
252256

253257
const write = () => {
254258
const stream = fs.createWriteStream(realPath);
@@ -270,6 +274,7 @@ module.exports = (core) => {
270274
* Renames given file or directory
271275
* @param {String} src The source file path from client
272276
* @param {String} dest The destination file path from client
277+
* @param {Object} [options={}] Options
273278
* @return {boolean}
274279
*/
275280
rename: crossWrapper('rename'),
@@ -278,24 +283,27 @@ module.exports = (core) => {
278283
* Copies given file or directory
279284
* @param {String} src The source file path from client
280285
* @param {String} dest The destination file path from client
286+
* @param {Object} [options={}] Options
281287
* @return {boolean}
282288
*/
283289
copy: crossWrapper('copy'),
284290

285291
/**
286292
* Removes given file or directory
287293
* @param {String} file The file path from client
294+
* @param {Object} [options={}] Options
288295
* @return {boolean}
289296
*/
290297
unlink: wrapper('remove'),
291298

292299
/**
293300
* Searches for files and folders
294301
* @param {String} file The file path from client
302+
* @param {Object} [options={}] Options
295303
* @return {boolean}
296304
*/
297-
search: vfs => (root, pattern) =>
298-
Promise.resolve(getRealPath(core, vfs.req, vfs.mount, root))
305+
search: vfs => (root, pattern, options = {}) =>
306+
Promise.resolve(getRealPath(core, options.session, vfs.mount, root))
299307
.then(realPath => {
300308
return fh.create()
301309
.paths(realPath)
@@ -323,16 +331,18 @@ module.exports = (core) => {
323331
/**
324332
* Touches a file
325333
* @param {String} file The file path from client
334+
* @param {Object} [options={}] Options
326335
* @return {boolean}
327336
*/
328337
touch: wrapper('ensureFile'),
329338

330339
/**
331340
* Gets the real filesystem path (internal only)
332341
* @param {String} file The file path from client
342+
* @param {Object} [options={}] Options
333343
* @return {string}
334344
*/
335-
realpath: vfs => file =>
336-
Promise.resolve(getRealPath(core, vfs.req, vfs.mount, file))
345+
realpath: vfs => (file, options = {}) =>
346+
Promise.resolve(getRealPath(core, options.session, vfs.mount, file))
337347
};
338348
};

0 commit comments

Comments
 (0)