/
fs.readFile.spec.js
121 lines (106 loc) · 3.04 KB
/
fs.readFile.spec.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
'use strict';
const helper = require('../helper');
const fs = require('fs');
const mock = require('../../lib/index');
const assert = helper.assert;
const withPromise = helper.withPromise;
describe('fs.readFile(filename, [options], callback)', function() {
// this is provided by fs.open, fs.fstat, and fs.read
// so more heavily tested elsewhere
beforeEach(function() {
mock({
'path/to/file.txt': 'file content'
});
});
afterEach(mock.restore);
it('allows a file to be read asynchronously', function(done) {
fs.readFile('path/to/file.txt', function(err, data) {
if (err) {
return done(err);
}
assert.isTrue(Buffer.isBuffer(data));
assert.equal(String(data), 'file content');
done();
});
});
withPromise.it('promise allows a file to be read asynchronously', function(
done
) {
fs.promises.readFile('path/to/file.txt').then(function(data) {
assert.isTrue(Buffer.isBuffer(data));
assert.equal(String(data), 'file content');
done();
}, done);
});
it('fails for directory', function(done) {
fs.readFile('path/to', function(err, data) {
assert.instanceOf(err, Error);
assert.equal(err.code, 'EISDIR');
done();
});
});
withPromise.it('promise fails for directory', function(done) {
fs.promises.readFile('path/to').then(
function() {
assert.fail('should not succeed.');
done();
},
function(err) {
assert.instanceOf(err, Error);
assert.equal(err.code, 'EISDIR');
done();
}
);
});
it('fails for bad path', function(done) {
fs.readFile('path/to/bogus', function(err, data) {
assert.instanceOf(err, Error);
// windows has different errno for ENOENT
assert.equal(err.code, 'ENOENT');
done();
});
});
withPromise.it('promise fails for bad path', function(done) {
fs.promises.readFile('path/to/bogus').then(
function() {
assert.fail('should not succeed.');
done();
},
function(err) {
assert.instanceOf(err, Error);
// windows has different errno for ENOENT
assert.equal(err.code, 'ENOENT');
done();
}
);
});
});
describe('fs.readFileSync(filename, [options])', function() {
// this is provided by fs.openSync, fs.fstatSync, and fs.readSync
// so more heavily tested elsewhere
beforeEach(function() {
mock({
'path/to/file.txt': 'file content'
});
});
afterEach(mock.restore);
it('allows a file to be read synchronously', function() {
const data = fs.readFileSync('path/to/file.txt');
assert.isTrue(Buffer.isBuffer(data));
assert.equal(String(data), 'file content');
});
it('fails for directory', function() {
try {
fs.readFileSync('path/to');
assert.fail('should not succeed.');
} catch (err) {
assert.instanceOf(err, Error);
assert.equal(err.code, 'EISDIR');
}
});
it('fails for bad path', function() {
assert.throws(function() {
fs.readFileSync('path/to/bogus');
});
});
});