-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathfs.writeFile-readFile.spec.js
More file actions
219 lines (178 loc) · 6.73 KB
/
Copy pathfs.writeFile-readFile.spec.js
File metadata and controls
219 lines (178 loc) · 6.73 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict';
const util = require('../lib/test-utils.js');
const expect = require('chai').expect;
describe('fs.writeFile, fs.readFile', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
const fs = util.fs();
expect(fs.writeFile).to.be.a('function');
expect(fs.readFile).to.be.a('function');
});
it('should error when path is wrong to readFile', function(done) {
const fs = util.fs();
fs.readFile('/no-such-file', 'utf8', function(error, data) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
expect(data).not.to.exist;
done();
});
});
it('should error when path is wrong to writeFile',function(done){
const fs = util.fs();
fs.writeFile('/tmp/myfile', '','utf8', function(error, result) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
expect(result).not.to.exist;
done();
});
});
it('should write, read a utf8 file without specifying utf8 in writeFile', function(done) {
const fs = util.fs();
const contents = 'This is a file.';
fs.writeFile('/myfile', contents, function(error) {
if(error) throw error;
fs.readFile('/myfile', 'utf8', function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents);
done();
});
});
});
it('should write a string when given a number for the data', function(done) {
var fs = util.fs();
var contents = 7;
var contentsAsString = '7';
fs.writeFile('/myfile', contents, function(error) {
if(error) throw error;
fs.readFile('/myfile', 'utf8', function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contentsAsString);
done();
});
});
});
it('should write, read a utf8 file with "utf8" option to writeFile', function(done) {
const fs = util.fs();
const contents = 'This is a file.';
fs.writeFile('/myfile', contents, 'utf8', function(error) {
if(error) throw error;
fs.readFile('/myfile', 'utf8', function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents);
done();
});
});
});
it('should write, read a utf8 file with {encoding: "utf8"} option to writeFile', function(done) {
const fs = util.fs();
const contents = 'This is a file.';
fs.writeFile('/myfile', contents, { encoding: 'utf8' }, function(error) {
if(error) throw error;
fs.readFile('/myfile', 'utf8', function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents);
done();
});
});
});
it('should write, read a binary file', function(done) {
const fs = util.fs();
// String and utf8 binary encoded versions of the same thing: 'This is a file.'
const binary = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]);
fs.writeFile('/myfile', binary, function(error) {
if(error) throw error;
fs.readFile('/myfile', function(error, data) {
expect(error).not.to.exist;
expect(data).to.deep.equal(binary);
done();
});
});
});
it('should follow symbolic links', function(done) {
const fs = util.fs();
const contents = 'This is a file.';
fs.writeFile('/myfile', '', { encoding: 'utf8' }, function(error) {
if(error) throw error;
fs.symlink('/myfile', '/myFileLink', function (error) {
if (error) throw error;
fs.writeFile('/myFileLink', contents, 'utf8', function (error) {
if (error) throw error;
fs.readFile('/myFileLink', 'utf8', function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents);
done();
});
});
});
});
});
});
/**
* fsPromises tests
*/
describe('fsPromises.writeFile, fsPromises.readFile', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
const fsPromises = util.fs().promises;
expect(fsPromises.writeFile).to.be.a('function');
expect(fsPromises.readFile).to.be.a('function');
});
it('should return a promise', function() {
const fsPromises = util.fs().promises;
const contents = 'This is a file.';
const p = fsPromises.writeFile('/myfile', contents);
expect(p).to.be.a('Promise');
p.then(() => {
expect(fsPromises.readFile('/myfile', 'utf8')).to.be.a('Promise');
});
return p;
});
it('should error when path is wrong to readFile', function() {
const fsPromises = util.fs().promises;
return fsPromises.readFile('/no-such-file', 'utf8')
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
});
});
it('should write, read a utf8 file without specifying utf8 in writeFile', function() {
const fsPromises = util.fs().promises;
const contents = 'This is a file.';
return fsPromises.writeFile('/myfile', contents)
.then( () => fsPromises.readFile('/myfile', 'utf8'))
.then(data => { expect(data).to.equal(contents); });
});
it('should write, read a utf8 file with "utf8" option to writeFile', function() {
const fsPromises = util.fs().promises;
const contents = 'This is a file.';
return fsPromises.writeFile('/myfile', contents, 'utf8')
.then( () => fsPromises.readFile('/myfile', 'utf8'))
.then(data => { expect(data).to.equal(contents); });
});
it('should write, read a utf8 file with {encoding: "utf8"} option to writeFile', function() {
const fsPromises = util.fs().promises;
const contents = 'This is a file.';
return fsPromises.writeFile('/myfile', contents, { encoding: 'utf8' })
.then( () => fsPromises.readFile('/myfile', 'utf8'))
.then(data => { expect(data).to.equal(contents); });
});
it('should write, read a binary file', function() {
const fsPromises = util.fs().promises;
// String and utf8 binary encoded versions of the same thing: 'This is a file.'
const binary = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]);
return fsPromises.writeFile('/myfile', binary)
.then( () => fsPromises.readFile('/myfile'))
.then(data => { expect(data).to.deep.equal(binary); });
});
it('should follow symbolic links', function() {
const fsPromises = util.fs().promises;
const contents = 'This is a file.';
return fsPromises.writeFile('/myfile', '', { encoding: 'utf8' })
.then( () => fsPromises.symlink('/myfile', '/myFileLink'))
.then( () => fsPromises.writeFile('/myFileLink', contents, 'utf8'))
.then( () => fsPromises.readFile('/myFileLink', 'utf8'))
.then(data => { expect(data).to.equal(contents); });
});
});