Skip to content

Commit b46177c

Browse files
committed
test: fix fs tests affected by optimizations
1 parent 85f2c1f commit b46177c

File tree

6 files changed

+44
-55
lines changed

6 files changed

+44
-55
lines changed

test/parallel/test-fs-read-stream-err.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ stream.on('error', common.mustCall((err_) => {
3636
}));
3737
}));
3838

39-
fs.close = common.mustCall((fd_, cb) => {
40-
assert.strictEqual(fd_, stream.fd);
41-
process.nextTick(cb);
39+
process.binding('fs').close = common.mustCall((fd, req) => {
40+
assert.strictEqual(fd, stream.fd);
41+
process.nextTick(req.oncomplete.bind(req));
4242
});
4343

44-
const read = fs.read;
45-
fs.read = function() {
44+
const read = process.binding('fs').read;
45+
process.binding('fs').read = function() {
4646
// first time is ok.
47-
read.apply(fs, arguments);
47+
read.apply(null, arguments);
4848
// then it breaks
49-
fs.read = common.mustCall(function() {
50-
const cb = arguments[arguments.length - 1];
49+
process.binding('fs').read = common.mustCall(function() {
50+
const req = arguments[arguments.length - 1];
5151
process.nextTick(() => {
52-
cb(err);
52+
req.oncomplete(err);
5353
});
5454
// and should not be called again!
55-
fs.read = () => {
55+
process.binding('fs').read = () => {
5656
throw new Error('BOOM!');
5757
};
5858
});

test/parallel/test-fs-sync-fd-leak.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ require('../common');
2424
const assert = require('assert');
2525
const fs = require('fs');
2626

27-
// ensure that (read|write|append)FileSync() closes the file descriptor
28-
fs.openSync = function() {
27+
process.binding('fs').open = function() {
2928
return 42;
3029
};
31-
fs.closeSync = function(fd) {
30+
// ensure that (read|write|append)FileSync() closes the file descriptor
31+
process.binding('fs').close = function(fd) {
3232
assert.strictEqual(fd, 42);
3333
close_called++;
3434
};
35-
fs.readSync = function() {
35+
process.binding('fs').read = function() {
3636
throw new Error('BAM');
3737
};
38-
fs.writeSync = function() {
38+
process.binding('fs').writeBuffer = function() {
3939
throw new Error('BAM');
4040
};
41-
4241
process.binding('fs').fstat = function() {
4342
throw new Error('BAM');
4443
};

test/parallel/test-fs-write-file-sync.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ let openCount = 0;
2828
let mode;
2929
let content;
3030

31-
// Need to hijack fs.open/close to make sure that things
32-
// get closed once they're opened.
33-
fs._openSync = fs.openSync;
34-
fs.openSync = openSync;
35-
fs._closeSync = fs.closeSync;
36-
fs.closeSync = closeSync;
31+
// Need to hijack binding to make sure that things get closed once they're
32+
// opened.
33+
const open = process.binding('fs').open;
34+
process.binding('fs').open = openSync;
35+
const close = process.binding('fs').close;
36+
process.binding('fs').close = closeSync;
3737

3838
// Reset the umask for testing
3939
process.umask(0o000);
@@ -85,10 +85,10 @@ assert.strictEqual(openCount, 0);
8585

8686
function openSync() {
8787
openCount++;
88-
return fs._openSync.apply(fs, arguments);
88+
return open.apply(null, arguments);
8989
}
9090

9191
function closeSync() {
9292
openCount--;
93-
return fs._closeSync.apply(fs, arguments);
93+
return close.apply(null, arguments);
9494
}

test/parallel/test-fs-write-stream-change-open.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,24 @@ const file = path.join(common.tmpDir, 'write.txt');
3030
common.refreshTmpDir();
3131

3232
const stream = fs.WriteStream(file);
33-
const _fs_close = fs.close;
34-
const _fs_open = fs.open;
35-
36-
// change the fs.open with an identical function after the WriteStream
37-
// has pushed it onto its internal action queue, but before it's
38-
// returned. This simulates AOP-style extension of the fs lib.
39-
fs.open = function() {
40-
return _fs_open.apply(fs, arguments);
33+
const close = process.binding('fs').close;
34+
const open = process.binding('fs').open;
35+
36+
// change the binding.open with an identical function after the WriteStream
37+
// has pushed it onto its internal action queue, but before it's returned.
38+
process.binding('fs').open = function() {
39+
return open.apply(null, arguments);
4140
};
4241

43-
fs.close = function(fd) {
42+
process.binding('fs').close = function(fd) {
4443
assert.ok(fd, 'fs.close must not be called with an undefined fd.');
45-
fs.close = _fs_close;
46-
fs.open = _fs_open;
44+
process.binding('fs').close = close;
45+
process.binding('fs').open = open;
4746
};
4847

4948
stream.write('foo');
5049
stream.end();
5150

5251
process.on('exit', function() {
53-
assert.strictEqual(fs.open, _fs_open);
52+
assert.strictEqual(process.binding('fs').open, open);
5453
});

test/parallel/test-fs-write-stream-err.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,44 +31,35 @@ const stream = fs.createWriteStream(common.tmpDir + '/out', {
3131
});
3232
const err = new Error('BAM');
3333

34-
const write = fs.write;
34+
const writeBuffer = process.binding('fs').writeBuffer;
3535
let writeCalls = 0;
36-
fs.write = function() {
36+
process.binding('fs').writeBuffer = common.mustCall(function() {
3737
switch (writeCalls++) {
3838
case 0:
39-
console.error('first write');
4039
// first time is ok.
41-
return write.apply(fs, arguments);
40+
return writeBuffer.apply(null, arguments);
4241
case 1:
4342
// then it breaks
44-
console.error('second write');
45-
const cb = arguments[arguments.length - 1];
43+
const req = arguments[arguments.length - 1];
4644
return process.nextTick(function() {
47-
cb(err);
45+
req.oncomplete(err);
4846
});
49-
default:
50-
// and should not be called again!
51-
throw new Error('BOOM!');
5247
}
53-
};
48+
}, 2);
5449

55-
fs.close = common.mustCall(function(fd_, cb) {
56-
console.error('fs.close', fd_, stream.fd);
57-
assert.strictEqual(fd_, stream.fd);
58-
process.nextTick(cb);
50+
process.binding('fs').close = common.mustCall(function(fd, req) {
51+
assert.strictEqual(fd, stream.fd);
52+
process.nextTick(req.oncomplete.bind(req));
5953
});
6054

6155
stream.on('error', common.mustCall(function(err_) {
62-
console.error('error handler');
6356
assert.strictEqual(stream.fd, null);
6457
assert.strictEqual(err_, err);
6558
}));
6659

6760

6861
stream.write(Buffer.allocUnsafe(256), function() {
69-
console.error('first cb');
7062
stream.write(Buffer.allocUnsafe(256), common.mustCall(function(err_) {
71-
console.error('second cb');
7263
assert.strictEqual(err_, err);
7364
}));
7465
});

test/parallel/test-npm-install.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ execFile(process.execPath, args, {
5555
const code = err.code;
5656
const signal = err.signal;
5757
stderr += '';
58-
var msg = `npm install got error code ${code}\nstderr:\n${stderr}`;
58+
let msg = `npm install got error code ${code}\nstderr:\n${stderr}`;
5959
assert.strictEqual(code, 0, msg);
6060
msg = `unexpected signal: ${signal}\nstderr:\n${stderr}`;
6161
assert.strictEqual(signal, null, msg);

0 commit comments

Comments
 (0)