mirror of
https://chromium.googlesource.com/chromium/tools/depot_tools.git
synced 2026-01-11 18:51:29 +00:00
Add AffectedFile.Extension() in presubmit_support
Add convenience method AffectedFile.Extension() for extracting file extension in a standard way. Add a test for this new functionality. Add a similar test for AffectedFile.UnixLocalPath(). Bug: None Change-Id: If591e751fb2e4fb5355ad0b6f93f310667849d68 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6273341 Commit-Queue: Anton Bershanskyi <bershanskyi@gmail.com> Reviewed-by: Dirk Pranke <dpranke@google.com> Reviewed-by: Gavin Mak <gavinmak@google.com>
This commit is contained in:
committed by
LUCI CQ
parent
0a3addbf63
commit
d447f41294
@@ -150,7 +150,7 @@ def CheckCIPDManifest(input_api, output_api):
|
|||||||
tests = []
|
tests = []
|
||||||
for path in affected_manifests:
|
for path in affected_manifests:
|
||||||
path = path.AbsoluteLocalPath()
|
path = path.AbsoluteLocalPath()
|
||||||
if path.endswith('.txt'):
|
if path.Extension() == '.txt':
|
||||||
tests.append(
|
tests.append(
|
||||||
input_api.canned_checks.CheckCIPDManifest(input_api,
|
input_api.canned_checks.CheckCIPDManifest(input_api,
|
||||||
output_api,
|
output_api,
|
||||||
|
|||||||
@@ -1041,10 +1041,10 @@ class AffectedFile(object):
|
|||||||
self._path = path
|
self._path = path
|
||||||
self._action = action
|
self._action = action
|
||||||
self._local_root = repository_root
|
self._local_root = repository_root
|
||||||
self._is_directory = None
|
self._diff_cache = diff_cache
|
||||||
self._cached_changed_contents = None
|
self._cached_changed_contents = None
|
||||||
self._cached_new_contents = None
|
self._cached_new_contents = None
|
||||||
self._diff_cache = diff_cache
|
self._extension = None
|
||||||
self._is_testable_file = None
|
self._is_testable_file = None
|
||||||
logging.debug('%s(%s)', self.__class__.__name__, self._path)
|
logging.debug('%s(%s)', self.__class__.__name__, self._path)
|
||||||
|
|
||||||
@@ -1161,6 +1161,20 @@ class AffectedFile(object):
|
|||||||
self._cached_changed_contents = result
|
self._cached_changed_contents = result
|
||||||
return self._cached_changed_contents[:]
|
return self._cached_changed_contents[:]
|
||||||
|
|
||||||
|
def Extension(self):
|
||||||
|
"""Returns the file extension as a string.
|
||||||
|
|
||||||
|
File extension is the portion of file name after the last dot, including the dot.
|
||||||
|
If file name has no dot (like 'OWNERS'), then returns an empty string ''.
|
||||||
|
If file has multiple extensions (like 'archive.tar.gz'), then the last
|
||||||
|
extension is returned (like '.gz')
|
||||||
|
This method is equivalent to NodeJS node:path method path.extname()
|
||||||
|
and Python3 pathlib PurePath.suffix() and os.path.splitext()
|
||||||
|
"""
|
||||||
|
if self._extension is None:
|
||||||
|
self._extension = os.path.splitext(self._path)[1]
|
||||||
|
return self._extension
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.LocalPath()
|
return self.LocalPath()
|
||||||
|
|
||||||
|
|||||||
@@ -1444,9 +1444,8 @@ class InputApiUnittest(PresubmitTestsBase):
|
|||||||
self.assertEqual(results[i].LocalPath(),
|
self.assertEqual(results[i].LocalPath(),
|
||||||
presubmit.normpath(item[1][i]))
|
presubmit.normpath(item[1][i]))
|
||||||
# Same number of expected results.
|
# Same number of expected results.
|
||||||
self.assertEqual(
|
self.assertEqual(sorted([f.UnixLocalPath() for f in results]),
|
||||||
sorted([f.LocalPath().replace(os.sep, '/') for f in results]),
|
sorted(item[1]))
|
||||||
sorted(item[1]))
|
|
||||||
|
|
||||||
def testDefaultOverrides(self):
|
def testDefaultOverrides(self):
|
||||||
input_api = presubmit.InputApi(self.fake_change, './PRESUBMIT.py',
|
input_api = presubmit.InputApi(self.fake_change, './PRESUBMIT.py',
|
||||||
@@ -1792,6 +1791,42 @@ class AffectedFileUnittest(PresubmitTestsBase):
|
|||||||
self.assertEqual(2, len(output))
|
self.assertEqual(2, len(output))
|
||||||
self.assertEqual(files[:2], output[:2])
|
self.assertEqual(files[:2], output[:2])
|
||||||
|
|
||||||
|
def testGetUnixLocalPath(self):
|
||||||
|
# If current platform already uses Unix-style paths,
|
||||||
|
# there is nothing to test
|
||||||
|
if os.path.sep == '/':
|
||||||
|
return
|
||||||
|
|
||||||
|
# If path separator is not forward slash, then we are on Windows and
|
||||||
|
# which uses backward slash
|
||||||
|
self.assertEqual('\\', os.path.sep)
|
||||||
|
|
||||||
|
cases = [('foo/blat.txt', 'foo/blat.txt'),
|
||||||
|
('foo\\blat.txt', 'foo/blat.txt'),
|
||||||
|
('C:\\development\\src\\chrome\\VERSION',
|
||||||
|
'C:/development/src/chrome/VERSION')]
|
||||||
|
for path, expectedUnixLocalPath in cases:
|
||||||
|
unixLocalPath = presubmit.GitAffectedFile(path, 'M',
|
||||||
|
self.fake_root_dir,
|
||||||
|
None).UnixLocalPath()
|
||||||
|
self.assertEqual(expectedUnixLocalPath, unixLocalPath)
|
||||||
|
|
||||||
|
def testGetExtension(self):
|
||||||
|
cases = [('foo/blat.txt', '.txt'), ('net/features.gni', '.gni'),
|
||||||
|
('archive.tar.gz', '.gz'), ('sub/archive.tar.gz', '.gz'),
|
||||||
|
('.hidden', ''), ('sub/.hidden', ''), ('OWNERS', '')]
|
||||||
|
|
||||||
|
# If current platform uses Windows-style paths, check them too
|
||||||
|
if os.path.sep != '/':
|
||||||
|
cases.append(('foo\\blat.txt', '.txt'))
|
||||||
|
cases.append(('C:\\development\\src\\chrome\\VERSION', ''))
|
||||||
|
cases.append(('C:\\development\\src\\.hidden', ''))
|
||||||
|
|
||||||
|
for path, expectedExtension in cases:
|
||||||
|
extension = presubmit.GitAffectedFile(path, 'M', self.fake_root_dir,
|
||||||
|
None).Extension()
|
||||||
|
self.assertEqual(expectedExtension, extension)
|
||||||
|
|
||||||
|
|
||||||
class ChangeUnittest(PresubmitTestsBase):
|
class ChangeUnittest(PresubmitTestsBase):
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user