|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +# Part of PyBLE (https://pyble.dev) — see /LICENSE. |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 12 | +sys.path.insert(0, str(REPO_ROOT / "tools" / "ci")) |
| 13 | + |
| 14 | +from android_avd_config import apply_overrides # noqa: E402 |
| 15 | + |
| 16 | + |
| 17 | +class AndroidAvdConfigTest(unittest.TestCase): |
| 18 | + def test_overrides_replace_every_existing_copy_of_a_key(self) -> None: |
| 19 | + with tempfile.TemporaryDirectory() as directory: |
| 20 | + config = Path(directory) / "config.ini" |
| 21 | + config.write_text( |
| 22 | + "AvdId=pyble\n" |
| 23 | + "disk.dataPartition.size=6G\n" |
| 24 | + "hw.ramSize = 1536\n" |
| 25 | + "disk.dataPartition.size=4096M\n" |
| 26 | + "unrelated.setting=yes\n", |
| 27 | + encoding="utf-8", |
| 28 | + ) |
| 29 | + |
| 30 | + apply_overrides( |
| 31 | + config, |
| 32 | + ( |
| 33 | + "disk.dataPartition.size=2048M", |
| 34 | + "hw.ramSize=3072", |
| 35 | + ), |
| 36 | + ) |
| 37 | + |
| 38 | + lines = config.read_text(encoding="utf-8").splitlines() |
| 39 | + self.assertEqual( |
| 40 | + [ |
| 41 | + line |
| 42 | + for line in lines |
| 43 | + if line.partition("=")[0].strip() |
| 44 | + == "disk.dataPartition.size" |
| 45 | + ], |
| 46 | + ["disk.dataPartition.size=2048M"], |
| 47 | + ) |
| 48 | + self.assertEqual( |
| 49 | + [ |
| 50 | + line |
| 51 | + for line in lines |
| 52 | + if line.partition("=")[0].strip() == "hw.ramSize" |
| 53 | + ], |
| 54 | + ["hw.ramSize=3072"], |
| 55 | + ) |
| 56 | + self.assertIn("AvdId=pyble", lines) |
| 57 | + self.assertIn("unrelated.setting=yes", lines) |
| 58 | + |
| 59 | + def test_rejects_duplicate_or_malformed_requested_overrides(self) -> None: |
| 60 | + with tempfile.TemporaryDirectory() as directory: |
| 61 | + config = Path(directory) / "config.ini" |
| 62 | + config.write_text("AvdId=pyble\n", encoding="utf-8") |
| 63 | + |
| 64 | + with self.assertRaisesRegex(ValueError, "duplicate"): |
| 65 | + apply_overrides(config, ("hw.ramSize=2048", "hw.ramSize=3072")) |
| 66 | + with self.assertRaisesRegex(ValueError, "KEY=VALUE"): |
| 67 | + apply_overrides(config, ("not-an-assignment",)) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + unittest.main() |
0 commit comments