Skip to content

Commit

Permalink
Merge pull request #250 from xen0n/tests
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
xen0n authored Dec 28, 2024
2 parents 34adb2e + 8674bf2 commit 3d23ce4
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
Empty file added tests/config/__init__.py
Empty file.
91 changes: 91 additions & 0 deletions tests/config/test_editor.py
Original file line number Diff line number Diff line change
@@ -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")
50 changes: 50 additions & 0 deletions tests/ruyipkg/test_checksum.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 3d23ce4

Please sign in to comment.