Skip to content

Commit

Permalink
test: refactored use of common methods in fs/promises tests
Browse files Browse the repository at this point in the history
Updated tests to ensure common.crashOnUnhandledRejection()
and the common/tmpdir refresh method and are only being called
once per file.
  • Loading branch information
willhayslett committed Apr 4, 2018
1 parent eb0955b commit 5310f92
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 33 deletions.
15 changes: 5 additions & 10 deletions test/parallel/test-fs-promises-file-handle-append-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

async function validateAppendBuffer() {
tmpdir.refresh();
common.crashOnUnhandledRejection();
tmpdir.refresh();
common.crashOnUnhandledRejection();

async function validateAppendBuffer() {
const filePath = path.resolve(tmpDir, 'tmp-append-file-buffer.txt');
const fileHandle = await open(filePath, 'a');
const buffer = Buffer.from('a&Dp'.repeat(100), 'utf8');
Expand All @@ -26,19 +26,14 @@ async function validateAppendBuffer() {
}

async function validateAppendString() {
// don't refresh the directory
common.crashOnUnhandledRejection();

const filePath = path.resolve(tmpDir, 'tmp-append-file-buffer.txt');
const filePath = path.resolve(tmpDir, 'tmp-append-file-string.txt');
const fileHandle = await open(filePath, 'a');
const buffer = Buffer.from('a&Dp'.repeat(100), 'utf8');
const string = 'x~yz'.repeat(100);

await fileHandle.appendFile(string);
const stringAsBuffer = Buffer.from(string, 'utf8');
const appendedFileData = fs.readFileSync(filePath);
const combinedBuffer = Buffer.concat([buffer, stringAsBuffer]);
assert.deepStrictEqual(appendedFileData, combinedBuffer);
assert.deepStrictEqual(appendedFileData, stringAsBuffer);
}

validateAppendBuffer()
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-fs-promises-file-handle-chmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

async function validateFilePermission() {
tmpdir.refresh();
common.crashOnUnhandledRejection();
tmpdir.refresh();
common.crashOnUnhandledRejection();

async function validateFilePermission() {
const filePath = path.resolve(tmpDir, 'tmp-chmod.txt');
const fileHandle = await open(filePath, 'w+', 0o444);
// file created with r--r--r-- 444
Expand All @@ -35,7 +35,7 @@ async function validateFilePermission() {
expectedAccess = newPermissions;
}

//change the permissions to rwxr--r-x
// change the permissions to rwxr--r-x
await fileHandle.chmod(newPermissions);
const statsAfterMod = fs.statSync(filePath);
assert.deepStrictEqual(statsAfterMod.mode & expectedAccess, expectedAccess);
Expand Down
8 changes: 3 additions & 5 deletions test/parallel/test-fs-promises-file-handle-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

async function validateRead() {
tmpdir.refresh();
common.crashOnUnhandledRejection();
tmpdir.refresh();
common.crashOnUnhandledRejection();

async function validateRead() {
const filePath = path.resolve(tmpDir, 'tmp-read-file.txt');
const fileHandle = await open(filePath, 'w+');
const buffer = Buffer.from('Hello world', 'utf8');
Expand All @@ -29,8 +29,6 @@ async function validateRead() {
}

async function validateEmptyRead() {
common.crashOnUnhandledRejection();

const filePath = path.resolve(tmpDir, 'tmp-read-empty-file.txt');
const fileHandle = await open(filePath, 'w+');
const buffer = Buffer.from('', 'utf8');
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-fs-promises-file-handle-readFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

async function validateReadFile() {
tmpdir.refresh();
common.crashOnUnhandledRejection();
tmpdir.refresh();
common.crashOnUnhandledRejection();

async function validateReadFile() {
const filePath = path.resolve(tmpDir, 'tmp-read-file.txt');
const fileHandle = await open(filePath, 'w+');
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');
Expand Down
13 changes: 5 additions & 8 deletions test/parallel/test-fs-promises-file-handle-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

async function validateWrite() {
tmpdir.refresh();
common.crashOnUnhandledRejection();
tmpdir.refresh();
common.crashOnUnhandledRejection();

const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file.txt');
async function validateWrite() {
const filePathForHandle = path.resolve(tmpDir, 'tmp-write.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');

Expand All @@ -26,10 +26,7 @@ async function validateWrite() {
}

async function validateEmptyWrite() {
tmpdir.refresh();
common.crashOnUnhandledRejection();

const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file.txt');
const filePathForHandle = path.resolve(tmpDir, 'tmp-empty-write.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from(''); // empty buffer

Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-fs-promises-file-handle-writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

async function validateWriteFile() {
tmpdir.refresh();
common.crashOnUnhandledRejection();
tmpdir.refresh();
common.crashOnUnhandledRejection();

async function validateWriteFile() {
const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file2.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');
Expand Down

0 comments on commit 5310f92

Please sign in to comment.