1 | 'use strict';
|
2 |
|
3 | const Binding = require('./binding');
|
4 | const FSError = require('./error');
|
5 | const FileSystem = require('./filesystem');
|
6 | const realBinding = process.binding('fs');
|
7 | const path = require('path');
|
8 | const fs = require('fs');
|
9 |
|
10 | const toNamespacedPath = FileSystem.toNamespacedPath;
|
11 |
|
12 | const realProcessProps = {
|
13 | cwd: process.cwd,
|
14 | chdir: process.chdir
|
15 | };
|
16 | const realCreateWriteStream = fs.createWriteStream;
|
17 | const realStats = realBinding.Stats;
|
18 | const realStatWatcher = realBinding.StatWatcher;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function patch(key) {
|
32 | const existingMethod = realBinding[key];
|
33 | realBinding[key] = function() {
|
34 | if (this._mockedBinding) {
|
35 | return this._mockedBinding[key].apply(this, arguments);
|
36 | } else {
|
37 | return existingMethod.apply(this, arguments);
|
38 | }
|
39 | }.bind(realBinding);
|
40 | }
|
41 |
|
42 | for (const key in Binding.prototype) {
|
43 | if (typeof realBinding[key] === 'function') {
|
44 |
|
45 | if (key !== 'Stats' && key !== 'StatWatcher') {
|
46 | patch(key);
|
47 | }
|
48 | }
|
49 | }
|
50 |
|
51 | function overrideBinding(binding) {
|
52 | realBinding._mockedBinding = binding;
|
53 |
|
54 | for (const key in binding) {
|
55 | if (typeof realBinding[key] === 'function') {
|
56 |
|
57 | if (key === 'Stats' || key === 'StatWatcher') {
|
58 | realBinding[key] = binding[key];
|
59 | }
|
60 | } else if (typeof realBinding[key] === 'undefined') {
|
61 | realBinding[key] = binding[key];
|
62 | }
|
63 | }
|
64 | }
|
65 |
|
66 | function overrideProcess(cwd, chdir) {
|
67 | process.cwd = cwd;
|
68 | process.chdir = chdir;
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | function overrideCreateWriteStream() {
|
96 | fs.createWriteStream = function(path, options) {
|
97 | const output = realCreateWriteStream(path, options);
|
98 |
|
99 | output._writev = undefined;
|
100 | return output;
|
101 | };
|
102 | }
|
103 |
|
104 | function restoreBinding() {
|
105 | delete realBinding._mockedBinding;
|
106 | realBinding.Stats = realStats;
|
107 | realBinding.StatWatcher = realStatWatcher;
|
108 | }
|
109 |
|
110 | function restoreProcess() {
|
111 | for (const key in realProcessProps) {
|
112 | process[key] = realProcessProps[key];
|
113 | }
|
114 | }
|
115 |
|
116 | function restoreCreateWriteStream() {
|
117 | fs.createWriteStream = realCreateWriteStream;
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | exports = module.exports = function mock(config, options) {
|
130 | const system = FileSystem.create(config, options);
|
131 | const binding = new Binding(system);
|
132 |
|
133 | overrideBinding(binding);
|
134 |
|
135 | let currentPath = process.cwd();
|
136 | overrideProcess(
|
137 | function cwd() {
|
138 | return currentPath;
|
139 | },
|
140 | function chdir(directory) {
|
141 | if (!binding.stat(toNamespacedPath(directory)).isDirectory()) {
|
142 | throw new FSError('ENOTDIR');
|
143 | }
|
144 | currentPath = path.resolve(currentPath, directory);
|
145 | }
|
146 | );
|
147 |
|
148 | overrideCreateWriteStream();
|
149 | };
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | exports.getMockRoot = function() {
|
156 | if (realBinding._mockedBinding) {
|
157 | return realBinding.getSystem().getRoot();
|
158 | } else {
|
159 | return {};
|
160 | }
|
161 | };
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | exports.restore = function() {
|
167 | restoreBinding();
|
168 | restoreProcess();
|
169 | restoreCreateWriteStream();
|
170 | };
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | exports.file = FileSystem.file;
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | exports.directory = FileSystem.directory;
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | exports.symlink = FileSystem.symlink;
|