From 0693cf08e66e27d4d8b11762e5c711a03024cc4c Mon Sep 17 00:00:00 2001 From: WANG Xuerui Date: Sat, 28 Dec 2024 16:20:04 +0800 Subject: [PATCH 1/2] tests: add tests for ConfigEditor --- tests/config/__init__.py | 0 tests/config/test_editor.py | 91 +++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 tests/config/__init__.py create mode 100644 tests/config/test_editor.py diff --git a/tests/config/__init__.py b/tests/config/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/config/test_editor.py b/tests/config/test_editor.py new file mode 100644 index 00000000..e990c132 --- /dev/null +++ b/tests/config/test_editor.py @@ -0,0 +1,91 @@ +import pathlib +import sys + +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli as tomllib + +import pytest + +from ruyi.config.editor import ConfigEditor +from ruyi.config.errors import ( + InvalidConfigKeyError, + InvalidConfigSectionError, + InvalidConfigValueTypeError, + MalformedConfigFileError, +) + + +@pytest.fixture +def temp_config_file(tmp_path: pathlib.Path) -> pathlib.Path: + return tmp_path / "config.toml" + + +def test_enter_exit(temp_config_file: pathlib.Path) -> None: + editor = ConfigEditor(temp_config_file) + with editor as e: + assert e is editor + e.set_value("installation.externally_managed", True) + # no stage() so no file writing + assert not temp_config_file.exists() + + +def test_set_value(temp_config_file: pathlib.Path) -> None: + with ConfigEditor(temp_config_file) as e: + with pytest.raises(InvalidConfigKeyError): + e.set_value("invalid_key", "value") + + with pytest.raises(InvalidConfigValueTypeError): + e.set_value("installation.externally_managed", "true") + + with pytest.raises(InvalidConfigValueTypeError): + e.set_value("installation.externally_managed", 1) + + e.set_value("installation.externally_managed", True) + e.stage() + + with open(temp_config_file, "rb") as fp: + content = tomllib.load(fp) + assert content["installation"]["externally_managed"] is True + + +def test_unset_value_remove_section(temp_config_file: pathlib.Path) -> None: + iem = ("installation", "externally_managed") + with ConfigEditor(temp_config_file) as e: + e.set_value(iem, True) + e.set_value("telemetry.mode", "off") + e.set_value("repo.remote", "http://test.example.com") + e.stage() + + with open(temp_config_file, "rb") as fp: + content = tomllib.load(fp) + assert content["installation"]["externally_managed"] is True + assert content["repo"]["remote"] == "http://test.example.com" + assert content["telemetry"]["mode"] == "off" + + with ConfigEditor(temp_config_file) as e: + e.unset_value(iem) + e.unset_value("telemetry.mode") + e.remove_section("repo") + e.stage() + + with pytest.raises(InvalidConfigSectionError): + e.remove_section("foo") + + with open(temp_config_file, "rb") as fp: + content = tomllib.load(fp) + assert "installation" in content + assert "repo" not in content + assert "telemetry" in content + assert "externally_managed" not in content["installation"] + assert "mode" not in content["telemetry"] + + +def test_malformed_config_file_error(temp_config_file: pathlib.Path) -> None: + with open(temp_config_file, "wb") as fp: + fp.write(b"repo = 1\n") + + with pytest.raises(MalformedConfigFileError): + with ConfigEditor(temp_config_file) as e: + e.set_value("repo.branch", "foo") From 8674bf26cd7b8c8023a47b142c7e66488773147d Mon Sep 17 00:00:00 2001 From: WANG Xuerui Date: Sat, 28 Dec 2024 16:27:39 +0800 Subject: [PATCH 2/2] tests: add tests for Checksummer --- tests/ruyipkg/test_checksum.py | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/ruyipkg/test_checksum.py diff --git a/tests/ruyipkg/test_checksum.py b/tests/ruyipkg/test_checksum.py new file mode 100644 index 00000000..fe518ef3 --- /dev/null +++ b/tests/ruyipkg/test_checksum.py @@ -0,0 +1,50 @@ +import io +import hashlib + +import pytest + +from ruyi.ruyipkg.checksum import get_hash_instance, Checksummer + + +def test_get_hash_instance_supported() -> None: + # these should not raise any exception + get_hash_instance("sha256") + get_hash_instance("sha512") + + +def test_get_hash_instance_unsupported() -> None: + with pytest.raises(ValueError, match="checksum algorithm md5 not supported"): + get_hash_instance("md5") + + +def test_checksummer_compute() -> None: + file_content = b"test content" + expected_sha256 = hashlib.sha256(file_content).hexdigest() + expected_sha512 = hashlib.sha512(file_content).hexdigest() + + file = io.BytesIO(file_content) + checksums = {"sha256": expected_sha256, "sha512": expected_sha512} + checksummer = Checksummer(file, checksums) + + computed_checksums = checksummer.compute() + assert computed_checksums["sha256"] == expected_sha256 + + +def test_checksummer_check() -> None: + file_content = b"test content" + expected_sha256 = hashlib.sha256(file_content).hexdigest() + expected_sha512 = hashlib.sha512(file_content).hexdigest() + + file = io.BytesIO(file_content) + checksums = {"sha256": expected_sha256, "sha512": expected_sha512} + checksummer = Checksummer(file, checksums) + + # This should not raise any exception + checksummer.check() + + # Modify the file content to cause a checksum mismatch + file = io.BytesIO(b"modified content") + checksummer = Checksummer(file, checksums) + + with pytest.raises(ValueError, match="wrong sha256 checksum"): + checksummer.check()