-
-
Notifications
You must be signed in to change notification settings - Fork 530
Fix for tox4 regression issue with setenv file and substitutions (#2435) #3521
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
Open
realitycheck
wants to merge
1
commit into
tox-dev:main
Choose a base branch
from
realitycheck:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,8 @@ def __init__( # noqa: C901, PLR0912 | |
return | ||
for line in raw.splitlines(): # noqa: PLR1702 | ||
if line.strip(): | ||
if line.startswith("file|"): # environment files to be handled later | ||
self._env_files.append(line[len("file|") :]) | ||
if self._is_file_line(line): | ||
self._env_files.append(self._parse_file_line(line)) | ||
else: | ||
try: | ||
key, value = self._extract_key_value(line) | ||
|
@@ -52,12 +52,20 @@ def __init__( # noqa: C901, PLR0912 | |
else: | ||
self._raw[key] = value | ||
|
||
@staticmethod | ||
def _is_file_line(line: str) -> bool: | ||
return line.startswith("file|") | ||
|
||
@staticmethod | ||
def _parse_file_line(line: str) -> str: | ||
return line[len("file|") :] | ||
|
||
def use_replacer(self, value: Replacer, args: ConfigLoadArgs) -> None: | ||
self._replacer = value | ||
for filename in self._env_files: | ||
self._read_env_file(filename, args) | ||
self._raw.update(self._stream_env_file(filename, args)) | ||
|
||
def _read_env_file(self, filename: str, args: ConfigLoadArgs) -> None: | ||
def _stream_env_file(self, filename: str, args: ConfigLoadArgs) -> Iterator[tuple[str, str]]: | ||
# Our rules in the documentation, some upstream environment file rules (we follow mostly the docker one): | ||
# - https://www.npmjs.com/package/dotenv#rules | ||
# - https://docs.docker.com/compose/env-file/ | ||
|
@@ -70,8 +78,7 @@ def _read_env_file(self, filename: str, args: ConfigLoadArgs) -> None: | |
env_line = env_line.strip() # noqa: PLW2901 | ||
if not env_line or env_line.startswith("#"): | ||
continue | ||
key, value = self._extract_key_value(env_line) | ||
self._raw[key] = value | ||
yield self._extract_key_value(env_line) | ||
|
||
@staticmethod | ||
def _extract_key_value(line: str) -> tuple[str, str]: | ||
|
@@ -100,10 +107,18 @@ def __iter__(self) -> Iterator[str]: | |
# start with the materialized ones, maybe we don't need to materialize the raw ones | ||
yield from self._materialized.keys() | ||
yield from list(self._raw.keys()) # iterating over this may trigger materialization and change the dict | ||
args = ConfigLoadArgs([], self._name, self._env_name) | ||
while self._needs_replacement: | ||
line = self._needs_replacement.pop(0) | ||
expanded_line = self._replacer(line, ConfigLoadArgs([], self._name, self._env_name)) | ||
sub_raw = dict(self._extract_key_value(sub_line) for sub_line in expanded_line.splitlines() if sub_line) | ||
expanded_line = self._replacer(line, args) | ||
sub_raw: dict[str, str] = {} | ||
for sub_line in filter(None, expanded_line.splitlines()): | ||
if not self._is_file_line(sub_line): | ||
sub_raw.__setitem__(*self._extract_key_value(sub_line)) | ||
else: | ||
for k, v in self._stream_env_file(self._parse_file_line(sub_line), args): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No single letter variables outside of comprehensions. |
||
if k not in self._raw: | ||
sub_raw[k] = v # noqa: PERF403 | ||
self._raw.update(sub_raw) | ||
self.changed = True # loading while iterating can cause these values to be missed | ||
yield from sub_raw.keys() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain the change here and why you done it this way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part you didn't really address. Writing a descriptive text involves the approach you've taken and why.