0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-fs-promises-file-handle-chmod.js
Tobias Nießen dd52c05046 test: avoid deep comparisons with literals
Comparing any value to any non-RegExp literal or undefined using
strictEqual (or notStrictEqual) passes if and only if deepStrictEqual
(or notDeepStrictEqual, respectively) passes.

Unnecessarily using deep comparisons adds confusion.

This patch adds an ESLint rule that forbids the use of deepStrictEqual
and notDeepStrictEqual when the expected value (i.e., the second
argument) is a non-RegExp literal or undefined.

For reference, an ESTree literal is defined as follows.

    extend interface Literal <: Expression {
        type: "Literal";
        value: string | boolean | null | number | RegExp | bigint;
    }

The value `undefined` is an `Identifier` with `name: 'undefined'`.

PR-URL: https://github.com/nodejs/node/pull/40634
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Voltrex <mohammadkeyvanzade94@gmail.com>
2021-11-02 10:11:49 +00:00

46 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
// The following tests validate base functionality for the fs.promises
// FileHandle.chmod method.
const fs = require('fs');
const { open } = fs.promises;
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;
tmpdir.refresh();
async function validateFilePermission() {
const filePath = path.resolve(tmpDir, 'tmp-chmod.txt');
const fileHandle = await open(filePath, 'w+', 0o444);
// File created with r--r--r-- 444
const statsBeforeMod = fs.statSync(filePath);
assert.strictEqual(statsBeforeMod.mode & 0o444, 0o444);
let expectedAccess;
const newPermissions = 0o765;
if (common.isWindows) {
// Chmod in Windows will only toggle read only/write access. The
// fs.Stats.mode in Windows is computed using read/write
// bits (not exec). Read-only at best returns 444; r/w 666.
// Refer: /deps/uv/src/win/fs.cfs;
expectedAccess = 0o664;
} else {
expectedAccess = newPermissions;
}
// Change the permissions to rwxr--r-x
await fileHandle.chmod(newPermissions);
const statsAfterMod = fs.statSync(filePath);
assert.deepStrictEqual(statsAfterMod.mode & expectedAccess, expectedAccess);
await fileHandle.close();
}
validateFilePermission().then(common.mustCall());