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} as JSON: {meta_text}' 44 ) from jde 45 self.assertIsInstance(meta, list) 46 found = False 47 for test_entry in meta: 48 self.assertIn('type', test_entry) 49 if test_entry['type'] != 'test': 50 continue 51 self.assertIn('test_name', test_entry) 52 self.assertIn('test_directory', test_entry) 53 if test_entry['test_name'] == TEST_TARGET_NAME: 54 found = True 55 self.assertIn('extra_metadata', test_entry) 56 self.assertEqual( 57 test_entry['extra_metadata'], EXTRA_METADATA_ENTRY 58 ) 59 self.assertTrue(found) 60 61 62def main() -> None: 63 """Tests that pw_test_group outputs expected metadata.""" 64 parser = argparse.ArgumentParser(description=__doc__) 65 parser.add_argument( 66 '--stamp-path', 67 type=pathlib.Path, 68 required=True, 69 help='Path to the stamp file output of pw_test_group', 70 ) 71 parser.add_argument( 72 'unittest_args', 73 nargs=argparse.REMAINDER, 74 help='Arguments after "--" are passed to unittest.', 75 ) 76 args = parser.parse_args() 77 # Use the stamp file location to find the location of the metadata file. 78 # Unfortunately, there's no reliable toolchain-agnostic way to grab the path 79 # to the metadata file itself within GN. 80 TestGroupMetadataTest.metadata_path = ( 81 args.stamp_path.parent / 'metadata_only_group.testinfo.json' 82 ) 83 unittest_args = sys.argv[:1] + args.unittest_args[1:] 84 unittest.main(argv=unittest_args) 85 86 87if __name__ == '__main__': 88 main() 89