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

test: add a common.debuglog function #22206

Closed
wants to merge 4 commits into from

Conversation

SirR4T
Copy link
Contributor

@SirR4T SirR4T commented Aug 9, 2018

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

Fixes #9314

@nodejs-github-bot nodejs-github-bot added the test Issues and PRs related to the tests. label Aug 9, 2018
@SirR4T
Copy link
Contributor Author

SirR4T commented Aug 9, 2018

@gibfahn: the test passes when invoked with NODE_DEBUG=test ./node test/parallel/test-common-log, but fails when invoked with ./node test/parallel/test-common-log, even though I'm trying to mutate process.env.NODE_DEBUGfrom within the test case.

How do I fix this?

@richardlau
Copy link
Member

the test passes when invoked with NODE_DEBUG=test ./node test/parallel/test-common-log, but fails when invoked with ./node test/parallel/test-common-log, even though I'm trying to mutate process.env.NODE_DEBUG from within the test case.

How do I fix this?

@SirR4T Please see #21914 (comment). I'd suggest that rather than hijack stderr the test should instead spawn another process (with NODE_DEBUG set in the env passed to the spawn) and capture the stderr from that,

@SirR4T
Copy link
Contributor Author

SirR4T commented Aug 9, 2018

thanks @richardlau ! fixed the test cases now.

* `msg` [<string>] The string to be printed to stderr
* `msgs` [<string>] Additional arguments to the function

Prints messages (optionally, formatted using `fmt`) to `process.stderr`, using `util.debuglog('test')`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why linter does not warn about this (maybe we do not lint docs outside api?) but it seems this line needs to be wrapped at 80 characters or less)

Copy link
Member

Choose a reason for hiding this comment

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

@vsemozhetbyt test is omitted from the markdown linting. I have a PR in the works to add it. Will have it ready to be opened today, I think.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@richardlau richardlau left a comment

Choose a reason for hiding this comment

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

Left some inline comments.

With regards to the test, it would be good to add the case where NODE_DEBUG is not set (no output expected) and/or possiblty also if NODE_DEBUG is set to something other than test.

const common = require('../common');

// argv[0] is the execPath, argv[1] is the test script filename
common.debuglog.apply(null, process.argv.slice(2));
Copy link
Member

Choose a reason for hiding this comment

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

This should either be inlined into the new test/parallel/test-common-debuglog.js (see test/parallel/test-child-process-spawnsync-env.js for how tests that spawn other processes are typically written) or this file should be moved to test/fixtures.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will move it to test/fixtures, since the same file will need to be invoked with on different child processes... Inlining it might make it needlessly complex, to test for different values of NODE_DEBUG

@@ -0,0 +1,18 @@
'use strict';

const common = require('../common'); // eslint-disable-line no-unused-vars
Copy link
Member

Choose a reason for hiding this comment

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

Just write

require('../common');

instead of disabling the lint rule.

Copy link
Contributor

@sagirk sagirk left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

You might want to wrap that long line at 80 chars, though.

@SirR4T
Copy link
Contributor Author

SirR4T commented Aug 10, 2018

@sagirk sorry, missed that! pushed now, thanks!

Copy link
Member

@richardlau richardlau left a comment

Choose a reason for hiding this comment

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

Left a couple of suggestions, otherwise LGTM.

