test: refactor test-fs-write-file-sync.js

This commit reduces shared state by introducing scopes and
block scoped variables. It also makes the monkey patching used
by the test more robust.

PR-URL: https://github.com/nodejs/node/pull/24834
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
cjihrig 2018-12-04 14:01:06 -05:00 committed by Rich Trott
parent 87592d1c48
commit df19b07d1f

View File

@ -21,87 +21,83 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
let openCount = 0;
let mode;
let content;
if (!common.isMainThread) if (!common.isMainThread)
common.skip('process.umask is not available in Workers'); common.skip('process.umask is not available in Workers');
// Need to hijack fs.open/close to make sure that things const assert = require('assert');
// get closed once they're opened. const path = require('path');
fs._openSync = fs.openSync; const fs = require('fs');
fs.openSync = openSync;
fs._closeSync = fs.closeSync;
fs.closeSync = closeSync;
// Reset the umask for testing
process.umask(0o000);
// On Windows chmod is only able to manipulate read-only bit. Test if creating // On Windows chmod is only able to manipulate read-only bit. Test if creating
// the file in read-only mode works. // the file in read-only mode works.
if (common.isWindows) { const mode = common.isWindows ? 0o444 : 0o755;
mode = 0o444;
} else { // Reset the umask for testing
mode = 0o755; process.umask(0o000);
}
const tmpdir = require('../common/tmpdir'); const tmpdir = require('../common/tmpdir');
tmpdir.refresh(); tmpdir.refresh();
// Test writeFileSync // Test writeFileSync
const file1 = path.join(tmpdir.path, 'testWriteFileSync.txt'); {
const file = path.join(tmpdir.path, 'testWriteFileSync.txt');
fs.writeFileSync(file1, '123', { mode }); fs.writeFileSync(file, '123', { mode });
const content = fs.readFileSync(file, { encoding: 'utf8' });
content = fs.readFileSync(file1, { encoding: 'utf8' }); assert.strictEqual(content, '123');
assert.strictEqual(content, '123'); assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
assert.strictEqual(fs.statSync(file1).mode & 0o777, mode);
// Test appendFileSync
const file2 = path.join(tmpdir.path, 'testAppendFileSync.txt');
fs.appendFileSync(file2, 'abc', { mode });
content = fs.readFileSync(file2, { encoding: 'utf8' });
assert.strictEqual(content, 'abc');
assert.strictEqual(fs.statSync(file2).mode & mode, mode);
// Test writeFileSync with file descriptor
const file3 = path.join(tmpdir.path, 'testWriteFileSyncFd.txt');
const fd = fs.openSync(file3, 'w+', mode);
fs.writeFileSync(fd, '123');
fs.closeSync(fd);
content = fs.readFileSync(file3, { encoding: 'utf8' });
assert.strictEqual(content, '123');
assert.strictEqual(fs.statSync(file3).mode & 0o777, mode);
// Verify that all opened files were closed.
assert.strictEqual(openCount, 0);
function openSync() {
openCount++;
return fs._openSync.apply(fs, arguments);
} }
function closeSync() { // Test appendFileSync
openCount--; {
return fs._closeSync.apply(fs, arguments); const file = path.join(tmpdir.path, 'testAppendFileSync.txt');
fs.appendFileSync(file, 'abc', { mode });
const content = fs.readFileSync(file, { encoding: 'utf8' });
assert.strictEqual(content, 'abc');
assert.strictEqual(fs.statSync(file).mode & mode, mode);
}
// Test writeFileSync with file descriptor
{
// Need to hijack fs.open/close to make sure that things
// get closed once they're opened.
const _openSync = fs.openSync;
const _closeSync = fs.closeSync;
let openCount = 0;
fs.openSync = (...args) => {
openCount++;
return _openSync(...args);
};
fs.closeSync = (...args) => {
openCount--;
return _closeSync(...args);
};
const file = path.join(tmpdir.path, 'testWriteFileSyncFd.txt');
const fd = fs.openSync(file, 'w+', mode);
fs.writeFileSync(fd, '123');
fs.closeSync(fd);
const content = fs.readFileSync(file, { encoding: 'utf8' });
assert.strictEqual(content, '123');
assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
// Verify that all opened files were closed.
assert.strictEqual(openCount, 0);
fs.openSync = _openSync;
fs.closeSync = _closeSync;
} }
// Test writeFileSync with flags // Test writeFileSync with flags
const file4 = path.join(tmpdir.path, 'testWriteFileSyncFlags.txt'); {
const file = path.join(tmpdir.path, 'testWriteFileSyncFlags.txt');
fs.writeFileSync(file4, 'hello ', { encoding: 'utf8', flag: 'a' }); fs.writeFileSync(file, 'hello ', { encoding: 'utf8', flag: 'a' });
fs.writeFileSync(file4, 'world!', { encoding: 'utf8', flag: 'a' }); fs.writeFileSync(file, 'world!', { encoding: 'utf8', flag: 'a' });
const content = fs.readFileSync(file, { encoding: 'utf8' });
content = fs.readFileSync(file4, { encoding: 'utf8' }); assert.strictEqual(content, 'hello world!');
assert.strictEqual(content, 'hello world!'); }