• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4
5// The following tests validate base functionality for the fs.promises
6// FileHandle.chmod method.
7
8const fs = require('fs');
9const { open } = fs.promises;
10const path = require('path');
11const tmpdir = require('../common/tmpdir');
12const assert = require('assert');
13const tmpDir = tmpdir.path;
14
15tmpdir.refresh();
16
17async function validateFilePermission() {
18  const filePath = path.resolve(tmpDir, 'tmp-chmod.txt');
19  const fileHandle = await open(filePath, 'w+', 0o444);
20  // File created with r--r--r-- 444
21  const statsBeforeMod = fs.statSync(filePath);
22  assert.deepStrictEqual(statsBeforeMod.mode & 0o444, 0o444);
23
24  let expectedAccess;
25  const newPermissions = 0o765;
26
27  if (common.isWindows) {
28    // Chmod in Windows will only toggle read only/write access. The
29    // fs.Stats.mode in Windows is computed using read/write
30    // bits (not exec). Read-only at best returns 444; r/w 666.
31    // Refer: /deps/uv/src/win/fs.cfs;
32    expectedAccess = 0o664;
33  } else {
34    expectedAccess = newPermissions;
35  }
36
37  // Change the permissions to rwxr--r-x
38  await fileHandle.chmod(newPermissions);
39  const statsAfterMod = fs.statSync(filePath);
40  assert.deepStrictEqual(statsAfterMod.mode & expectedAccess, expectedAccess);
41
42  await fileHandle.close();
43}
44
45validateFilePermission().then(common.mustCall());
46