|
| 1 | +# ******************************************************************************* |
| 2 | +# Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | +# |
| 4 | +# See the NOTICE file(s) distributed with this work for additional |
| 5 | +# information regarding copyright ownership. |
| 6 | +# |
| 7 | +# This program and the accompanying materials are made available under the |
| 8 | +# terms of the Apache License Version 2.0 which is available at |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# SPDX-License-Identifier: Apache-2.0 |
| 12 | +# ******************************************************************************* |
| 13 | +"""Shared pytest fixtures and test utilities.""" |
| 14 | + |
| 15 | +import json |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import pytest |
| 19 | +from lib.known_good import KnownGood, Module, load_known_good |
| 20 | + |
| 21 | +# --------------------------------------------------------------------------- |
| 22 | +# Constants |
| 23 | +# --------------------------------------------------------------------------- |
| 24 | + |
| 25 | +MINIMAL_JSON = { |
| 26 | + "modules": { |
| 27 | + "target_sw": { |
| 28 | + "score_baselibs": { |
| 29 | + "repo": "https://github.com/eclipse-score/baselibs.git", |
| 30 | + "hash": "abc123", |
| 31 | + } |
| 32 | + } |
| 33 | + }, |
| 34 | + "timestamp": "2026-01-01T00:00:00+00:00Z", |
| 35 | +} |
| 36 | + |
| 37 | +FULL_JSON = { |
| 38 | + "modules": { |
| 39 | + "target_sw": { |
| 40 | + "score_baselibs": { |
| 41 | + "repo": "https://github.com/eclipse-score/baselibs.git", |
| 42 | + "hash": "158fe6a7b791c58f6eac5f7e4662b8db0cf9ac6e", |
| 43 | + "bazel_patches": ["//patches/baselibs:003-acl-fixes-for-aarch64.patch"], |
| 44 | + "metadata": { |
| 45 | + "extra_test_config": [ |
| 46 | + "//score/json:base_library=nlohmann", |
| 47 | + "//score/memory/shared/flags:use_typedshmd=False", |
| 48 | + ], |
| 49 | + "exclude_test_targets": [ |
| 50 | + "//score/language/safecpp/aborts_upon_exception:abortsuponexception_toolchain_test", |
| 51 | + "//score/containers:dynamic_array_test", |
| 52 | + "//score/mw/log/configuration:*", |
| 53 | + "//score/json/examples:*", |
| 54 | + ], |
| 55 | + "langs": ["cpp"], |
| 56 | + }, |
| 57 | + }, |
| 58 | + "score_persistency": { |
| 59 | + "repo": "https://github.com/eclipse-score/persistency.git", |
| 60 | + "hash": "438bf9b5c447fd41ad43b321679dd3d1b3a6c737", |
| 61 | + "metadata": {"code_root_path": "//src/..."}, |
| 62 | + }, |
| 63 | + }, |
| 64 | + "tooling": { |
| 65 | + "score_crates": { |
| 66 | + "repo": "https://github.com/eclipse-score/score-crates.git", |
| 67 | + "hash": "90539da0fd3e7e23e01f2b4de1679f7dfadd3b6b", |
| 68 | + }, |
| 69 | + "score_itf": { |
| 70 | + "repo": "https://github.com/eclipse-score/itf.git", |
| 71 | + "hash": "44c75debab696a9c967455110a2c32f201159cdd", |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + "timestamp": "2026-01-01T00:00:00+00:00Z", |
| 76 | +} |
| 77 | + |
| 78 | +KNOWN_GOOD_JSON = Path(__file__).parents[3] / "known_good.json" |
| 79 | + |
| 80 | + |
| 81 | +# --------------------------------------------------------------------------- |
| 82 | +# Helper functions |
| 83 | +# --------------------------------------------------------------------------- |
| 84 | + |
| 85 | + |
| 86 | +def make_known_good(**overrides) -> KnownGood: |
| 87 | + """Build a minimal KnownGood with one module. |
| 88 | +
|
| 89 | + Args: |
| 90 | + **overrides: Additional fields to override in the module data. |
| 91 | +
|
| 92 | + Returns: |
| 93 | + KnownGood instance with a single module in target_sw group. |
| 94 | + """ |
| 95 | + module_data = { |
| 96 | + "repo": "https://github.com/eclipse-score/baselibs.git", |
| 97 | + "hash": "abc123def456abc123def456abc123def456abc123", |
| 98 | + **overrides, |
| 99 | + } |
| 100 | + module = Module.from_dict("score_baselibs", module_data) |
| 101 | + return KnownGood( |
| 102 | + modules={"target_sw": {"score_baselibs": module}}, |
| 103 | + timestamp="2026-01-01T00:00:00+00:00Z", |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +# --------------------------------------------------------------------------- |
| 108 | +# Fixtures - File-based |
| 109 | +# --------------------------------------------------------------------------- |
| 110 | + |
| 111 | + |
| 112 | +@pytest.fixture |
| 113 | +def minimal_json_file(tmp_path: Path) -> Path: |
| 114 | + """Create a temporary JSON file with minimal known good data.""" |
| 115 | + p = tmp_path / "known_good.json" |
| 116 | + p.write_text(json.dumps(MINIMAL_JSON)) |
| 117 | + return p |
| 118 | + |
| 119 | + |
| 120 | +@pytest.fixture |
| 121 | +def full_json_file(tmp_path: Path) -> Path: |
| 122 | + """Create a temporary JSON file with full known good data.""" |
| 123 | + p = tmp_path / "known_good.json" |
| 124 | + p.write_text(json.dumps(FULL_JSON)) |
| 125 | + return p |
| 126 | + |
| 127 | + |
| 128 | +# --------------------------------------------------------------------------- |
| 129 | +# Fixtures - KnownGood objects |
| 130 | +# --------------------------------------------------------------------------- |
| 131 | + |
| 132 | + |
| 133 | +@pytest.fixture |
| 134 | +def minimal_known_good() -> KnownGood: |
| 135 | + """Create a minimal KnownGood instance.""" |
| 136 | + return make_known_good() |
| 137 | + |
| 138 | + |
| 139 | +@pytest.fixture |
| 140 | +def multi_group_known_good() -> KnownGood: |
| 141 | + """Create a KnownGood instance with multiple groups.""" |
| 142 | + m1 = Module.from_dict( |
| 143 | + "score_baselibs", |
| 144 | + { |
| 145 | + "repo": "https://github.com/eclipse-score/baselibs.git", |
| 146 | + "hash": "aaa", |
| 147 | + }, |
| 148 | + ) |
| 149 | + m2 = Module.from_dict( |
| 150 | + "score_crates", |
| 151 | + { |
| 152 | + "repo": "https://github.com/eclipse-score/score-crates.git", |
| 153 | + "hash": "bbb", |
| 154 | + }, |
| 155 | + ) |
| 156 | + return KnownGood( |
| 157 | + modules={ |
| 158 | + "target_sw": {"score_baselibs": m1}, |
| 159 | + "tooling": {"score_crates": m2}, |
| 160 | + }, |
| 161 | + timestamp="2026-02-01T00:00:00+00:00Z", |
| 162 | + ) |
| 163 | + |
| 164 | + |
| 165 | +@pytest.fixture |
| 166 | +def real_known_good() -> KnownGood: |
| 167 | + """Load the actual known_good.json from the repository root.""" |
| 168 | + return load_known_good(KNOWN_GOOD_JSON) |
| 169 | + |
| 170 | + |
| 171 | +# --------------------------------------------------------------------------- |
| 172 | +# Fixtures - Test data for check_approvals |
| 173 | +# --------------------------------------------------------------------------- |
| 174 | + |
| 175 | + |
| 176 | +@pytest.fixture |
| 177 | +def modules_maintainers(): |
| 178 | + """Sample modules and maintainers for testing.""" |
| 179 | + return { |
| 180 | + "module_a": [ |
| 181 | + { |
| 182 | + "name": "Alice Developer", |
| 183 | + "email": "alice@example.com", |
| 184 | + "github": "alice", |
| 185 | + "github_user_id": 100, |
| 186 | + }, |
| 187 | + { |
| 188 | + "name": "Bob Developer", |
| 189 | + "email": "bob@example.com", |
| 190 | + "github": "bob", |
| 191 | + "github_user_id": 101, |
| 192 | + }, |
| 193 | + ], |
| 194 | + "module_b": [ |
| 195 | + { |
| 196 | + "name": "Charlie Developer", |
| 197 | + "email": "charlie@example.com", |
| 198 | + "github": "charlie", |
| 199 | + "github_user_id": 102, |
| 200 | + } |
| 201 | + ], |
| 202 | + "module_c": [ |
| 203 | + { |
| 204 | + "name": "Diana Developer", |
| 205 | + "email": "diana@example.com", |
| 206 | + "github": "diana", |
| 207 | + "github_user_id": 103, |
| 208 | + }, |
| 209 | + { |
| 210 | + "name": "Eve Developer", |
| 211 | + "email": "eve@example.com", |
| 212 | + "github": "eve", |
| 213 | + "github_user_id": 104, |
| 214 | + }, |
| 215 | + ], |
| 216 | + } |
0 commit comments