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

Support defining test reporter using environment variable #46484

Closed
remcohaszing opened this issue Feb 3, 2023 · 16 comments · Fixed by #46688
Closed

Support defining test reporter using environment variable #46484

remcohaszing opened this issue Feb 3, 2023 · 16 comments · Fixed by #46688
Labels
feature request Issues that request new features to be added to Node.js. test_runner Issues and PRs related to the test runner subsystem.

Comments

@remcohaszing
Copy link

What is the problem this feature will solve?

#45712 added support for custom test reporters, which is great!

It’s still a but cumbersome having to specify the --test-reporter flag for every test run.

What is the feature you are proposing to solve the problem?

Use an environment variable NODE_TEST_REPORTER. If this is set, this will override the default tap runner. The command line argument will still take precedence. If this is specified, it will entirely ignore the NODE_TEST_REPORTER environment variable.

# Uses the tap formatter
node --test

# Uses the spec formatter
NODE_TEST_REPORTER=spec node --test

# Uses the tap formatter
NODE_TEST_REPORTER=spec node --test --test-reporter tap

This allows users to define their preferred formatter in their environment, e.g. in ~/.bashrc or ~/.zshrc

What alternatives have you considered?

It could also be supported in NODE_OPTIONS. However, people use this for different purposes too.

@remcohaszing remcohaszing added the feature request Issues that request new features to be added to Node.js. label Feb 3, 2023
@cjihrig
Copy link
Contributor

cjihrig commented Feb 3, 2023

It's unlikely that we will add an environment variable for this. The general consensus is for things like that to be done via NODE_OPTIONS (otherwise we would end up supporting CLI flags and environment variables for every option supported by Node).

@MoLow MoLow added the test_runner Issues and PRs related to the test runner subsystem. label Feb 4, 2023
@MoLow
Copy link
Member

MoLow commented Feb 4, 2023

@cjihrig --test-reporter is not allowed in NODE_OPTIONS and neither are any other test runner flags. perhaps we should change that

@cjihrig
Copy link
Contributor

cjihrig commented Feb 4, 2023

@MoLow sounds fine to me. The main reason for not doing so up to this point is that we'll need to parse the NODE_OPTIONS environment variable in the CLI runner when spawning child processes to make sure this logic is preserved.

@SRHerzog
Copy link
Contributor

Re: the last couple of comments, I can dig into this if no one else is busy with it.

@remcohaszing
Copy link
Author

The problem I see putting this in NODE_OPTIONS, is that a user may specify multiple --test-reporter and --test-reporter-destination flags, and order matters. Should these flags, when specified in NODE_OPTIONS, be discarded if the user passes these flags explicitly? Should they be appended?

Another solution could be to introduce a Node user configuration file ($XDG_CONFIG_HOME/node/config.json), but thats quite a big deviation from current approaches for options. This could contain other options, e.g. to configure the repl.

{
  "repl": {
    "ignoreUndefined": true
  },
  "test-reporter": [
    "spec",
    ["tap", "output.tap"]
  ]
}

@SRHerzog
Copy link
Contributor

I think the intuitive behavior would be to ignore the values in NODE_OPTIONS when flags are passed via the CLI.

@cjihrig
Copy link
Contributor

cjihrig commented Feb 15, 2023

NODE_OPTIONS has documented behavior for such edge cases: https://nodejs.org/api/cli.html#node_optionsoptions

SRHerzog added a commit to SRHerzog/node that referenced this issue Feb 16, 2023
Adds --test-reporter and --test-reporter-destination as
allowable options in NODE_OPTIONS.

Fixes nodejs#46484
SRHerzog added a commit to SRHerzog/node that referenced this issue Feb 16, 2023
Adds --test-reporter and --test-reporter-destination as
allowable options in NODE_OPTIONS.

Fixes nodejs#46484
SRHerzog added a commit to SRHerzog/node that referenced this issue Feb 16, 2023
Adds --test-reporter and --test-reporter-destination as
allowable options in NODE_OPTIONS.

Fixes: nodejs#46484
@SRHerzog
Copy link
Contributor

SRHerzog commented Feb 16, 2023

This mostly works but will break on a --test-reporter or --test-reporter-destination value with a space in it, because I just split the NODE_OPTIONS string rather than properly parsing out quoted values. I can keep going to fully parse NODE_OPTIONS, assuming we want to make sure that values with spaces are supported properly.

