Skip to content

Commit 2e3bf04

Browse files
authored
Move Environment class to its own module (#5041)
Making `tmt.utils` smaller, but also part of the secrets implementation: by moving `Environment` and few other bits, it should be possible to close access to `os.environ`. A lot of changes, but just moves - some `tmt.utils.Environment` turned into `from tmt.utils.environment import Environment` + bare `Environment`; the string would have to change anyway, so at least we can treat `Environment` as a well-known name in the codebase. Related to #2609.
1 parent b9010ea commit 2e3bf04

56 files changed

Lines changed: 759 additions & 689 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/unit/test_guest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
from tmt.utils import (
2222
Command,
2323
CommandOutput,
24-
Environment,
2524
GeneralError,
2625
OnProcessEndCallback,
2726
OnProcessStartCallback,
2827
Path,
2928
RunError,
3029
ShellScript,
3130
)
31+
from tmt.utils.environment import Environment
3232
from tmt.utils.wait import Waiting
3333

3434
from . import TEST_CONTAINERS

tests/unit/test_steps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import tmt.guest
99
import tmt.queue
1010
import tmt.steps
11-
import tmt.utils
1211
from tmt.log import Logger
1312
from tmt.steps import Phase
1413
from tmt.utils import GeneralError
14+
from tmt.utils.environment import Environment
1515

1616

1717
class TestPhaseAssertFeelingSafe:
@@ -77,7 +77,7 @@ def go(
7777
self,
7878
*,
7979
guest: tmt.guest.Guest,
80-
environment: Optional[tmt.utils.Environment] = None,
80+
environment: Optional[Environment] = None,
8181
logger: Logger,
8282
) -> None:
8383
pass

tests/unit/test_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
duration_to_seconds,
4040
filter_paths,
4141
)
42+
from tmt.utils.environment import Environment
4243
from tmt.utils.git import (
4344
clonable_git_url,
4445
git_add,
@@ -1536,7 +1537,7 @@ def test_locate_key_origin_empty_defined(id_tree_empty: fmf.Tree) -> None:
15361537
""", # noqa: E501
15371538
),
15381539
# environment
1539-
(tmt.utils.Environment.from_dict({'FOO': 'BAR'}), None, 'FOO\033[0m: BAR'),
1540+
(Environment.from_dict({'FOO': 'BAR'}), None, 'FOO\033[0m: BAR'),
15401541
# fmf context
15411542
(
15421543
tmt.utils.FmfContext({'foo': ['bar', 'baz']}),

tmt/base/core.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@
6464
from tmt.lint import LinterOutcome, LinterReturn
6565
from tmt.result import ResultInterpret
6666
from tmt.utils import (
67-
Environment,
6867
FmfContext,
6968
Path,
7069
ShellScript,
7170
normalize_shell_script,
7271
to_yaml,
7372
verdict,
7473
)
74+
from tmt.utils.environment import Environment
7575
from tmt.utils.themes import style
7676

7777
if TYPE_CHECKING:
@@ -1198,11 +1198,11 @@ class Test(
11981198
normalize=normalize_require,
11991199
exporter=lambda value: [dependency.to_minimal_spec() for dependency in value],
12001200
)
1201-
environment: tmt.utils.Environment = field(
1202-
default_factory=tmt.utils.Environment,
1203-
normalize=tmt.utils.Environment.normalize,
1201+
environment: Environment = field(
1202+
default_factory=Environment,
1203+
normalize=Environment.normalize,
12041204
serialize=lambda environment: environment.to_fmf_spec(),
1205-
unserialize=lambda serialized: tmt.utils.Environment.from_fmf_spec(serialized),
1205+
unserialize=lambda serialized: Environment.from_fmf_spec(serialized),
12061206
exporter=lambda environment: environment.to_fmf_spec(),
12071207
)
12081208

tmt/base/plan.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,15 @@
4949
from tmt.lint import LinterOutcome, LinterReturn
5050
from tmt.utils import (
5151
Command,
52-
Environment,
53-
EnvVarValue,
5452
FmfContext,
5553
GeneralError,
56-
HasEnvironment,
5754
HasPlanWorkdir,
5855
HasRunWorkdir,
5956
HasUserAnchorPath,
6057
style,
6158
to_yaml,
6259
)
60+
from tmt.utils.environment import Environment, EnvVarValue, HasEnvironment
6361

6462
if TYPE_CHECKING:
6563
import tmt.cli

tmt/base/run.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@
3939
from tmt.result import Result
4040
from tmt.utils import (
4141
Command,
42-
Environment,
4342
GeneralError,
44-
HasEnvironment,
4543
HasRunWorkdir,
4644
HasUserAnchorPath,
4745
Path,
4846
StateFormat,
4947
WorkdirArgumentType,
5048
)
49+
from tmt.utils.environment import Environment, HasEnvironment
5150

5251
if TYPE_CHECKING:
5352
import tmt.cli
@@ -73,7 +72,7 @@ class RunData(SerializableContainer):
7372
environment: Environment = field(
7473
default_factory=Environment,
7574
serialize=lambda environment: environment.to_fmf_spec(),
76-
unserialize=lambda serialized: tmt.utils.Environment.from_fmf_spec(serialized),
75+
unserialize=lambda serialized: Environment.from_fmf_spec(serialized),
7776
)
7877

7978

@@ -333,7 +332,7 @@ def _environment_from_cli(self) -> Environment:
333332

334333
assert self.tree is not None # narrow type
335334

336-
return tmt.utils.Environment.from_cli_options(
335+
return Environment.from_cli_options(
337336
raw_cli_environment_files=self.opt('environment-file') or [],
338337
raw_cli_environment=self.opt('environment') or [],
339338
file_root=Path(self.tree.root) if self.tree.root else None,

tmt/checks/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)
1515
from tmt.plugins import PluginRegistry
1616
from tmt.utils import NormalizeKeysMixin
17+
from tmt.utils.environment import Environment
1718

1819
if TYPE_CHECKING:
1920
import tmt.base.core
@@ -163,7 +164,7 @@ def go(
163164
*,
164165
event: CheckEvent,
165166
invocation: 'TestInvocation',
166-
environment: Optional[tmt.utils.Environment] = None,
167+
environment: Optional[Environment] = None,
167168
logger: tmt.log.Logger,
168169
) -> list['CheckResult']:
169170
"""
@@ -268,7 +269,7 @@ def before_test(
268269
*,
269270
check: CheckT,
270271
invocation: 'TestInvocation',
271-
environment: Optional[tmt.utils.Environment] = None,
272+
environment: Optional[Environment] = None,
272273
logger: tmt.log.Logger,
273274
) -> list['CheckResult']:
274275
return []
@@ -279,7 +280,7 @@ def after_test(
279280
*,
280281
check: CheckT,
281282
invocation: 'TestInvocation',
282-
environment: Optional[tmt.utils.Environment] = None,
283+
environment: Optional[Environment] = None,
283284
logger: tmt.log.Logger,
284285
) -> list['CheckResult']:
285286
return []

tmt/checks/avc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
render_command_report,
2222
safe_call,
2323
)
24+
from tmt.utils.environment import Environment
2425
from tmt.utils.hints import hints_as_notes
2526

