|
| 1 | +# Copyright Contributors to the Beaker project. |
| 2 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + |
| 4 | +import unittest |
| 5 | + |
| 6 | +from bkr.common.resources import ( |
| 7 | + resource_stream, |
| 8 | + resource_string, |
| 9 | + resource_listdir, |
| 10 | + resource_exists, |
| 11 | + iter_entry_points, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class ResourceStreamTest(unittest.TestCase): |
| 16 | + def test_resource_stream_returns_readable_file_object(self): |
| 17 | + f = resource_stream("bkr.common", "schema/beaker-job.rng") |
| 18 | + try: |
| 19 | + data = f.read() |
| 20 | + self.assertIsInstance(data, bytes) |
| 21 | + self.assertGreater(len(data), 0) |
| 22 | + self.assertIn(b"<grammar", data) |
| 23 | + finally: |
| 24 | + f.close() |
| 25 | + |
| 26 | + |
| 27 | +class ResourceStringTest(unittest.TestCase): |
| 28 | + def test_resource_string_returns_bytes(self): |
| 29 | + data = resource_string("bkr.common", "schema/beaker-job.rng") |
| 30 | + self.assertIsInstance(data, bytes) |
| 31 | + self.assertGreater(len(data), 0) |
| 32 | + self.assertIn(b"<grammar", data) |
| 33 | + |
| 34 | + |
| 35 | +class ResourceListdirTest(unittest.TestCase): |
| 36 | + def test_resource_listdir_returns_list(self): |
| 37 | + entries = resource_listdir("bkr.common", "schema") |
| 38 | + self.assertIsInstance(entries, list) |
| 39 | + self.assertIn("beaker-job.rng", entries) |
| 40 | + self.assertIn("beaker-task.rng", entries) |
| 41 | + |
| 42 | + |
| 43 | +class ResourceExistsTest(unittest.TestCase): |
| 44 | + def test_resource_exists_true_for_file(self): |
| 45 | + self.assertTrue(resource_exists("bkr.common", "schema/beaker-job.rng")) |
| 46 | + |
| 47 | + def test_resource_exists_true_for_directory(self): |
| 48 | + self.assertTrue(resource_exists("bkr.common", "schema")) |
| 49 | + |
| 50 | + def test_resource_exists_false(self): |
| 51 | + self.assertFalse(resource_exists("bkr.common", "schema/nonexistent.xyz")) |
| 52 | + |
| 53 | + |
| 54 | +class IterEntryPointsTest(unittest.TestCase): |
| 55 | + def test_iter_entry_points_returns_iterable(self): |
| 56 | + eps = iter_entry_points("console_scripts") |
| 57 | + result = list(eps) |
| 58 | + self.assertIsInstance(result, list) |
| 59 | + |
| 60 | + def test_iter_entry_points_empty_group(self): |
| 61 | + eps = iter_entry_points("nonexistent.group.12345") |
| 62 | + result = list(eps) |
| 63 | + self.assertEqual(result, []) |
0 commit comments