GeezerVison is showing github.com — tools: support environment variables via comments · nodejs/node@b8d4715
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Env: 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 | + | | 9 | +// This test verifies that the Python test runner can set environment variables | | 10 | +// via comments in the test file, similar to how we set flags via comments. | | 11 | +// Ref: https://github.com/nodejs/node/issues/58179 | | 12 | +assert.strictEqual(process.env.A_SET_ENV_VAR, 'A_SET_ENV_VAR_VALUE'); | | 13 | +assert.strictEqual(process.env.B_SET_ENV_VAR, 'B_SET_ENV_VAR_VALUE'); | | 14 | +// Check interop with flags | | 15 | +const flag = require('internal/options').getOptionValue('--test-isolation'); | | 16 | +assert.strictEqual(flag, 'none'); |
Collapse file Copy file name to clipboardExpand all lines: test/message/test-testpy-env-var-via-comment.out Collapse file Copy file name to clipboardExpand all lines: test/message/testcfg.py +16-2Lines changed: 16 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 33 | 33 | from functools import reduce | 34 | 34 |
| 35 | 35 | FLAGS_PATTERN = re.compile(r"//\s+Flags:(.*)") | | 36 | +ENV_PATTERN = re.compile(r"//\s+Env:(.*)") | 36 | 37 |
| 37 | 38 | class MessageTestCase(test.TestCase): | 38 | 39 |
| @@ -89,20 +90,33 @@ def IsFailureOutput(self, output): | 89 | 90 | return True | 90 | 91 | return False | 91 | 92 |
| | 93 | + def _parse_source_env(self, source): | | 94 | + env_match = ENV_PATTERN.search(source) | | 95 | + env = {} | | 96 | + if env_match: | | 97 | + for env_pair in env_match.group(1).strip().split(): | | 98 | + var, value = env_pair.split('=') | | 99 | + env[var] = value | | 100 | + return env | | 101 | + | 92 | 102 | def GetLabel(self): | 93 | 103 | return "%s %s" % (self.mode, self.GetName()) | 94 | 104 |
| 95 | 105 | def GetName(self): | 96 | 106 | return self.path[-1] | 97 | 107 |
| 98 | | - def GetCommand(self): | | 108 | + def GetRunConfiguration(self): | 99 | 109 | result = [self.config.context.GetVm(self.arch, self.mode)] | 100 | 110 | source = open(self.file).read() | 101 | 111 | flags_match = FLAGS_PATTERN.search(source) | | 112 | + envs = self._parse_source_env(source) | 102 | 113 | if flags_match: | 103 | 114 | result += flags_match.group(1).strip().split() | 104 | 115 | result.append(self.file) | 105 | | - return result | | 116 | + return { | | 117 | + 'command': result, | | 118 | + 'envs': envs | | 119 | + } | 106 | 120 |
| 107 | 121 | def GetSource(self): | 108 | 122 | return (open(self.file).read() | |
Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Env: 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 | +}); |
Collapse file Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Env: 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 | + | | 9 | +// This test verifies that the Python test runner can set environment variables | | 10 | +// via comments in the test file, similar to how we set flags via comments. | | 11 | +// Ref: https://github.com/nodejs/node/issues/58179 | | 12 | +assert.strictEqual(process.env.A_SET_ENV_VAR, 'A_SET_ENV_VAR_VALUE'); | | 13 | +assert.strictEqual(process.env.B_SET_ENV_VAR, 'B_SET_ENV_VAR_VALUE'); | | 14 | +// Check interop with flags | | 15 | +const flag = require('internal/options').getOptionValue('--test-isolation'); | | 16 | +assert.strictEqual(flag, 'none'); |
Collapse file Copy file name to clipboardExpand all lines: test/pseudo-tty/test-testpy-env-var-via-comment.out Collapse file Copy file name to clipboardExpand all lines: test/pseudo-tty/testcfg.py +16-2Lines changed: 16 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 37 | 37 |
| 38 | 38 | FLAGS_PATTERN = re.compile(r"//\s+Flags:(.*)") | 39 | 39 | PTY_HELPER = join(dirname(__file__), '../../tools/pseudo-tty.py') | | 40 | +ENV_PATTERN = re.compile(r"//\s+Env:(.*)") | 40 | 41 |
| 41 | 42 | class TTYTestCase(test.TestCase): | 42 | 43 |
| @@ -90,20 +91,33 @@ def IsFailureOutput(self, output): | 90 | 91 | return True | 91 | 92 | return False | 92 | 93 |
| | 94 | + def _parse_source_env(self, source): | | 95 | + env_match = ENV_PATTERN.search(source) | | 96 | + env = {} | | 97 | + if env_match: | | 98 | + for env_pair in env_match.group(1).strip().split(): | | 99 | + var, value = env_pair.split('=') | | 100 | + env[var] = value | | 101 | + return env | | 102 | + | 93 | 103 | def GetLabel(self): | 94 | 104 | return "%s %s" % (self.mode, self.GetName()) | 95 | 105 |
| 96 | 106 | def GetName(self): | 97 | 107 | return self.path[-1] | 98 | 108 |
| 99 | | - def GetCommand(self): | | 109 | + def GetRunConfiguration(self): | 100 | 110 | result = [self.config.context.GetVm(self.arch, self.mode)] | 101 | 111 | source = open(self.file).read() | 102 | 112 | flags_match = FLAGS_PATTERN.search(source) | | 113 | + envs = self._parse_source_env(source) | 103 | 114 | if flags_match: | 104 | 115 | result += flags_match.group(1).strip().split() | 105 | 116 | result.append(self.file) | 106 | | - return result | | 117 | + return { | | 118 | + 'command': result, | | 119 | + 'envs': envs | | 120 | + } | 107 | 121 |
| 108 | 122 | def GetSource(self): | 109 | 123 | return (open(self.file).read() | |
Collapse file Copy file name to clipboardExpand all lines: test/testpy/__init__.py +15-2Lines changed: 15 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 34 | 34 |
| 35 | 35 | FLAGS_PATTERN = re.compile(r"//\s+Flags:(.*)") | 36 | 36 | LS_RE = re.compile(r'^test-.*\.m?js$') | | 37 | +ENV_PATTERN = re.compile(r"//\s+Env:(.*)") | 37 | 38 |
| 38 | 39 | class SimpleTestCase(test.TestCase): | 39 | 40 |
| @@ -48,17 +49,26 @@ def __init__(self, path, file, arch, mode, context, config, additional=None): | 48 | 49 | else: | 49 | 50 | self.additional_flags = [] | 50 | 51 |
| | 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 | 51 | 60 |
| 52 | 61 | def GetLabel(self): | 53 | 62 | return "%s %s" % (self.mode, self.GetName()) | 54 | 63 |
| 55 | 64 | def GetName(self): | 56 | 65 | return self.path[-1] | 57 | 66 |
| 58 | | - def GetCommand(self): | | 67 | + def GetRunConfiguration(self): | 59 | 68 | result = [self.config.context.GetVm(self.arch, self.mode)] | 60 | 69 | source = open(self.file, encoding='utf8').read() | 61 | 70 | flags_match = FLAGS_PATTERN.search(source) | | 71 | + envs = self._parse_source_env(source) | 62 | 72 | if flags_match: | 63 | 73 | flags = flags_match.group(1).strip().split() | 64 | 74 | # The following block reads config.gypi to extract the v8_enable_inspector | @@ -93,7 +103,10 @@ def GetCommand(self): | 93 | 103 |
| 94 | 104 | result += [self.file] | 95 | 105 |
| 96 | | - return result | | 106 | + return { | | 107 | + 'command': result, | | 108 | + 'envs': envs | | 109 | + } | 97 | 110 |
| 98 | 111 | def GetSource(self): | 99 | 112 | return open(self.file).read() | |
Collapse file Copy file name to clipboardExpand all lines: tools/test.py +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -602,12 +602,21 @@ def RunCommand(self, command, env): | 602 | 602 |
| 603 | 603 | def Run(self): | 604 | 604 | 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({ | 606 | 611 | "TEST_SERIAL_ID": "%d" % self.serial_id, | 607 | 612 | "TEST_THREAD_ID": "%d" % self.thread_id, | 608 | 613 | "TEST_PARALLEL" : "%d" % self.parallel, | 609 | 614 | "GITHUB_STEP_SUMMARY": "", | 610 | 615 | }) | | 616 | + result = self.RunCommand( | | 617 | + command, | | 618 | + envs | | 619 | + ) | 611 | 620 | finally: | 612 | 621 | # Tests can leave the tty in non-blocking mode. If the test runner | 613 | 622 | # tries to print to stdout/stderr after that and the tty buffer is | |
|