Skip to content

Commit

Permalink
Replace unittest patch by monkeypatch
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclert-canonical committed Dec 13, 2024
1 parent 0d26737 commit 7303df9
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions tests/unit/test_architecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

from unittest.mock import patch

from architecture import is_wrong_architecture

TEST_MANIFEST = """
Expand All @@ -15,38 +13,34 @@
"""


def test_wrong_architecture_file_not_found():
def test_wrong_architecture_file_not_found(monkeypatch):
"""Tests if the function returns False when the charm file doesn't exist."""
with (
patch("os.environ", return_value={"CHARM_DIR": "/tmp"}),
patch("pathlib.Path.exists", return_value=False),
):
assert not is_wrong_architecture()
monkeypatch.setattr("os.environ", {"CHARM_DIR": "/tmp"})
monkeypatch.setattr("pathlib.Path.exists", lambda: False)
assert not is_wrong_architecture()


def test_wrong_architecture_amd64():
def test_wrong_architecture_amd64(monkeypatch):
"""Tests if the function correctly identifies arch when charm is AMD."""
with (
patch("os.environ", return_value={"CHARM_DIR": "/tmp"}),
patch("pathlib.Path.exists", return_value=True),
patch("pathlib.Path.read_text", return_value=TEST_MANIFEST.format(arch="amd64")),
patch("platform.machine") as machine,
):
machine.return_value = "x86_64"
assert not is_wrong_architecture()
machine.return_value = "aarch64"
assert is_wrong_architecture()


def test_wrong_architecture_arm64():
monkeypatch.setattr("os.environ", {"CHARM_DIR": "/tmp"})
monkeypatch.setattr("pathlib.Path.exists", lambda: True)
monkeypatch.setattr("pathlib.Path.read_text", lambda: TEST_MANIFEST.format(arch="amd64"))

monkeypatch.setattr("platform.machine", lambda: "x86_64")
assert not is_wrong_architecture()

monkeypatch.setattr("platform.machine", lambda: "aarch64")
assert is_wrong_architecture()


def test_wrong_architecture_arm64(monkeypatch):
"""Tests if the function correctly identifies arch when charm is ARM."""
with (
patch("os.environ", return_value={"CHARM_DIR": "/tmp"}),
patch("pathlib.Path.exists", return_value=True),
patch("pathlib.Path.read_text", return_value=TEST_MANIFEST.format(arch="arm64")),
patch("platform.machine") as machine,
):
machine.return_value = "x86_64"
assert is_wrong_architecture()
machine.return_value = "aarch64"
assert not is_wrong_architecture()
monkeypatch.setattr("os.environ", {"CHARM_DIR": "/tmp"})
monkeypatch.setattr("pathlib.Path.exists", lambda: True)
monkeypatch.setattr("pathlib.Path.read_text", lambda: TEST_MANIFEST.format(arch="amd64"))

monkeypatch.setattr("platform.machine", lambda: "x86_64")
assert is_wrong_architecture()

monkeypatch.setattr("platform.machine", lambda: "aarch64")
assert not is_wrong_architecture()

0 comments on commit 7303df9

Please sign in to comment.