Skip to content

Commit 830bb71

Browse files
committed
tools: support environment variables via comments
1 parent 25fe802 commit 830bb71

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
// Environment Variables: A_SET_ENV_VAR=A_SET_ENV_VAR_VALUE B_SET_ENV_VAR=B_SET_ENV_VAR_VALUE
4+
// Flags: --test-isolation=none --expose-internals
5+
6+
require('../common');
7+
const assert = require('node:assert');
8+
const { describe, it } = require('node:test');
9+
10+
11+
// This test verifies that the Python test runner can set environment variables
12+
// via comments in the test file, similar to how we set flags via comments.
13+
// Ref: https://github.com/nodejs/node/issues/58179
14+
describe('testpy env var via comment', () => {
15+
it('should set env var A_SET_ENV_VAR', () => {
16+
assert.strictEqual(process.env.A_SET_ENV_VAR, 'A_SET_ENV_VAR_VALUE');
17+
});
18+
it('should set env var B_SET_ENV_VAR', () => {
19+
assert.strictEqual(process.env.B_SET_ENV_VAR, 'B_SET_ENV_VAR_VALUE');
20+
});
21+
22+
it('should interop with flags', () => {
23+
const flag = require('internal/options').getOptionValue('--test-isolation');
24+
assert.strictEqual(flag, 'none');
25+
});
26+
});

test/testpy/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
FLAGS_PATTERN = re.compile(r"//\s+Flags:(.*)")
3636
LS_RE = re.compile(r'^test-.*\.m?js$')
37+
ENV_PATTERN = re.compile(r"//\s+Environment Variables:(.*)")
3738

3839
class SimpleTestCase(test.TestCase):
3940

@@ -48,17 +49,26 @@ def __init__(self, path, file, arch, mode, context, config, additional=None):
4849
else:
4950
self.additional_flags = []
5051

52+
def _parse_source_env(self, source):
53+
env_match = ENV_PATTERN.search(source)
54+
env = {}
55+
if env_match:
56+
for env_pair in env_match.group(1).strip().split():
57+
var, value = env_pair.split('=')
58+
env[var] = value
59+
return env
5160

5261
def GetLabel(self):
5362
return "%s %s" % (self.mode, self.GetName())
5463

5564
def GetName(self):
5665
return self.path[-1]
5766

58-
def GetCommand(self):
67+
def GetRunConfiguration(self):
5968
result = [self.config.context.GetVm(self.arch, self.mode)]
6069
source = open(self.file, encoding='utf8').read()
6170
flags_match = FLAGS_PATTERN.search(source)
71+
envs = self._parse_source_env(source)
6272
if flags_match:
6373
flags = flags_match.group(1).strip().split()
6474
# The following block reads config.gypi to extract the v8_enable_inspector
@@ -93,7 +103,10 @@ def GetCommand(self):
93103

94104
result += [self.file]
95105

96-
return result
106+
return {
107+
'command': result,
108+
'envs': envs
109+
}
97110

98111
def GetSource(self):
99112
return open(self.file).read()

tools/test.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,21 @@ def RunCommand(self, command, env):
602602

603603
def Run(self):
604604
try:
605-
result = self.RunCommand(self.GetCommand(), {
605+
run_configuration = self.GetRunConfiguration()
606+
command = run_configuration['command']
607+
envs = {}
608+
if 'envs' in run_configuration:
609+
envs.update(run_configuration['envs'])
610+
envs.update({
606611
"TEST_SERIAL_ID": "%d" % self.serial_id,
607612
"TEST_THREAD_ID": "%d" % self.thread_id,
608613
"TEST_PARALLEL" : "%d" % self.parallel,
609614
"GITHUB_STEP_SUMMARY": "",
610615
})
616+
result = self.RunCommand(
617+
command,
618+
envs
619+
)
611620
finally:
612621
# Tests can leave the tty in non-blocking mode. If the test runner
613622
# tries to print to stdout/stderr after that and the tty buffer is

0 commit comments

Comments
 (0)