|
| 1 | +# Copyright (C) 2019-2024 Intel Corporation |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +import argparse |
| 5 | +import logging |
| 6 | +import unittest |
| 7 | + |
| 8 | +from codebasin.config import _ExtendMatchAction, _StoreSplitAction |
| 9 | + |
| 10 | + |
| 11 | +class TestActions(unittest.TestCase): |
| 12 | + """ |
| 13 | + Test that custom argparse.Action classes work as expected. |
| 14 | + These classes enable handling of complex user-defined compiler options. |
| 15 | + """ |
| 16 | + |
| 17 | + def setUp(self): |
| 18 | + logging.disable() |
| 19 | + |
| 20 | + def test_store_split_init(self): |
| 21 | + """Check that store_split recognizes custom arguments""" |
| 22 | + action = _StoreSplitAction(["--foo"], "foo", sep=",", format="$value") |
| 23 | + self.assertEqual(action.sep, ",") |
| 24 | + self.assertEqual(action.format, "$value") |
| 25 | + |
| 26 | + action = _StoreSplitAction(["--foo"], "foo") |
| 27 | + self.assertEqual(action.sep, None) |
| 28 | + self.assertEqual(action.format, None) |
| 29 | + |
| 30 | + def test_store_split(self): |
| 31 | + """Check that argparse calls store_split correctly""" |
| 32 | + namespace = argparse.Namespace() |
| 33 | + namespace.passes = {} |
| 34 | + |
| 35 | + parser = argparse.ArgumentParser() |
| 36 | + parser.add_argument("--foo", action=_StoreSplitAction, sep=",") |
| 37 | + parser.add_argument( |
| 38 | + "--bar", |
| 39 | + action=_StoreSplitAction, |
| 40 | + sep=",", |
| 41 | + format="prefix-$value-suffix", |
| 42 | + ) |
| 43 | + parser.add_argument("--baz", action=_StoreSplitAction, type=int) |
| 44 | + parser.add_argument( |
| 45 | + "--qux", |
| 46 | + action=_StoreSplitAction, |
| 47 | + sep=",", |
| 48 | + dest="passes", |
| 49 | + ) |
| 50 | + |
| 51 | + args, _ = parser.parse_known_args(["--foo=one,two"], namespace) |
| 52 | + self.assertEqual(args.foo, ["one", "two"]) |
| 53 | + |
| 54 | + args, _ = parser.parse_known_args(["--bar=one,two"], namespace) |
| 55 | + self.assertEqual(args.bar, ["prefix-one-suffix", "prefix-two-suffix"]) |
| 56 | + |
| 57 | + with self.assertRaises(TypeError): |
| 58 | + args, _ = parser.parse_known_args(["--baz=1"], namespace) |
| 59 | + |
| 60 | + args, _ = parser.parse_known_args(["--qux=one,two"], namespace) |
| 61 | + self.assertEqual(args.passes, {"--qux": ["one", "two"]}) |
| 62 | + |
| 63 | + def test_extend_match_init(self): |
| 64 | + """Check that extend_match recognizes custom arguments""" |
| 65 | + action = _ExtendMatchAction( |
| 66 | + ["--foo"], |
| 67 | + "foo", |
| 68 | + pattern="*", |
| 69 | + format="$value", |
| 70 | + ) |
| 71 | + self.assertEqual(action.pattern, "*") |
| 72 | + self.assertEqual(action.format, "$value") |
| 73 | + |
| 74 | + action = _ExtendMatchAction(["--foo"], "foo") |
| 75 | + self.assertEqual(action.pattern, None) |
| 76 | + self.assertEqual(action.format, None) |
| 77 | + |
| 78 | + def test_extend_match(self): |
| 79 | + """Check that argparse calls store_split correctly""" |
| 80 | + namespace = argparse.Namespace() |
| 81 | + namespace.passes = {} |
| 82 | + |
| 83 | + parser = argparse.ArgumentParser() |
| 84 | + parser.add_argument( |
| 85 | + "--foo", |
| 86 | + action=_ExtendMatchAction, |
| 87 | + pattern=r"option_(\d+)", |
| 88 | + default=[], |
| 89 | + ) |
| 90 | + parser.add_argument( |
| 91 | + "--bar", |
| 92 | + action=_ExtendMatchAction, |
| 93 | + pattern=r"option_(\d+)", |
| 94 | + format="prefix-$value-suffix", |
| 95 | + default=[], |
| 96 | + ) |
| 97 | + parser.add_argument("--baz", action=_ExtendMatchAction, type=int) |
| 98 | + parser.add_argument( |
| 99 | + "--qux", |
| 100 | + action=_ExtendMatchAction, |
| 101 | + pattern=r"option_(\d+)", |
| 102 | + dest="passes", |
| 103 | + ) |
| 104 | + |
| 105 | + args, _ = parser.parse_known_args( |
| 106 | + ["--foo=option_1,option_2"], |
| 107 | + namespace, |
| 108 | + ) |
| 109 | + self.assertEqual(args.foo, ["1", "2"]) |
| 110 | + |
| 111 | + args, _ = parser.parse_known_args( |
| 112 | + ["--bar=option_1,option_2"], |
| 113 | + namespace, |
| 114 | + ) |
| 115 | + self.assertEqual(args.bar, ["prefix-1-suffix", "prefix-2-suffix"]) |
| 116 | + |
| 117 | + with self.assertRaises(TypeError): |
| 118 | + args, _ = parser.parse_known_args(["--baz=1"], namespace) |
| 119 | + |
| 120 | + args, _ = parser.parse_known_args( |
| 121 | + ["--qux=option_1,option_2"], |
| 122 | + namespace, |
| 123 | + ) |
| 124 | + self.assertEqual(args.passes, {"--qux": ["1", "2"]}) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + unittest.main() |
0 commit comments