Skip to content

Commit

Permalink
test: expand test coverage of fs.js
Browse files Browse the repository at this point in the history
* 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)

PR-URL: #10972
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
vinimdocarmo authored and italoacasas committed Jan 30, 2017
1 parent 1fae98b commit 580a453
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/parallel/test-fs-truncate-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'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')));

fs.closeSync(fd);
fs.unlinkSync(filename);
11 changes: 11 additions & 0 deletions test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('')));
}));
}

0 comments on commit 580a453

Please sign in to comment.