Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | # |
| 4 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 5 | import sys |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 6 | from unittest.case import TestCase |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 7 | from contextlib import contextmanager |
| 8 | from io import StringIO |
| 9 | from oe.utils import packages_filter_out_system, trim_version, multiprocess_launch |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 10 | |
| 11 | class TestPackagesFilterOutSystem(TestCase): |
| 12 | def test_filter(self): |
| 13 | """ |
| 14 | Test that oe.utils.packages_filter_out_system works. |
| 15 | """ |
| 16 | try: |
| 17 | import bb |
| 18 | except ImportError: |
| 19 | self.skipTest("Cannot import bb") |
| 20 | |
| 21 | d = bb.data_smart.DataSmart() |
| 22 | d.setVar("PN", "foo") |
| 23 | |
| 24 | d.setVar("PACKAGES", "foo foo-doc foo-dev") |
| 25 | pkgs = packages_filter_out_system(d) |
| 26 | self.assertEqual(pkgs, []) |
| 27 | |
| 28 | d.setVar("PACKAGES", "foo foo-doc foo-data foo-dev") |
| 29 | pkgs = packages_filter_out_system(d) |
| 30 | self.assertEqual(pkgs, ["foo-data"]) |
| 31 | |
| 32 | d.setVar("PACKAGES", "foo foo-locale-en-gb") |
| 33 | pkgs = packages_filter_out_system(d) |
| 34 | self.assertEqual(pkgs, []) |
| 35 | |
| 36 | d.setVar("PACKAGES", "foo foo-data foo-locale-en-gb") |
| 37 | pkgs = packages_filter_out_system(d) |
| 38 | self.assertEqual(pkgs, ["foo-data"]) |
| 39 | |
| 40 | |
| 41 | class TestTrimVersion(TestCase): |
| 42 | def test_version_exception(self): |
| 43 | with self.assertRaises(TypeError): |
| 44 | trim_version(None, 2) |
| 45 | with self.assertRaises(TypeError): |
| 46 | trim_version((1, 2, 3), 2) |
| 47 | |
| 48 | def test_num_exception(self): |
| 49 | with self.assertRaises(ValueError): |
| 50 | trim_version("1.2.3", 0) |
| 51 | with self.assertRaises(ValueError): |
| 52 | trim_version("1.2.3", -1) |
| 53 | |
| 54 | def test_valid(self): |
| 55 | self.assertEqual(trim_version("1.2.3", 1), "1") |
| 56 | self.assertEqual(trim_version("1.2.3", 2), "1.2") |
| 57 | self.assertEqual(trim_version("1.2.3", 3), "1.2.3") |
| 58 | self.assertEqual(trim_version("1.2.3", 4), "1.2.3") |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 59 | |
| 60 | |
| 61 | class TestMultiprocessLaunch(TestCase): |
| 62 | |
| 63 | def test_multiprocesslaunch(self): |
| 64 | import bb |
| 65 | |
| 66 | def testfunction(item, d): |
Andrew Geissler | 5f35090 | 2021-07-23 13:09:54 -0400 | [diff] [blame] | 67 | if item == "2": |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 68 | raise KeyError("Invalid number %s" % item) |
| 69 | return "Found %s" % item |
| 70 | |
| 71 | def dummyerror(msg): |
| 72 | print("ERROR: %s" % msg) |
| 73 | def dummyfatal(msg): |
| 74 | print("ERROR: %s" % msg) |
| 75 | raise bb.BBHandledException() |
| 76 | |
| 77 | @contextmanager |
| 78 | def captured_output(): |
| 79 | new_out, new_err = StringIO(), StringIO() |
| 80 | old_out, old_err = sys.stdout, sys.stderr |
| 81 | try: |
| 82 | sys.stdout, sys.stderr = new_out, new_err |
| 83 | yield sys.stdout, sys.stderr |
| 84 | finally: |
| 85 | sys.stdout, sys.stderr = old_out, old_err |
| 86 | |
| 87 | d = bb.data_smart.DataSmart() |
| 88 | bb.error = dummyerror |
| 89 | bb.fatal = dummyfatal |
| 90 | |
| 91 | # Assert the function returns the right results |
| 92 | result = multiprocess_launch(testfunction, ["3", "4", "5", "6"], d, extraargs=(d,)) |
| 93 | self.assertIn("Found 3", result) |
| 94 | self.assertIn("Found 4", result) |
| 95 | self.assertIn("Found 5", result) |
| 96 | self.assertIn("Found 6", result) |
| 97 | self.assertEqual(len(result), 4) |
| 98 | |
| 99 | # Assert the function prints exceptions |
| 100 | with captured_output() as (out, err): |
| 101 | self.assertRaises(bb.BBHandledException, multiprocess_launch, testfunction, ["1", "2", "3", "4", "5", "6"], d, extraargs=(d,)) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 102 | self.assertIn("KeyError: 'Invalid number 2'", out.getvalue()) |