1# Copyright 2024 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Tests for pw_cli.decorators.""" 15 16# Redefine __package__ so we can test against a real package name. 17__package__ = 'pw_cli.tests' # pylint: disable=redefined-builtin 18 19import os 20import re 21import unittest 22import warnings 23 24from pw_cli.decorators import deprecated 25 26 27_VAL = 5 28 29 30def _not_deprecated() -> int: 31 return _VAL 32 33 34@deprecated('Use `_not_deprecated()`') 35def _deprecated_func() -> int: 36 return _not_deprecated() 37 38 39class TestDeprecationAnnotation(unittest.TestCase): 40 """Tests for @deprecated annotation.""" 41 42 def test_deprecated(self): 43 expected_file_path = os.path.sep.join( 44 ('pw_cli', 'py', 'decorators_test.py') 45 ) 46 expected_warning_re = ' '.join( 47 ( 48 re.escape(expected_file_path) + r':[0-9]*:', 49 r'pw_cli\.tests\.decorators_test\._deprecated_func\(\)', 50 r'is deprecated\.', 51 r'Use `_not_deprecated\(\)`', 52 ) 53 ) 54 55 with warnings.catch_warnings(record=True) as caught_warnings: 56 warnings.simplefilter("always") 57 n = _deprecated_func() 58 self.assertEqual(n, _VAL) 59 self.assertEqual(len(caught_warnings), 1) 60 61 self.assertRegex( 62 str(caught_warnings[0].message), re.compile(expected_warning_re) 63 ) 64 65 66if __name__ == '__main__': 67 unittest.main() 68