Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test: adding 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.
  • Loading branch information
willhayslett committed Apr 4, 2018
commit f98fc81653619cc1b5006014ab916aebd1811003
50 changes: 50 additions & 0 deletions test/parallel/test-fs-promises-file-handle-append-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'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;

async function validateAppendBuffer({ filePath,
fileHandle,
bufferToAppend }) {
await fileHandle.appendFile(bufferToAppend);
const appendedFileData = fs.readFileSync(filePath);
assert.deepStrictEqual(appendedFileData, bufferToAppend);
}

async function validateAppendString({ filePath,
fileHandle,
stringToAppend,
bufferToAppend }) {
await fileHandle.appendFile(stringToAppend);
const stringAsBuffer = Buffer.from(stringToAppend, 'utf8');
const appendedFileData = fs.readFileSync(filePath);
const combinedBuffer = Buffer.concat([bufferToAppend, stringAsBuffer]);
assert.deepStrictEqual(appendedFileData, combinedBuffer);
}

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

const filePath = path.resolve(tmpDir, 'tmp-append-file.txt');
const appendFileDetails = {
filePath,
fileHandle: await open(filePath, 'a'),
bufferToAppend: Buffer.from('a&Dp'.repeat(100), 'utf8'),
stringToAppend: 'x~yz'.repeat(100)
};

await validateAppendBuffer(appendFileDetails);
await validateAppendString(appendFileDetails);
}

executeTests().then(common.mustCall());
43 changes: 43 additions & 0 deletions test/parallel/test-fs-promises-file-handle-chmod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'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;

async function validateFilePermission({ filePath, fileHandle }) {
//file created with r/w, should not have execute
fs.access(filePath, fs.constants.X_OK, common.mustCall(async (err) => {
//err should an instance of Error
assert.deepStrictEqual(err instanceof Error, true);
//add execute permissions
await fileHandle.chmod(0o765);

fs.access(filePath, fs.constants.X_OK, common.mustCall((err) => {
//err should be undefined or null
assert.deepStrictEqual(!err, true);
}));
}));
}

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

const filePath = path.resolve(tmpDir, 'tmp-chmod.txt');
const chmodFileDetails = {
filePath,
fileHandle: await open(filePath, 'w+', 0o666)
};

await validateFilePermission(chmodFileDetails);
}

executeTests().then(common.mustCall());