aboutsummaryrefslogtreecommitdiff
path: root/test/parallel
diff options
context:
space:
mode:
authorcjihrig <cjihrig@gmail.com>2018-12-04 14:01:06 -0500
committerRich Trott <rtrott@gmail.com>2018-12-06 19:52:02 -0800
commitdf19b07d1fdda7dc7f531f6014cf7c3b0f2978fc (patch)
treeaee3e2d66b45d4b66aa408f97cc9c22d319d7645 /test/parallel
parent87592d1c48cde271eccc92ff31d4a5e7e6f4fb4e (diff)
downloadandroid-node-v8-df19b07d1fdda7dc7f531f6014cf7c3b0f2978fc.tar.gz
android-node-v8-df19b07d1fdda7dc7f531f6014cf7c3b0f2978fc.tar.bz2
android-node-v8-df19b07d1fdda7dc7f531f6014cf7c3b0f2978fc.zip
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>
Diffstat (limited to 'test/parallel')
-rw-r--r--test/parallel/test-fs-write-file-sync.js120
1 files changed, 58 insertions, 62 deletions
diff --git a/test/parallel/test-fs-write-file-sync.js b/test/parallel/test-fs-write-file-sync.js
index cb0f16f370..743a385854 100644
--- a/test/parallel/test-fs-write-file-sync.js
+++ b/test/parallel/test-fs-write-file-sync.js
@@ -21,87 +21,83 @@
'use strict';
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)
common.skip('process.umask is not available in Workers');
-// Need to hijack fs.open/close to make sure that things
-// get closed once they're opened.
-fs._openSync = fs.openSync;
-fs.openSync = openSync;
-fs._closeSync = fs.closeSync;
-fs.closeSync = closeSync;
-
-// Reset the umask for testing
-process.umask(0o000);
+const assert = require('assert');
+const path = require('path');
+const fs = require('fs');
// On Windows chmod is only able to manipulate read-only bit. Test if creating
// the file in read-only mode works.
-if (common.isWindows) {
- mode = 0o444;
-} else {
- mode = 0o755;
-}
+const mode = common.isWindows ? 0o444 : 0o755;
+
+// Reset the umask for testing
+process.umask(0o000);
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
// Test writeFileSync
-const file1 = path.join(tmpdir.path, 'testWriteFileSync.txt');
-
-fs.writeFileSync(file1, '123', { mode });
+{
+ const file = path.join(tmpdir.path, 'testWriteFileSync.txt');
-content = fs.readFileSync(file1, { encoding: 'utf8' });
-assert.strictEqual(content, '123');
-
-assert.strictEqual(fs.statSync(file1).mode & 0o777, mode);
+ fs.writeFileSync(file, '123', { mode });
+ const content = fs.readFileSync(file, { encoding: 'utf8' });
+ assert.strictEqual(content, '123');
+ assert.strictEqual(fs.statSync(file).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 file = path.join(tmpdir.path, 'testAppendFileSync.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);
+ fs.appendFileSync(file, 'abc', { mode });
+ const content = fs.readFileSync(file, { encoding: 'utf8' });
+ assert.strictEqual(content, 'abc');
+ assert.strictEqual(fs.statSync(file).mode & mode, mode);
}
-function closeSync() {
- openCount--;
- return fs._closeSync.apply(fs, arguments);
+// 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
-const file4 = path.join(tmpdir.path, 'testWriteFileSyncFlags.txt');
-
-fs.writeFileSync(file4, 'hello ', { encoding: 'utf8', flag: 'a' });
-fs.writeFileSync(file4, 'world!', { encoding: 'utf8', flag: 'a' });
+{
+ const file = path.join(tmpdir.path, 'testWriteFileSyncFlags.txt');
-content = fs.readFileSync(file4, { encoding: 'utf8' });
-assert.strictEqual(content, 'hello world!');
+ fs.writeFileSync(file, 'hello ', { encoding: 'utf8', flag: 'a' });
+ fs.writeFileSync(file, 'world!', { encoding: 'utf8', flag: 'a' });
+ const content = fs.readFileSync(file, { encoding: 'utf8' });
+ assert.strictEqual(content, 'hello world!');
+}