@simoneb
Copy link
Contributor

simoneb commented Feb 16, 2023

@SRHerzog why do we need to parse them again? Surely there must be a place in the codebase where the parsing is already done for us, and there are other options which support a space between the option name and the value, e.g. --require.

@SRHerzog
Copy link
Contributor

NODE_OPTIONS is parsed in the C++ layer and merged with the CLI arguments, along with defaults where appropriate. It's read by the JavaScript layer as a Map after that merge is complete. There isn't a binding for the JavaScript runtime to retrieve NODE_OPTIONS in tokenized form or extract the NODE_OPTIONS values from the overall options map, so we would have to add that kind of functionality to node_options.cc if we want to avoid parsing the whole string again.

@simoneb
Copy link
Contributor

simoneb commented Feb 17, 2023

Gotcha, I'm not familiar enough with the codebase to know when things happen exactly, I was just assuming that they must be happening somewhere. I assume that that when happens to late for us to make any use of it in this context. I wonder, aren't there other NODE_OPTIONS entries which the test runner is already making use of? How are they parsed?

@SRHerzog
Copy link
Contributor

The only option specific to the test runner that can currently be passed through NODE_OPTIONS is --test-only. That one is passed through to child processes if it's set from the CLI and not filtered out like --test-reporter and --test-reporter-destination.

@simoneb
Copy link
Contributor

simoneb commented Feb 17, 2023

Apologies Steve I'm just commenting without any good level of understanding of what's actually going on here. Why can't we do the same as with --test-only then?

@SRHerzog
Copy link
Contributor

I think it's necessary for --test-reporter and --test-reporter-destination to not be passed through from the parent test process to the child test process, because the child must report its test output through stdout in the default TAP format in order to be parsed by the parent.

@simoneb
Copy link
Contributor

simoneb commented Feb 17, 2023

Ok this makes more sense now, thanks for clarifying. I'm not particularly fond of the solution but I don't have any better ideas.

@SRHerzog
Copy link
Contributor

SRHerzog commented Feb 18, 2023

I agree that parsing and filtering the command line args and NODE_OPTIONS seems hacky. A cleaner implementation would be to add another option to pass to the child process that causes it to override the normal logic for identifying test reporters and destinations.

async function setupTestReporters(testsStream) {
  const isChildTestProcess = getOptionValue('--test-child-process');
  const destinations = isChildTestProcess ? [kDefaultDestination] :
    getOptionValue('--test-reporter-destination');
  const reporters = isChildTestProcess ? [kDefaultReporter] :
    getOptionValue('--test-reporter');
  ...

SRHerzog added a commit to SRHerzog/node that referenced this issue Feb 23, 2023
Adds --test-reporter and --test-reporter-destination as
allowable options in NODE_OPTIONS. Also adds the CLI flag
--test-child-process to allow forcing the default
test-reporter for inter-process communication.

Fixes: nodejs#46484
SRHerzog added a commit to SRHerzog/node that referenced this issue Mar 6, 2023
Adds --test-reporter and --test-reporter-destination as
allowable options in NODE_OPTIONS. Also adds the CLI flag
--test-child-process to allow forcing the default
test-reporter for inter-process communication.

Fixes: nodejs#46484
nodejs-github-bot pushed a commit that referenced this issue Mar 14, 2023
Adds --test-reporter and --test-reporter-destination as
allowable options in NODE_OPTIONS. Also adds the CLI flag
--test-child-process to allow forcing the default
test-reporter for inter-process communication.

Fixes: #46484
PR-URL: #46688
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
targos pushed a commit that referenced this issue Mar 18, 2023
Adds --test-reporter and --test-reporter-destination as
allowable options in NODE_OPTIONS. Also adds the CLI flag
--test-child-process to allow forcing the default
test-reporter for inter-process communication.

Fixes: #46484
PR-URL: #46688
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
danielleadams pushed a commit that referenced this issue Jul 6, 2023
Adds --test-reporter and --test-reporter-destination as
allowable options in NODE_OPTIONS. Also adds the CLI flag
--test-child-process to allow forcing the default
test-reporter for inter-process communication.

Fixes: #46484
PR-URL: #46688
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants