Skip to content

Commit 9a0dc49

Browse files
committed
Environment.environ instead of os.environ
1 parent fcf3879 commit 9a0dc49

19 files changed

Lines changed: 66 additions & 53 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ builtins-ignorelist = ["help", "format", "input", "filter", "copyright", "max"]
455455
"pathlib.PosixPath".msg = "Use tmt._compat.pathlib.Path instead."
456456
"pydantic".msg = "Use tmt._compat.pydantic instead."
457457
"warnings.deprecated".msg = "Use tmt._compat.warnings.deprecated instead."
458+
"os.environ".msg = "Use tmt.utils.environment.Environment.environ instead."
459+
"os.getenv".msg = "Use tmt.utils.environment.Environment.environ instead."
458460
"os.path".msg = "Use tmt._compat.pathlib.Path and pathlib instead."
459461
# Banning builtins is not yet supported: https://github.com/astral-sh/ruff/issues/10079
460462
# "builtins.open".msg = "Use Path.{write_text,append_text,read_text,write_bytes,read_bytes} instead."

tests/core/environment-file/data/environment_file_data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import os
2-
31
import pytest
42

3+
from tmt.utils import Environment
4+
55
env_vars_parametrization = (
66
("env_name", "value"),
77
(
@@ -18,4 +18,4 @@
1818

1919
@pytest.mark.parametrize(*env_vars_parametrization)
2020
def test_environment_file_with_variables(env_name, value):
21-
assert os.environ[env_name] == value
21+
assert Environment.environ[env_name] == value

tests/execute/tty/data/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import sys
55

6-
step = os.getenv('STEP', '')
6+
step = os.getenv('STEP', '') # noqa: TID251
77

88
isatty = (sys.stdin.isatty(), sys.stdout.isatty(), sys.stderr.isatty())
99

tests/execute/tty/ptty-wrapper

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ import os
77
import pty
88
import sys
99

10-
os.environ["TERM"] = "vt100"
10+
os.environ["TERM"] = "vt100" # noqa: TID251
1111
exit_status = pty.spawn(("/bin/bash", "-c", " ".join(sys.argv[1:])))
1212
sys.exit(os.waitstatus_to_exitcode(exit_status))

tests/unit/test_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import tmt.utils
1414
from tmt.base.core import FmfId, expand_node_data
1515
from tmt.base.links import Link, LinkNeedle, Links
16-
from tmt.utils import Path, SpecificationError
16+
from tmt.utils import Environment, Path, SpecificationError
1717

1818
if TYPE_CHECKING:
1919
from tests import RunTmt
@@ -294,7 +294,7 @@ def test_expand_node_data(monkeypatch) -> None:
294294

295295
expected = [*_expected, [*_expected], {f'key{i}': value for i, value in enumerate(_expected)}]
296296

297-
for envvar in os.environ:
297+
for envvar in Environment.environ:
298298
monkeypatch.delenv(envvar)
299299

300300
for envvar, value in environ.items():

tests/unit/test_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ def test_inject_auth_git_url(monkeypatch) -> None:
178178
"""
179179

180180
# empty environment
181-
monkeypatch.setattr('os.environ', {})
181+
monkeypatch.setattr('Environment.environ', {})
182182
assert inject_auth_git_url('input_text') == 'input_text'
183183

184184
suffix = '_glab'
185185
# https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#clone-repository-using-personal-access-token
186186
# username can be anything but cannot be an empty string
187187
monkeypatch.setattr(
188-
'os.environ',
188+
'Environment.environ',
189189
{
190190
f'{tmt.utils.git.INJECT_CREDENTIALS_URL_PREFIX}{suffix}': 'https://gitlab.com/namespace/project',
191191
f'{tmt.utils.git.INJECT_CREDENTIALS_VALUE_PREFIX}{suffix}': 'foo:abcdefgh',
@@ -201,7 +201,7 @@ def test_inject_auth_git_url(monkeypatch) -> None:
201201
# https://github.blog/2012-09-21-easier-builds-and-deployments-using-git-over-https-and-oauth/
202202
# just token or username is used (value before @)
203203
monkeypatch.setattr(
204-
'os.environ',
204+
'Environment.environ',
205205
{
206206
f'{tmt.utils.git.INJECT_CREDENTIALS_URL_PREFIX}{suffix}': 'https://github.com/namespace/project',
207207
f'{tmt.utils.git.INJECT_CREDENTIALS_VALUE_PREFIX}{suffix}': 'abcdefgh',

tmt/config/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import functools
2-
import os
32
from contextlib import suppress
43
from typing import Optional, TypeVar, cast
54

@@ -29,8 +28,8 @@ def effective_config_dir() -> Path:
2928
:py:const:`DEFAULT_CONFIG_DIR` is picked.
3029
"""
3130

32-
if 'TMT_CONFIG_DIR' in os.environ:
33-
return Path(os.environ['TMT_CONFIG_DIR']).expanduser()
31+
if 'TMT_CONFIG_DIR' in tmt.utils.Environment.environ:
32+
return Path(tmt.utils.Environment.environ['TMT_CONFIG_DIR']).expanduser()
3433

3534
return DEFAULT_CONFIG_DIR.expanduser()
3635

tmt/guest/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def configure_ssh_options() -> tmt.utils.RawCommand:
181181

182182
options: tmt.utils.RawCommand = []
183183

184-
for name, value in os.environ.items():
184+
for name, value in Environment.environ.items():
185185
match = re.match(r'TMT_SSH_([a-zA-Z_]+)', name)
186186

187187
if not match:

tmt/libraries/beakerlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def _do_fetch(self, directory: Path) -> None:
363363
else:
364364
self.parent.debug(f"Cloning '{self.identifier}' for '{self}'.", level=3)
365365

366-
environment = Environment.from_environ()
366+
environment = Environment.environ
367367
environment["GIT_ASKPASS"] = EnvVarValue("echo")
368368

369369
tmt.utils.git.git_clone(

tmt/log.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import io
3030
import itertools
3131
import logging
32-
import os
3332
import sys
3433
from typing import (
3534
TYPE_CHECKING,
@@ -128,7 +127,7 @@ def create_decolorizer(apply_colors: bool) -> Callable[[str], str]:
128127
def _debug_level_from_global_envvar() -> int:
129128
import tmt.utils
130129

131-
raw_value = os.getenv('TMT_DEBUG', None)
130+
raw_value = tmt.utils.Environment.environ.get('TMT_DEBUG', None)
132131

133132
if raw_value is None:
134133
return 0
@@ -182,15 +181,17 @@ def decide_colorization(no_color: bool, force_color: bool) -> tuple[bool, bool]:
182181
for logging colorization.
183182
"""
184183

184+
from tmt.utils import Environment
185+
185186
# Default values: assume colors & unicorns everywhere.
186187
apply_colors_output = apply_colors_logging = True
187188

188189
# Enforce colors if `--force-color` was used, or `TMT_FORCE_COLOR` envvar is set.
189-
if force_color or 'TMT_FORCE_COLOR' in os.environ:
190+
if force_color or 'TMT_FORCE_COLOR' in Environment.environ:
190191
apply_colors_output = apply_colors_logging = True
191192

192193
# Disable coloring if `--no-color` was used, or `NO_COLOR` or `TMT_NO_COLOR` envvar is set.
193-
elif no_color or 'NO_COLOR' in os.environ or 'TMT_NO_COLOR' in os.environ:
194+
elif no_color or 'NO_COLOR' in Environment.environ or 'TMT_NO_COLOR' in Environment.environ:
194195
apply_colors_output = apply_colors_logging = False
195196

196197
# Autodetection, disable colors when not talking to a terminal.

0 commit comments

Comments
 (0)