This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.js
156 lines (115 loc) · 4.47 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* global describe, it */
const heimdall = require('heimdalljs');
const FSMonitor = require('./');
const expect = require('chai').expect;
const fs = require('fs');
const path = require('path');
const originalFS = Object.assign({}, fs);
describe('FSMonitor', function() {
beforeEach(function() {
process.env.HEIMDALL_FS_MONITOR_CALL_TRACING = 0;
});
it('will only allow one active instance at a time', function() {
let monitor0 = new FSMonitor();
let monitor1 = new FSMonitor();
monitor0.start();
monitor1.start();
expect(monitor0.state, 'monitor0 (m0 active)').to.eql('active');
expect(monitor1.state, 'monitor1 (m0 active)').to.eql('idle');
monitor0.stop();
monitor1.start();
monitor0.start();
expect(monitor0.state, 'monitor0 (m1 active)').to.eql('idle');
expect(monitor1.state, 'monitor1 (m1 active)').to.eql('active');
monitor1.stop();
monitor0.stop();
});
it('does not mutate the prototype of classes on fs [GH#22]', function() {
let monitor = new FSMonitor();
expect(typeof fs.Stats.prototype.isFile).to.equal('function');
monitor.start();
expect(typeof fs.Stats.prototype.isFile).to.equal('function', 'after updating fs');
monitor.stop();
expect(typeof fs.Stats.prototype.isFile).to.equal('function');
});
it('avoids mutating known classes on `fs` [GH#22]', function() {
let monitor = new FSMonitor();
expect(fs.Stats.prototype.isFile).to.be;
expect(fs.Stats).to.equal(originalFS.Stats);
expect(fs.Dirent).to.equal(originalFS.Dirent);
expect(fs.FSWatcher).to.equal(originalFS.FSWatcher);
expect(fs.FileHandle).to.equal(originalFS.FileHandle);
expect(fs.ReadStream).to.equal(originalFS.ReadStream);
expect(fs.WriteStream).to.equal(originalFS.WriteStream);
try {
monitor.start();
// should not have been changed
expect(fs.Stats).to.equal(originalFS.Stats);
expect(fs.Dirent).to.equal(originalFS.Dirent);
expect(fs.FSWatcher).to.equal(originalFS.FSWatcher);
expect(fs.FileHandle).to.equal(originalFS.FileHandle);
expect(fs.ReadStream).to.equal(originalFS.ReadStream);
expect(fs.WriteStream).to.equal(originalFS.WriteStream);
} finally {
// ensure we stop and detach even if we fail an assertion
monitor.stop();
}
expect(fs.Stats).to.equal(originalFS.Stats);
expect(fs.Dirent).to.equal(originalFS.Dirent);
expect(fs.FSWatcher).to.equal(originalFS.FSWatcher);
expect(fs.FileHandle).to.equal(originalFS.FileHandle);
expect(fs.ReadStream).to.equal(originalFS.ReadStream);
expect(fs.WriteStream).to.equal(originalFS.WriteStream);
});
it('should be able to gather call tracking data from fs commands', function() {
process.env.HEIMDALL_FS_MONITOR_CALL_TRACING = 1;
const monitor = new FSMonitor();
monitor.start();
fs.readFileSync(path.resolve(__dirname, 'index.js'));
monitor.stop();
const expected = [
'readFileSync',
'openSync',
'readSync',
'closeSync'
];
if(process.version.match(/v6/)) {
expected.push('fstatSync');
}
expect(Object.keys(heimdall.current.stats.fs).sort()).to.deep.equal(expected.sort());
expect(Object.keys(heimdall.current.stats.fs.readFileSync.invocations).length).to.equal(1);
expect(Object.keys(heimdall.current.stats.fs.readFileSync.invocations[Object.keys(heimdall.current.stats.fs.readFileSync.invocations)[0]])).to.deep.equal([
'lineNumber',
'fileName',
'count'
]);
});
it('should not be able to gather call tracking data from fs commands', function() {
heimdall.current.stats.fs = {};
const monitor = new FSMonitor();
monitor.start();
fs.readFileSync(path.resolve(__dirname, 'index.js'));
monitor.stop();
const expected = [
'readFileSync',
'openSync',
'readSync',
'closeSync'
];
if(process.version.match(/v6/)) {
expected.push('fstatSync');
}
expect(Object.keys(heimdall.current.stats.fs).sort()).to.deep.equal(expected.sort());
expect(Object.keys(heimdall.current.stats.fs.readFileSync.invocations).length).to.equal(0);
});
describe('.prototype.stop', function() {
it('restores fs functions to their defaults', function() {
let monitor = new FSMonitor();
expect(fs.statSync).to.equal(originalFS.statSync);
monitor.start();
expect(fs.statSync).to.not.equal(originalFS.statSync);
monitor.stop();
expect(fs.statSync).to.equal(originalFS.statSync);
});
});
});