1#!/usr/bin/env python3 2# Copyright 2020 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 for clang_tidy.""" 16 17import pathlib 18import unittest 19from unittest import mock 20 21from pw_toolchain import clang_tidy 22 23 24class ClangTidyTest(unittest.TestCase): 25 """Unit tests for the clang-tidy wrapper.""" 26 @mock.patch('subprocess.run', autospec=True) 27 def test_source_exclude_filters(self, mock_run): 28 # Build the path using joinpath to use OS-appropriate separators on both 29 # Windows and Linux. 30 source_file = pathlib.Path('..').joinpath('third_party').joinpath( 31 'somefile.cc') 32 source_root = pathlib.Path('..') 33 source_exclude = ['third_party.*'] 34 got = clang_tidy.main(False, 'clang-tidy', source_file, source_root, 35 None, source_exclude, list(), list()) 36 37 # Return code is zero. 38 self.assertEqual(got, 0) 39 # No calls to subprocess: we filtered out the source file. 40 self.assertEqual(len(mock_run.mock_calls), 0) 41 42 @mock.patch('subprocess.run', autospec=True) 43 def test_source_exclude_does_not_filter(self, mock_run): 44 mock_run.return_value.returncode = 0 45 source_file = pathlib.Path('..').joinpath('third_party').joinpath( 46 'somefile.cc') 47 source_root = pathlib.Path('..') 48 source_exclude = ['someotherdir.*'] 49 got = clang_tidy.main(False, 'clang-tidy', source_file, source_root, 50 None, source_exclude, list(), list()) 51 52 # Return code is zero. 53 self.assertEqual(got, 0) 54 # One call to subprocess: we did not filter out the source file. 55 # There will be more than one mock call because accessing return value 56 # attributes also produces mock_calls. 57 self.assertGreater(len(mock_run.mock_calls), 0) 58 59 60if __name__ == '__main__': 61 unittest.main() 62