{
process.env.NODE_DEBUG = 'test';
const { stderr } = spawnSync(process.execPath, [
path.resolve(__dirname, '../fixtures/common-debuglog.js'),
Copy link
Member

Choose a reason for hiding this comment

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

* `msgs` [<string>] Additional arguments to the function

Prints messages (optionally, formatted using `fmt`) to `process.stderr`,
using `util.debuglog('test')`.
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps the wording should clarify that the messages are conditionally printed to stderr based on the value of NODE_DEBUG ?

Copy link
Member

Choose a reason for hiding this comment

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

Or maybe omit the stderr part, e.g.

* `msg` [<string>] The string to be printed

and

Prints messages (optionally, formatted using `fmt`) using `util.debuglog('test')`.

const common = require('../common');

// argv[0] is the execPath, argv[1] is the test script filename
common.debuglog.apply(null, process.argv.slice(2));
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 be simplified to

common.debuglog(process.argv.slice(2));

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that ends up printing TEST 2461: [ 'message' ] instead of TEST 2461: message, since slice returns an array. So resorted to common.debuglog(...process.argv.slice(2)) instead.

@SirR4T SirR4T force-pushed the fix9314-add-common-log branch 3 times, most recently from 37ca6e2 to 80295d8 Compare August 10, 2018 07:05
@gdams
Copy link
Member

gdams commented Aug 12, 2018

@Trott
Copy link
Member

Trott commented Aug 13, 2018

Could stand to get one more approval. @nodejs/testing

@Trott
Copy link
Member

Trott commented Aug 13, 2018

(Not approving myself because I generally don't 👍 adding stuff to common that we're not actually using--the thing is enough of a monolith already--but I won't stand in the way if others think this will be useful.)

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.

I am -0 on this. I agree with @Trott that we have a lot in common already and adding more is not ideal. Besides the fact that we could just all agree on always using test in which case it would be possible to just directly use util.debuglog().

Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

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

Doing design review

@refack
Copy link
Contributor

refack commented Aug 21, 2018

Hello @SirR4T and thank you very much for this contribution.
I would like us to do a design review of this change.

  1. Could you explain why you chose this solution?
  2. What are the use cases you envision this will solve?
  3. Is this sufficiently different than running through test.py?
  4. Does this require changes in test.py?
  5. Is this better then hijacking console.log?

@jasnell
Copy link
Member

jasnell commented Aug 21, 2018

Is this sufficiently different than running through test.py?
Does this require changes in test.py?

Given that the PR does not touch test.py and does not change test.py, how are these questions relevant to this PR?

@jasnell
Copy link
Member

jasnell commented Aug 21, 2018

Is this better then hijacking console.log?

I definitely believe so in that it makes the intent of the statements far more explicit and gives clear separation between output that is purely intended as debug output vs. output that is part of exercising the test.

@refack
Copy link
Contributor

refack commented Aug 21, 2018

Given that the PR does not touch test.py and does not change test.py, how are these questions relevant to this PR?

Is this sufficiently different than running through test.py?

When running tests through test.py it will swallow stdout and stderr if the tests pass. Providing the "noise reduction" requirement.

Does this require changes in test.py?

If we move tracing output to use common.debuglog IMHO we should change test.py to set NODE_DEBUG=test in certain configuration, so that tracing information will still be available.

@SirR4T
Copy link
Contributor Author

SirR4T commented Aug 22, 2018

Hi @refack !

  1. I was basing my approach on this comment by @gibfahn :

I guess there's always a balance, but I'd say that for me a console.log (or I guess a common.log()) can sometimes be as helpful as a comment, depending on the situation. I agree that too many of them adds complexity.

Using the existing debuglog in a common.log() makes sense to me.

  1. When tackling test cases, I see myself adding some log statements for debugging, in two varieties:

    1. ones that are trivial, and would ultimately be removed (has execution reached here yet? what is the typeof of this thingy?)
    2. ones that are informational, and might make sense to keep them in, if someone else were trying to revisit the test case. (on nth repetition, total bytes sent were x, total bytes received were y).

    Having a debuglog() allows to add statements of the second type, in a non-invasive way (won't
    clutter test output, unless NODE_DEBUG is set), imho.

  2. test.py is helpful when running a whole suite of tests. We may not want to set NODE_DEBUG, and thereby clutter the test output, when many tests are run. (On the contrary, if all the tests for a module (say child_process) followed a pattern while using debuglog() (something like say, to track which ones are being emitted by the child fork, as opposed to parent fork), it might be helpful there too.)

  3. I have not looked into how test.py works, and haven't changed it for this PR either.

  4. Not too sure if this is better or not, but I believe with this change, we may start discouraging the usage of console.log() (and 2.i type statements) in tests, in favour of using debuglog() (and 2.ii type statements) if and where necessary. Maybe even add a linter rule, to disallow console.log statements in tests.

@SirR4T
Copy link
Contributor Author

SirR4T commented Aug 27, 2018

Hi @refack ! how do we proceed on this?

@SirR4T
Copy link
Contributor Author

SirR4T commented Sep 5, 2018

Hi @refack ! Ping! how do we move forward here?

@BridgeAR
Copy link
Member

BridgeAR commented Sep 5, 2018

@nodejs/tsc please weight in.

@cjihrig
Copy link
Contributor

cjihrig commented Sep 5, 2018

I'm against this change, since util.debuglog() can just be used directly, but I won't block this.

Copy link
Contributor

@ofrobots ofrobots left a comment

Choose a reason for hiding this comment

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

Seems like a convenient and readable thing to have in the tests. LGTM.

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

cc @Trott there are a lot of console.log() statements in test/parallel, it might be good for code&learn exercises.

@@ -804,3 +804,6 @@ exports.runWithInvalidFD = function(func) {

exports.printSkipMessage('Could not generate an invalid fd');
};

const utilDebugLog = util.debuglog('test');
Copy link
Member

@joyeecheung joyeecheung Oct 2, 2018

Choose a reason for hiding this comment

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

If the intention is to get all the tests to slowly refactor to use common.debuglog, then I am -1 to this since I think something like out/Release/node test/paralell/test-*.js should still give useful output without any tweak to the environment.

In that sense at least this PR needs to also modify test.py to add NODE_DEBUG=test to

print "Command: %s" % EscapeCommand(failed.command)

or even a hint in CONTRIBUTING.md (even though I believe many people don't read it through before contributing) if that's where it's going, otherwise when the test fail, the hint won't be too useful to a new contributor.

Copy link
Member

Choose a reason for hiding this comment

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

If the intention is to get all the tests to slowly refactor to use common.debuglog, then I am -1 to this since I think something like out/Release/node test/paralell/test-*.js should still give useful output without any tweak to the environment.

@joyeecheung do you mean that it should still give output for tests that fail, or that it should always give output for all tests?

Copy link
Member

@joyeecheung joyeecheung Oct 5, 2018

Choose a reason for hiding this comment

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

@gibfahn I am not sure if it's reasonable to only give output for tests that fail unless we implement this in the test runner - we test our process.exit() hooks in JS, among many things. If the hooks are broken, common.debuglog() can be broken as well - unless we setup some signal handlers in the native layer to do this, but I am not sure if that is a good idea.

@Trott
Copy link
Member

Trott commented Dec 5, 2018

Seems like this has stalled and has some mild opposition. I'm going to close it, but if it's still being worked on, feel free to re-open or comment asking for someone else to re-open.

Thanks for the pull request and I hope you continue to contribute!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a common.log for tests