Skip to content
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

assert: use a default message in assert #18319

Closed
wants to merge 2 commits into from

Conversation

BridgeAR
Copy link
Member

In case no arguments are passed to assert.ok it should just
use a default message. Otherwise assert.ok can not be used as
a callback.

The PR that changed the behavior before is semver-major and has not yet been released.
So I just changed the original changed entry.

The reason for the change is something like this:

const assert = require('assert');
const EventEmitter = require('events');

const e = new EventEmitter();
e.once('hello', assert);
e.emit('hello');
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

assert

@nodejs-github-bot nodejs-github-bot added the assert Issues and PRs related to the assert subsystem. label Jan 23, 2018
@BridgeAR BridgeAR added the semver-major PRs that contain breaking changes and should be released in the next major version. label Jan 23, 2018
@apapirovski
Copy link
Member

apapirovski commented Jan 23, 2018

I really can't decide how I feel about this... I can't even decide how I feel about the original change. I mean, assert.strictEqual() doesn't throw an assertion right now, right? Seems strange that assert.ok() is the one exception...

@BridgeAR
Copy link
Member Author

@apapirovski yes, assert.strictEqual() returns true. That is the case because "both" values are undefined. assert.notStrictEqual() throws. Enforcing the value was originally meant to get people to use assert.fail() in such a case. But as there are actually more use cases, a default message is probably better than undefined == true, don't you agree? (because the latter is actually what this PR is about to do)

@@ -664,6 +663,7 @@ property set equal to the value of the `message` parameter. If the `message`
parameter is `undefined`, a default error message is assigned. If the `message`
parameter is an instance of an [`Error`][] then it will be thrown instead of the
`AssertionError`.
If no arguments are passed in at all `message` will be set to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incomplete sentence?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, somehow I did not finish it... solved.

@BridgeAR BridgeAR requested a review from a team January 24, 2018 11:27
In case no arguments are passed to `assert.ok` it should just
use a default message. Otherwise `assert.ok` can not be used as
a callback.
@BridgeAR
Copy link
Member Author

Rebased due to conflicts.

CI https://ci.nodejs.org/job/node-test-pull-request/12707/

Copy link
Member

@joyeecheung joyeecheung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm, press the button too soon...how does changing this to ERR_ASSERTION makes it possible to be used in callbacks?

@BridgeAR
Copy link
Member Author

@joyeecheung i tried to highlight that in the descriptionof the PR. It it all about not providing a value. An error will be thrown one way or the other, the question is what error. I personally reconsidered and think a default is best in such a case.

@joyeecheung
Copy link
Member

@BridgeAR By default do you mean "AssertionError: No value argument passed to assert.ok? Why is it better than ERR_MISSING_ARGS though?

@BridgeAR
Copy link
Member Author

@joyeecheung yes, that is what I mean. It might be "expected" behavior to not pass in a value in that case. That would probably mainly be the case by using e.g. spread (assert.ok(...input)) or by something like Reflect.apply.

In both cases we notify the user that no arguments were passed to the function but with this PR it is "acceptable" to do so. With the current implementation it is considered a faulty behavior.

@joyeecheung
Copy link
Member

@BridgeAR I think I understand now, thanks. With the example in OP:

assert.js:72
  throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: No value argument passed to `assert.ok()`
    at Object.onceWrapper (events.js:223:13)
    at EventEmitter.emit (events.js:131:13)
    at Object.<anonymous> (/Users/joyee/projects/node/assert-test.js:7:3)
    at Module._compile (module.js:661:30)
    at Object.Module._extensions..js (module.js:672:10)
    at Module.load (module.js:578:32)
    at tryModuleLoad (module.js:518:12)
    at Function.Module._load (module.js:510:3)
    at Function.Module.runMain (module.js:702:10)
    at startup (bootstrap_node.js:194:16)
assert.js:141
    throw new TypeError('ERR_MISSING_ARGS', 'value');
    ^

TypeError [ERR_MISSING_ARGS]: The "value" argument must be specified
    at innerOk (assert.js:141:11)
    at EventEmitter.ok (assert.js:222:3)
    at Object.onceWrapper (events.js:223:13)
    at EventEmitter.emit (events.js:131:13)
    at Object.<anonymous> (/Users/joyee/projects/node/assert-test.js:7:3)
    at Module._compile (module.js:661:30)
    at Object.Module._extensions..js (module.js:672:10)
    at Module.load (module.js:578:32)
    at tryModuleLoad (module.js:518:12)
    at Function.Module._load (module.js:510:3)

The first one does seem to be less confusing, although I am not sure if ERR_ASSERTION is better than ERR_MISSING_ARGS in this case, but I would not block it. Can you add a test to demonstrate how it would be useful? Something like in the OP or with assert(...[]) would be fine.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@BridgeAR
Copy link
Member Author

@joyeecheung I changed a testcase to outline the use case further.

New CI: https://ci.nodejs.org/job/node-test-pull-request/12806/

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Jan 30, 2018
@BridgeAR
Copy link
Member Author

BridgeAR commented Feb 1, 2018

Landed in 3cd7977

@BridgeAR BridgeAR closed this Feb 1, 2018
BridgeAR added a commit to BridgeAR/node that referenced this pull request Feb 1, 2018
In case no arguments are passed to `assert.ok` it should just
use a default message. Otherwise `assert.ok` can not be used as
a callback.

PR-URL: nodejs#18319
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
@addaleax addaleax removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Feb 4, 2018
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
In case no arguments are passed to `assert.ok` it should just
use a default message. Otherwise `assert.ok` can not be used as
a callback.

PR-URL: nodejs#18319
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
@BridgeAR BridgeAR deleted the no-args-simple-assert branch April 1, 2019 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert Issues and PRs related to the assert subsystem. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants