|
| 1 | +import logging |
| 2 | +from types import SimpleNamespace |
| 3 | + |
| 4 | +# Ensure modifier package is initialized before importing PropertyModifier. |
| 5 | +import src.core.modifiers # noqa: F401 |
| 6 | +from src.core.props import PropertyModifier |
| 7 | + |
| 8 | + |
| 9 | +def _write_minimal_props_global_json(tmp_path): |
| 10 | + devices_common = tmp_path / "devices" / "common" |
| 11 | + devices_common.mkdir(parents=True, exist_ok=True) |
| 12 | + (devices_common / "props_global.json").write_text( |
| 13 | + """{ |
| 14 | + "common": {}, |
| 15 | + "eu_rom": { |
| 16 | + "ro.product.mod_device": "{base_code}_xiaomieu_global" |
| 17 | + }, |
| 18 | + "global_rom": { |
| 19 | + "ro.product.mod_device": "{global_mod_device}" |
| 20 | + }, |
| 21 | + "cn_rom": { |
| 22 | + "ro.product.mod_device": "{base_code}" |
| 23 | + } |
| 24 | +} |
| 25 | +""", |
| 26 | + encoding="utf-8", |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def _build_ctx(tmp_path, *, is_port_eu_rom, is_port_global_rom, port_global_region): |
| 31 | + target_dir = tmp_path / "target" |
| 32 | + target_dir.mkdir(parents=True, exist_ok=True) |
| 33 | + return SimpleNamespace( |
| 34 | + target_dir=target_dir, |
| 35 | + stock_rom_code="pudding", |
| 36 | + target_rom_version="OS2.0.0.TEST", |
| 37 | + is_port_eu_rom=is_port_eu_rom, |
| 38 | + is_port_global_rom=is_port_global_rom, |
| 39 | + port_global_region=port_global_region, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def _run_update_and_read_mod_device(tmp_path, ctx): |
| 44 | + prop_file = ctx.target_dir / "build.prop" |
| 45 | + prop_file.write_text("ro.product.mod_device=old_value\n", encoding="utf-8") |
| 46 | + modifier = PropertyModifier(ctx, logger=logging.getLogger("test.props.mod_device")) |
| 47 | + modifier._update_general_info() |
| 48 | + for line in prop_file.read_text(encoding="utf-8").splitlines(): |
| 49 | + if line.startswith("ro.product.mod_device="): |
| 50 | + return line.split("=", 1)[1] |
| 51 | + raise AssertionError("ro.product.mod_device was not found after update") |
| 52 | + |
| 53 | + |
| 54 | +def test_update_general_info_preserves_global_region_suffix_for_mod_device(tmp_path, monkeypatch): |
| 55 | + _write_minimal_props_global_json(tmp_path) |
| 56 | + monkeypatch.chdir(tmp_path) |
| 57 | + ctx = _build_ctx( |
| 58 | + tmp_path, |
| 59 | + is_port_eu_rom=False, |
| 60 | + is_port_global_rom=True, |
| 61 | + port_global_region="tw", |
| 62 | + ) |
| 63 | + |
| 64 | + value = _run_update_and_read_mod_device(tmp_path, ctx) |
| 65 | + assert value == "pudding_tw_global" |
| 66 | + |
| 67 | + |
| 68 | +def test_update_general_info_uses_generic_global_suffix_when_region_missing(tmp_path, monkeypatch): |
| 69 | + _write_minimal_props_global_json(tmp_path) |
| 70 | + monkeypatch.chdir(tmp_path) |
| 71 | + ctx = _build_ctx( |
| 72 | + tmp_path, |
| 73 | + is_port_eu_rom=False, |
| 74 | + is_port_global_rom=True, |
| 75 | + port_global_region="", |
| 76 | + ) |
| 77 | + |
| 78 | + value = _run_update_and_read_mod_device(tmp_path, ctx) |
| 79 | + assert value == "pudding_global" |
0 commit comments