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"""git repo module tests""" 16 17from unittest import mock 18import re 19import pathlib 20import unittest 21 22from pw_presubmit import git_repo 23 24 25class TestGitRepo(unittest.TestCase): 26 """Tests for git_repo.py""" 27 28 GIT_ROOT = pathlib.Path("/dev/null/test") 29 SUBMODULES = [ 30 pathlib.Path("third_party/pigweed"), 31 pathlib.Path("vendor/anycom/p1"), 32 pathlib.Path("vendor/anycom/p2"), 33 ] 34 GIT_SUBMODULES_OUT = "\n".join([str(x) for x in SUBMODULES]) 35 36 def setUp(self) -> None: 37 self.git_stdout = mock.patch.object( 38 git_repo, "git_stdout", autospec=True 39 ).start() 40 self.git_stdout.return_value = self.GIT_SUBMODULES_OUT 41 self.root = mock.patch.object(git_repo, "root", autospec=True).start() 42 self.root.return_value = self.GIT_ROOT 43 super().setUp() 44 45 def tearDown(self) -> None: 46 mock.patch.stopall() 47 super().tearDown() 48 49 def test_mock_root(self): 50 """Ensure our mock works since so many of our tests depend upon it.""" 51 self.assertEqual(git_repo.root(), self.GIT_ROOT) 52 53 def test_discover_submodules_1(self): 54 paths = git_repo.discover_submodules(superproject_dir=self.GIT_ROOT) 55 self.assertIn(self.GIT_ROOT, paths) 56 57 def test_discover_submodules_2(self): 58 paths = git_repo.discover_submodules(superproject_dir=self.GIT_ROOT) 59 self.assertIn(self.SUBMODULES[2], paths) 60 61 def test_discover_submodules_with_exclude_str(self): 62 paths = git_repo.discover_submodules( 63 superproject_dir=self.GIT_ROOT, 64 excluded_paths=(self.GIT_ROOT.as_posix(),), 65 ) 66 self.assertNotIn(self.GIT_ROOT, paths) 67 68 def test_discover_submodules_with_exclude_regex(self): 69 paths = git_repo.discover_submodules( 70 superproject_dir=self.GIT_ROOT, 71 excluded_paths=(re.compile("third_party/.*"),), 72 ) 73 self.assertNotIn(self.SUBMODULES[0], paths) 74 75 def test_discover_submodules_with_exclude_str_miss(self): 76 paths = git_repo.discover_submodules( 77 superproject_dir=self.GIT_ROOT, 78 excluded_paths=(re.compile("pigweed"),), 79 ) 80 self.assertIn(self.SUBMODULES[-1], paths) 81 82 def test_discover_submodules_with_exclude_regex_miss_1(self): 83 paths = git_repo.discover_submodules( 84 superproject_dir=self.GIT_ROOT, 85 excluded_paths=(re.compile("foo/.*"),), 86 ) 87 self.assertIn(self.GIT_ROOT, paths) 88 for module in self.SUBMODULES: 89 self.assertIn(module, paths) 90 91 def test_discover_submodules_with_exclude_regex_miss_2(self): 92 paths = git_repo.discover_submodules( 93 superproject_dir=self.GIT_ROOT, 94 excluded_paths=(re.compile("pigweed"),), 95 ) 96 self.assertIn(self.GIT_ROOT, paths) 97 for module in self.SUBMODULES: 98 self.assertIn(module, paths) 99 100 101if __name__ == '__main__': 102 unittest.main() 103