Skip to content

Commit

Permalink
filter items only once
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter committed Jan 1, 2025
1 parent fec320b commit 02638c7
Showing 1 changed file with 52 additions and 66 deletions.
118 changes: 52 additions & 66 deletions cairo/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,21 @@ def pytest_collection_modifyitems(session, config, items):
session.cairo_programs = {}
session.main_paths = {}
session.test_hashes = {}
fspaths = sorted(
list(
{
item.fspath
for item in items
if (
hasattr(item, "fixturenames")
and set(item.fixturenames)
& {
"cairo_file",
"main_path",
"cairo_program",
"cairo_run",
}
)
cairo_items = [
item
for item in items
if (
hasattr(item, "fixturenames")
and set(item.fixturenames)
& {
"cairo_file",
"main_path",
"cairo_program",
"cairo_run",
}
)
)
]
fspaths = sorted(list({item.fspath for item in cairo_items}))

# Distribute compilation using modulo
worker_count = getattr(config, "workerinput", {}).get("workercount", 1)
Expand All @@ -216,63 +213,52 @@ def pytest_collection_modifyitems(session, config, items):

# Wait for all workers to finish
all_paths = []
for item in items:
if hasattr(item, "fixturenames") and set(item.fixturenames) & {
"cairo_file",
"main_path",
"cairo_program",
"cairo_run",
}:
cairo_file = get_cairo_file(item.fspath)
dump_path = session.build_dir / cairo_file.relative_to(
Path().cwd()
).with_suffix(".json")
all_paths.append(dump_path)
for item in cairo_items:
cairo_file = get_cairo_file(item.fspath)
dump_path = session.build_dir / cairo_file.relative_to(
Path().cwd()
).with_suffix(".json")
all_paths.append(dump_path)

while not all([dump_path.exists() for dump_path in all_paths]):
logger.info(
f"Worker {worker_id} with index {worker_index} / {worker_count} waiting for other workers to finish"
)
# 0.25 seconds as observed to be one of the smallest time over the current test files
time.sleep(0.25)

# Select tests
for item in items:
if hasattr(item, "fixturenames") and set(item.fixturenames) & {
"cairo_file",
"main_path",
"cairo_program",
"cairo_run",
}:
if item.fspath not in session.cairo_files:
cairo_file = get_cairo_file(item.fspath)
session.cairo_files[item.fspath] = cairo_file
if item.fspath not in session.main_paths:
main_path = get_main_path(cairo_file)
session.main_paths[item.fspath] = main_path
if item.fspath not in session.cairo_programs:
dump_path = session.build_dir / cairo_file.relative_to(
Path().cwd()
).with_suffix(".json")
cairo_program = get_cairo_program(cairo_file, main_path, dump_path)
session.cairo_programs[item.fspath] = cairo_program

cairo_program = session.cairo_programs[item.fspath]
test_hash = xxhash.xxh64(
program_hash(cairo_program)
+ file_hash(item.fspath)
+ item.nodeid.encode()
+ file_hash(Path(__file__).parent / "fixtures" / "runner.py")
+ file_hash(Path(__file__).parent / "utils" / "serde.py")
+ file_hash(Path(__file__).parent / "utils" / "args_gen.py")
).hexdigest()
session.test_hashes[item.nodeid] = test_hash

if config.getoption("no_skip_mark"):
item.own_markers = [
mark for mark in item.own_markers if mark.name != "skip"
]

if test_hash in tests_to_skip and config.getoption("skip_cached_tests"):
item.add_marker(pytest.mark.skip(reason="Cached results"))
for item in cairo_items:
if item.fspath not in session.cairo_files:
cairo_file = get_cairo_file(item.fspath)
session.cairo_files[item.fspath] = cairo_file
if item.fspath not in session.main_paths:
main_path = get_main_path(cairo_file)
session.main_paths[item.fspath] = main_path
if item.fspath not in session.cairo_programs:
dump_path = session.build_dir / cairo_file.relative_to(
Path().cwd()
).with_suffix(".json")
cairo_program = get_cairo_program(cairo_file, main_path, dump_path)
session.cairo_programs[item.fspath] = cairo_program

cairo_program = session.cairo_programs[item.fspath]
test_hash = xxhash.xxh64(
program_hash(cairo_program)
+ file_hash(item.fspath)
+ item.nodeid.encode()
+ file_hash(Path(__file__).parent / "fixtures" / "runner.py")
+ file_hash(Path(__file__).parent / "utils" / "serde.py")
+ file_hash(Path(__file__).parent / "utils" / "args_gen.py")
).hexdigest()
session.test_hashes[item.nodeid] = test_hash

if config.getoption("no_skip_mark"):
item.own_markers = [
mark for mark in item.own_markers if mark.name != "skip"
]

if test_hash in tests_to_skip and config.getoption("skip_cached_tests"):
item.add_marker(pytest.mark.skip(reason="Cached results"))

yield

0 comments on commit 02638c7

Please sign in to comment.