-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: modernize JS in some test-fs-*.js #23031
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @jy95! Welcome and thanks for the pull request.
Most of these changes are 👍 but I'm not sure about the .map()
usages for assigning constants because:
-
It makes the code less readable/maintainable. (Example: The use of
.map()
intest-fs-access.js
makes that code more difficult to understand and maintain for no discernible benefit.) -
It makes it more difficult to introduce block scoping of these constants for individual test cases. (Example:
test-fs-append-file-sync.js
changes. I'd much rather see each variable block scoped to just the test case that needs it but using.map()
to assign them all at the outset prevents that.)
Would you consider removing those or at least moving them to a separate pull request so that they can be considered/discussed separately and the more straightforward changes (like introducing the ternary in test-fs-open-mode-mask.js
to make it possible to make mode
a constant) can be merged separately?
I removed them : I only keep the ternary way because I think it is more clear in that way ^^ |
test/parallel/test-fs-access.js
Outdated
@@ -13,10 +13,12 @@ const { internalBinding } = require('internal/test/binding'); | |||
const { UV_ENOENT } = internalBinding('uv'); | |||
|
|||
const tmpdir = require('../common/tmpdir'); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Unrelated whitespace change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oups : fixed
test/parallel/test-fs-access.js
Outdated
const doesNotExist = path.join(tmpdir.path, '__this_should_not_exist'); | ||
const readOnlyFile = path.join(tmpdir.path, 'read_only_file'); | ||
const readWriteFile = path.join(tmpdir.path, 'read_write_file'); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oups : fixed
@@ -7,13 +7,8 @@ const assert = require('assert'); | |||
const path = require('path'); | |||
const fs = require('fs'); | |||
|
|||
let mode; | |||
const mode = (common.isWindows) ? 0o444 : 0o644; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Parentheses unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that they are not necessary for simple case (like this one). ESLint didn't remove them
Thanks! One other thing: Can you also leave the |
test/parallel/test-fs-open-flags.js
Outdated
const O_TRUNC = fs.constants.O_TRUNC || 0; | ||
const O_WRONLY = fs.constants.O_WRONLY || 0; | ||
// 0 if not found in fs.constants | ||
const { O_APPEND = 0, O_CREAT = 0, O_EXCL = 0, O_RDONLY = 0, O_RDWR = 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you keep these each on separate lines?
const {
O_APPEND = 0,
O_CREAT = 0,
...
} = fs.constants;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok for readability : I will change
test/parallel/test-fs-stat-bigint.js
Outdated
bigintStats.isSymbolicLink(), | ||
numStats.isSymbolicLink() | ||
); | ||
['isBlockDevice', 'isCharacterDevice', 'isDirectory', 'isFIFO', 'isFile', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
separate lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean one line for each item of the array ? Sure if you want
test/parallel/test-fs-copyfile.js
Outdated
assert.strictEqual(typeof UV_FS_COPYFILE_FICLONE, 'number'); | ||
assert.strictEqual(typeof UV_FS_COPYFILE_FICLONE_FORCE, 'number'); | ||
[COPYFILE_EXCL, COPYFILE_FICLONE, COPYFILE_FICLONE_FORCE, UV_FS_COPYFILE_EXCL, | ||
UV_FS_COPYFILE_FICLONE, UV_FS_COPYFILE_FICLONE_FORCE] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
separate lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same that above : I will change
@Trott You mean : put them in their original place ? Sorry but I don't see the problem with the current block-scoping ... I will still change it as someone agreed with you :) Hope I succeed my first commit squashing after these changes ^^ |
@jy95 What I mean is that if we want to add block scoping for the individual test cases inside the file (and we do), then the declarations will have to be in separate blocks. The declarations will have to be where they are now, basically. So we'll just end up moving them back. |
(By the way, if you want to block scope each of the test cases in that file, go for it.) |
I don't think it will be useful for this file ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not convinced about these changes as they decrease the readability in an error case when changing the individual assertions to an array.
Each assertion will currently show the line in the stack trace but when changing it to an array, we have to add extra information to the output to know what entry actually failed.
@jy95 thanks a lot for the PR! There is definitely a lot of code that needs cleanup, I am just not sure that all of these changes will actually lead to the better. I do like the destructuring though :) |
It is also possible to add a custom message using the forEach parameter [fs.F_OK, fs.R_OK, fs.W_OK, fs.X_OK].forEach(
(cons) => assert.strictEqual(typeof cons, 'number',
`The foreach test for fs constants for index: ${index}`)
); or using Object.entries : Object.entries(
{ 'fs.F_OK' : fs.F_OK , 'fs.R_OK': fs.R_OK, 'fs.W_OK': fs.W_OK, 'fs.X_OK': fs.X_OK }
).forEach(
([varName,val]) => assert.strictEqual(typeof val, 'number', `Test failed for fs.${varName}`)
) Notice : There is maybe a way to create the object automatically if there exist a function to get javascript variable name ... |
@jy95 I am aware that it's possible to add a message property and if the test would have done that originally, I would have signed off on it (in case it would not only contain the index but also the actual difference of the assertion). Changing it now does not seem to improve anything as the actual code is more difficult to read and not a lot of code lines are reduced this way. |
For the moment, I only verified +/- 20 test files but what I saw, generic function(s) could improve at least readability / maintenance. For example , using a structure like this (first draft : surely there are things that must evolue ) : {
// the name you want to see in console log if there is assert error(s)
testCaseName: 'Testing Fs constants'
// The default assert fct that would be invoked
defaultAssertFct: assert.strictEqual,
// defaultMessage (hidden param) : it will be an AssertionError with default message
// '${testCaseName} - The test N°${index} failed' , other parameters will be the same one
tests: [
// actual and expected are required , you can override the default message
{actual : typeof fs.F_OK, expected: 'number', message: 'F_OK failed'},
// possibility to change assert function (optional, default to defaultAssertFct)
{actual : 1, expected: 2, assertFct: assert.notDeepStrictEqual},
// ... other idea can improve this basic design
]
} Of couse, it is completely up to you to choose how you initialize the tests array following the circumstances ^^ For example , I prefer to write on the readability field : {
testCaseName: 'Testing Fs constants',
defaultAssertFct: assert.strictEqual,
tests: [fs.F_OK, fs.R_OK, fs.W_OK, fs.X_OK].map(
(cons) => {actual: typeof cons, expected: 'number' }
)
} instead of [fs.F_OK, fs.R_OK, fs.W_OK, fs.X_OK].forEach(
(cons) => assert.strictEqual(typeof cons, 'number')
); |
The original version (four separate assertions) seems much more readable than both of these. I'd prefer to use |
I agree with @tniessen and I think it would be best to just revert the assertion changes. |
Maybe I will ask review about the feature request (last message) I would like to add inside the assert into a another issue . For the one in |
I agree with @tniessen and @BridgeAR that the current version is a bit better from a DAMP-not-DRY perspective. More concretely, the refactor makes the assertions less useful. In the current test, if an assertion fails, you know exactly why because it's right there in the line of code that failed: AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
false !== true
at verifyStats (/Users/trott/io.js/test/parallel/test-fs-stat-bigint.js:53:14)
Line 53-56 is: assert.strictEqual(
bigintStats.isSocket(),
numStats.isSocket()
); If we instead go with what's in this PR, we won't know that it's the That can be solved by adding a third argument to I'd greatly prefer those changes be moved to their on PR and we land the improvements here in the other two files. |
Ok : feel free to change it if you prefer that way ... |
Resume Build CI: https://ci.nodejs.org/job/node-test-pull-request/19196/ |
Landed in 841caef. Thanks for the contribution! 🎉 |
Update test-fs-open-flags to take advantage of destructuring and default values. Update test-fs-open-mode-mask to use a ternary to make use of a constant possible. PR-URL: nodejs#23031 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
Update test-fs-open-flags to take advantage of destructuring and default values. Update test-fs-open-mode-mask to use a ternary to make use of a constant possible. PR-URL: #23031 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
Update test-fs-open-flags to take advantage of destructuring and default values. Update test-fs-open-mode-mask to use a ternary to make use of a constant possible. PR-URL: nodejs#23031 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
Update test-fs-open-flags to take advantage of destructuring and default values. Update test-fs-open-mode-mask to use a ternary to make use of a constant possible. PR-URL: #23031 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
Update test-fs-open-flags to take advantage of destructuring and default values. Update test-fs-open-mode-mask to use a ternary to make use of a constant possible. PR-URL: #23031 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
Update test-fs-open-flags to take advantage of destructuring and default values. Update test-fs-open-mode-mask to use a ternary to make use of a constant possible. PR-URL: #23031 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesDescription
Replaced several variable initialization with spread / forEach / Object.assign(...Object.entries(obj).map()) .