1 | var realFs = require('fs');
|
2 | var path = require('path');
|
3 |
|
4 | var rewire = require('rewire');
|
5 |
|
6 | var Binding = require('./binding');
|
7 | var FileSystem = require('./filesystem');
|
8 |
|
9 | var minor = process.versions.node.split('.').slice(0, 2).join('.');
|
10 | var versions = {
|
11 | '0.8': 'fs-0.8.26.js',
|
12 | '0.9': 'fs-0.9.12.js',
|
13 | '0.10': 'fs-0.10.24.js',
|
14 | '0.11': 'fs-0.11.10.js'
|
15 | };
|
16 |
|
17 | var fsName = versions[minor];
|
18 | if (!fsName) {
|
19 | throw new Error('Unsupported Node version: ' + process.versions.node);
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var mockFs = rewire(path.join(__dirname, '..', 'node', fsName));
|
29 | var originalBinding = mockFs.__get__('binding');
|
30 | var originalStats = mockFs.Stats;
|
31 | for (var name in mockFs) {
|
32 | realFs[name] = mockFs[name];
|
33 | }
|
34 |
|
35 | function setBinding(binding, Stats) {
|
36 | mockFs.__set__('binding', binding);
|
37 | mockFs.Stats = realFs.Stats = Stats;
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | var exports = module.exports = function mock(config) {
|
46 | var system = FileSystem.create(config);
|
47 | var binding = new Binding(system);
|
48 | setBinding(binding, binding.Stats);
|
49 | };
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | exports.restore = function() {
|
56 | setBinding(originalBinding, originalStats);
|
57 | };
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | exports.fs = function(config) {
|
66 | var system = FileSystem.create(config);
|
67 | var binding = new Binding(system);
|
68 |
|
69 |
|
70 | var mockFs = rewire(path.join(__dirname, '..', 'node', fsName));
|
71 | mockFs.__set__('binding', binding);
|
72 |
|
73 |
|
74 | mockFs.Stats = binding.Stats;
|
75 |
|
76 | return mockFs;
|
77 | };
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | exports.file = FileSystem.file;
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | exports.directory = FileSystem.directory;
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | exports.symlink = FileSystem.symlink;
|