-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathfill.py
More file actions
306 lines (260 loc) · 10 KB
/
fill.py
File metadata and controls
306 lines (260 loc) · 10 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""CLI entry point for the `fill` pytest-based command."""
from typing import Any, List
import click
import pytest
from .base import PytestCommand, PytestExecution, common_pytest_options
from .processors import (
HelpFlagsProcessor,
StdoutFlagsProcessor,
WatchFlagsProcessor,
)
from .watcher import FileWatcher
class FillCommand(PytestCommand):
"""Pytest command for the fill operation."""
def __init__(self, **kwargs: Any) -> None:
"""Initialize fill command with processors."""
super().__init__(
config_file="pytest-fill.ini",
argument_processors=[
HelpFlagsProcessor("fill"),
StdoutFlagsProcessor(),
WatchFlagsProcessor(),
],
**kwargs,
)
def create_executions(
self, pytest_args: List[str]
) -> List[PytestExecution]:
"""
Create execution plan supporting two-phase pre-allocation group
generation.
Returns:
- Single-phase execution when `--use-pre-alloc-groups` is set,
regardless of `--generate-all-formats` (pre-alloc groups
already exist on disk from a previous run).
- Phase-1-only execution when `--generate-pre-alloc-groups` is
set without `--generate-all-formats` (CI generates pre-alloc
on a dedicated runner without wasting time on phase 2).
- Two-phase execution when `--generate-all-formats` is set.
- Normal single-phase execution otherwise.
"""
processed_args = self.process_arguments(pytest_args)
processed_args = self._add_default_ignores(processed_args)
if "--use-pre-alloc-groups" in processed_args:
# Pre-alloc groups already exist: single-phase fill only.
return self._create_single_phase_with_pre_alloc_groups(
processed_args
)
if self._should_use_two_phase_execution(processed_args):
two_phase = self._create_two_phase_executions(processed_args)
if (
"--generate-pre-alloc-groups" in processed_args
and "--generate-all-formats" not in processed_args
):
# Phase 1 only: generate pre-alloc groups without filling.
return [two_phase[0]]
return two_phase
# Normal single-phase execution
return [
PytestExecution(
config_file=self.config_path,
args=processed_args,
allowed_exit_codes=self.allowed_exit_codes,
)
]
def _create_two_phase_executions(
self, args: List[str]
) -> List[PytestExecution]:
"""
Create two-phase execution: pre-allocation group generation + fixture
filling.
"""
# Phase 1: Pre-allocation group generation (clean and minimal output)
phase1_args = self._create_phase1_args(args)
# Phase 2: Main fixture generation (full user options)
phase2_args = self._create_phase2_args(args)
return [
PytestExecution(
config_file=self.config_path,
args=phase1_args,
description="generating pre-allocation groups",
allowed_exit_codes=[
*self.allowed_exit_codes,
pytest.ExitCode.NO_TESTS_COLLECTED,
],
),
PytestExecution(
config_file=self.config_path,
args=phase2_args,
description="filling test fixtures",
),
]
def _create_single_phase_with_pre_alloc_groups(
self, args: List[str]
) -> List[PytestExecution]:
"""Create single execution using existing pre-allocation groups."""
return [
PytestExecution(
config_file=self.config_path,
args=args,
)
]
def _add_default_ignores(self, args: List[str]) -> List[str]:
"""Add default ignore paths for directories not used by fill."""
# Directories to ignore by default
default_ignores = [
"tests/evm_tools",
"tests/json_loader",
"tests/fixtures",
]
# Add ignore flags for each directory
ignore_args = []
for ignore_path in default_ignores:
ignore_args.extend(["--ignore", ignore_path])
return args + ignore_args
def _create_phase1_args(self, args: List[str]) -> List[str]:
"""Create arguments for phase 1 (pre-allocation group generation)."""
# Start with all args, then remove what we don't want for phase 1
filtered_args = self._remove_unwanted_phase1_args(args)
# Add required phase 1 flags (with quiet output by default)
phase1_args = [
"--generate-pre-alloc-groups",
"-qq", # Quiet pytest output by default (user -v/-vv/-vvv can
# override)
] + filtered_args
return phase1_args
def _create_phase2_args(self, args: List[str]) -> List[str]:
"""Create arguments for phase 2 (fixture filling)."""
# Remove --generate-pre-alloc-groups and --clean, then add --use-pre-
# alloc-groups
phase2_args = self._remove_generate_pre_alloc_groups_flag(args)
phase2_args = self._remove_clean_flag(phase2_args)
phase2_args = self._add_use_pre_alloc_groups_flag(phase2_args)
return phase2_args
def _remove_unwanted_phase1_args(self, args: List[str]) -> List[str]:
"""Remove arguments we don't want in phase 1 (pre-state generation)."""
unwanted_flags = {
# Output format flags
"--html",
# Report flags (we'll add our own -qq)
"-q",
"--quiet",
"-qq",
"--tb",
# Pre-allocation group flags (we'll add our own)
"--generate-pre-alloc-groups",
"--use-pre-alloc-groups",
"--generate-all-formats",
}
filtered_args = []
i = 0
while i < len(args):
arg = args[i]
# Skip unwanted flags
if arg in unwanted_flags:
# Skip flag and its value if it takes one
if arg in ["--html", "--tb", "-n"] and i + 1 < len(args):
i += 2 # Skip flag and value
else:
i += 1 # Skip just the flag
# Skip unwanted flags with = format
elif any(arg.startswith(f"{flag}=") for flag in unwanted_flags):
i += 1
else:
filtered_args.append(arg)
i += 1
return filtered_args
def _remove_generate_pre_alloc_groups_flag(
self, args: List[str]
) -> List[str]:
"""
Remove --generate-pre-alloc-groups flag but keep --generate-all-formats
for phase 2.
"""
return [arg for arg in args if arg != "--generate-pre-alloc-groups"]
def _remove_clean_flag(self, args: List[str]) -> List[str]:
"""Remove --clean flag from argument list."""
return [arg for arg in args if arg != "--clean"]
def _add_use_pre_alloc_groups_flag(self, args: List[str]) -> List[str]:
"""Add --use-pre-alloc-groups flag to argument list."""
return args + ["--use-pre-alloc-groups"]
def _should_use_two_phase_execution(self, args: List[str]) -> bool:
"""Determine if two-phase execution is needed."""
return (
"--generate-pre-alloc-groups" in args
or "--generate-all-formats" in args
)
def _is_watch_mode(self, args: List[str]) -> bool:
"""Check if any watch flag is present in arguments."""
return any(flag in args for flag in ["--watch", "--watcherfall"])
def _is_verbose_watch_mode(self, args: List[str]) -> bool:
"""
Check if verbose watch flag (--watcherfall)
is present in arguments.
"""
return "--watcherfall" in args
def execute(self, pytest_args: List[str]) -> None:
"""Execute the command with optional watch mode support."""
if self._is_watch_mode(pytest_args):
self._execute_with_watch(pytest_args)
else:
super().execute(pytest_args)
def _execute_with_watch(self, pytest_args: List[str]) -> None:
"""Execute fill command in watch mode."""
verbose = self._is_verbose_watch_mode(pytest_args)
watcher = FileWatcher(console=self.runner.console, verbose=verbose)
watcher.run_with_watch(pytest_args)
class PhilCommand(FillCommand):
"""Friendly fill command with emoji reporting."""
def create_executions(
self, pytest_args: List[str]
) -> List[PytestExecution]:
"""Create execution with emoji report options."""
processed_args = self.process_arguments(pytest_args)
emoji_args = processed_args + [
"-o",
"report_passed=🦄",
"-o",
"report_xpassed=🌈",
"-o",
"report_failed=👾",
"-o",
"report_xfailed=🦺",
"-o",
"report_skipped=🦘",
"-o",
"report_error=🚨",
]
return [
PytestExecution(
config_file=self.config_path,
args=emoji_args,
allowed_exit_codes=self.allowed_exit_codes,
)
]
@click.command(
context_settings={
"ignore_unknown_options": True,
}
)
@common_pytest_options
def fill(pytest_args: List[str], **kwargs: Any) -> None:
"""Entry point for the fill command."""
del kwargs
command = FillCommand()
command.execute(list(pytest_args))
@click.command(
context_settings={
"ignore_unknown_options": True,
}
)
@common_pytest_options
def phil(pytest_args: List[str], **kwargs: Any) -> None:
"""Friendly alias for the fill command."""
del kwargs
command = PhilCommand()
command.execute(list(pytest_args))
if __name__ == "__main__":
# to allow debugging in vscode: in launch config, set "module":
# "cli.pytest_commands.fill"
fill(prog_name="fill")