2627
if TYPE_CHECKING:
@@ -493,7 +494,7 @@ def before_test(
493494
*,
494495
check: 'AvcCheck',
495496
invocation: 'TestInvocation',
496-
environment: Optional[tmt.utils.Environment] = None,
497+
environment: Optional[Environment] = None,
497498
logger: tmt.log.Logger,
498499
) -> list[CheckResult]:
499500
if invocation.guest.facts.has_selinux:
@@ -507,7 +508,7 @@ def after_test(
507508
*,
508509
check: 'AvcCheck',
509510
invocation: 'TestInvocation',
510-
environment: Optional[tmt.utils.Environment] = None,
511+
environment: Optional[Environment] = None,
511512
logger: tmt.log.Logger,
512513
) -> list[CheckResult]:
513514
if not invocation.guest.facts.has_selinux:

tmt/checks/coredump.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from tmt.container import container, field
1010
from tmt.result import CheckResult, ResultOutcome, save_failures
1111
from tmt.utils import Command, Path, ShellScript
12+
from tmt.utils.environment import Environment
1213

1314
if TYPE_CHECKING:
1415
from tmt.guest import Guest
@@ -530,7 +531,7 @@ def before_test(
530531
*,
531532
check: "CoredumpCheck",
532533
invocation: "TestInvocation",
533-
environment: Optional[tmt.utils.Environment] = None,
534+
environment: Optional[Environment] = None,
534535
logger: tmt.log.Logger,
535536
) -> list[CheckResult]:
536537
"""Check for crashes before the test starts."""
@@ -568,7 +569,7 @@ def after_test(
568569
*,
569570
check: "CoredumpCheck",
570571
invocation: "TestInvocation",
571-
environment: Optional[tmt.utils.Environment] = None,
572+
environment: Optional[Environment] = None,
572573
logger: tmt.log.Logger,
573574
) -> list[CheckResult]:
574575
"""Check for crashes after the test finishes."""

tmt/checks/dmesg.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from tmt.guest import GuestCapability
1212
from tmt.result import CheckResult, ResultOutcome, save_failures
1313
from tmt.utils import Path, Stopwatch
14+
from tmt.utils.environment import Environment
1415
from tmt.utils.hints import hints_as_notes
1516

1617
if TYPE_CHECKING:
@@ -211,7 +212,7 @@ def before_test(
211212
*,
212213
check: 'DmesgCheck',
213214
invocation: 'TestInvocation',
214-
environment: Optional[tmt.utils.Environment] = None,
215+
environment: Optional[Environment] = None,
215216
logger: tmt.log.Logger,
216217
) -> list[CheckResult]:
217218
if not invocation.guest.facts.has_capability(GuestCapability.SYSLOG_ACTION_READ_ALL):
@@ -232,7 +233,7 @@ def after_test(
232233
*,
233234
check: 'DmesgCheck',
234235
invocation: 'TestInvocation',
235-
environment: Optional[tmt.utils.Environment] = None,
236+
environment: Optional[Environment] = None,
236237
logger: tmt.log.Logger,
237238
) -> list[CheckResult]:
238239
if not invocation.guest.facts.has_capability(GuestCapability.SYSLOG_ACTION_READ_ALL):

0 commit comments

Comments
 (0)