add apt unittest

This commit is contained in:
Michael Vogt 2014-10-29 20:44:44 +01:00 committed by Toshio Kuratomi
parent 76fc436b08
commit f475769d3a
3 changed files with 47 additions and 3 deletions

View file

@ -230,10 +230,10 @@ def expand_dpkg_options(dpkg_options_compressed):
def expand_pkgspec_from_fnmatches(m, pkgspec, cache):
new_pkgspec = []
for name_or_fnmatch_or_version in pkgspec:
pkgname_or_fnmatch_pattern = name_or_fnmatch_or_version.split("=")[0]
pkgname_or_fnmatch_pattern, version = package_split(name_or_fnmatch_or_version)
# note that any of these chars is not allowed in a (debian) pkgname
if [c for c in pkgname_or_fnmatch_pattern if c in "*?[]!"]:
if "=" in pkgname_or_fnmatch_pattern:
if version:
m.fail_json(msg="pkgname wildcard and version can not be mixed")
# handle multiarch pkgnames, the idea is that "apt*" should
# only select native packages. But "apt*:i386" should still work
@ -568,4 +568,6 @@ def main():
# import module snippets
from ansible.module_utils.basic import *
main()
# FIXME: if __name__ == "__main__": ?
if "ANSIBLE_IN_HAPPY_UNITTEST_LAND" not in os.environ:
main()

0
tests/__init__.py Normal file
View file

42
tests/test_apt.py Normal file
View file

@ -0,0 +1,42 @@
import collections
import mock
import os
import unittest
# FIXME: this is not super elegant
os.environ["ANSIBLE_IN_HAPPY_UNITTEST_LAND"] = "1"
from packaging.apt import (
expand_pkgspec_from_fnmatches,
)
class AptExpandPkgspecTestCase(unittest.TestCase):
def setUp(self):
FakePackage = collections.namedtuple("Package", ("name",))
self.fake_cache = [ FakePackage("apt"),
FakePackage("apt-utils"),
]
def test_trivil(self):
foo = ["apt"]
self.assertEqual(
expand_pkgspec_from_fnmatches(None, foo, self.fake_cache), foo)
def test_bug_28(self):
foo = ["apt=1.0*"]
self.assertEqual(
expand_pkgspec_from_fnmatches(None, foo, self.fake_cache), foo)
def test_pkgname_wildcard_version_wildcard_fails(self):
foo = ["apt*=1.0*"]
m_mock = mock.Mock()
expand_pkgspec_from_fnmatches(m_mock, foo, self.fake_cache)
self.assertTrue(m_mock.fail_json.called)
def test_pkgname_expands(self):
foo = ["apt*"]
m_mock = mock.Mock()
self.assertEqual(
expand_pkgspec_from_fnmatches(m_mock, foo, self.fake_cache),
["apt", "apt-utils"])