pungi/tests/test_ostree_utils.py
Lubomír Sedlář ff5a7e6377 Make python3-mock dependency optional
https://fedoraproject.org/wiki/Changes/RemovePythonMockUsage

Prefer using unittest.mock to a standalone package. The separate
packages should only really be needed on Python 2.7 these days.

The test requirements file is updated to only require mock on old
Python, and the dependency is removed from setup.py to avoid issues
there.

Relates: https://src.fedoraproject.org/rpms/pungi/pull-request/9

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
2024-01-26 09:45:19 +01:00

53 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
try:
from unittest import mock
except ImportError:
import mock
import os
from tests import helpers
from pungi.ostree import utils
class GetRefFromTreefileTest(helpers.PungiTestCase):
def setUp(self):
super(GetRefFromTreefileTest, self).setUp()
self.json = os.path.join(self.topdir, "treefile.json")
self.yaml = os.path.join(self.topdir, "treefile.yaml")
def test_reads_json(self):
helpers.touch(self.json, """{"ref": "master"}""")
self.assertEqual(utils.get_ref_from_treefile(self.json), "master")
def test_reads_yaml(self):
helpers.touch(self.yaml, """ref: master""")
self.assertEqual(utils.get_ref_from_treefile(self.yaml), "master")
@mock.patch("pungi.ostree.utils.getBaseArch")
def test_replaces_basearch(self, getBaseArch):
getBaseArch.return_value = "x86_64"
helpers.touch(self.json, """{"ref": "${basearch}/master"}""")
self.assertEqual(utils.get_ref_from_treefile(self.json), "x86_64/master")
@mock.patch("pungi.ostree.utils.getBaseArch")
def test_replaces_basearch_for_given_arch(self, getBaseArch):
getBaseArch.return_value = "x86_64"
helpers.touch(self.json, """{"ref": "${basearch}/master"}""")
self.assertEqual(
utils.get_ref_from_treefile(self.json, arch="foo"), "x86_64/master"
)
self.assertEqual(getBaseArch.call_args_list, [mock.call("foo")])
def test_handles_invalid_json(self):
helpers.touch(self.json, """{"ref" "master"}""")
self.assertIsNone(utils.get_ref_from_treefile(self.json))
def test_handles_invalid_yaml(self):
helpers.touch(self.yaml, """{ ref\n - master""")
self.assertIsNone(utils.get_ref_from_treefile(self.yaml))
def test_handles_missing_file(self):
self.assertIsNone(utils.get_ref_from_treefile(self.json))