-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathexecute_fill.py
More file actions
244 lines (208 loc) · 8.59 KB
/
Copy pathexecute_fill.py
File metadata and controls
244 lines (208 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""Shared pytest fixtures and hooks for EEST generation modes (fill and execute)."""
from enum import StrEnum, unique
from typing import List
import pytest
from ethereum_test_execution import BaseExecute, LabeledExecuteFormat
from ethereum_test_fixtures import BaseFixture, LabeledFixtureFormat
from ethereum_test_specs import BaseTest
from ethereum_test_types import EOA, Alloc, ChainConfig
from ..spec_version_checker.spec_version_checker import EIPSpecTestItem
@unique
class OpMode(StrEnum):
"""Operation mode for the fill and execute."""
CONSENSUS = "consensus"
BENCHMARKING = "benchmarking"
ALL_FIXTURE_PARAMETERS = {
"genesis_environment",
"env",
}
"""
List of test parameters that have a default fixture value which can be retrieved and used
for the test instance if it was not explicitly specified when calling from the test
function.
All parameter names included in this list must define a fixture in one of the plugins.
"""
@pytest.hookimpl(tryfirst=True)
def pytest_configure(config: pytest.Config):
"""
Pytest hook called after command line options have been parsed and before
test collection begins.
Couple of notes:
1. Register the plugin's custom markers and process command-line options.
Custom marker registration:
https://docs.pytest.org/en/7.1.x/how-to/writing_plugins.html#registering-custom-markers
2. `@pytest.hookimpl(tryfirst=True)` is applied to ensure that this hook is
called before the pytest-html plugin's pytest_configure to ensure that
it uses the modified `htmlpath` option.
"""
if config.pluginmanager.has_plugin("pytest_plugins.filler.filler"):
for fixture_format in BaseFixture.formats.values():
config.addinivalue_line(
"markers",
(f"{fixture_format.format_name.lower()}: {fixture_format.description}"),
)
for label, labeled_fixture_format in LabeledFixtureFormat.registered_labels.items():
config.addinivalue_line(
"markers",
(f"{label}: {labeled_fixture_format.description}"),
)
elif config.pluginmanager.has_plugin("pytest_plugins.execute.execute"):
for execute_format in BaseExecute.formats.values():
config.addinivalue_line(
"markers",
(f"{execute_format.format_name.lower()}: {execute_format.description}"),
)
for label, labeled_execute_format in LabeledExecuteFormat.registered_labels.items():
config.addinivalue_line(
"markers",
(f"{label}: {labeled_execute_format.description}"),
)
else:
raise Exception("Neither the filler nor the execute plugin is loaded.")
for spec_type in BaseTest.spec_types.values():
for marker, description in spec_type.supported_markers.items():
config.addinivalue_line(
"markers",
(f"{marker}: {description}"),
)
if not hasattr(config, "op_mode"):
config.op_mode = OpMode.CONSENSUS # type: ignore[attr-defined]
config.addinivalue_line(
"markers",
"yul_test: a test case that compiles Yul code.",
)
config.addinivalue_line(
"markers",
"compile_yul_with(fork): Always compile Yul source using the corresponding evm version.",
)
config.addinivalue_line(
"markers",
"fill: Markers to be added in fill mode only.",
)
config.addinivalue_line(
"markers",
"execute: Markers to be added in execute mode only.",
)
config.addinivalue_line(
"markers",
"benchmark: Tests relevant to benchmarking EVMs.",
)
config.addinivalue_line(
"markers",
"exception_test: Negative tests that include an invalid block or transaction.",
)
config.addinivalue_line(
"markers",
"eip_checklist(item_id, eip=None): Mark a test as implementing a specific checklist item. "
"The first positional parameter is the checklist item ID. "
"The optional 'eip' keyword parameter specifies additional EIPs covered by the test.",
)
config.addinivalue_line(
"markers",
"derived_test: Mark a test as a derived test (E.g. a BlockchainTest that is derived "
"from a StateTest).",
)
config.addinivalue_line(
"markers",
"tagged: Marks a static test as tagged. Tags are used to generate dynamic "
"addresses for static tests at fill time. All tagged tests are compatible with "
"dynamic address generation.",
)
config.addinivalue_line(
"markers",
"untagged: Marks a static test as untagged. Tags are used to generate dynamic "
"addresses for static tests at fill time. Untagged tests are incompatible with "
"dynamic address generation.",
)
config.addinivalue_line(
"markers",
"verify_sync: Marks a test to be run with `consume sync`, verifying blockchain "
"engine tests and having hive clients sync after payload execution.",
)
config.addinivalue_line(
"markers",
"pre_alloc_group: Control shared pre-allocation grouping (use "
'"separate" for isolated group or custom string for named groups)',
)
config.addinivalue_line(
"markers",
"pre_alloc_modify: Marks a test to apply plugin-specific pre_alloc_group modifiers",
)
config.addinivalue_line(
"markers",
"slow: Marks a test as slow (deselect with '-m \"not slow\"')",
)
config.addinivalue_line(
"markers",
"ported_from: Marks a test as ported from ethereum/tests",
)
config.addinivalue_line(
"markers",
"valid_for_bpo_forks: Marks a test as valid for BPO forks",
)
config.addinivalue_line(
"markers",
"mainnet: Specialty tests crafted for running on mainnet and sanity checking.",
)
@pytest.fixture(scope="function")
def test_case_description(request: pytest.FixtureRequest) -> str:
"""Fixture to extract and combine docstrings from the test class and the test function."""
description_unavailable = (
"No description available - add a docstring to the python test class or function."
)
test_class_doc = ""
test_function_doc = ""
if hasattr(request.node, "cls"):
test_class_doc = f"Test class documentation:\n{request.cls.__doc__}" if request.cls else ""
if hasattr(request.node, "function"):
test_function_doc = f"{request.function.__doc__}" if request.function.__doc__ else ""
if not test_class_doc and not test_function_doc:
return description_unavailable
combined_docstring = f"{test_class_doc}\n\n{test_function_doc}".strip()
return combined_docstring
def pytest_make_parametrize_id(config: pytest.Config, val: str, argname: str):
"""
Pytest hook called when generating test ids. We use this to generate
more readable test ids for the generated tests.
"""
return f"{argname}_{val}"
SPEC_TYPES_PARAMETERS: List[str] = list(BaseTest.spec_types.keys())
def pytest_runtest_call(item: pytest.Item):
"""Pytest hook called in the context of test execution."""
if isinstance(item, EIPSpecTestItem):
return
class InvalidFillerError(Exception):
def __init__(self, message):
super().__init__(message)
if not isinstance(item, pytest.Function):
return
if "state_test" in item.fixturenames and "blockchain_test" in item.fixturenames:
raise InvalidFillerError(
"A filler should only implement either a state test or a blockchain test; not both."
)
# Check that the test defines either test type as parameter.
if not any(i for i in item.funcargs if i in SPEC_TYPES_PARAMETERS):
pytest.fail(
"Test must define either one of the following parameters to "
+ "properly generate a test: "
+ ", ".join(SPEC_TYPES_PARAMETERS)
)
# Global `sender` fixture that can be overridden by tests.
@pytest.fixture
def sender(pre: Alloc) -> EOA:
"""Fund an EOA from pre-alloc."""
return pre.fund_eoa()
@pytest.fixture(scope="session")
def chain_config() -> ChainConfig:
"""Return chain configuration."""
return ChainConfig()
def pytest_addoption(parser: pytest.Parser):
"""Add command-line options to pytest."""
static_filler_group = parser.getgroup("static", "Arguments defining static filler behavior")
static_filler_group.addoption(
"--fill-static-tests",
action="store_true",
dest="fill_static_tests_enabled",
default=None,
help=("Enable reading and filling from static test files."),
)