Files: d456a6cc8589489e558e6164750474ed2cfc961e / test / test-50-fs-runtime-layer-2 / test-x-index.js
12769 bytesRaw
1 | /* eslint-disable max-statements-per-line */ |
2 | |
3 | 'use strict'; |
4 | |
5 | const fs = require('fs'); |
6 | const path = require('path'); |
7 | const assert = require('assert'); |
8 | const theFile = path.join(__dirname, 'test-z-asset.css'); |
9 | const theDirectory = __dirname; |
10 | |
11 | function dumpError (error) { |
12 | let s = error.message; |
13 | if (s === 'Bad argument') { |
14 | s = 'fd must be a file descriptor'; |
15 | } else |
16 | if ((s === 'EBADF, bad file descriptor') && (error.syscall === 'fstat')) { |
17 | s = 'EBADF: bad file descriptor, fstat'; |
18 | } else |
19 | if (s === 'EBADF, bad file descriptor') { |
20 | s = 'EBADF: bad file descriptor'; |
21 | } else |
22 | if (s === 'EBADF: bad file descriptor, close') { |
23 | s = 'EBADF: bad file descriptor'; |
24 | } else |
25 | if (s === 'EISDIR, illegal operation on a directory') { |
26 | s = 'EISDIR: illegal operation on a directory, read'; |
27 | } |
28 | console.log(s); |
29 | } |
30 | |
31 | test01(); |
32 | |
33 | function test01 () { |
34 | console.log('<<< test01 >>>'); |
35 | |
36 | fs.stat(theFile, function (error, stats) { |
37 | console.log('fs.stat.error === null', error === null); |
38 | console.log('stats.size', stats.size); |
39 | fs.open(theFile, 'r', function (error2, fd) { |
40 | console.log('fs.open.error2 === null', error2 === null); |
41 | console.log('typeof fd', typeof fd); |
42 | fs.fstat(fd, function (error3, fstats) { |
43 | console.log('fs.fstat.error3 === null', error3 === null); |
44 | console.log('fstats.size', fstats.size); |
45 | const buffer = Buffer.alloc(stats.size / 2 | 0); |
46 | fs.read(fd, buffer, 0, buffer.length, null, function (error4, bytesRead, buffer2) { |
47 | console.log('fs.read.error4 === null', error4 === null); |
48 | console.log('buffer === buffer2', buffer === buffer2); // should be same instances |
49 | const data2 = buffer2.toString('utf8', 0, buffer2.length); |
50 | console.log('data2', data2); |
51 | fs.close(fd, function (error5, wtf) { |
52 | console.log('fs.close.error5 === null', error5 === null); |
53 | console.log('typeof wtf', typeof wtf); |
54 | fs.readFile(theFile, function (error6, buffer3) { |
55 | console.log('fs.readFile.error6 === null', error6 === null); |
56 | const data3 = buffer3.toString('utf8', 0, buffer3.length); |
57 | console.log('data3', data3); |
58 | const buffer4 = buffer3; |
59 | fs.writeFile(theFile, buffer4, function (error7, wtf2) { |
60 | if (process.pkg) { |
61 | assert.equal(typeof error7, 'object'); // TODO maybe code=EACCESS? |
62 | } else { |
63 | assert.equal(error7, null); |
64 | } |
65 | console.log('typeof wtf2', typeof wtf2); |
66 | fs.readdir(theDirectory, function (error8, list) { |
67 | console.log('fs.readdir.error8 === null', error8 === null); |
68 | console.log('Array.isArray(list)', Array.isArray(list)); |
69 | fs.exists(theFile, function (value, wtf3) { |
70 | console.log('value', value); |
71 | console.log('typeof wtf3', typeof wtf3); |
72 | fs.exists(theDirectory, function (value2, wtf4) { |
73 | console.log('value2', value2); |
74 | console.log('typeof wtf4', typeof wtf4); |
75 | fs.realpath(theFile, function (error9, real) { |
76 | console.log('fs.realpath.error9 === null', error9 === null); |
77 | console.log('typeof real', typeof real); |
78 | test01e(fd); |
79 | }); |
80 | console.log('after fs.realpath'); |
81 | }); |
82 | console.log('after fs.exists(theDirectory)'); |
83 | }); |
84 | console.log('after fs.exists(theFile)'); |
85 | }); |
86 | console.log('after fs.readdir'); |
87 | }); |
88 | console.log('after fs.writeFile'); |
89 | }); |
90 | console.log('after fs.readFile'); |
91 | }); |
92 | console.log('after fs.close'); |
93 | }); |
94 | console.log('after fs.read'); |
95 | }); |
96 | console.log('after fs.fstat'); |
97 | }); |
98 | console.log('after fs.open'); |
99 | }); |
100 | console.log('after fs.stat'); |
101 | } |
102 | |
103 | function test01e (badFd) { |
104 | console.log('<<< test01e >>>'); |
105 | |
106 | fs.stat('notExists', function (error) { |
107 | console.log('fs.stat.error.code', error.code); |
108 | fs.open('notExists', 'r', function (error2, fd) { |
109 | console.log('fs.open.error2.code', error2.code); |
110 | fd = badFd; |
111 | fs.fstat(fd, function (error3) { |
112 | console.log('fs.fstat.error3.code', error3.code); |
113 | const buffer = Buffer.alloc(1024); |
114 | fs.read(fd, buffer, 0, buffer.length, null, function (error4, bytesRead, buffer2) { |
115 | console.log('fs.read.error4.code', error4.code); |
116 | console.log('typeof bytesRead', typeof bytesRead); |
117 | console.log('typeof buffer2', typeof buffer2); |
118 | fs.close(fd, function (error5, wtf) { |
119 | console.log('fs.close.error5.code', error5.code); |
120 | console.log('typeof wtf', typeof wtf); |
121 | fs.readFile(theDirectory, function (error6, buffer3) { |
122 | console.log('fs.readFile.error6.code', error6.code); |
123 | console.log('typeof buffer3', typeof buffer3); |
124 | fs.readFile('notExists', function (error7, buffer4) { |
125 | console.log('fs.readFile.error7.code', error7.code); |
126 | console.log('typeof buffer4', typeof buffer4); |
127 | const buffer5 = Buffer.alloc(1024); |
128 | fs.writeFile(theFile + '/canNotWrite', buffer5, function (error8, wtf2) { |
129 | assert(error8.code === 'ENOENT' || |
130 | error8.code === 'ENOTDIR'); |
131 | console.log('typeof wtf2', typeof wtf2); |
132 | fs.readdir(theFile, function (error9, list) { |
133 | console.log('fs.readdir.error9.code', error9.code); |
134 | console.log('typeof list', typeof list); |
135 | fs.readdir('notExists', function (error10, list2) { |
136 | console.log('fs.readdir.error10.code', error10.code); |
137 | console.log('typeof list2', typeof list2); |
138 | fs.exists('notExists', function (value, wtf3) { |
139 | console.log('value', value); |
140 | console.log('typeof wtf3', typeof wtf3); |
141 | fs.realpath('notExists', function (error11, real) { |
142 | console.log('fs.realpath.error11.code', error11.code); |
143 | console.log('typeof real', typeof real); |
144 | test02(); |
145 | }); |
146 | console.log('after fs.realpath'); |
147 | }); |
148 | console.log('after fs.exists'); |
149 | }); |
150 | console.log('after fs.readdir(notExists)'); |
151 | }); |
152 | console.log('after fs.readdir(theFile)'); |
153 | }); |
154 | console.log('after fs.writeFile'); |
155 | }); |
156 | console.log('after fs.readFile(notExists)'); |
157 | }); |
158 | console.log('after fs.readFile(theDirectory)'); |
159 | }); |
160 | console.log('after fs.close'); |
161 | }); |
162 | console.log('after fs.read'); |
163 | }); |
164 | console.log('after fs.fstat'); |
165 | }); |
166 | console.log('after fs.open'); |
167 | }); |
168 | console.log('after fs.stat'); |
169 | } |
170 | |
171 | function test02 () { |
172 | console.log('<<< test02 >>>'); |
173 | |
174 | const stats = fs.statSync(theFile); |
175 | console.log('stats.size', stats.size); |
176 | const fd = fs.openSync(theFile, 'r'); |
177 | const fstats = fs.fstatSync(fd); |
178 | console.log('fstats.size', fstats.size); |
179 | const buffer = Buffer.alloc(stats.size); |
180 | const bytesRead = fs.readSync(fd, buffer, 0, buffer.length); |
181 | console.log('bytesRead', bytesRead); |
182 | const data = buffer.toString('utf8', 0, buffer.length); |
183 | console.log('data', data); |
184 | fs.closeSync(fd); |
185 | test03(); |
186 | } |
187 | |
188 | function test03 () { |
189 | console.log('<<< test03 >>>'); |
190 | |
191 | const stats = fs.statSync(theFile); |
192 | console.log('stats.size', stats.size); |
193 | const fd = fs.openSync(theFile, 'r'); |
194 | const fstats = fs.fstatSync(fd); |
195 | console.log('fstats.size', fstats.size); |
196 | const buffer = Buffer.alloc(6); |
197 | let bytesRead = fs.readSync(fd, buffer, 0, 6); |
198 | console.log('bytesRead_a', bytesRead, 'buffer', buffer[0], buffer[1]); |
199 | bytesRead = fs.readSync(fd, buffer, 0, 6); |
200 | console.log('bytesRead_b', bytesRead, 'buffer', buffer[0], buffer[1]); |
201 | bytesRead = fs.readSync(fd, buffer, 1, 5); |
202 | console.log('bytesRead_c', bytesRead, 'buffer', buffer[1], buffer[2]); |
203 | try { bytesRead = fs.readSync(fd, buffer, 1, 6); } catch (error) { dumpError(error); } |
204 | console.log('bytesRead_d', bytesRead, 'buffer', buffer[1], buffer[2]); |
205 | bytesRead = fs.readSync(fd, buffer, 5, 1); |
206 | console.log('bytesRead_e', bytesRead, 'buffer', buffer[4], buffer[5]); |
207 | try { bytesRead = fs.readSync(fd, buffer, 6, 0); } catch (error) { dumpError(error); } |
208 | console.log('bytesRead_f', bytesRead, 'buffer', buffer[4], buffer[5]); |
209 | try { bytesRead = fs.readSync(fd, buffer, -1, 5); } catch (error) { dumpError(error); } |
210 | console.log('bytesRead_g', bytesRead, 'buffer', buffer[4], buffer[5]); |
211 | try { bytesRead = fs.readSync(fd, buffer, -1, 9); } catch (error) { dumpError(error); } |
212 | console.log('bytesRead_h', bytesRead, 'buffer', buffer[4], buffer[5]); |
213 | bytesRead = fs.readSync(fd, buffer, 0, 6); |
214 | console.log('bytesRead_i', bytesRead, 'buffer', buffer[0], buffer[1]); |
215 | bytesRead = fs.readSync(fd, buffer, 0, 6); |
216 | console.log('bytesRead_j', bytesRead, 'buffer', buffer[0], buffer[1]); |
217 | bytesRead = fs.readSync(fd, buffer, 0, 6); |
218 | console.log('bytesRead_k', bytesRead, 'buffer', buffer[0], buffer[1]); |
219 | bytesRead = fs.readSync(fd, buffer, 0, 6); |
220 | console.log('bytesRead_l', bytesRead, 'buffer', buffer[0], buffer[1]); |
221 | bytesRead = fs.readSync(fd, buffer, 0, 6); |
222 | console.log('bytesRead_m', bytesRead, 'buffer', buffer[0], buffer[1]); |
223 | bytesRead = fs.readSync(fd, buffer, 0, 6, 20); |
224 | console.log('bytesRead_n', bytesRead, 'buffer', buffer[0], buffer[1]); |
225 | bytesRead = fs.readSync(fd, buffer, 0, 6, 80); |
226 | console.log('bytesRead_o', bytesRead, 'buffer', buffer[0], buffer[1]); |
227 | // this does not work properly. TODO some rework of readFromSnapshot is needed |
228 | // try { bytesRead = fs.readSync(fd, buffer, 424242, 1); } catch (error) { dumpError(error); } |
229 | // console.log('bytesRead_p', bytesRead, 'buffer', buffer[0], buffer[1]); |
230 | fs.closeSync(fd); |
231 | test04(); |
232 | } |
233 | |
234 | function test04 () { |
235 | const stats = fs.statSync(theDirectory); |
236 | console.log(stats.mode); |
237 | const fd = fs.openSync(theDirectory, 'r'); |
238 | const fstats = fs.fstatSync(fd); |
239 | console.log(fstats.mode); |
240 | const buffer = Buffer.from([ 12, 34, 56, 78 ]); |
241 | let bytesRead; |
242 | try { bytesRead = fs.readSync(fd, buffer, 0, 6); } catch (error) { dumpError(error); } |
243 | console.log(bytesRead, buffer[0], buffer[1]); |
244 | try { bytesRead = fs.readSync(fd, buffer, 6, 0); } catch (error) { dumpError(error); } |
245 | console.log(bytesRead, buffer[0], buffer[1]); |
246 | try { bytesRead = fs.readSync(fd, buffer, -1, 3); } catch (error) { dumpError(error); } |
247 | console.log(bytesRead, buffer[0], buffer[1]); |
248 | try { bytesRead = fs.readSync(fd, buffer, 0, 4); } catch (error) { dumpError(error); } |
249 | console.log(bytesRead, buffer[0], buffer[1]); |
250 | fs.closeSync(fd); |
251 | test05(); |
252 | } |
253 | |
254 | function test05 () { |
255 | const fd = 'incorrect fd as string'; |
256 | const buffer = Buffer.from([ 12, 34, 56, 78 ]); |
257 | let bytesRead; |
258 | try { bytesRead = fs.readSync(fd, buffer, 0, 6); } catch (error) { dumpError(error); } |
259 | console.log(bytesRead, buffer[0], buffer[1]); |
260 | try { console.log(fs.fstatSync(fd)); } catch (error) { dumpError(error); } |
261 | console.log(bytesRead, buffer[0], buffer[1]); |
262 | try { fs.closeSync(fd); } catch (error) { dumpError(error); } |
263 | test06(); |
264 | } |
265 | |
266 | function test06 () { |
267 | const fd = 7890; |
268 | const buffer = Buffer.from([ 12, 34, 56, 78 ]); |
269 | let bytesRead; |
270 | try { bytesRead = fs.readSync(fd, buffer, 0, 6); } catch (error) { dumpError(error); } |
271 | console.log(bytesRead, buffer[0], buffer[1]); |
272 | try { console.log(fs.fstatSync(fd)); } catch (error) { dumpError(error); } |
273 | console.log(bytesRead, buffer[0], buffer[1]); |
274 | try { fs.closeSync(fd); console.log('EBADF: bad file descriptor'); } catch (error) { dumpError(error); } |
275 | test07(); |
276 | } |
277 | |
278 | function test07 () { |
279 | console.log('before createReadStream'); |
280 | const rs = fs.createReadStream(theFile); |
281 | console.log('after createReadStream'); |
282 | |
283 | rs.on('open', function () { |
284 | console.log('open'); |
285 | }); |
286 | |
287 | rs.on('readable', function () { |
288 | console.log('before read'); |
289 | let r = rs.read(); |
290 | console.log('after read'); |
291 | if (!r) { |
292 | r = 'null'; |
293 | } else |
294 | if (r.length >= 2) { |
295 | console.log('length:', r.length); |
296 | r = r[0].toString() + ', ' + r[1].toString(); |
297 | } |
298 | console.log('readable:', r); |
299 | }); |
300 | |
301 | rs.on('end', function () { |
302 | console.log('end'); |
303 | }); |
304 | } |
305 |
Built with git-ssb-web