Skip to content

Commit 126b03e

Browse files
willhayslettTrott
authored andcommitted
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>
1 parent 0876a03 commit 126b03e

6 files changed

+231
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
// The following tests validate base functionality for the fs/promises
6+
// FileHandle.appendFile method.
7+
8+
const fs = require('fs');
9+
const { open } = require('fs/promises');
10+
const path = require('path');
11+
const tmpdir = require('../common/tmpdir');
12+
const assert = require('assert');
13+
const tmpDir = tmpdir.path;
14+
15+
tmpdir.refresh();
16+
common.crashOnUnhandledRejection();
17+
18+
async function validateAppendBuffer() {
19+
const filePath = path.resolve(tmpDir, 'tmp-append-file-buffer.txt');
20+
const fileHandle = await open(filePath, 'a');
21+
const buffer = Buffer.from('a&Dp'.repeat(100), 'utf8');
22+
23+
await fileHandle.appendFile(buffer);
24+
const appendedFileData = fs.readFileSync(filePath);
25+
assert.deepStrictEqual(appendedFileData, buffer);
26+
}
27+
28+
async function validateAppendString() {
29+
const filePath = path.resolve(tmpDir, 'tmp-append-file-string.txt');
30+
const fileHandle = await open(filePath, 'a');
31+
const string = 'x~yz'.repeat(100);
32+
33+
await fileHandle.appendFile(string);
34+
const stringAsBuffer = Buffer.from(string, 'utf8');
35+
const appendedFileData = fs.readFileSync(filePath);
36+
assert.deepStrictEqual(appendedFileData, stringAsBuffer);
37+
}
38+
39+
validateAppendBuffer()
40+
.then(validateAppendString)
41+
.then(common.mustCall());
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
// The following tests validate base functionality for the fs/promises
6+
// FileHandle.chmod method.
7+
8+
const fs = require('fs');
9+
const { open } = require('fs/promises');
10+
const path = require('path');
11+
const tmpdir = require('../common/tmpdir');
12+
const assert = require('assert');
13+
const tmpDir = tmpdir.path;
14+
15+
tmpdir.refresh();
16+
common.crashOnUnhandledRejection();
17+
18+
async function validateFilePermission() {
19+
const filePath = path.resolve(tmpDir, 'tmp-chmod.txt');
20+
const fileHandle = await open(filePath, 'w+', 0o444);
21+
// file created with r--r--r-- 444
22+
const statsBeforeMod = fs.statSync(filePath);
23+
assert.deepStrictEqual(statsBeforeMod.mode & 0o444, 0o444);
24+
25+
let expectedAccess;
26+
const newPermissions = 0o765;
27+
28+
if (common.isWindows) {
29+
// chmod in Windows will only toggle read only/write access. the
30+
// fs.Stats.mode in Windows is computed using read/write
31+
// bits (not exec). read only at best returns 444; r/w 666.
32+
// refer: /deps/uv/src/win/fs.cfs;
33+
expectedAccess = 0o664;
34+
} else {
35+
expectedAccess = newPermissions;
36+
}
37+
38+
// change the permissions to rwxr--r-x
39+
await fileHandle.chmod(newPermissions);
40+
const statsAfterMod = fs.statSync(filePath);
41+
assert.deepStrictEqual(statsAfterMod.mode & expectedAccess, expectedAccess);
42+
}
43+
44+
validateFilePermission().then(common.mustCall());
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
// The following tests validate base functionality for the fs/promises
6+
// FileHandle.read method.
7+
8+
const fs = require('fs');
9+
const { open } = require('fs/promises');
10+
const path = require('path');
11+
const tmpdir = require('../common/tmpdir');
12+
const assert = require('assert');
13+
const tmpDir = tmpdir.path;
14+
15+
tmpdir.refresh();
16+
common.crashOnUnhandledRejection();
17+
18+
async function validateRead() {
19+
const filePath = path.resolve(tmpDir, 'tmp-read-file.txt');
20+
const fileHandle = await open(filePath, 'w+');
21+
const buffer = Buffer.from('Hello world', 'utf8');
22+
23+
const fd = fs.openSync(filePath, 'w+');
24+
fs.writeSync(fd, buffer, 0, buffer.length);
25+
fs.closeSync(fd);
26+
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
27+
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
28+
assert.deepStrictEqual(buffer, readAsyncHandle.buffer);
29+
}
30+
31+
async function validateEmptyRead() {
32+
const filePath = path.resolve(tmpDir, 'tmp-read-empty-file.txt');
33+
const fileHandle = await open(filePath, 'w+');
34+
const buffer = Buffer.from('', 'utf8');
35+
36+
const fd = fs.openSync(filePath, 'w+');
37+
fs.writeSync(fd, buffer, 0, buffer.length);
38+
fs.closeSync(fd);
39+
const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
40+
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
41+
}
42+
43+
validateRead()
44+
.then(validateEmptyRead)
45+
.then(common.mustCall());
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
// The following tests validate base functionality for the fs/promises
6+
// FileHandle.readFile method.
7+
8+
const fs = require('fs');
9+
const { open } = require('fs/promises');
10+
const path = require('path');
11+
const tmpdir = require('../common/tmpdir');
12+
const assert = require('assert');
13+
const tmpDir = tmpdir.path;
14+
15+
tmpdir.refresh();
16+
common.crashOnUnhandledRejection();
17+
18+
async function validateReadFile() {
19+
const filePath = path.resolve(tmpDir, 'tmp-read-file.txt');
20+
const fileHandle = await open(filePath, 'w+');
21+
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');
22+
23+
const fd = fs.openSync(filePath, 'w+');
24+
fs.writeSync(fd, buffer, 0, buffer.length);
25+
fs.closeSync(fd);
26+
27+
const readFileData = await fileHandle.readFile();
28+
assert.deepStrictEqual(buffer, readFileData);
29+
}
30+
31+
validateReadFile()
32+
.then(common.mustCall());
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
// The following tests validate base functionality for the fs/promises
6+
// FileHandle.read method.
7+
8+
const fs = require('fs');
9+
const { open } = require('fs/promises');
10+
const path = require('path');
11+
const tmpdir = require('../common/tmpdir');
12+
const assert = require('assert');
13+
const tmpDir = tmpdir.path;
14+
15+
tmpdir.refresh();
16+
common.crashOnUnhandledRejection();
17+
18+
async function validateWrite() {
19+
const filePathForHandle = path.resolve(tmpDir, 'tmp-write.txt');
20+
const fileHandle = await open(filePathForHandle, 'w+');
21+
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');
22+
23+
await fileHandle.write(buffer, 0, buffer.length);
24+
const readFileData = fs.readFileSync(filePathForHandle);
25+
assert.deepStrictEqual(buffer, readFileData);
26+
}
27+
28+
async function validateEmptyWrite() {
29+
const filePathForHandle = path.resolve(tmpDir, 'tmp-empty-write.txt');
30+
const fileHandle = await open(filePathForHandle, 'w+');
31+
const buffer = Buffer.from(''); // empty buffer
32+
33+
await fileHandle.write(buffer, 0, buffer.length);
34+
const readFileData = fs.readFileSync(filePathForHandle);
35+
assert.deepStrictEqual(buffer, readFileData);
36+
}
37+
38+
validateWrite()
39+
.then(validateEmptyWrite)
40+
.then(common.mustCall());
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
// The following tests validate base functionality for the fs/promises
6+
// FileHandle.readFile method.
7+
8+
const fs = require('fs');
9+
const { open } = require('fs/promises');
10+
const path = require('path');
11+
const tmpdir = require('../common/tmpdir');
12+
const assert = require('assert');
13+
const tmpDir = tmpdir.path;
14+
15+
tmpdir.refresh();
16+
common.crashOnUnhandledRejection();
17+
18+
async function validateWriteFile() {
19+
const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file2.txt');
20+
const fileHandle = await open(filePathForHandle, 'w+');
21+
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');
22+
23+
await fileHandle.writeFile(buffer);
24+
const readFileData = fs.readFileSync(filePathForHandle);
25+
assert.deepStrictEqual(buffer, readFileData);
26+
}
27+
28+
validateWriteFile()
29+
.then(common.mustCall());

0 commit comments

Comments
 (0)