Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 2 | # Copyright 2015, Google Inc. |
| 3 | # All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 31 | """Filter out tests based on file differences compared to merge target branch""" |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 32 | |
| 33 | from subprocess import call, check_output |
| 34 | |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 35 | # Whitelist for all tests |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame^] | 36 | # If whitelist item should only trigger some tests, the item should be |
| 37 | # added to this list and the trigger list of tests that should be run |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 38 | starts_with_whitelist = ['templates/', |
| 39 | 'doc/', |
| 40 | 'examples/', |
| 41 | 'summerofcode/', |
| 42 | 'src/cpp', |
| 43 | 'src/csharp', |
| 44 | 'src/node', |
| 45 | 'src/objective-c', |
| 46 | 'src/php', |
| 47 | 'src/python', |
| 48 | 'src/ruby', |
| 49 | 'test/core', |
| 50 | 'test/cpp', |
| 51 | 'test/distrib/cpp', |
| 52 | 'test/distrib/csharp', |
| 53 | 'test/distrib/node', |
| 54 | 'test/distrib/php', |
| 55 | 'test/distrib/python', |
| 56 | 'test/distrib/ruby'] |
| 57 | |
| 58 | ends_with_whitelist = ['README.md', |
| 59 | 'LICENSE'] |
| 60 | |
| 61 | # Triggers for core tests |
| 62 | core_starts_with_triggers = ['test/core'] |
| 63 | |
| 64 | # Triggers for c++ tests |
| 65 | cpp_starts_with_triggers = ['src/cpp', |
| 66 | 'test/cpp', |
| 67 | 'test/distrib/cpp'] |
| 68 | |
| 69 | # Triggers for c# tests |
| 70 | csharp_starts_with_triggers = ['src/csharp', |
| 71 | 'test/distrib/csharp'] |
| 72 | |
| 73 | # Triggers for node tests |
| 74 | node_starts_with_triggers = ['src/node', |
| 75 | 'test/distrib/node'] |
| 76 | |
| 77 | # Triggers for objective-c tests |
| 78 | objc_starts_with_triggers = ['src/objective-c'] |
| 79 | |
| 80 | # Triggers for php tests |
| 81 | php_starts_with_triggers = ['src/php', |
| 82 | 'test/distrib/php'] |
| 83 | |
| 84 | # Triggers for python tests |
| 85 | python_starts_with_triggers = ['src/python', |
| 86 | 'test/distrib/python'] |
| 87 | |
| 88 | # Triggers for ruby tests |
| 89 | ruby_starts_with_triggers = ['src/ruby', |
| 90 | 'test/distrib/ruby'] |
| 91 | |
| 92 | |
| 93 | def _filter_whitelist(whitelist, triggers): |
| 94 | """ |
| 95 | Removes triggers from whitelist |
| 96 | :param whitelist: list to remove values from |
| 97 | :param triggers: list of values to remove from whitelist |
| 98 | :return: filtered whitelist |
| 99 | """ |
| 100 | filtered_whitelist = list(whitelist) |
| 101 | for trigger in triggers: |
| 102 | if trigger in filtered_whitelist: |
| 103 | filtered_whitelist.remove(trigger) |
| 104 | else: |
| 105 | """ |
| 106 | If the trigger is not found in the whitelist, then there is likely |
| 107 | a mistake in the whitelist or trigger list, which needs to be addressed |
| 108 | to not wrongly skip tests |
| 109 | """ |
| 110 | print("ERROR: '%s' trigger not in whitelist. Please fix this!" % trigger) |
| 111 | return filtered_whitelist |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 112 | |
| 113 | |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame^] | 114 | def _get_changed_files(base_branch): |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 115 | """ |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 116 | Get list of changed files between current branch and base of target merge branch |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 117 | """ |
| 118 | # git fetch might need to be called on Jenkins slave |
| 119 | # todo(mattkwong): remove or uncomment below after seeing if Jenkins needs this |
| 120 | # call(['git', 'fetch']) |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame^] | 121 | |
| 122 | # get file changes between branch and merge-base of specified branch |
| 123 | # not combined to be Windows friendly |
| 124 | base_commit = check_output(["git", "merge-base", base_branch, "HEAD"]).rstrip() |
| 125 | return check_output(["git", "diff", base_commit, "--name-only"]).splitlines() |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 126 | |
| 127 | |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 128 | def _can_skip_tests(file_names, starts_with_whitelist=[], ends_with_whitelist=[]): |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 129 | """ |
| 130 | Determines if tests are skippable based on if all file names do not match |
| 131 | any begin or end triggers |
| 132 | :param file_names: list of changed files generated by _get_changed_files() |
| 133 | :param starts_with_triggers: tuple of strings to match with beginning of file names |
| 134 | :param ends_with_triggers: tuple of strings to match with end of file names |
| 135 | :return: safe to skip tests |
| 136 | """ |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 137 | # convert lists to tuple to pass into str.startswith() and str.endswith() |
| 138 | starts_with_whitelist = tuple(starts_with_whitelist) |
| 139 | ends_with_whitelist = tuple(ends_with_whitelist) |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 140 | for file_name in file_names: |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 141 | if starts_with_whitelist and not file_name.startswith(starts_with_whitelist) and \ |
| 142 | ends_with_whitelist and not file_name.endswith(ends_with_whitelist): |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 143 | return False |
| 144 | return True |
| 145 | |
| 146 | |
| 147 | def _remove_irrelevant_tests(tests, tag): |
| 148 | """ |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame^] | 149 | Filters out tests by config or language - will not remove sanitizer tests |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 150 | :param tests: list of all tests generated by run_tests_matrix.py |
| 151 | :param tag: string representing language or config to filter - "_(language)_" or "_(config)" |
| 152 | :return: list of relevant tests |
| 153 | """ |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 154 | # todo(mattkwong): find a more reliable way to filter tests - don't use shortname |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame^] | 155 | return [test for test in tests if |
| 156 | tag not in test.shortname or |
| 157 | '_msan' in test.shortname or |
| 158 | '_asan' in test.shortname or |
| 159 | '_tsan' in test.shortname] |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 160 | |
| 161 | |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame^] | 162 | def _remove_irrelevant_sanitizer_tests(tests, language_tag=""): |
| 163 | """ |
| 164 | Filters out sanitizer tests - can specify a language to filter - this should be c++ only |
| 165 | :param tests: list of all tests generated by run_tests_matrix.py |
| 166 | :param language_tag: string specifying a language from which to filter sanitizer tests - "_(language)_" |
| 167 | :return: list of relevant tests |
| 168 | """ |
| 169 | if language_tag: |
| 170 | return [test for test in tests if not language_tag in test.shortname and |
| 171 | not '_asan' in test.shortname and |
| 172 | not '_msan' in test.shortname and |
| 173 | not '_tsan' in test.shortname] |
| 174 | else: |
| 175 | return [test for test in tests if |
| 176 | '_asan' not in test.shortname and |
| 177 | '_msan' not in test.shortname and |
| 178 | '_tsan' not in test.shortname] |
| 179 | |
| 180 | def filter_tests(tests, base_branch): |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 181 | """ |
| 182 | Filters out tests that are safe to ignore |
| 183 | :param tests: list of all tests generated by run_tests_matrix.py |
| 184 | :return: list of relevant tests |
| 185 | """ |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame^] | 186 | print("Finding file differences between %s repo and current branch..." % base_branch) |
| 187 | changed_files = _get_changed_files(base_branch) |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 188 | for changed_file in changed_files: |
| 189 | print(changed_file) |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 190 | |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 191 | # Filter core tests |
| 192 | skip_core = _can_skip_tests(changed_files, |
| 193 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, core_starts_with_triggers), |
| 194 | ends_with_whitelist=ends_with_whitelist) |
| 195 | if skip_core: |
| 196 | tests = _remove_irrelevant_tests(tests, '_c_') |
| 197 | |
| 198 | # Filter c++ tests |
| 199 | skip_cpp = _can_skip_tests(changed_files, |
| 200 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, cpp_starts_with_triggers), |
| 201 | ends_with_whitelist=ends_with_whitelist) |
| 202 | if skip_cpp: |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame^] | 203 | tests = _remove_irrelevant_tests(tests, '_c++_') |
| 204 | tests = _remove_irrelevant_sanitizer_tests(tests, language_tag='_c++_') |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 205 | |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame^] | 206 | # Sanitizer tests skipped if core and c++ are skipped |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 207 | if skip_core and skip_cpp: |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame^] | 208 | tests = _remove_irrelevant_sanitizer_tests(tests) |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 209 | |
| 210 | # Filter c# tests |
| 211 | skip_csharp = _can_skip_tests(changed_files, |
| 212 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, csharp_starts_with_triggers), |
| 213 | ends_with_whitelist=ends_with_whitelist) |
| 214 | if skip_csharp: |
| 215 | tests = _remove_irrelevant_tests(tests, '_csharp_') |
| 216 | |
| 217 | # Filter node tests |
| 218 | skip_node = _can_skip_tests(changed_files, |
| 219 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, node_starts_with_triggers), |
| 220 | ends_with_whitelist=ends_with_whitelist) |
| 221 | if skip_node: |
| 222 | tests = _remove_irrelevant_tests(tests, '_node_') |
| 223 | |
| 224 | # Filter objc tests |
| 225 | skip_objc = _can_skip_tests(changed_files, |
| 226 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, objc_starts_with_triggers), |
| 227 | ends_with_whitelist=ends_with_whitelist) |
| 228 | if skip_objc: |
| 229 | tests = _remove_irrelevant_tests(tests, '_objc_') |
| 230 | |
| 231 | # Filter php tests |
| 232 | skip_php = _can_skip_tests(changed_files, |
| 233 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, php_starts_with_triggers), |
| 234 | ends_with_whitelist=ends_with_whitelist) |
| 235 | if skip_php: |
| 236 | tests = _remove_irrelevant_tests(tests, '_php_') |
Matt Kwong | f3f2872 | 2016-10-07 15:25:56 -0700 | [diff] [blame^] | 237 | tests = _remove_irrelevant_tests(tests, '_php7_') |
Matt Kwong | 037704d | 2016-10-06 18:18:11 -0700 | [diff] [blame] | 238 | |
| 239 | # Filter python tests |
| 240 | skip_python = _can_skip_tests(changed_files, |
| 241 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, python_starts_with_triggers), |
| 242 | ends_with_whitelist=ends_with_whitelist) |
| 243 | if skip_python: |
| 244 | tests = _remove_irrelevant_tests(tests, '_python_') |
| 245 | |
| 246 | # Filter ruby tests |
| 247 | skip_ruby = _can_skip_tests(changed_files, |
| 248 | starts_with_whitelist=_filter_whitelist(starts_with_whitelist, ruby_starts_with_triggers), |
| 249 | ends_with_whitelist=ends_with_whitelist) |
| 250 | if skip_ruby: |
| 251 | tests = _remove_irrelevant_tests(tests, '_ruby_') |
| 252 | |
Matt Kwong | e9163f0 | 2016-10-05 11:42:55 -0700 | [diff] [blame] | 253 | return tests |