diff options
author | 2022-08-26 17:20:49 +0300 | |
---|---|---|
committer | 2022-08-26 17:21:44 +0300 | |
commit | 627077bc34350b4609ee328467b23ff00be58cdc (patch) | |
tree | 98012ce0a5665e9d97c22a60921fad1e49f23b83 /tests | |
parent | scripts/test_*.py: modernize tests to pytest (diff) | |
download | pkgcore-627077bc34350b4609ee328467b23ff00be58cdc.tar.gz pkgcore-627077bc34350b4609ee328467b23ff00be58cdc.tar.bz2 pkgcore-627077bc34350b4609ee328467b23ff00be58cdc.zip |
pkgsets/test_*.py: modernize tests to pytest
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/pkgsets/test_filelist.py | 119 | ||||
-rw-r--r-- | tests/pkgsets/test_installed.py | 18 |
2 files changed, 55 insertions, 82 deletions
diff --git a/tests/pkgsets/test_filelist.py b/tests/pkgsets/test_filelist.py index 21a120bad..d41d244f7 100644 --- a/tests/pkgsets/test_filelist.py +++ b/tests/pkgsets/test_filelist.py @@ -1,14 +1,12 @@ import os -import tempfile -from pkgcore import log, os_data +import pytest +from pkgcore import os_data from pkgcore.ebuild.atom import atom from pkgcore.pkgsets import filelist -from pkgcore.test import callback_logger, protect_logging -from snakeoil.test import TestCase -class TestFileList(TestCase): +class TestFileList: kls = staticmethod(filelist.FileList) @@ -22,95 +20,72 @@ class TestFileList(TestCase): except IndexError: return os.getgid() - def setUp(self): - self.fd, self.fn = tempfile.mkstemp() + def gen_pkgset(self, tmp_path, contents): + (tmp_path / 'file').write_text(contents) + return self.kls(tmp_path / 'file', gid=self.gid) - def tearDown(self): - os.close(self.fd) - try: - os.unlink(self.fn) - except IOError: - pass - - def gen_pkgset(self, contents): - os.write(self.fd, contents.encode()) - return self.kls(self.fn, gid=self.gid) - - def test_contains(self): - self.assertIn( - atom("x11-base/xorg-x11"), self.gen_pkgset("x11-base/xorg-x11")) + def test_contains(self, tmp_path): + assert atom("x11-base/xorg-x11") in self.gen_pkgset(tmp_path, "x11-base/xorg-x11") - def test_len(self): - self.assertEqual( - len(self.gen_pkgset("x11-base/xorg-x11\ndev-util/diffball")), 2) + def test_len(self, tmp_path): + assert len(self.gen_pkgset(tmp_path, "x11-base/xorg-x11\ndev-util/diffball")) == 2 - def test_iter(self): - self.assertEqual( - sorted(self.gen_pkgset("dev-util/diffball\ndev-util/bsdiff")), - sorted(atom(x) for x in ["dev-util/diffball", "dev-util/bsdiff"])) + def test_iter(self, tmp_path): + assert set(self.gen_pkgset(tmp_path, "dev-util/diffball\ndev-util/bsdiff")) == \ + {atom(x) for x in ["dev-util/diffball", "dev-util/bsdiff"]} - def test_add(self): - s = self.gen_pkgset("dev-util/diffball\n=dev-util/bsdiff-0.4") + def test_add(self, tmp_path): + s = self.gen_pkgset(tmp_path, "dev-util/diffball\n=dev-util/bsdiff-0.4") s.add(atom("dev-util/foon")) s.add(atom("=dev-util/lib-1")) s.flush() - with open(self.fn) as f: - self.assertEqual(list(map(atom, (x.strip() for x in f))), - sorted(map(atom, ("dev-util/diffball", "=dev-util/bsdiff-0.4", - "dev-util/foon", "=dev-util/lib-1")))) - def test_remove(self): - s = self.gen_pkgset("=dev-util/diffball-0.4\ndev-util/bsdiff") + assert {atom(line) for line in (tmp_path / 'file').read_text().splitlines()} == \ + set(map(atom, ("dev-util/diffball", "=dev-util/bsdiff-0.4", + "dev-util/foon", "=dev-util/lib-1"))) + + def test_remove(self, tmp_path): + s = self.gen_pkgset(tmp_path, "=dev-util/diffball-0.4\ndev-util/bsdiff") s.remove(atom("=dev-util/diffball-0.4")) s.flush() - with open(self.fn) as f: - self.assertEqual(sorted(x.strip() for x in f if x.strip()), - ["dev-util/bsdiff"]) + assert {line.strip() for line in (tmp_path / 'file').read_text().splitlines()} == \ + {"dev-util/bsdiff"} - def test_subset_awareness(self): - s = self.gen_pkgset("@world\ndev-util/bsdiff") - self.assertRaises(ValueError, sorted, s) + def test_subset_awareness(self, tmp_path): + s = self.gen_pkgset(tmp_path, "@world\ndev-util/bsdiff") + with pytest.raises(ValueError): + sorted(s) - def test_ignore_comments(self): - s = self.gen_pkgset("#foon\ndev-util/bsdiff") - self.assertEqual([str(x) for x in s], ['dev-util/bsdiff']) + def test_ignore_comments(self, tmp_path): + s = self.gen_pkgset(tmp_path, "#foon\ndev-util/bsdiff") + assert [str(x) for x in s] == ['dev-util/bsdiff'] class TestWorldFile(TestFileList): kls = staticmethod(filelist.WorldFile) - def test_add(self): - s = self.gen_pkgset("dev-util/bsdiff") + def test_add(self, tmp_path): + s = self.gen_pkgset(tmp_path, "dev-util/bsdiff") s.add(atom("dev-util/foon")) s.add(atom("=dev-util/lib-1")) s.add(atom("dev-util/mylib:2")) s.flush() - with open(self.fn) as f: - self.assertEqual(sorted(x.strip() for x in f), - sorted(("dev-util/bsdiff", "dev-util/foon", "dev-util/lib", - "dev-util/mylib:2"))) + assert {line.strip() for line in (tmp_path / 'file').read_text().splitlines()} == \ + {"dev-util/bsdiff", "dev-util/foon", "dev-util/lib", "dev-util/mylib:2"} - def test_remove(self): - s = self.gen_pkgset("dev-util/diffball\ndev-util/bsdiff") + def test_remove(self, tmp_path): + s = self.gen_pkgset(tmp_path, "dev-util/diffball\ndev-util/bsdiff") s.remove(atom("=dev-util/diffball-0.4")) s.flush() - with open(self.fn) as f: - self.assertEqual(sorted(x.strip() for x in f if x.strip()), - ["dev-util/bsdiff"]) - - @protect_logging(log.logging.root) - def test_subset_awareness(self): - callbacks = [] - log.logging.root.handlers = [callback_logger(callbacks.append)] - s = self.gen_pkgset("@world\ndev-util/bsdiff") - self.assertRaises(ValueError, sorted, s) - - # TODO: re-enable once we move to using pytest and pytest-catchlog to properly catch logging output - # @protect_logging(log.logging.root) - # def test_subset_awareness2(self): - # callbacks = [] - # log.logging.root.handlers = [callback_logger(callbacks.append)] - # s = self.gen_pkgset("@world\ndev-util/bsdiff") - # self.assertEqual([str(x) for x in s], ['dev-util/bsdiff']) - # self.assertIn("set item 'world'", str(callbacks[0])) + assert {line.strip() for line in (tmp_path / 'file').read_text().splitlines()} == \ + {"dev-util/bsdiff"} + + def test_subset_awareness(self, tmp_path): + s = self.gen_pkgset(tmp_path, "@world\ndev-util/bsdiff") + sorted(s) + + def test_subset_awareness2(self, tmp_path, caplog): + s = self.gen_pkgset(tmp_path, "@world\ndev-util/bsdiff") + assert [str(x) for x in s] == ['dev-util/bsdiff'] + assert "set item 'world'" in caplog.text diff --git a/tests/pkgsets/test_installed.py b/tests/pkgsets/test_installed.py index 8d5a32f25..824756285 100644 --- a/tests/pkgsets/test_installed.py +++ b/tests/pkgsets/test_installed.py @@ -24,17 +24,15 @@ class FakePkg: class TestInstalled: def test_iter(self): - fake_vdb = SimpleTree( - {"dev-util": { + fake_vdb = SimpleTree({ + "dev-util": { "diffball": ["1.0"], "bsdiff": ["1.2", "1.3"], - } - }, pkg_klass=FakePkg) + } + }, pkg_klass=FakePkg) - ipkgset = installed.Installed([fake_vdb]) - pkgs = sorted(["dev-util/diffball", "dev-util/bsdiff", "dev-util/bsdiff"]) - assert pkgs == sorted(ipkgset) + assert set(installed.Installed([fake_vdb])) == \ + {"dev-util/diffball", "dev-util/bsdiff"} - vpkgset = installed.VersionedInstalled([fake_vdb]) - cpvs = sorted(["dev-util/diffball-1.0", "dev-util/bsdiff-1.2", "dev-util/bsdiff-1.3"]) - assert cpvs == sorted(vpkgset) + assert set(installed.VersionedInstalled([fake_vdb])) == \ + {"dev-util/diffball-1.0", "dev-util/bsdiff-1.2", "dev-util/bsdiff-1.3"} |