|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +// This test ensures that truncate works on non-readable files |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const fs = require('fs'); |
| 9 | +const fsPromises = fs.promises; |
| 10 | +const path = require('path'); |
| 11 | +const tmpdir = require('../common/tmpdir'); |
| 12 | +tmpdir.refresh(); |
| 13 | + |
| 14 | +const expected = Buffer.from('good'); |
| 15 | + |
| 16 | +function checkContents(filepath) { |
| 17 | + fs.chmodSync(filepath, 0o400); |
| 18 | + assert.deepStrictEqual( |
| 19 | + fs.readFileSync(filepath), |
| 20 | + expected |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +(async () => { |
| 25 | + const MODE = 0o200; |
| 26 | + { |
| 27 | + const filepath = path.resolve(tmpdir.path, 'promises.txt'); |
| 28 | + fs.writeFileSync(filepath, 'good bad'); |
| 29 | + fs.chmodSync(filepath, MODE); |
| 30 | + await fsPromises.truncate(filepath, 4); |
| 31 | + checkContents(filepath); |
| 32 | + } |
| 33 | + { |
| 34 | + const filepath = path.resolve(tmpdir.path, 'callback.txt'); |
| 35 | + fs.writeFileSync(filepath, 'good bad'); |
| 36 | + fs.chmodSync(filepath, MODE); |
| 37 | + fs.truncate(filepath, 4, common.mustSucceed(() => { |
| 38 | + checkContents(filepath); |
| 39 | + })); |
| 40 | + } |
| 41 | + { |
| 42 | + const filepath = path.resolve(tmpdir.path, 'synchronous.txt'); |
| 43 | + fs.writeFileSync(filepath, 'good bad'); |
| 44 | + fs.chmodSync(filepath, MODE); |
| 45 | + fs.truncateSync(filepath, 4); |
| 46 | + checkContents(filepath); |
| 47 | + } |
| 48 | +})().then(common.mustCall()); |
0 commit comments