blob: 6cc06d9c4094c5f5f17bae0f895ef4d6b7eb7c3e [file] [log] [blame]
Matt Kwonge9163f02016-10-05 11:42:55 -07001#!/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
31"""Filter out tests based on file differences compared to grpc:master"""
32
33from subprocess import call, check_output
34
35# triggers to skip c++ tests
36run_cpp_starts_with_triggers = ('src/core',
37 'src/cpp',
38 'test/core',
39 'test/cpp')
40
41
42def _get_changed_files():
43 """
44 Get list of changed files between current branch and gRPC master branch
45 """
46 # git fetch might need to be called on Jenkins slave
47 # todo(mattkwong): remove or uncomment below after seeing if Jenkins needs this
48 # call(['git', 'fetch'])
49 # this also collects files that are changed in the repo but not updated in the branch
50 return check_output(["git", "diff", "--name-only", "..origin/master"]).split()
51
52
53def _can_skip_tests(file_names, starts_with_triggers=(), ends_with_triggers=()):
54 """
55 Determines if tests are skippable based on if all file names do not match
56 any begin or end triggers
57 :param file_names: list of changed files generated by _get_changed_files()
58 :param starts_with_triggers: tuple of strings to match with beginning of file names
59 :param ends_with_triggers: tuple of strings to match with end of file names
60 :return: safe to skip tests
61 """
62 for file_name in file_names:
63 if starts_with_triggers and file_name.startswith(starts_with_triggers) or \
64 ends_with_triggers and file_name.endswith(ends_with_triggers):
65 return False
66 return True
67
68
69def _remove_irrelevant_tests(tests, tag):
70 """
71 Filters out tests by config or language
72 :param tests: list of all tests generated by run_tests_matrix.py
73 :param tag: string representing language or config to filter - "_(language)_" or "_(config)"
74 :return: list of relevant tests
75 """
76 return [test for test in tests if not tag in test.shortname]
77
78
79def filter_tests(tests):
80 """
81 Filters out tests that are safe to ignore
82 :param tests: list of all tests generated by run_tests_matrix.py
83 :return: list of relevant tests
84 """
85 print("Finding file differences between grpc:master repo and pull request...")
86 changed_files = _get_changed_files()
87 for changed_file in changed_files:
88 print(changed_file)
89 # C++, tsan, msan, and asan have the same filter
90 if _can_skip_tests(changed_files, starts_with_triggers=run_cpp_starts_with_triggers):
91 tests = _remove_irrelevant_tests(tests, '_c++_') # filter out c++ tests
92 tests = _remove_irrelevant_tests(tests, '_tsan') # filter out tsan tests
93 tests = _remove_irrelevant_tests(tests, '_msan') # filter out msan tests
94 tests = _remove_irrelevant_tests(tests, '_asan') # filter out asan tests
95 return tests