Skip to content
Permalink
Browse files

test: add tests for fs/promises.js fileHandle methods

Working to increase test code coverage for fs/promises.js.
Added tests for fileHandle.appendFile and fileHandle.chmod.

PR-URL: #19605
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information...
willhayslett authored and Trott committed Mar 26, 2018
1 parent 0876a03 commit 126b03e2f9e5a695b025a1a70eb49b88de43d080
@@ -0,0 +1,41 @@
'use strict';

const common = require('../common');

// The following tests validate base functionality for the fs/promises
// FileHandle.appendFile method.

const fs = require('fs');
const { open } = require('fs/promises');
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

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');

await fileHandle.appendFile(buffer);
const appendedFileData = fs.readFileSync(filePath);
assert.deepStrictEqual(appendedFileData, buffer);
}

async function validateAppendString() {
const filePath = path.resolve(tmpDir, 'tmp-append-file-string.txt');
const fileHandle = await open(filePath, 'a');
const string = 'x~yz'.repeat(100);

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

validateAppendBuffer()
.then(validateAppendString)
.then(common.mustCall());
@@ -0,0 +1,44 @@
'use strict';

const common = require('../common');

// The following tests validate base functionality for the fs/promises
// FileHandle.chmod method.

const fs = require('fs');
const { open } = require('fs/promises');
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

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
const statsBeforeMod = fs.statSync(filePath);
assert.deepStrictEqual(statsBeforeMod.mode & 0o444, 0o444);

let expectedAccess;
const newPermissions = 0o765;

if (common.isWindows) {
// chmod in Windows will only toggle read only/write access. the
// fs.Stats.mode in Windows is computed using read/write
// bits (not exec). read only at best returns 444; r/w 666.
// refer: /deps/uv/src/win/fs.cfs;
expectedAccess = 0o664;
} else {
expectedAccess = newPermissions;
}

// change the permissions to rwxr--r-x
await fileHandle.chmod(newPermissions);
const statsAfterMod = fs.statSync(filePath);
assert.deepStrictEqual(statsAfterMod.mode & expectedAccess, expectedAccess);
}

validateFilePermission().then(common.mustCall());
@@ -0,0 +1,45 @@
'use strict';

const common = require('../common');

// The following tests validate base functionality for the fs/promises
// FileHandle.read method.

const fs = require('fs');
const { open } = require('fs/promises');
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

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');

const fd = fs.openSync(filePath, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
fs.closeSync(fd);
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
assert.deepStrictEqual(buffer, readAsyncHandle.buffer);
}

async function validateEmptyRead() {
const filePath = path.resolve(tmpDir, 'tmp-read-empty-file.txt');
const fileHandle = await open(filePath, 'w+');
const buffer = Buffer.from('', 'utf8');

const fd = fs.openSync(filePath, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
fs.closeSync(fd);
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
}

validateRead()
.then(validateEmptyRead)
.then(common.mustCall());
@@ -0,0 +1,32 @@
'use strict';

const common = require('../common');

// The following tests validate base functionality for the fs/promises
// FileHandle.readFile method.

const fs = require('fs');
const { open } = require('fs/promises');
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

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');

const fd = fs.openSync(filePath, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
fs.closeSync(fd);

const readFileData = await fileHandle.readFile();
assert.deepStrictEqual(buffer, readFileData);
}

validateReadFile()
.then(common.mustCall());
@@ -0,0 +1,40 @@
'use strict';

const common = require('../common');

// The following tests validate base functionality for the fs/promises
// FileHandle.read method.

const fs = require('fs');
const { open } = require('fs/promises');
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

tmpdir.refresh();
common.crashOnUnhandledRejection();

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');

await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);
}

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

await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);
}

validateWrite()
.then(validateEmptyWrite)
.then(common.mustCall());
@@ -0,0 +1,29 @@
'use strict';

const common = require('../common');

// The following tests validate base functionality for the fs/promises
// FileHandle.readFile method.

const fs = require('fs');
const { open } = require('fs/promises');
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

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');

await fileHandle.writeFile(buffer);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);
}

validateWriteFile()
.then(common.mustCall());

0 comments on commit 126b03e

Please sign in to comment.
You can’t perform that action at this time.