xref: /aosp_15_r20/external/pigweed/pw_unit_test/py/test_group_metadata_test.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1#!/usr/bin/env python3
2# Copyright 2023 The Pigweed Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5# use this file except in compliance with the License. You may obtain a copy of
6# the License at
7#
8#     https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations under
14# the License.
15"""Tests that pw_test_group outputs expected metadata."""
16
17import argparse
18import json
19import pathlib
20import sys
21import unittest
22
23TEST_TARGET_NAME = 'metadata_only_test'
24EXTRA_METADATA_ENTRY = {'extra_key': 'extra_value'}
25
26
27class TestGroupMetadataTest(unittest.TestCase):
28    """Tests that pw_test_group outputs expected metadata."""
29
30    metadata_path: pathlib.Path
31
32    def test_metadata_exists(self) -> None:
33        """Asserts that the metadata file has been created."""
34        self.assertTrue(self.metadata_path.exists())
35
36    def test_metadata_has_test_entry(self) -> None:
37        """Asserts that the expected test entry is present."""
38        meta_text = self.metadata_path.read_text()
39        try:
40            meta = json.loads(meta_text)
41        except json.decoder.JSONDecodeError as jde:
42            raise ValueError(
43                f'Failed to decode file {self.metadata_path} '
44                f'as JSON: {meta_text}'
45            ) from jde
46        self.assertIsInstance(meta, list)
47        found = False
48        for test_entry in meta:
49            self.assertIn('test_type', test_entry)
50            if test_entry['test_type'] != 'unit_test':
51                continue
52            self.assertIn('test_name', test_entry)
53            self.assertIn('test_directory', test_entry)
54            if test_entry['test_name'] == TEST_TARGET_NAME:
55                found = True
56                self.assertIn('extra_metadata', test_entry)
57                self.assertEqual(
58                    test_entry['extra_metadata'], EXTRA_METADATA_ENTRY
59                )
60        self.assertTrue(found)
61
62
63def main() -> None:
64    """Tests that pw_test_group outputs expected metadata."""
65    parser = argparse.ArgumentParser(description=__doc__)
66    parser.add_argument(
67        '--metadata-path',
68        type=pathlib.Path,
69        required=True,
70        help='Path to the metadata.',
71    )
72    parser.add_argument(
73        'unittest_args',
74        nargs=argparse.REMAINDER,
75        help='Arguments after "--" are passed to unittest.',
76    )
77    args = parser.parse_args()
78    TestGroupMetadataTest.metadata_path = args.metadata_path
79    unittest_args = sys.argv[:1] + args.unittest_args[1:]
80    unittest.main(argv=unittest_args)
81
82
83if __name__ == '__main__':
84    main()
85