Commit 40a3d0b6 authored by agable@chromium.org's avatar agable@chromium.org

Recursively find all tests in a git repo.

This method is necessary to allow good code organization (i.e., unittests live
adjacent to the source files they test) in the new infra.git repository.

R=maruel@chromium.org

Review URL: https://codereview.chromium.org/281013002

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@270564 0039d316-1c4b-4281-b951-d872f2087c98
parent 46b87414
...@@ -562,6 +562,36 @@ def GetUnitTests(input_api, output_api, unit_tests, env=None): ...@@ -562,6 +562,36 @@ def GetUnitTests(input_api, output_api, unit_tests, env=None):
return results return results
def GetUnitTestsRecursively(input_api, output_api, directory,
whitelist, blacklist):
"""Gets all files in the directory tree (git repo) that match the whitelist.
Restricts itself to only find files within the Change's source repo, not
dependencies.
"""
def check(filename):
return (any(input_api.re.match(f, filename) for f in whitelist) and
not any(input_api.re.match(f, filename) for f in blacklist))
tests = []
to_run = found = 0
for filepath in input_api.change.AllFiles(directory):
found += 1
if check(filepath):
to_run += 1
tests.append(filepath)
input_api.logging.debug('Found %d files, running %d' % (found, to_run))
if not to_run:
return [
output_api.PresubmitPromptWarning(
'Out of %d files, found none that matched w=%r, b=%r in directory %s'
% (found, whitelist, blacklist, directory))
]
return GetUnitTests(input_api, output_api, tests)
def GetPythonUnitTests(input_api, output_api, unit_tests): def GetPythonUnitTests(input_api, output_api, unit_tests):
"""Run the unit tests out of process, capture the output and use the result """Run the unit tests out of process, capture the output and use the result
code to determine success. code to determine success.
...@@ -744,7 +774,7 @@ def GetPylint(input_api, output_api, white_list=None, black_list=None, ...@@ -744,7 +774,7 @@ def GetPylint(input_api, output_api, white_list=None, black_list=None,
if True: if True:
return [GetPylintCmd(files)] return [GetPylintCmd(files)]
else: else:
return map(GetPylintCmd, files) return map(lambda x: GetPylintCmd([x]), files)
def RunPylint(input_api, *args, **kwargs): def RunPylint(input_api, *args, **kwargs):
......
...@@ -869,6 +869,10 @@ class Change(object): ...@@ -869,6 +869,10 @@ class Change(object):
raise AttributeError(self, attr) raise AttributeError(self, attr)
return self.tags.get(attr) return self.tags.get(attr)
def AllFiles(self, root=None):
"""List all files under source control in the repo."""
raise NotImplementedError()
def AffectedFiles(self, include_dirs=False, include_deletes=True, def AffectedFiles(self, include_dirs=False, include_deletes=True,
file_filter=None): file_filter=None):
"""Returns a list of AffectedFile instances for all files in the change. """Returns a list of AffectedFile instances for all files in the change.
...@@ -965,11 +969,23 @@ class SvnChange(Change): ...@@ -965,11 +969,23 @@ class SvnChange(Change):
return [os.path.join(self.RepositoryRoot(), f[1]) return [os.path.join(self.RepositoryRoot(), f[1])
for f in changelists[self.Name()]] for f in changelists[self.Name()]]
def AllFiles(self, root=None):
"""List all files under source control in the repo."""
root = root or self.RepositoryRoot()
return subprocess.check_output(
['svn', 'ls', '-R', '.'], cwd=root).splitlines()
class GitChange(Change): class GitChange(Change):
_AFFECTED_FILES = GitAffectedFile _AFFECTED_FILES = GitAffectedFile
scm = 'git' scm = 'git'
def AllFiles(self, root=None):
"""List all files under source control in the repo."""
root = root or self.RepositoryRoot()
return subprocess.check_output(
['git', 'ls-files', '--', '.'], cwd=root).splitlines()
def ListRelevantPresubmitFiles(files, root): def ListRelevantPresubmitFiles(files, root):
"""Finds all presubmit files that apply to a given set of source files. """Finds all presubmit files that apply to a given set of source files.
......
...@@ -1773,7 +1773,7 @@ class AffectedFileUnittest(PresubmitTestsBase): ...@@ -1773,7 +1773,7 @@ class AffectedFileUnittest(PresubmitTestsBase):
class ChangeUnittest(PresubmitTestsBase): class ChangeUnittest(PresubmitTestsBase):
def testMembersChanged(self): def testMembersChanged(self):
members = [ members = [
'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', 'AllFiles',
'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name', 'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name',
'RepositoryRoot', 'RightHandSideLines', 'ServerPaths', 'RepositoryRoot', 'RightHandSideLines', 'ServerPaths',
'SetDescriptionText', 'TAG_LINE_RE', 'SetDescriptionText', 'TAG_LINE_RE',
...@@ -1885,7 +1885,7 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -1885,7 +1885,7 @@ class CannedChecksUnittest(PresubmitTestsBase):
'RunPythonUnitTests', 'RunPylint', 'RunPythonUnitTests', 'RunPylint',
'RunUnitTests', 'RunUnitTestsInDirectory', 'RunUnitTests', 'RunUnitTestsInDirectory',
'GetPythonUnitTests', 'GetPylint', 'GetPythonUnitTests', 'GetPylint',
'GetUnitTests', 'GetUnitTestsInDirectory', 'GetUnitTests', 'GetUnitTestsInDirectory', 'GetUnitTestsRecursively',
] ]
# If this test fails, you should add the relevant test. # If this test fails, you should add the relevant test.
self.compareMembers(presubmit_canned_checks, members) self.compareMembers(presubmit_canned_checks, members)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment