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

console: make console consistent with the standard - overridable #12454

Closed

Conversation

Wandalen
Copy link
Contributor

@Wandalen Wandalen commented Apr 17, 2017

make console overridable by custom console( without delete )

add test suite from w3c/web-platform-tests( console-is-a-namespace )
few test cases commented out from the test suite
to make nodejs console even more consistent further changes needed
which will come in the next commit
which could be more breaking than this one

Fixes: #11805
Ref: https://console.spec.whatwg.org/#console-namespace
Ref: https://heycam.github.io/webidl/#es-namespaces

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)

@nodejs-github-bot nodejs-github-bot added the lib / src Issues and PRs related to general changes in the lib or src directory. label Apr 17, 2017
const prototype1 = Object.getPrototypeOf(console);
const prototype2 = Object.getPrototypeOf(prototype1);
// console.log('console',Object.getOwnPropertyNames(console));
// console.log('prototype1',Object.getOwnPropertyNames(prototype1));
Copy link
Member

Choose a reason for hiding this comment

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

Where did these two lines come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

const common = require('../common');
const assert = require('assert');
const { test, assert_equals, assert_true, assert_false } = common.WPT;

Copy link
Member

Choose a reason for hiding this comment

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

Can you use the standard header for WPT tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi TinothyGu. Do you mean remove assert_false from the list?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the confusion. I was referring to the "WPT Refs" block in the file I linked. Specifically, a permalink to the source of the file as well as a URL pointing to the license under which the WPT test is used should be included for compliance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for explanation. Added.

'The [[Prototype]]\'s [[Prototype]] must be Object Prototype');

}, 'The prototype chain must be correct');

Copy link
Member

Choose a reason for hiding this comment

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

And please use the standard footer here, since the test below is not part of WPT.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added /* eslint-enable */

return console;
},
set: function(customConsole) {
console = customConsole;
Copy link
Member

Choose a reason for hiding this comment

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

This assignment will invalidate the if (console) check above in the getter, in the case that global.console is deliberately set to any falsy value like global.console = undefined;. Instead, do the same thing you did in the getter and use Object.defineProperty() on global.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TimothyGu not certain I understood you. global.console = undefined is completely legal operation according to the standard. @TimothyGu purpose of the PR is making nodejs console consistent with the standard. Do you understand it, right?

Copy link
Contributor Author

@Wandalen Wandalen Apr 17, 2017

Choose a reason for hiding this comment

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

@TimothyGu removing set will make console not-overridable again. If you see a problem please explain it to find better solution than suggested by you.

Copy link
Member

Choose a reason for hiding this comment

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

What I meant was:

// Initially, the `console` variable is `undefined`, since console will be lazily
// loaded in the getter.

global.console = undefined;
// global.console's setter is called; the `console` variable remains `undefined`.

assert.strictEqual(global.console, undefined);
// global.console's getter is called
// Since the `console` cache variable is `undefined` and therefore false-y,
// the getter still calls NativeModule.require() and returns the object
// obtained from it, instead of returning `undefined` as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Thinking about solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested solution. Good point actually. I added a separate test suite specifically for the problem. It sets global.console = undefined above requires. Otherwise something in requires read global.console.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TimothyGu any further requests/insights?

@TimothyGu TimothyGu added console Issues and PRs related to the console subsystem. and removed lib / src Issues and PRs related to general changes in the lib or src directory. labels Apr 17, 2017
configurable: true,
writable: true,
enumerable: false,
value: console
Copy link
Member

Choose a reason for hiding this comment

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

customConsole

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh mine. I'm sleepy. It's late here. Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Want to add more tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TimothyGu added few more tests.


/* eslint-disable */
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/master/console/console-is-a-namespace.any.js
Copy link
Member

Choose a reason for hiding this comment

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

Better reference a specific commit hash here (40e451c instead of master) in case the file changes or turns into a 404

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -268,15 +268,29 @@
var console;
Object.defineProperty(global, 'console', {
configurable: true,
enumerable: true,
enumerable: false,
Copy link
Member

Choose a reason for hiding this comment

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

These changes need to be semver-major as this might break user code, although hopefully very rarely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense.

License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/

assert.doesNotThrow(() => {
Copy link
Member

Choose a reason for hiding this comment

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

This test doesn't come from the WPT, and we don't use it in this test. I think we can get rid of it :)

const assert = require('assert');
const { test, assert_equals, assert_true, assert_false } = common.WPT;

assert.doesNotThrow(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Same with L23. We can get rid of this test from this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello @watilde

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean exclude a duplicate?

Copy link
Contributor Author

@Wandalen Wandalen Apr 17, 2017

Choose a reason for hiding this comment

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

@watilde I removed the duplicate. Also there is a comment, explaining the test case above is not from WPT. The first test case is useful. It fails with original version of booststrap_node.js. Do you suggest to move the first test case into separate test suite?

Copy link
Member

Choose a reason for hiding this comment

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

Oh you're right! It's better to keep the test case to verify the update, and we don't need to move it into a separate file since this test is related to namespace :)

@benjamingr benjamingr added the semver-major PRs that contain breaking changes and should be released in the next major version. label Apr 17, 2017

// Initially, the `console` variable is `undefined`, since console will be
// lazily loaded in the getter.

Copy link
Member

Choose a reason for hiding this comment

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

make jslint threw an error:

node/test/parallel/test-console-assing-undefined.js
1:1 error Mandatory module "common" must be loaded required-modules

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding require common gives "'common' is assigned a value but never used". What would be appropriate resolution of the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simulating of usage or adding a directive?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just don't assign it anywhere. Instead of const common = require('../common'); do require('../common');.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aqrln Smart. Do you participate in OdesaJS this year?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@watilde fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Wandalen almost there :) The common module should be the first one that a test requires. Please see our guide on writing tests.

Do you participate in OdessaJS this year?

Yep.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aqrln I shall share my experience too if organizers won't be mean :)

@TimothyGu
Copy link
Member

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

return console;
},
set: function(customConsole) {
Copy link
Member

Choose a reason for hiding this comment

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

you could use a set(customConsole) { shorthand here (for the getter as well if you like) :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@addaleax thank you for your advices. I used => shorthand as you advised in the recent commit.

Copy link
Contributor Author

@Wandalen Wandalen Apr 22, 2017

Choose a reason for hiding this comment

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

@addaleax can/should I push that changes after TimothyGu launched remote testing?

Copy link
Member

Choose a reason for hiding this comment

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

@Wandalen go ahead and push it. The CI is done already (the "pending" windows-fanned instance is having some infrastructural issues). And we can always launch a new CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

writable: true,
enumerable: false,
value: console
});
Copy link
Member

Choose a reason for hiding this comment

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

could this maybe use the setter directly, i.e. as global.console = console?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@addaleax I tried that. global.console = console fails at test/common.js:407 with message

AssertionError: Unexpected global(s) found: console

Copy link
Member

@TimothyGu TimothyGu Apr 22, 2017

Choose a reason for hiding this comment

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

You can fix that in test-console-is-a-namespace.js by calling common.allowGlobals('console').

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fail comes form

=== release console_low_stack_space ===                    
Path: message/console_low_stack_space
Hello, World!
assert.js:86
  throw new assert.AssertionError({

I tried common.allowGlobals('console') in that file and 2 I create, it didn't help. Maybe I do something wrong.

@refack
Copy link
Contributor

refack commented Apr 22, 2017

@Wandalen could you add a link to the standard in the first comment (I assume it's https://console.spec.whatwg.org/#console-namespace)

@addaleax
Copy link
Member

@nodejs/ctc This would require another CTC approval, do you mind taking a look?


// Tests above are not from WPT.

/* eslint-disable */
Copy link
Contributor

Choose a reason for hiding this comment

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

If Node collaborators should not modify these tests in any way, please add a comment here stating that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cjihrig like that?

Copy link
Contributor

@cjihrig cjihrig May 1, 2017

Choose a reason for hiding this comment

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

I would put the comment inside the existing WPT Refs: block comment and say something like "The following tests are copied from upstream verbatim. Do not modify."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cjihrig done.

Copy link
Contributor Author

@Wandalen Wandalen May 1, 2017

Choose a reason for hiding this comment

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

@cjihrig do you want any other improvement?

Copy link
Contributor Author

@Wandalen Wandalen May 11, 2017

Choose a reason for hiding this comment

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

@cjihrig are you going approve my PR? Do you have any objection? Do you make all newcomers feel unwelcome here or it's personal?

Copy link
Member

Choose a reason for hiding this comment

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

@Wandalen ... this kind of comment is unwarranted. We are all busy and this repository has a high level of activity that is often difficult to keep track of. Many of us receive hundreds of notifications per day. Patience would be appreciated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jasnell could you please tell what would be an appropriate comment there? I caught another issue with ArrayBuffer. Looking forward to starting work on that issue once you will approve the PR. At the point, I clearly see that the majority of the community is friendly, but the minority make me feel unwelcome here.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Wandalen try not to get frustrated, and really don't take it personally.
Initially I also had a hard time trying to deduce people's intention and emotions from just these text messages. It's hard, I misunderstood some stuff, I took it personally, and I got frustrated, and then angry, and that leads to nowhere good.
If you found another issue to channel your energy into, I say go for it. This PR will most probably land in due time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@refack thank you for kind words.

@Fishrock123
Copy link
Member

Fishrock123 commented May 1, 2017

I personally think all these specs make way too many things unenumberable without much reason, so... -1 on that? I guess?

@Wandalen
Copy link
Contributor Author

Wandalen commented May 1, 2017

@Fishrock123 on what? enumerability off or the whole propose?

@Wandalen
Copy link
Contributor Author

Wandalen commented May 1, 2017

@Trott Trott force-pushed the console-consistent-with-standard branch from 98388d5 to 662a8ba Compare August 16, 2017 03:47
@Trott
Copy link
Member

Trott commented Aug 16, 2017

I'd like to see this get across the finish line or, if that's not something that's going to happen, closed. So, I rebased, fixed a failing test, pulled in the latest/greatest from the WPT test suite for one test, and updated some comments.

PTAL!

@Fishrock123 Is your -1 a blocking -1 or just an expression of annoyance with standards bodies' fondness for making things unenumerable?

@cjihrig I think your changes-requested can be dismissed, but let me know if that's not right.

If anyone is all "Wait, what, no way! Don't do this!", now's a great time to register your opinion.

Update the test from the most recent WPT contents. Copyedit some
existing comments.
@Trott Trott force-pushed the console-consistent-with-standard branch from 662a8ba to 490374f Compare August 16, 2017 04:01
const self = global;

/* eslint-disable */
/* The following tests are copied from */
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd still prefer if this said not to modify, not just that it's copied.

Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

LGTM but it would be great to have the comment nit addressed.

@BridgeAR BridgeAR self-assigned this Aug 28, 2017
@BridgeAR
Copy link
Member

BridgeAR commented Aug 28, 2017

When I just ran the tests for it again I realized that this is not working.

It works with the following patch

--- a/test/message/console_low_stack_space.js
+++ b/test/message/console_low_stack_space.js
@@ -16,7 +16,7 @@ function a() {
   try {
     return a();
   } catch (e) {
-    compiledConsole = consoleDescriptor.get();
+    compiledConsole = consoleDescriptor.value;
     if (compiledConsole.log) {
       // Using `console.log` itself might not succeed yet, but the code for it
       // has been compiled.

Addressing the nit

diff --git a/test/parallel/test-console-is-a-namespace.js b/test/parallel/test-console-is-a-namespace.js
index 1beb392621..28b0668f41 100644
--- a/test/parallel/test-console-is-a-namespace.js
+++ b/test/parallel/test-console-is-a-namespace.js
@@ -13,7 +13,7 @@ assert.doesNotThrow(() => {
 const self = global;
 
 /* eslint-disable */
-/* The following tests are copied from */
+/* The following tests should not be modified as they are copied from */
 /* WPT Refs:
    https://github.com/w3c/web-platform-tests/blob/40e451c/console/console-is-a-namespace.any.js
    License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html

I would also like to add the following patch because I think it is a bad idea to add commented code to core. Instead it should be added when necessary.

diff --git a/test/parallel/test-console-is-a-namespace.js b/test/parallel/test-console-is-a-namespace.js
index f413000805..1beb392621 100644
--- a/test/parallel/test-console-is-a-namespace.js
+++ b/test/parallel/test-console-is-a-namespace.js
@@ -38,12 +38,4 @@ test(() => {
   assert_false("Console" in self);
 }, "Console (uppercase, as if it were an interface) must not exist");
 
-
-// test(() => {
-//   const prototype1 = Object.getPrototypeOf(console);
-//   const prototype2 = Object.getPrototypeOf(prototype1);
-
-//   assert_equals(Object.getOwnPropertyNames(prototype1).length, 0, "The [[Prototype]] must have no properties");
-//   assert_equals(prototype2, Object.prototype, "The [[Prototype]]'s [[Prototype]] must be %ObjectPrototype%");
-// }, "The prototype chain must be correct");
 /* eslint-enable */

@Wandalen
Copy link
Contributor Author

Hello @BridgeAR. What test cases does it break? Why did you added enumerable: true? I believe there should be enumerable: false.

@BridgeAR
Copy link
Member

I did not change the console to be enumerable (your test case would also fail in that case) and you actually struggled with the test case that I fixed. The default for enumerable is false that is why I did not declare it in my former clean up. Anyhow, I removed the code cleanup from my patches above as I am going to open a broader clean up after this PR. The tests failed because global.console gets redefined with a value and has no getter after the first call and therefore the test failed and the get() had to be changed to value.

So please feel free to add my patches so this can land. @Wandalen

Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

The test must be fixed and I would like the other two comments addressed as well.

@BridgeAR
Copy link
Member

Ping @Wandalen this now needs a rebase.

@BridgeAR
Copy link
Member

Closing this due to the long inactivity. @Wandalen please leave a comment if you would like to pursue this further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console 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.

console - writable property