Repository: nodejs/node
Pull request: test: support standalone env comment in tests

Following up on this comment [https://github.com/nodejs/node/pull/59503#issuecomment-3204950851](https://github.com/nodejs/node/pull/59503#issuecomment-3204950851), I'm handling the missing case.


Changed files:
filename: test/common/index.js
patch: @@ -116,34 +116,38 @@ if (process.argv.length === 2 &&
     require('cluster').isPrimary &&
     fs.existsSync(process.argv[1])) {
   const { flags, envs } = parseTestMetadata();
-  for (const flag of flags) {
-    if (!process.execArgv.includes(flag) &&
-        // If the binary is build without `intl` the inspect option is
-        // invalid. The test itself should handle this case.
-        (process.features.inspector || !flag.startsWith('--inspect'))) {
-      console.log(
-        'NOTE: The test started as a child_process using these flags:',
-        inspect(flags),
-        'And these environment variables:',
-        inspect(envs),
-        'Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.',
-      );
-      const { spawnSync } = require('child_process');
-      const args = [...flags, ...process.execArgv, ...process.argv.slice(1)];
-      const options = {
-        encoding: 'utf8',
-        stdio: 'inherit',
-        env: {
-          ...process.env,
-          ...envs,
-        },
-      };
-      const result = spawnSync(process.execPath, args, options);
-      if (result.signal) {
-        process.kill(0, result.signal);
-      } else {
-        process.exit(result.status);
-      }
+
+  const flagsTriggerSpawn = flags.some((flag) => (
+    !process.execArgv.includes(flag) &&
+    // If the binary is build without `intl` the inspect option is
+    // invalid. The test itself should handle this case.
+    (process.features.inspector || !flag.startsWith('--inspect'))
+  ));
+  const envsTriggerSpawn = Object.keys(envs).some((key) => process.env[key] !== envs[key]);
+
+  if (flagsTriggerSpawn || envsTriggerSpawn) {
+    console.log(
+      'NOTE: The test started as a child_process using these flags:',
+      inspect(flags),
+      'And these environment variables:',
+      inspect(envs),
+      'Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.',
+    );
+    const { spawnSync } = require('child_process');
+    const args = [...flags, ...process.execArgv, ...process.argv.slice(1)];
+    const options = {
+      encoding: 'utf8',
+      stdio: 'inherit',
+      env: {
+        ...process.env,
+        ...envs,
+      },
+    };
+    const result = spawnSync(process.execPath, args, options);
+    if (result.signal) {
+      process.kill(0, result.signal);
+    } else {
+      process.exit(result.status);
     }
   }
 }

filename: test/parallel/test-parse-test-only-envs.js
patch: @@ -0,0 +1,20 @@
+'use strict';
+
+// Env: A_SET_ENV_VAR=A_SET_ENV_VAR_VALUE B_SET_ENV_VAR=B_SET_ENV_VAR_VALUE
+
+require('../common');
+const assert = require('node:assert');
+const { describe, it } = require('node:test');
+
+
+// This test verifies that a test file that requires 'common' can set environment variables
+// via comments in the test file, similar to how we set flags via comments.
+// Ref: https://github.com/nodejs/node/issues/58179
+describe('env var via comment', () => {
+  it('should set env var A_SET_ENV_VAR', () => {
+    assert.strictEqual(process.env.A_SET_ENV_VAR, 'A_SET_ENV_VAR_VALUE');
+  });
+  it('should set env var B_SET_ENV_VAR', () => {
+    assert.strictEqual(process.env.B_SET_ENV_VAR, 'B_SET_ENV_VAR_VALUE');
+  });
+});