From e74898dc63238668655fde274ee935acf39f6f5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20do=20Carmo?= Date: Tue, 24 Jan 2017 00:58:00 -0300 Subject: [PATCH 1/2] test: expand test coverage of fs.js * test calling truncateSync() passing a file descriptor * test calling truncate() passing undefined as the 2nd argument Refs: https://coverage.nodejs.org/coverage-8ab561b2432bdae3/root/fs.js.html (line 673 and 692) --- test/parallel/test-fs-truncate-sync.js | 17 +++++++++++++++++ test/parallel/test-fs-truncate.js | 11 +++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/parallel/test-fs-truncate-sync.js diff --git a/test/parallel/test-fs-truncate-sync.js b/test/parallel/test-fs-truncate-sync.js new file mode 100644 index 00000000000000..6445dcfdfb28f7 --- /dev/null +++ b/test/parallel/test-fs-truncate-sync.js @@ -0,0 +1,17 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const path = require('path'); +const fs = require('fs'); +const tmp = common.tmpDir; + +common.refreshTmpDir(); + +const filename = path.resolve(tmp, 'truncate-sync-file.txt'); + +fs.writeFileSync(filename, 'hello world', 'utf8'); + +const fd = fs.openSync(filename, 'r+'); + +fs.truncateSync(fd, 5); +assert(fs.readFileSync(fd).equals(Buffer.from('hello'))); diff --git a/test/parallel/test-fs-truncate.js b/test/parallel/test-fs-truncate.js index 80c938b8d43a03..57d706bddbf8f6 100644 --- a/test/parallel/test-fs-truncate.js +++ b/test/parallel/test-fs-truncate.js @@ -146,3 +146,14 @@ function testFtruncate(cb) { assert(fs.readFileSync(file4).equals(Buffer.from('Hi\u0000\u0000'))); })); } + +{ + const file5 = path.resolve(tmp, 'truncate-file-5.txt'); + fs.writeFileSync(file5, 'Hi'); + const fd = fs.openSync(file5, 'r+'); + process.on('exit', () => fs.closeSync(fd)); + fs.ftruncate(fd, undefined, common.mustCall(function(err) { + assert.ifError(err); + assert(fs.readFileSync(file5).equals(Buffer.from(''))); + })); +} From a9cb100359a24362759e0398898e2b8600fa2390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20do=20Carmo?= Date: Tue, 24 Jan 2017 11:16:50 -0300 Subject: [PATCH 2/2] test: close and unlink file --- test/parallel/test-fs-truncate-sync.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/parallel/test-fs-truncate-sync.js b/test/parallel/test-fs-truncate-sync.js index 6445dcfdfb28f7..a7ce2f4d97f3fe 100644 --- a/test/parallel/test-fs-truncate-sync.js +++ b/test/parallel/test-fs-truncate-sync.js @@ -15,3 +15,6 @@ const fd = fs.openSync(filename, 'r+'); fs.truncateSync(fd, 5); assert(fs.readFileSync(fd).equals(Buffer.from('hello'))); + +fs.closeSync(fd); +fs.unlinkSync(filename);