diff --git a/.github/scripts/run_platform_tests.py b/.github/scripts/run_platform_tests.py new file mode 100644 index 0000000..46a7067 --- /dev/null +++ b/.github/scripts/run_platform_tests.py @@ -0,0 +1,93 @@ +"""Run one of exactly two exhaustive, deterministic Windows non-slow shards.""" + +import argparse +import hashlib +import json +from collections import Counter +from pathlib import Path + +import pytest + + +def validate_partition(nodeids, partitions): + """Fail before execution if any collected test is missing, repeated, or unknown.""" + if len(nodeids) != len(set(nodeids)): + raise pytest.UsageError("platform collection contains duplicate node IDs") + if len(partitions) != 2 or any(not part for part in partitions): + raise pytest.UsageError("platform partition requires exactly two nonempty shards") + if Counter(node for part in partitions for node in part) != Counter(nodeids): + raise pytest.UsageError("platform shards must cover every selected test exactly once") + + +def partition_nodeids(nodeids): + # Alternating individual cases, rather than whole directories/files, spreads + # expensive parameterized SQLite tests across both fixed-size CI jobs. + ordered = sorted(nodeids) + partitions = [ordered[0::2], ordered[1::2]] + validate_partition(nodeids, partitions) + return partitions + + +class PlatformPartition: + def __init__(self, shard, manifest=Path("platform-partition.json")): + if shard not in (0, 1): + raise pytest.UsageError("platform shard must be 0 or 1") + self.shard = shard + self.manifest = manifest + + @pytest.hookimpl(hookwrapper=True, tryfirst=True) + def pytest_collection_modifyitems(self, config, items): + # Run after ordinary selection hooks, particularly pytest's -m filter. + # Both jobs collect the entire suite before assigning any non-slow case. + yield + nodeids = [item.nodeid for item in items] + partitions = partition_nodeids(nodeids) + # Keep validation independent of construction so future partition changes + # cannot silently drop/duplicate tests before modifying pytest's items. + validate_partition(nodeids, partitions) + selected = set(partitions[self.shard]) + omitted = [item for item in items if item.nodeid not in selected] + items[:] = [item for item in items if item.nodeid in selected] + config.hook.pytest_deselected(items=omitted) + ordered = sorted(nodeids) + self.manifest.write_text( + json.dumps( + { + "schema_version": 1, + "selection": "not slow", + "shard": self.shard, + "shard_count": 2, + "collection_sha256": hashlib.sha256( + "\n".join(ordered).encode("utf-8") + ).hexdigest(), + "full_collection": ordered, + "partitions": partitions, + "selected": partitions[self.shard], + }, + indent=2, + ensure_ascii=False, + ) + + "\n", + encoding="utf-8", + ) + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--shard", required=True, type=int, choices=(0, 1)) + parser.add_argument("--collect-only", action="store_true") + args = parser.parse_args(argv) + pytest_args = [ + "-m", + "not slow", + "--junitxml=platform-tests.xml", + "-o", + "junit_family=legacy", + ] + if args.collect_only: + pytest_args.append("--collect-only") + return pytest.main(pytest_args, plugins=[PlatformPartition(args.shard)]) + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/.github/workflows/platform.yml b/.github/workflows/platform.yml new file mode 100644 index 0000000..b01fb44 --- /dev/null +++ b/.github/workflows/platform.yml @@ -0,0 +1,64 @@ +name: Platform compatibility + +on: + push: + branches: [main] + pull_request: + branches: [main] + types: [opened, synchronize, reopened, edited] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: platform-${{ github.ref }} + cancel-in-progress: true + +jobs: + platform: + name: Non-slow (${{ matrix.os }}, Python 3.13, ${{ matrix.partition }}) + runs-on: ${{ matrix.os }} + timeout-minutes: 20 + strategy: + fail-fast: false + max-parallel: 2 + matrix: + include: + - os: windows-latest + partition: shard-0 + command: python .github/scripts/run_platform_tests.py --shard 0 + - os: windows-latest + partition: shard-1 + command: python .github/scripts/run_platform_tests.py --shard 1 + - os: macos-latest + partition: all + command: python -m pytest -m "not slow" --junitxml=platform-tests.xml -o junit_family=legacy + env: + # Keep multilingual source/fixture text and subprocess output deterministic. + PYTHONUTF8: "1" + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: "3.13" + cache: pip + cache-dependency-path: pyproject.toml + - name: Install test dependencies and console launcher + run: python -m pip install -e ".[dev]" + - name: Verify executable fixture provenance before collection + run: python -m pytest tests/test_fixture_provenance.py + - name: Run exhaustive non-slow partition + # No coverage denominator or scale matrix here. Resource-warning gates + # remain enabled; required Chromium interaction runs in the Linux job. + run: ${{ matrix.command }} + - name: Upload platform test results + if: always() + uses: actions/upload-artifact@v4 + with: + name: platform-tests-${{ matrix.os }}-py313-${{ matrix.partition }} + path: | + platform-tests.xml + platform-partition.json + if-no-files-found: warn + retention-days: 7 diff --git a/.gitignore b/.gitignore index 9e24bf3..add18f6 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,8 @@ dist/ .coverage .coverage.* coverage.xml +platform-tests.xml +platform-partition.json htmlcov/ .pytest_cache/ .ruff_cache/ diff --git a/README.md b/README.md index 93bc3fe..c9f040c 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,16 @@ For the regular suite use `pytest -m 'not slow'`. Default-capacity scale and non-editable installed exporter tests are described in the [dataset acceptance guide](docs/en/dataset-acceptance.md). +The supplemental platform workflow runs Python 3.13 non-slow tests on macOS and +Windows. Windows uses two deterministic, disjoint shards of the full non-slow +collection; macOS runs it in one job. All three jobs retain a 20-minute limit, +with at most two running concurrently. Each Windows shard validates exhaustive +coverage before execution and uploads its full collection/partition manifest +alongside JUnit results. No tests are skipped to meet the time limit. To reproduce +one Windows partition locally, run +`python .github/scripts/run_platform_tests.py --shard 0` (or `--shard 1`); +add `--collect-only` to inspect the partition without executing tests. + ### Building distributions Build from a fresh checkout or a new worktree at the intended commit, with no diff --git a/README_zh.md b/README_zh.md index ec00160..4c6a38d 100644 --- a/README_zh.md +++ b/README_zh.md @@ -147,6 +147,14 @@ black --check . && ruff check . # 检查格式和 lint 常规套件可用 `pytest -m 'not slow'`。默认容量规模及非 editable 安装包导出测试见 [数据集验收指南](docs/zh/dataset-acceptance.md)。 +补充的平台工作流在 macOS 和 Windows 上运行 Python 3.13 非 slow 测试。 +Windows 将完整的非 slow 集合确定性地划分为两个互不重叠的分片;macOS 使用一个任务。 +三个任务均保持 20 分钟上限,最多同时运行两个。每个 Windows 分片在执行前验证 +划分无遗漏、无重复,并随 JUnit 结果上传完整的收集及分片清单。不会为了满足时间上限 +而跳过测试。本地复现一个 Windows 分片可运行 +`python .github/scripts/run_platform_tests.py --shard 0`(或 `--shard 1`); +增加 `--collect-only` 可仅检查划分,不执行测试。 + ### 构建分发包 请从目标提交的全新 checkout 或新 worktree 构建,确保没有已有的 `build/` 和 diff --git a/benchmarks/dataset_import_baseline.py b/benchmarks/dataset_import_baseline.py index ce61b4f..be9f7df 100644 --- a/benchmarks/dataset_import_baseline.py +++ b/benchmarks/dataset_import_baseline.py @@ -13,7 +13,6 @@ import logging import os import platform -import resource import sqlite3 import subprocess import sys @@ -23,6 +22,11 @@ from contextlib import closing from pathlib import Path +try: + import resource +except ImportError: # Windows has no POSIX getrusage module. + resource = None + MODES = ("single-db", "pt-snap-native-v2", "compatibility-v1") @@ -30,6 +34,11 @@ def rss_kib(raw: float, system: str) -> float: return raw / 1024 if system == "darwin" else raw +def process_peak_rss() -> float | None: + """Return this process's high-water RSS, or explicitly unavailable on Windows.""" + return None if resource is None else resource.getrusage(resource.RUSAGE_SELF).ru_maxrss + + def source_identity(source: Path, expected: str) -> dict: if source.is_symlink() or source.resolve() != source or not source.is_file(): raise ValueError("Source must be an explicit canonical regular file") @@ -158,7 +167,8 @@ def no_rebuild(*_args, **_kwargs): service._msinsight_backend.dump_to_dataset = no_rebuild result = service.import_snapshot(options) wall = time.perf_counter() - start - usage = resource.getrusage(resource.RUSAGE_SELF) # this process ONLY, before analysis + peak_rss = process_peak_rss() # this process ONLY, before analysis + peak_rss_kib = rss_kib(peak_rss, sys.platform) if peak_rss is not None else None if result.reused != (args.phase == "reuse"): raise AssertionError("Cold/reuse measurement did not follow requested cache path") members = [result.db_path] if result.db_path.is_file() else sorted(result.db_path.rglob("*.db")) @@ -225,10 +235,16 @@ def no_rebuild(*_args, **_kwargs): "mode": args.mode, "phase": args.phase, "import_wall_seconds": wall, - "isolated_import_process_peak_rss_raw": usage.ru_maxrss, - "isolated_import_process_peak_rss_kib": rss_kib(usage.ru_maxrss, sys.platform), - "isolated_import_process_peak_rss_mib": rss_kib(usage.ru_maxrss, sys.platform) / 1024, - "rss_scope": "RUSAGE_SELF fresh child high-water through import, before queries; includes interpreter/imports", + "isolated_import_process_peak_rss_raw": peak_rss, + "isolated_import_process_peak_rss_kib": peak_rss_kib, + "isolated_import_process_peak_rss_mib": ( + peak_rss_kib / 1024 if peak_rss_kib is not None else None + ), + "rss_scope": ( + "RUSAGE_SELF fresh child high-water through import, before queries; includes interpreter/imports" + if peak_rss is not None + else "unavailable: this platform does not provide POSIX resource.getrusage" + ), "phase_timings": dict(phases), "phase_timing_scope": "named wrapped functions, inclusive; sharded replay includes SQLite writes/backfills, NOT parser-only", "phase_rss": { diff --git a/src/pt_snap_cli/core/snapshot_import_backend.py b/src/pt_snap_cli/core/snapshot_import_backend.py index b24ea66..2317622 100644 --- a/src/pt_snap_cli/core/snapshot_import_backend.py +++ b/src/pt_snap_cli/core/snapshot_import_backend.py @@ -95,6 +95,11 @@ def close_backup() -> None: raise os.replace(tmp_db_path, db_path) except OSError as exc: + # Windows cannot unlink a file while its mkstemp handle is open. + # Publication has not committed, so release the secure backup + # before removing it; the original destination is still intact. + close_backup() + backup_fd = None if backup_path is not None: try: backup_path.unlink(missing_ok=True) diff --git a/tests/core/test_context_cache.py b/tests/core/test_context_cache.py index 0214dd2..0a7111f 100644 --- a/tests/core/test_context_cache.py +++ b/tests/core/test_context_cache.py @@ -90,6 +90,10 @@ def test_same_mtime_replacement_refreshes_cached_context(self, tmp_path: Path, r conn.commit() conn.close() os.utime(replacement, ns=(original_stat.st_atime_ns, original_stat.st_mtime_ns)) + if os.name == "nt": + # Windows forbids replacing an open SQLite file. Release the handle + # but retain the cache entry so signature invalidation is still tested. + first.close() os.replace(replacement, db_path) second = cache.get(db_path) @@ -121,6 +125,10 @@ def test_replaced_database_redetects_callstack_layout(self, tmp_path: Path, requ conn.execute("CREATE TABLE trace_entry_0 (id INTEGER PRIMARY KEY, callstackId INTEGER)") conn.commit() conn.close() + if os.name == "nt": + # Windows forbids replacing an open SQLite file. Release the handle + # but retain the cache entry so signature invalidation is still tested. + first.close() os.replace(replacement, db_path) second = cache.get(db_path) diff --git a/tests/core/test_dataset_content_cache.py b/tests/core/test_dataset_content_cache.py index ff5e7ad..1e43ad9 100644 --- a/tests/core/test_dataset_content_cache.py +++ b/tests/core/test_dataset_content_cache.py @@ -220,7 +220,10 @@ def test_immutable_v1_entry_cannot_seed_default_reuse(tmp_path, monkeypatch): root = make_dataset(tmp_path / "dataset", devices=(0,), slices=1) monkeypatch.setenv("PT_SNAP_DATASET_CACHE", "immutable") resolver.DatasetResolver().inspect(root) - assert not resolver._VALIDATED[root].content_verified + if os.name == "posix": + assert not resolver._VALIDATED[root].content_verified + else: + assert root not in resolver._VALIDATED monkeypatch.delenv("PT_SNAP_DATASET_CACHE") calls = [] original = resolver.validate_dataset diff --git a/tests/core/test_dataset_id_pagination.py b/tests/core/test_dataset_id_pagination.py index df4756c..56e4254 100644 --- a/tests/core/test_dataset_id_pagination.py +++ b/tests/core/test_dataset_id_pagination.py @@ -269,6 +269,7 @@ def million_event_dataset(tmp_path_factory): return root +@pytest.mark.slow @pytest.mark.parametrize("template", ["event", "allocation"]) @pytest.mark.parametrize("direction", ["ASC", "DESC"]) def test_million_offset_keeps_fetched_work_constant(million_event_dataset, template, direction): diff --git a/tests/core/test_dataset_import_failures.py b/tests/core/test_dataset_import_failures.py index 852c895..0c9fb2a 100644 --- a/tests/core/test_dataset_import_failures.py +++ b/tests/core/test_dataset_import_failures.py @@ -23,10 +23,12 @@ def isolate(tmp_path, monkeypatch): def test_directory_descriptors_closed_on_success_and_failure(tmp_path, monkeypatch, fail): service, options, first, old, focus = saved_state(tmp_path) descriptors = [] + identities = [] original = SplitService._stage_identity def track(path): result = original(path) + identities.append(result) if result[2] is not None: descriptors.append(result[2]) return result @@ -43,7 +45,9 @@ def track(path): assert_preserved(tmp_path, first, old, focus) else: service.import_snapshot(options) - assert descriptors + assert identities # Windows uses path identity rather than directory descriptors. + assert bool(descriptors) == (os.name != "nt") + assert list(first.db_path.parent.iterdir()) == [first.db_path] for fd in descriptors: with pytest.raises(OSError): os.fstat(fd) diff --git a/tests/core/test_dataset_integrity_modes.py b/tests/core/test_dataset_integrity_modes.py index d9d1e75..d92338c 100644 --- a/tests/core/test_dataset_integrity_modes.py +++ b/tests/core/test_dataset_integrity_modes.py @@ -101,13 +101,15 @@ def test_cold_warm_actual_hash_bytes_and_validator_counts(artifact, mode, work): for state in ("cold", "warm"): work.clear() assert resolver.DatasetResolver().inspect(root) is not None - passes = cold_member_passes if state == "cold" else int(mode == "strong") + # Immutable reuse requires POSIX change-time tokens; Windows revalidates. + cold_validation = state == "cold" or (mode == "immutable" and os.name != "posix") + passes = cold_member_passes if cold_validation else int(mode == "strong") for path in members: assert work[("hashes", path)] == passes assert work[("bytes", path)] == passes * path.stat().st_size - manifest_passes = 2 if state == "cold" else int(mode == "strong") + manifest_passes = 2 if cold_validation else int(mode == "strong") assert work[("bytes", manifest)] == manifest_passes * manifest.stat().st_size - assert work["validators"] == int(state == "cold") + assert work["validators"] == int(cold_validation) evidence[state] = { "hash_read_bytes": sum( v for k, v in work.items() if isinstance(k, tuple) and k[0] == "bytes" @@ -147,7 +149,7 @@ def test_hidden_change_tokens_document_immutable_trust_boundary(artifact, mode, # Simulate a filesystem concealing ctime too. This violates immutable mode's # explicit caller contract; that mode must not be described as strong integrity. monkeypatch.setattr(resolver, "_generation", lambda *args: first.generation) - if mode == "strong": + if mode == "strong" or os.name != "posix": with pytest.raises(DatabaseSchemaError): resolver.DatasetResolver().inspect(root) assert root not in resolver._VALIDATED diff --git a/tests/core/test_dataset_lifecycle_scratch.py b/tests/core/test_dataset_lifecycle_scratch.py index 9a523e7..2f41356 100644 --- a/tests/core/test_dataset_lifecycle_scratch.py +++ b/tests/core/test_dataset_lifecycle_scratch.py @@ -82,8 +82,11 @@ def build(database, items, maximum): original(database, items, maximum) path = Path(database.connection.execute("PRAGMA database_list").fetchone()[2]) observed.append(database.connection) - assert os.stat(path.parent).st_mode & 0o777 == 0o700 - assert os.stat(path).st_mode & 0o777 == 0o600 + # Windows exposes read-only flags, not POSIX permission bits. The + # actual route, byte bound and handle/temporary cleanup run everywhere. + if os.name != "nt": + assert os.stat(path.parent).st_mode & 0o777 == 0o700 + assert os.stat(path).st_mode & 0o777 == 0o600 assert path.stat().st_size <= maximum assert database.connection.getlimit(sqlite3.SQLITE_LIMIT_ATTACHED) == limit diff --git a/tests/core/test_dataset_validation_cache.py b/tests/core/test_dataset_validation_cache.py index 094ecc2..b5ef817 100644 --- a/tests/core/test_dataset_validation_cache.py +++ b/tests/core/test_dataset_validation_cache.py @@ -36,15 +36,32 @@ def forbidden(*args, **kwargs): pytest.fail("warm resolution must not hash or validate shard contents") -def test_new_resolvers_reuse_verified_generation_without_hash_or_sql(artifact, monkeypatch): +def test_new_resolvers_respect_platform_generation_reuse_policy(artifact, monkeypatch): first = DatasetResolver().inspect(artifact) - monkeypatch.setattr(dataset_resolver, "_hash_file", forbidden) - monkeypatch.setattr(native_dataset_contract, "_hash_file", forbidden) - monkeypatch.setattr(dataset_resolver, "validate_dataset", forbidden) - monkeypatch.setattr(dataset_resolver, "validate_native_dataset", forbidden) - monkeypatch.setattr(sqlite3, "connect", forbidden) + calls = [] + for module, name in ( + (dataset_resolver, "_hash_file"), + (native_dataset_contract, "_hash_file"), + (dataset_resolver, "validate_dataset"), + (dataset_resolver, "validate_native_dataset"), + (sqlite3, "connect"), + ): + original = getattr(module, name) + + def tracked(*args, _original=original, _name=name, **kwargs): + calls.append(_name) + return _original(*args, **kwargs) + + monkeypatch.setattr(module, name, tracked) second = DatasetResolver().inspect(artifact / "manifest.json") assert second == first and second is not first + if os.name == "posix": + assert calls == [] + else: + # Windows creation time cannot authorize metadata-only immutable reuse. + assert "_hash_file" in calls and "connect" in calls + assert "validate_dataset" in calls or "validate_native_dataset" in calls + assert artifact not in dataset_resolver._VALIDATED second.require_unchanged() @@ -88,7 +105,10 @@ def test_restored_mtime_size_does_not_hide_content_tampering(artifact): after.st_size, after.st_mtime_ns, ) - assert before.st_ctime_ns != after.st_ctime_ns + if os.name == "posix": + assert before.st_ctime_ns != after.st_ctime_ns + else: + assert artifact not in dataset_resolver._VALIDATED with pytest.raises(DatabaseSchemaError): DatasetResolver().inspect(artifact) @@ -134,11 +154,14 @@ def test_cached_manifest_cannot_be_mutated_by_a_previous_caller(tmp_path): def test_cache_is_bounded_and_untrusted_change_tokens_disable_reuse(tmp_path, monkeypatch): + # Default content-verified caching is available on every platform. + monkeypatch.delenv("PT_SNAP_DATASET_CACHE") monkeypatch.setattr(dataset_resolver, "_CACHE_LIMIT", 2) for i in range(3): DatasetResolver().inspect(make_dataset(tmp_path / str(i), devices=(0,), slices=1)) assert len(dataset_resolver._VALIDATED) == 2 root = make_dataset(tmp_path / "uncached", devices=(0,), slices=1) + monkeypatch.setenv("PT_SNAP_DATASET_CACHE", "immutable") monkeypatch.setattr(dataset_resolver, "_cacheable", lambda generation: False) DatasetResolver().inspect(root) assert root not in dataset_resolver._VALIDATED @@ -381,6 +404,7 @@ def test_strong_operation_guard_hashes_even_when_filesystem_tokens_look_unchange def test_disabling_cache_does_not_reuse_an_existing_entry(tmp_path, monkeypatch): + monkeypatch.delenv("PT_SNAP_DATASET_CACHE") root = make_dataset(tmp_path / "dataset", devices=(0,), slices=1) DatasetResolver().inspect(root) assert root in dataset_resolver._VALIDATED @@ -404,3 +428,24 @@ def fail(*args, **kwargs): monkeypatch.setattr(dataset_resolver, "_hash_file", fail) with pytest.raises(DatabaseSchemaError, match="member became unreadable"): resolved.require_unchanged() + + +def test_immutable_request_never_trusts_windows_creation_time(artifact, monkeypatch): + from types import SimpleNamespace + + # Exercise the real conservative policy even when this regression runs on Linux. + monkeypatch.setattr(dataset_resolver, "os", SimpleNamespace(name="nt", environ=os.environ)) + first = DatasetResolver().inspect(artifact) + assert not dataset_resolver._cacheable(first.generation) + assert artifact not in dataset_resolver._VALIDATED + assert DatasetResolver().inspect(artifact) == first + assert artifact not in dataset_resolver._VALIDATED + member = next(artifact.glob("*/*.db")) + device = member.parent.name.removeprefix("device_") + with closing(sqlite3.connect(member)) as conn, conn: + conn.execute(f"UPDATE trace_entry_{device} SET action=100 WHERE id>=0") + monkeypatch.setattr(dataset_resolver, "_generation", lambda *args: first.generation) + with pytest.raises(DatabaseSchemaError): + DatasetResolver().inspect(artifact) + with pytest.raises(DatabaseSchemaError, match="content changed"): + first.require_unchanged() diff --git a/tests/core/test_focus_service.py b/tests/core/test_focus_service.py index 023db70..2e1ba6b 100644 --- a/tests/core/test_focus_service.py +++ b/tests/core/test_focus_service.py @@ -50,12 +50,12 @@ def test_get_focus_with_missing_db_keeps_state(self, tmp_path: Path) -> None: focus_dir = tmp_path / ".pt-snap" focus_dir.mkdir() (focus_dir / "focus.json").write_text( - json.dumps({"db_path": "/missing.db", "device_id": 0}) + json.dumps({"db_path": str(tmp_path / "missing.db"), "device_id": 0}) ) state = FocusService().get_focus() - assert state.db_path == Path("/missing.db") + assert state.db_path == tmp_path / "missing.db" assert state.device_id == 0 assert state.available_devices == [] @@ -84,7 +84,7 @@ def test_set_device_without_focus_fails(self) -> None: def test_set_device_with_missing_db_fails(self, tmp_path: Path) -> None: focus_dir = tmp_path / ".pt-snap" focus_dir.mkdir() - (focus_dir / "focus.json").write_text(json.dumps({"db_path": "/missing.db"})) + (focus_dir / "focus.json").write_text(json.dumps({"db_path": str(tmp_path / "missing.db")})) with pytest.raises(DatabaseMissingError): FocusService().set_device(0) diff --git a/tests/core/test_import_backend_failures.py b/tests/core/test_import_backend_failures.py index e41cdbd..3d12044 100644 --- a/tests/core/test_import_backend_failures.py +++ b/tests/core/test_import_backend_failures.py @@ -92,6 +92,34 @@ def fail_focus(path): assert list(output.iterdir()) == [destination] +def test_failed_publication_closes_backup_before_unlink(staged_backend, monkeypatch): + """Enforce Windows open-file deletion semantics on every test platform.""" + backend, source, output, destination, descriptors = staged_backend + original = destination.read_bytes() + real_unlink = Path.unlink + removed = [] + + def require_closed(path, *args, **kwargs): + if path.suffix == ".rollback": + assert descriptors + for fd in descriptors: + with pytest.raises(OSError): + os.fstat(fd) + removed.append(path) + return real_unlink(path, *args, **kwargs) + + def fail_publish(*args, **kwargs): + raise OSError("publication failed") + + monkeypatch.setattr(Path, "unlink", require_closed) + monkeypatch.setattr("pt_snap_cli.core.snapshot_import_backend.os.replace", fail_publish) + with pytest.raises(ImportExecutionError, match="publication failed"): + backend.dump_to_db(source, output, post_publish=lambda path: None) + assert len(removed) == 1 + assert destination.read_bytes() == original + assert list(output.iterdir()) == [destination] + + def test_zero_backup_write_fails_without_publishing(staged_backend, monkeypatch): backend, source, output, destination, _ = staged_backend monkeypatch.setattr("pt_snap_cli.core.snapshot_import_backend.os.write", lambda *args: 0) diff --git a/tests/core/test_import_service.py b/tests/core/test_import_service.py index fa5ea72..80bd173 100644 --- a/tests/core/test_import_service.py +++ b/tests/core/test_import_service.py @@ -254,10 +254,14 @@ def test_import_output_path_uses_full_filename(tmp_path: Path) -> None: assert result.db_path.name == "snapshot.pickle.db" -@pytest.mark.parametrize("character", ["?", "#"]) +@pytest.mark.parametrize("character", ["?", "#", "%", " "]) def test_import_supports_sqlite_uri_characters_in_filename(character: str, tmp_path: Path) -> None: import_service_cls = _import_service_type() service = import_service_cls() + # A literal ? is not a Windows filename. Its legal percent-encoded spelling + # still tests that SQLite URI handling does not decode a path twice. + if character == "?" and sys.platform == "win32": + character = "%3F" snapshot_file = tmp_path / f"snapshot{character}.pickle" snapshot_file.write_bytes(EMPTY_CACHE_SNAPSHOT.read_bytes()) @@ -656,17 +660,28 @@ def test_rollback_backup_writes_through_secure_mkstemp_fd( victim.write_text("safe") real_mkstemp = tempfile.mkstemp + attempts = [] def racing_mkstemp(*args, **kwargs): fd, name = real_mkstemp(*args, **kwargs) - # Simulate an attacker that swaps the secure temp file for a symlink - # pointing at a victim file before the backend starts writing. + if kwargs.get("suffix") != ".rollback": + return fd, name + # Target the rollback backup, not unrelated snapshot staging files. + # Windows denies unlinking its open fd, preventing this race outright. try: - os.unlink(name) - except FileNotFoundError: - pass - os.symlink(victim, name) - return fd, name + try: + os.unlink(name) + except PermissionError: + if os.name != "nt": + raise + attempts.append("blocked-open-file") + else: + os.symlink(victim, name) + attempts.append("redirected-path") + return fd, name + except BaseException: + os.close(fd) # The failing fixture never transferred ownership. + raise monkeypatch.setattr(backend_module.tempfile, "mkstemp", racing_mkstemp) @@ -688,6 +703,7 @@ def fail_focus_write(*args, **kwargs): ) ) + assert attempts == ["blocked-open-file" if os.name == "nt" else "redirected-path"] # Victim must remain untouched; the secure fd prevented a symlink redirect. assert victim.read_text() == "safe" # The destination database must be restored from the safe rollback copy. diff --git a/tests/core/test_split_service.py b/tests/core/test_split_service.py index 7206888..34bc114 100644 --- a/tests/core/test_split_service.py +++ b/tests/core/test_split_service.py @@ -682,16 +682,19 @@ def test_publication_symlink_race_is_not_replaced( if not broken: target.mkdir() real_publish = SplitService._publish_directory + original_targets = [] def race(stage: Path, destination: Path) -> None: destination.symlink_to(target, target_is_directory=True) + original_targets.append(destination.readlink()) real_publish(stage, destination) monkeypatch.setattr(SplitService, "_publish_directory", staticmethod(race)) with pytest.raises(SplitError, match="publication"): SplitService().split(_options(tmp_path, device=0)) assert (tmp_path / "split").is_symlink() - assert (tmp_path / "split").readlink() == target + # Windows reports an extended-length target; compare before/after exactly. + assert [(tmp_path / "split").readlink()] == original_targets assert not list(tmp_path.glob(".split.pt-snap-*")) diff --git a/tests/skills/test_setup_contract.py b/tests/skills/test_setup_contract.py index f74b033..162552f 100644 --- a/tests/skills/test_setup_contract.py +++ b/tests/skills/test_setup_contract.py @@ -95,10 +95,28 @@ def test_setup_ownership_distinguishes_environments_sharing_an_interpreter( evidence, status = _run_ownership_probe(selected, cli) identities = json.loads(evidence) - assert identities["selected"]["executable"] == str(selected) - assert identities["cli"]["executable"] == str(cli) - if selected_name != cli_name: - assert identities["selected"]["prefix"] != identities["cli"]["prefix"] + # CPython on macOS may canonicalize a directory alias in sys.executable + # and sys.prefix. Check actual interpreter evidence, not the launcher spelling; + # importantly, execute the original paths rather than their shared realpath. + child_env = os.environ.copy() + child_env.pop("__PYVENV_LAUNCHER__", None) + for role, interpreter in (("selected", selected), ("cli", cli)): + actual = subprocess.run( + [ + interpreter, + "-c", + "import json, sys; print(json.dumps(dict(executable=sys.executable, prefix=sys.prefix)))", + ], + check=True, + capture_output=True, + env=child_env, + text=True, + timeout=20, + ) + assert identities[role] == json.loads(actual.stdout) + selected_prefix = Path(identities["selected"]["prefix"]).resolve() + cli_prefix = Path(identities["cli"]["prefix"]).resolve() + assert (selected_prefix == cli_prefix) is matches assert status == ( "matches selected Python" if matches else "CLI belongs to another Python environment" ) diff --git a/tests/test_cli.py b/tests/test_cli.py index 4cd95d1..684b06d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,6 +1,7 @@ """Tests for CLI.""" import json +import shlex import shutil import sqlite3 from pathlib import Path @@ -668,7 +669,10 @@ def test_focus_database_session(self, sample_db: Path) -> None: """Test 'focus --session' prints export without writing focus files.""" result = runner.invoke(app, ["focus", str(sample_db), "--session"]) assert result.exit_code == 0 - assert result.stdout.strip() == f"export PT_SNAP_DB_PATH={sample_db.resolve()}" + assert shlex.split(result.stdout.strip()) == [ + "export", + f"PT_SNAP_DB_PATH={sample_db.resolve()}", + ] assert not (Path.cwd() / ".pt-snap" / "focus.json").exists() def test_focus_database_session_and_global_conflict(self, sample_db: Path) -> None: diff --git a/tests/test_config.py b/tests/test_config.py index 9b2c34e..857d9a3 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -451,10 +451,12 @@ def test_atomic_project_focus_write_preserves_existing_mode(self, tmp_path: Path config = Config() focus_file = config.write_project_focus(tmp_path / "first.db", base_dir=tmp_path) focus_file.chmod(0o640) + # Windows chmod preserves only the read-only flag, not POSIX group bits. + expected_mode = focus_file.stat().st_mode & 0o777 config.write_project_focus(tmp_path / "second.db", base_dir=tmp_path) - assert focus_file.stat().st_mode & 0o777 == 0o640 + assert focus_file.stat().st_mode & 0o777 == expected_mode def test_atomic_write_new_file_uses_private_mode(self, monkeypatch, tmp_path: Path) -> None: config = Config() @@ -467,4 +469,12 @@ def test_atomic_write_new_file_uses_private_mode(self, monkeypatch, tmp_path: Pa config.write_project_focus(tmp_path / "second.db", base_dir=tmp_path) - assert config_path.stat().st_mode & 0o777 == 0o600 + if os.name == "nt": + # chmod(0o600) is a writable file on Windows; ACL privacy is not a + # POSIX mode-bit contract. Keep the atomic-write assertion active. + assert config_path.stat().st_mode & 0o200 + else: + assert config_path.stat().st_mode & 0o777 == 0o600 + assert json.loads(config_path.read_text())[DB_PATH_KEY] == str( + (tmp_path / "second.db").resolve() + ) diff --git a/tests/test_dataset_focus.py b/tests/test_dataset_focus.py index 2c42d67..931843f 100644 --- a/tests/test_dataset_focus.py +++ b/tests/test_dataset_focus.py @@ -372,6 +372,7 @@ def test_live_sidecar_rejected_before_any_sqlite_open( if dangling: sidecar.symlink_to(tmp_path / "missing-sidecar-target") assert sidecar.is_symlink() and not sidecar.exists() + original_target = sidecar.readlink() else: sidecar.touch() before = hashes(root) @@ -388,7 +389,8 @@ def forbidden(*args, **kwargs): assert before == hashes(root) assert inventory == sorted(str(p.relative_to(root)) for p in root.rglob("*")) if dangling: - assert sidecar.readlink() == tmp_path / "missing-sidecar-target" + # Preserve the OS-reported target spelling, including Windows \?\ paths. + assert sidecar.readlink() == original_target @pytest.mark.parametrize("manifest_path", [False, True]) diff --git a/tests/test_dataset_import_baseline.py b/tests/test_dataset_import_baseline.py index b3d3533..2df08af 100644 --- a/tests/test_dataset_import_baseline.py +++ b/tests/test_dataset_import_baseline.py @@ -171,3 +171,26 @@ def test_recorded_performance_report_is_structural_not_a_speed_gate(): assert record["individual_shard_bytes"] and record["total_disk_bytes"] > 0 assert all(q["status"] == "passed" for q in record["queries"].values()) # Deliberately no timing/RSS/speedup threshold: these are observations. + + +def test_peak_rss_is_explicitly_unavailable_without_posix_resource(monkeypatch): + from benchmarks import dataset_import_baseline as baseline + + monkeypatch.setattr(baseline, "resource", None) + assert baseline.process_peak_rss() is None + + +def test_peak_rss_uses_current_process_only(monkeypatch): + from types import SimpleNamespace + + from benchmarks import dataset_import_baseline as baseline + + calls = [] + + def getrusage(who): + calls.append(who) + return SimpleNamespace(ru_maxrss=1234) + + monkeypatch.setattr(baseline, "resource", SimpleNamespace(RUSAGE_SELF=0, getrusage=getrusage)) + assert baseline.process_peak_rss() == 1234 + assert calls == [0] diff --git a/tests/test_platform_contracts.py b/tests/test_platform_contracts.py new file mode 100644 index 0000000..53100e7 --- /dev/null +++ b/tests/test_platform_contracts.py @@ -0,0 +1,224 @@ +"""Exercise actual OS paths, SQLite handles and launchers on every CI platform. + +These are deliberately ordinary non-slow tests. They do not simulate Windows +using POSIX paths and do not treat a Linux pass as macOS/Windows evidence. +""" + +import dataclasses +import json +import os +import sqlite3 +import stat +import subprocess +import sys +import sysconfig +import tempfile +from contextlib import closing +from pathlib import Path + +import pytest + +from pt_snap_cli.api import SnapshotAnalyzer +from pt_snap_cli.context import Context +from pt_snap_cli.core import dataset_lifecycle_scratch as scratch +from pt_snap_cli.core import dataset_lifecycle_sql +from pt_snap_cli.core.errors import QueryExecutionError +from pt_snap_cli.core.import_service import ImportService +from pt_snap_cli.core.query_limits import QueryLimits +from tests.core.test_dataset_import import hashes, source_options + +ROOT = Path(__file__).resolve().parents[1] +SPECIAL_NAME = "平台 #100% + spaces" + + +@pytest.fixture(autouse=True) +def isolate(tmp_path, monkeypatch): + home = tmp_path / "home" + home.mkdir() + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("HOME", str(home)) + monkeypatch.setenv("USERPROFILE", str(home)) + monkeypatch.setattr(Path, "home", lambda: home) + for name in ("PT_SNAP_DB_PATH", "PT_SNAP_QUERY_TIMEOUT", "PT_SNAP_SKILLS_DIR"): + monkeypatch.delenv(name, raising=False) + + +@pytest.fixture +def readonly_dataset(tmp_path): + parent = tmp_path / SPECIAL_NAME + parent.mkdir() + options = dataclasses.replace(source_options(parent), set_focus=False) + root = ImportService().import_snapshot(options).db_path + originals = {path: path.stat().st_mode for path in root.rglob("*") if path.is_file()} + try: + for path in originals: + path.chmod(stat.S_IREAD) + assert not path.stat().st_mode & stat.S_IWUSR + yield root + finally: + # Windows cannot unlink a read-only file; restore only our fixture files. + for path, mode in originals.items(): + if path.exists(): + path.chmod(mode) + + +@pytest.mark.parametrize("relative", [False, True], ids=["absolute", "relative"]) +@pytest.mark.parametrize("persistent", [False, True], ids=["scoped", "persistent"]) +def test_context_special_paths_use_readonly_uri_and_release_handles( + tmp_path, monkeypatch, relative, persistent +): + directory = tmp_path / SPECIAL_NAME + directory.mkdir() + path = directory / "snapshot #1%25.db" + with closing(sqlite3.connect(path)) as conn: + conn.executescript( + "CREATE TABLE dictionary (id INTEGER);" + "CREATE TABLE trace_entry_0 (id INTEGER PRIMARY KEY, callstack TEXT);" + "INSERT INTO trace_entry_0 VALUES (7, 'selected database');" + ) + before = path.read_bytes() + opened = [] + connect = sqlite3.connect + + def record(uri, /, *args, **kwargs): + assert isinstance(uri, str) and uri.startswith("file:") + assert uri.endswith("?mode=ro") and "immutable" not in uri + assert "%23" in uri and "%25" in uri and kwargs["uri"] is True + connection = connect(uri, *args, **kwargs) + opened.append(connection) + return connection + + monkeypatch.setattr(sqlite3, "connect", record) + requested = path.relative_to(tmp_path) if relative else path + with closing(Context(requested, persistent=persistent)) as context: + with context.connect() as conn: + assert tuple(conn.execute("SELECT * FROM trace_entry_0").fetchone()) == ( + 7, + "selected database", + ) + with pytest.raises(sqlite3.OperationalError, match="readonly"): + conn.execute("DELETE FROM trace_entry_0") + assert opened + for connection in opened: + with pytest.raises(sqlite3.ProgrammingError, match="closed"): + connection.execute("SELECT 1") + assert path.read_bytes() == before + assert list(directory.iterdir()) == [path] + renamed = path.with_name("released.db") + path.rename(renamed) + renamed.unlink() # Fails on Windows if an owned SQLite handle escaped. + assert not list(directory.iterdir()) + + +@pytest.mark.parametrize("manifest_path", [False, True], ids=["directory", "manifest"]) +def test_readonly_native_members_and_special_paths_remain_unchanged( + readonly_dataset, manifest_path +): + root = readonly_dataset + before = hashes(root) + target = root / "manifest.json" if manifest_path else root + with SnapshotAnalyzer(target) as analyzer: + assert analyzer.get_database_overview()["dataset"]["real_event_count"] == 9 + assert analyzer.execute_query("event", {"id": 7})["rows"][0]["id"] == 7 + assert analyzer.execute_query("memory_peak")["rows"] + assert analyzer.execute_query("block")["rows"] + assert hashes(root) == before + assert not any(root.rglob("*-wal")) + assert not any(root.rglob("*-shm")) + assert not any(root.rglob("*-journal")) + # Renaming away and back checks directory/member handle release on Windows + # without discarding the fixture's original permission-restoration paths. + renamed = root.with_name("released dataset") + root.rename(renamed) + renamed.rename(root) + + +@pytest.mark.parametrize("fail", [False, True], ids=["success", "scratch-exhaustion"]) +def test_scratch_uses_os_temp_directory_and_closes_before_cleanup( + tmp_path, monkeypatch, readonly_dataset, fail +): + root = readonly_dataset + before = hashes(root) + temp_root = tmp_path / ("temporary " + SPECIAL_NAME) + temp_root.mkdir() + for name in ("TMPDIR", "TEMP", "TMP"): + monkeypatch.setenv(name, str(temp_root)) + monkeypatch.setattr(tempfile, "tempdir", None) + assert Path(tempfile.gettempdir()) == temp_root + created, connections = [], [] + original_build = scratch._ScratchSources.build + + def observed_build(database, items, maximum): + connections.append(database.connection) + path = Path(database.connection.execute("PRAGMA database_list").fetchone()[2]) + assert path.parent.parent == temp_root + assert path.is_file() + created.append(path) + return original_build(database, items, maximum) + + monkeypatch.setattr(scratch._ScratchSources, "build", observed_build) + monkeypatch.setattr(dataset_lifecycle_sql, "_ATTACHED_OBSERVATION_LIMIT", 0) + limits = QueryLimits(max_scratch_db_bytes=4096 if fail else 512 * 1024**2) + with SnapshotAnalyzer(root, query_limits=limits) as analyzer: + if fail: + with pytest.raises(QueryExecutionError, match="scratch space exhausted"): + analyzer.execute_query("block") + else: + rows = analyzer.execute_query("block")["rows"] + assert rows + # Compare with the attached route using the same finalized members. + monkeypatch.setattr(dataset_lifecycle_sql, "_ATTACHED_OBSERVATION_LIMIT", 4096) + assert analyzer.execute_query("block")["rows"] == rows + assert created + for connection in connections: + with pytest.raises(sqlite3.ProgrammingError, match="closed"): + connection.execute("SELECT 1") + assert not list(temp_root.iterdir()) + assert all(not path.exists() and not path.parent.exists() for path in created) + assert hashes(root) == before + temp_root.rmdir() + + +def test_installed_console_launcher_queries_special_paths_and_reports_json_errors( + tmp_path, readonly_dataset +): + # Exercise the real generated console launcher, including Windows .exe, not + # CliRunner or python -m. Clean-wheel acceptance separately checks packaging. + launcher = Path(sysconfig.get_path("scripts")) / ( + "pt-snap.exe" if os.name == "nt" else "pt-snap" + ) + assert ( + launcher.is_file() + ), f"Install the package in {sys.executable} before testing its launcher" + env = os.environ.copy() + env.pop("__PYVENV_LAUNCHER__", None) + env["PYTHONPATH"] = str(ROOT / "src") # Select this worktree in shared editable dev installs. + env["PYTHONUTF8"] = "1" + before = hashes(readonly_dataset) + + def run(*arguments): + return subprocess.run( + [str(launcher), *arguments], + cwd=tmp_path, + env=env, + capture_output=True, + text=True, + encoding="utf-8", + timeout=30, + check=False, + ) + + version = run("--version") + assert version.returncode == 0, version.stderr + assert "pt-snap" in version.stdout.lower() + result = run("query", str(readonly_dataset), "--template-use", "memory_peak", "--json") + assert result.returncode == 0 and result.stderr == "", result.stdout + result.stderr + payload = json.loads(result.stdout) + assert payload["ok"] is True and payload["rows"] + missing = run("overview", str(tmp_path / "missing #100% 数据.db"), "--json") + assert missing.returncode == 1 and missing.stdout == "", missing.stdout + missing.stderr + error = json.loads(missing.stderr) + assert error["ok"] is False and error["error"]["code"] == "DATABASE_NOT_FOUND" + assert hashes(readonly_dataset) == before + assert not (tmp_path / ".pt-snap").exists() + assert not any(readonly_dataset.rglob("*-journal")) diff --git a/tests/test_platform_workflow.py b/tests/test_platform_workflow.py new file mode 100644 index 0000000..d4900d3 --- /dev/null +++ b/tests/test_platform_workflow.py @@ -0,0 +1,187 @@ +"""Keep supplemental platform CI bounded without hiding compatibility failures.""" + +import importlib.util +import json +import subprocess +import sys +from pathlib import Path + +import pytest +import yaml + +ROOT = Path(__file__).resolve().parents[1] +RUNNER = ROOT / ".github/scripts/run_platform_tests.py" + + +def runner(): + spec = importlib.util.spec_from_file_location("platform_test_runner", RUNNER) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def workflow(): + return yaml.load( + (ROOT / ".github/workflows/platform.yml").read_text(encoding="utf-8"), + Loader=yaml.BaseLoader, + ) + + +def test_platform_ci_runs_exactly_three_bounded_non_linux_python_313_jobs(): + data = workflow() + assert set(data["on"]) == {"push", "pull_request", "workflow_dispatch"} + assert data["on"]["push"]["branches"] == ["main"] + assert data["on"]["pull_request"]["branches"] == ["main"] + assert set(data["on"]["pull_request"]["types"]) == { + "opened", + "synchronize", + "reopened", + "edited", + } + assert data["permissions"] == {"contents": "read"} + assert data["concurrency"]["cancel-in-progress"] == "true" + assert set(data["jobs"]) == {"platform"} + job = data["jobs"]["platform"] + assert job["runs-on"] == "${{ matrix.os }}" + assert job["strategy"] == { + "fail-fast": "false", + "max-parallel": "2", + "matrix": { + "include": [ + { + "os": "windows-latest", + "partition": "shard-0", + "command": "python .github/scripts/run_platform_tests.py --shard 0", + }, + { + "os": "windows-latest", + "partition": "shard-1", + "command": "python .github/scripts/run_platform_tests.py --shard 1", + }, + { + "os": "macos-latest", + "partition": "all", + "command": 'python -m pytest -m "not slow" --junitxml=platform-tests.xml -o junit_family=legacy', + }, + ] + }, + } + assert 1 <= int(job["timeout-minutes"]) <= 20 + setup = next(step for step in job["steps"] if step.get("uses") == "actions/setup-python@v5") + assert setup["with"]["python-version"] == "3.13" + + +def test_platform_ci_preserves_full_non_slow_collection_and_failure_evidence(monkeypatch): + job = workflow()["jobs"]["platform"] + steps = job["steps"] + commands = [step["run"] for step in steps if "run" in step] + assert commands == [ + 'python -m pip install -e ".[dev]"', + "python -m pytest tests/test_fixture_provenance.py", + "${{ matrix.command }}", + ] + assert "continue-on-error" not in job + assert all("continue-on-error" not in step for step in steps) + assert all("PYTHONWARNINGS" not in env for env in (workflow().get("env", {}), job["env"])) + upload = next(step for step in steps if step.get("uses") == "actions/upload-artifact@v4") + assert upload["if"] == "always()" + assert upload["with"]["path"].splitlines() == ["platform-tests.xml", "platform-partition.json"] + assert "${{ matrix.os }}" in upload["with"]["name"] + assert "${{ matrix.partition }}" in upload["with"]["name"] + module = runner() + calls = [] + monkeypatch.setattr(module.pytest, "main", lambda *args, **kwargs: calls.append((args, kwargs))) + module.main(["--shard", "0"]) + assert calls[0][0] == ( + ["-m", "not slow", "--junitxml=platform-tests.xml", "-o", "junit_family=legacy"], + ) + assert calls[0][1]["plugins"][0].shard == 0 + + +def test_partition_is_deterministic_disjoint_exhaustive_and_balanced(): + module = runner() + nodeids = [f"tests/test_sample.py::test_value[{n}]" for n in range(101)] + parts = module.partition_nodeids(nodeids) + assert parts == module.partition_nodeids(list(reversed(nodeids))) + assert len(parts[0]) == 51 and len(parts[1]) == 50 + assert not set(parts[0]) & set(parts[1]) + assert set(parts[0]) | set(parts[1]) == set(nodeids) + + +@pytest.mark.parametrize( + "nodeids,parts", + [ + (["a", "b"], [["a"], []]), + (["a", "b", "c"], [["a"], ["b"]]), + (["a", "b"], [["a", "b"], ["a"]]), + (["a", "b"], [["a"], ["unknown"]]), + (["a", "b"], [["a", "b"]]), + (["a", "a"], [["a"], ["a"]]), + ], +) +def test_partition_rejects_missing_duplicate_unknown_or_empty_coverage(nodeids, parts): + with pytest.raises(pytest.UsageError): + runner().validate_partition(nodeids, parts) + + +@pytest.mark.parametrize("shard", [-1, 2]) +def test_partition_rejects_unknown_shard(shard): + with pytest.raises(pytest.UsageError): + runner().PlatformPartition(shard) + + +def run_toy_suite(tmp_path, shard, *, corrupt=False): + # Exercise real pytest marker filtering and execution, not only the helper. + (tmp_path / "pytest.ini").write_text( + "[pytest]\nmarkers = slow: large fixture\n", encoding="utf-8" + ) + (tmp_path / "test_toy.py").write_text( + "import pytest\n" + "@pytest.mark.parametrize('value', range(7))\n" + "def test_regular(value):\n assert value >= 0\n" + "@pytest.mark.slow\ndef test_slow():\n assert False\n", + encoding="utf-8", + ) + code = ( + "import importlib.util, pytest\n" + f"spec = importlib.util.spec_from_file_location('runner', {str(RUNNER)!r})\n" + "module = importlib.util.module_from_spec(spec)\nspec.loader.exec_module(module)\n" + ) + if corrupt: + code += "module.partition_nodeids = lambda ids: [[ids[0]], [ids[1]]]\n" + code += ( + f"raise SystemExit(pytest.main(['-m', 'not slow', '-v', '--junitxml=toy.xml'], " + f"plugins=[module.PlatformPartition({shard})]))\n" + ) + return subprocess.run( + [sys.executable, "-c", code], cwd=tmp_path, text=True, capture_output=True, check=False + ) + + +def test_real_pytest_shards_cover_exact_non_slow_collection(tmp_path): + manifests = [] + for shard in (0, 1): + result = run_toy_suite(tmp_path, shard) + assert result.returncode == 0, result.stdout + result.stderr + manifest = json.loads((tmp_path / "platform-partition.json").read_text(encoding="utf-8")) + assert manifest["shard"] == shard + assert manifest["selection"] == "not slow" + assert len(manifest["full_collection"]) == 7 + assert all("test_slow" not in node for node in manifest["full_collection"]) + assert f"{len(manifest['selected'])} passed" in result.stdout + manifests.append(manifest) + assert manifests[0]["collection_sha256"] == manifests[1]["collection_sha256"] + assert manifests[0]["full_collection"] == manifests[1]["full_collection"] + assert not set(manifests[0]["selected"]) & set(manifests[1]["selected"]) + assert ( + sorted(manifests[0]["selected"] + manifests[1]["selected"]) + == manifests[0]["full_collection"] + ) + + +def test_real_pytest_partition_corruption_fails_before_execution(tmp_path): + result = run_toy_suite(tmp_path, 0, corrupt=True) + assert result.returncode != 0 + assert "must cover every selected test exactly once" in result.stdout + result.stderr + assert " PASSED" not in result.stdout + assert not (tmp_path / "platform-partition.json").exists() diff --git a/tests/test_snapshot_analyzer_cache.py b/tests/test_snapshot_analyzer_cache.py index a584757..a93cdc0 100644 --- a/tests/test_snapshot_analyzer_cache.py +++ b/tests/test_snapshot_analyzer_cache.py @@ -96,6 +96,10 @@ def test_db_file_replacement_invalidates_cache(self, valid_db: Path, analyzer) - conn.execute("CREATE TABLE trace_entry_1 (id INTEGER PRIMARY KEY)") conn.commit() conn.close() + if os.name == "nt": + # Keep the entry to test automatic signature invalidation, while + # releasing the handle Windows requires publishers to close first. + first_ctx.close() os.replace(new_path, valid_db) second_ctx = analyzer.context_cache.get(valid_db)