GeezerVison is showing github.com — test: increase coverage for fs.promises.truncate · nodejs/node@94dcdfb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | +const path = require('path'); | | 6 | +const { open, readFile } = require('fs').promises; | | 7 | +const tmpdir = require('../common/tmpdir'); | | 8 | + | | 9 | +tmpdir.refresh(); | | 10 | +common.crashOnUnhandledRejection(); | | 11 | + | | 12 | +async function validateTruncate() { | | 13 | + const text = 'Hello world'; | | 14 | + const filename = path.resolve(tmpdir.path, 'truncate-file.txt'); | | 15 | + const fileHandle = await open(filename, 'w+'); | | 16 | + | | 17 | + const buffer = Buffer.from(text, 'utf8'); | | 18 | + await fileHandle.write(buffer, 0, buffer.length); | | 19 | + | | 20 | + assert.deepStrictEqual((await readFile(filename)).toString(), text); | | 21 | + | | 22 | + await fileHandle.truncate(5); | | 23 | + assert.deepStrictEqual((await readFile(filename)).toString(), 'Hello'); | | 24 | +} | | 25 | + | | 26 | +validateTruncate().then(common.mustCall()); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-promises.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,13 +17,15 @@ const { | 17 | 17 | mkdir, | 18 | 18 | mkdtemp, | 19 | 19 | open, | | 20 | + readFile, | 20 | 21 | readdir, | 21 | 22 | readlink, | 22 | 23 | realpath, | 23 | 24 | rename, | 24 | 25 | rmdir, | 25 | 26 | stat, | 26 | 27 | symlink, | | 28 | + truncate, | 27 | 29 | unlink, | 28 | 30 | utimes | 29 | 31 | } = fsPromises; | @@ -99,6 +101,8 @@ function verifyStatObject(stat) { | 99 | 101 | const ret2 = await handle.read(Buffer.alloc(buf2Len), 0, buf2Len, 0); | 100 | 102 | assert.strictEqual(ret2.bytesRead, buf2Len); | 101 | 103 | assert.deepStrictEqual(ret2.buffer, buf2); | | 104 | + await truncate(dest, 5); | | 105 | + assert.deepStrictEqual((await readFile(dest)).toString(), 'hello'); | 102 | 106 |
| 103 | 107 | await chmod(dest, 0o666); | 104 | 108 | await handle.chmod(0o666); | |
|