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 presubmit tools.""" 16 17import dataclasses 18import re 19import unittest 20 21from pw_presubmit import presubmit 22 23 24class TestFileFilter(unittest.TestCase): 25 """Test FileFilter class""" 26 27 @dataclasses.dataclass 28 class TestData: 29 filter: presubmit.FileFilter 30 value: str 31 expected: bool 32 33 test_scenarios = ( 34 TestData(presubmit.FileFilter(endswith=('bar', 'foo')), 'foo', True), 35 TestData(presubmit.FileFilter(endswith=('bar', 'boo')), 'foo', False), 36 TestData( 37 presubmit.FileFilter(exclude=(re.compile('a/.+'),), name=('foo',)), 38 '/a/b/c/foo', 39 False, 40 ), 41 TestData( 42 presubmit.FileFilter(exclude=(re.compile('x/.+'),), name=('foo',)), 43 '/a/b/c/foo', 44 True, 45 ), 46 TestData( 47 presubmit.FileFilter(exclude=(re.compile('a+'), re.compile('b+'))), 48 'cccc', 49 True, 50 ), 51 TestData(presubmit.FileFilter(name=('foo',)), 'foo', True), 52 TestData(presubmit.FileFilter(name=('foo',)), 'food', False), 53 TestData(presubmit.FileFilter(name=(re.compile('foo'),)), 'foo', True), 54 TestData( 55 presubmit.FileFilter(name=(re.compile('foo'),)), 'food', False 56 ), 57 TestData(presubmit.FileFilter(name=(re.compile('fo+'),)), 'foo', True), 58 TestData(presubmit.FileFilter(name=(re.compile('fo+'),)), 'fd', False), 59 TestData( 60 presubmit.FileFilter(suffix=('.exe',)), 'a/b.py/foo.exe', True 61 ), 62 TestData( 63 presubmit.FileFilter(suffix=('.py',)), 'a/b.py/foo.exe', False 64 ), 65 TestData( 66 presubmit.FileFilter(suffix=('.exe',)), 'a/b.py/foo.py.exe', True 67 ), 68 TestData( 69 presubmit.FileFilter(suffix=('.py',)), 'a/b.py/foo.py.exe', False 70 ), 71 TestData(presubmit.FileFilter(suffix=('.a', '.b')), 'foo.b', True), 72 TestData(presubmit.FileFilter(suffix=('.a', '.b')), 'foo.c', False), 73 ) 74 75 def test_matches(self): 76 for test_num, test_data in enumerate(self.test_scenarios): 77 with self.subTest(i=test_num): 78 self.assertEqual( 79 test_data.filter.matches(test_data.value), 80 test_data.expected, 81 ) 82 83 84def _fake_function_1(_): 85 """Fake presubmit function.""" 86 87 88def _fake_function_2(_): 89 """Fake presubmit function.""" 90 91 92def _all_substeps(program): 93 substeps = {} 94 for step in program: 95 # pylint: disable=protected-access 96 for sub in step.substeps(): 97 substeps[sub.name or step.name] = sub._func 98 # pylint: enable=protected-access 99 return substeps 100 101 102class ProgramsTest(unittest.TestCase): 103 """Tests the presubmit Programs abstraction.""" 104 105 def setUp(self): 106 self._programs = presubmit.Programs( 107 first=[_fake_function_1, (), [(_fake_function_2,)]], 108 second=[_fake_function_2], 109 ) 110 111 def test_empty(self): 112 self.assertEqual({}, presubmit.Programs()) 113 114 def test_access_present_members_first(self): 115 self.assertEqual('first', self._programs['first'].name) 116 self.assertEqual( 117 ('_fake_function_1', '_fake_function_2'), 118 tuple(x.name for x in self._programs['first']), 119 ) 120 121 self.assertEqual(2, len(self._programs['first'])) 122 substeps = _all_substeps( 123 self._programs['first'] # pylint: disable=protected-access 124 ).values() 125 self.assertEqual(2, len(substeps)) 126 self.assertEqual((_fake_function_1, _fake_function_2), tuple(substeps)) 127 128 def test_access_present_members_second(self): 129 self.assertEqual('second', self._programs['second'].name) 130 self.assertEqual( 131 ('_fake_function_2',), 132 tuple(x.name for x in self._programs['second']), 133 ) 134 135 self.assertEqual(1, len(self._programs['second'])) 136 substeps = _all_substeps( 137 self._programs['second'] # pylint: disable=protected-access 138 ).values() 139 self.assertEqual(1, len(substeps)) 140 self.assertEqual((_fake_function_2,), tuple(substeps)) 141 142 def test_access_missing_member(self): 143 with self.assertRaises(KeyError): 144 _ = self._programs['not_there'] 145 146 def test_all_steps(self): 147 all_steps = self._programs.all_steps() 148 self.assertEqual(len(all_steps), 2) 149 all_substeps = _all_substeps(all_steps.values()) 150 self.assertEqual(len(all_substeps), 2) 151 152 # pylint: disable=protected-access 153 self.assertEqual(all_substeps['_fake_function_1'], _fake_function_1) 154 self.assertEqual(all_substeps['_fake_function_2'], _fake_function_2) 155 # pylint: enable=protected-access 156 157 158class ContextTest(unittest.TestCase): 159 def test_presubmitcontext(self): # pylint: disable=no-self-use 160 _ = presubmit.PresubmitContext.create_for_testing() 161 162 def test_lucicontext(self): # pylint: disable=no-self-use 163 _ = presubmit.LuciContext.create_for_testing() 164 165 def test_lucitrigger(self): # pylint: disable=no-self-use 166 _ = presubmit.LuciTrigger.create_for_testing() 167 168 169if __name__ == '__main__': 170 unittest.main() 171