Skip to content

Commit 62ddebc

Browse files
committed
fix TestsWithAdjust conversion, address comments
1 parent d34c3f4 commit 62ddebc

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

tmt/guest/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
container,
5151
field,
5252
key_to_option,
53+
option_to_key,
5354
)
5455
from tmt.package_managers import (
5556
FileSystemPath,
@@ -1325,6 +1326,7 @@ def from_spec( # type: ignore[override]
13251326
return super().from_spec(raw_data, logger) # type: ignore[call-arg]
13261327

13271328
def to_spec(self) -> tmt.steps._RawStepData:
1329+
"""Convert to a form suitable for saving in a specification file"""
13281330
spec = super().to_spec()
13291331

13301332
spec.pop('facts', None) # type: ignore[typeddict-item]
@@ -1336,20 +1338,27 @@ def to_spec(self) -> tmt.steps._RawStepData:
13361338
return spec
13371339

13381340
def to_minimal_spec(self) -> tmt.steps._RawStepData:
1341+
"""
1342+
Convert to a form suitable for saving in a specification
1343+
file while skipping empty values.
1344+
"""
13391345
spec = {**super().to_minimal_spec()}
13401346

13411347
spec.pop('facts', None)
13421348
spec.pop('-OPTIONLESS-FIELDS', None)
13431349

1350+
# Some fields need special handling.
1351+
# Map them to functions that will correctly convert them.
13441352
field_map: dict[str, Callable[[Any], Any]] = {
13451353
'ansible': lambda ansible: ansible.to_spec() if self.ansible else {},
13461354
'environment': lambda environment: environment.to_fmf_spec(),
13471355
'hardware': lambda hardware: hardware.to_minimal_spec() if hardware else None,
13481356
}
13491357
for key, transform in field_map.items():
1350-
value = getattr(self, key, None)
1358+
value = getattr(self, option_to_key(key), None)
13511359
if value is not None:
13521360
value = transform(value)
1361+
# Do not include empty values
13531362
if value in (None, [], {}):
13541363
spec.pop(key, None)
13551364
else:

tmt/recipe.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
container,
1111
field,
1212
key_to_option,
13+
option_to_key,
1314
)
1415
from tmt.log import Logger
1516
from tmt.result import ResultInterpret
@@ -234,7 +235,7 @@ def to_minimal_spec(self) -> _RawRecipeTest:
234235
}
235236

236237
for key, transform in field_map.items():
237-
value = getattr(self, key, None)
238+
value = getattr(self, option_to_key(key), None)
238239
if value is not None:
239240
value = transform(value)
240241
if value in (None, [], {}):
@@ -471,7 +472,7 @@ def to_minimal_spec(self) -> _RawRecipePlan:
471472
}
472473

473474
for key, transform in field_map.items():
474-
value = getattr(self, key, None)
475+
value = getattr(self, option_to_key(key), None)
475476
if value is not None:
476477
value = transform(value)
477478
if value in (None, [], {}):

tmt/steps/discover/shell.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
import tmt.utils
1515
import tmt.utils.git
1616
from tmt._compat.typing import Self
17-
from tmt.container import SerializableContainer, SpecBasedContainer, container, field
17+
from tmt.container import (
18+
SerializableContainer,
19+
SpecBasedContainer,
20+
container,
21+
field,
22+
option_to_key,
23+
)
1824
from tmt.steps import _RawStepData
1925
from tmt.steps.prepare.distgit import insert_to_prepare_step
2026
from tmt.utils import (
@@ -187,6 +193,8 @@ def to_spec(self) -> dict[str, Any]:
187193
def to_minimal_spec(self) -> dict[str, Any]:
188194
data = {key: value for key, value in self.items() if value not in (None, [], {})}
189195

196+
# Some fields need special handling.
197+
# Map them to functions that will correctly convert them.
190198
field_map: dict[str, Callable[[Any], Any]] = {
191199
'link': lambda link: link.to_spec(),
192200
'require': lambda requires: [require.to_spec() for require in requires],
@@ -196,9 +204,10 @@ def to_minimal_spec(self) -> dict[str, Any]:
196204
}
197205

198206
for key, transform in field_map.items():
199-
value = getattr(self, key, None)
207+
value = getattr(self, option_to_key(key), None)
200208
if value is not None:
201209
value = transform(value)
210+
# Do not include empty values
202211
if value in (None, [], {}):
203212
data.pop(key, None)
204213
else:

tmt/steps/execute/upgrade.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ class ExecuteUpgradeData(ExecuteInternalData):
9595
normalize=tmt.utils.normalize_string_list,
9696
)
9797

98+
# ignore[override] & cast: two base classes define to_spec(), with conflicting
99+
# formal types.
100+
def to_spec(self) -> dict[str, Any]: # type: ignore[override]
101+
return {**super().to_spec(), 'test': [test.to_spec() for test in self.test]}
102+
103+
# ignore[override] & cast: two base classes define to_spec(), with conflicting
104+
# formal types.
105+
def to_minimal_spec(self) -> dict[str, Any]: # type: ignore[override]
106+
spec = super().to_minimal_spec()
107+
spec.pop('test', None)
108+
if self.test:
109+
spec['test'] = [test.to_minimal_spec() for test in self.test]
110+
return spec
111+
98112

99113
@tmt.steps.provides_method('upgrade')
100114
class ExecuteUpgrade(ExecuteInternal):

tmt/steps/provision/connect.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import tmt.steps
66
import tmt.steps.provision
77
import tmt.utils
8-
from tmt.container import container, field
8+
from tmt.container import container, field, option_to_key
99
from tmt.guest import RebootMode
1010
from tmt.utils import Command, ShellScript
1111
from tmt.utils.wait import Waiting
@@ -74,15 +74,17 @@ def to_spec(self) -> tmt.steps._RawStepData:
7474
'systemd-soft-reboot': str(self.systemd_soft_reboot)
7575
if isinstance(self.systemd_soft_reboot, ShellScript)
7676
else None,
77-
'hard-reboot': str(self.systemd_soft_reboot)
78-
if isinstance(self.systemd_soft_reboot, ShellScript)
77+
'hard-reboot': str(self.hard_reboot)
78+
if isinstance(self.hard_reboot, ShellScript)
7979
else None,
8080
},
8181
)
8282

8383
def to_minimal_spec(self) -> tmt.steps._RawStepData:
8484
spec = {**super().to_minimal_spec()}
8585

86+
# Some fields need special handling.
87+
# Map them to functions that will correctly convert them.
8688
field_map: dict[str, Callable[[Any], Any]] = {
8789
'soft-reboot': lambda reboot: str(reboot) if isinstance(reboot, ShellScript) else None,
8890
'systemd-soft-reboot': lambda reboot: (
@@ -92,9 +94,10 @@ def to_minimal_spec(self) -> tmt.steps._RawStepData:
9294
}
9395

9496
for key, transform in field_map.items():
95-
value = getattr(self, key, None)
97+
value = getattr(self, option_to_key(key), None)
9698
if value is not None:
9799
value = transform(value)
100+
# Do not include empty values
98101
if value in (None, [], {}):
99102
spec.pop(key, None)
100103
else:

0 commit comments

Comments
 (0)