1 | 'use strict';
|
2 |
|
3 | var realFs = require('fs');
|
4 | var path = require('path');
|
5 |
|
6 | var rewire = require('rewire');
|
7 | var semver = require('semver');
|
8 |
|
9 | var Binding = require('./binding');
|
10 | var FileSystem = require('./filesystem');
|
11 |
|
12 | var versions = {
|
13 | '0.8.x': 'fs-0.8.26.js',
|
14 | '0.9.x': 'fs-0.9.12.js',
|
15 | '0.10.x': 'fs-0.10.28.js',
|
16 | '0.11 - 0.11.14': 'fs-0.11.13.js',
|
17 | '0.11.15 - 0.12.x': 'fs-0.12.0.js',
|
18 | '1.x.x': 'fs-1.1.0.js'
|
19 | };
|
20 | var nodeVersion = process.versions.node;
|
21 | var fsName;
|
22 |
|
23 | Object.keys(versions).forEach(function (version) {
|
24 | if (semver.satisfies(nodeVersion, version)) {
|
25 | fsName = versions[version];
|
26 | return false;
|
27 | }
|
28 | });
|
29 |
|
30 | if (!fsName) {
|
31 | throw new Error('Unsupported Node version: ' + nodeVersion);
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | var mockFs = rewire(path.join(__dirname, '..', 'node', fsName));
|
41 | var originalBinding = mockFs.__get__('binding');
|
42 | var originalStats = mockFs.Stats;
|
43 | for (var name in mockFs) {
|
44 | var descriptor = Object.getOwnPropertyDescriptor(realFs, name);
|
45 |
|
46 | if (!descriptor || descriptor && descriptor.writable) {
|
47 | realFs[name] = mockFs[name];
|
48 | }
|
49 | }
|
50 |
|
51 | function setBinding(binding, Stats) {
|
52 | mockFs.__set__('binding', binding);
|
53 | mockFs.Stats = realFs.Stats = Stats;
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | var exports = module.exports = function mock(config) {
|
62 | var system = FileSystem.create(config);
|
63 | var binding = new Binding(system);
|
64 | setBinding(binding, binding.Stats);
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | exports.restore = function() {
|
72 | setBinding(originalBinding, originalStats);
|
73 | };
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | exports.fs = function(config) {
|
82 | var system = FileSystem.create(config);
|
83 | var binding = new Binding(system);
|
84 |
|
85 |
|
86 | var mockFs = rewire(path.join(__dirname, '..', 'node', fsName));
|
87 | mockFs.__set__('binding', binding);
|
88 |
|
89 |
|
90 | mockFs.Stats = binding.Stats;
|
91 |
|
92 | return mockFs;
|
93 | };
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | exports.file = FileSystem.file;
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | exports.directory = FileSystem.directory;
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | exports.symlink = FileSystem.symlink;
|