Merge pull request #291 from iam/root_flag_outer_directory

cpplint: --root now supports non-subdirectories
This commit is contained in:
Mark Mentovai 2017-11-08 17:36:27 -05:00 committed by GitHub
commit d9e5e4dcb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 7 deletions

64
cpplint/cpplint.py vendored
View File

@ -1754,6 +1754,30 @@ def GetIndentLevel(line):
else: else:
return 0 return 0
def PathSplitToList(path):
"""Returns the path split into a list by the separator.
Args:
path: An absolute or relative path (e.g. '/a/b/c/' or '../a')
Returns:
A list of path components (e.g. ['a', 'b', 'c]).
"""
lst = []
while True:
(head, tail) = os.path.split(path)
if head == path: # absolute paths end
lst.append(head)
break
if tail == path: # relative paths end
lst.append(tail)
break
path = head
lst.append(tail)
lst.reverse()
return lst
def GetHeaderGuardCPPVariable(filename): def GetHeaderGuardCPPVariable(filename):
"""Returns the CPP variable that should be used as a header guard. """Returns the CPP variable that should be used as a header guard.
@ -1776,13 +1800,39 @@ def GetHeaderGuardCPPVariable(filename):
fileinfo = FileInfo(filename) fileinfo = FileInfo(filename)
file_path_from_root = fileinfo.RepositoryName() file_path_from_root = fileinfo.RepositoryName()
if _root:
suffix = os.sep def FixupPathFromRoot():
# On Windows using directory separator will leave us with # Process the file path with the --root flag if it was set.
# "bogus escape error" unless we properly escape regex. if not _root:
if suffix == '\\': return file_path_from_root
suffix += '\\'
file_path_from_root = re.sub('^' + _root + suffix, '', file_path_from_root) def StripListPrefix(lst, prefix):
# f(['x', 'y'], ['w, z']) -> None (not a valid prefix)
if lst[:len(prefix)] != prefix:
return None
# f(['a, 'b', 'c', 'd'], ['a', 'b']) -> ['c', 'd']
return lst[(len(prefix)):]
# root behavior:
# --root=subdir , lstrips subdir from the header guard
maybe_path = StripListPrefix(PathSplitToList(file_path_from_root),
PathSplitToList(_root))
if maybe_path:
return os.path.join(*maybe_path)
# --root=.. , will prepend the outer directory to the header guard
full_path = fileinfo.FullName()
root_abspath = os.path.abspath(_root)
maybe_path = StripListPrefix(PathSplitToList(full_path),
PathSplitToList(root_abspath))
if maybe_path:
return os.path.join(*maybe_path)
# --root=FAKE_DIR is ignored
return file_path_from_root
file_path_from_root = FixupPathFromRoot()
return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_' return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_'

View File

@ -4232,6 +4232,12 @@ class CpplintTest(CpplintTestBase):
self.assertEquals('CPPLINT_CPPLINT_TEST_HEADER_H_', self.assertEquals('CPPLINT_CPPLINT_TEST_HEADER_H_',
cpplint.GetHeaderGuardCPPVariable(file_path)) cpplint.GetHeaderGuardCPPVariable(file_path))
#
# test --root flags:
# this changes the cpp header guard prefix
#
# left-strip the header guard by using a root dir inside of the repo dir.
cpplint._root = 'cpplint' cpplint._root = 'cpplint'
self.assertEquals('CPPLINT_TEST_HEADER_H_', self.assertEquals('CPPLINT_TEST_HEADER_H_',
cpplint.GetHeaderGuardCPPVariable(file_path)) cpplint.GetHeaderGuardCPPVariable(file_path))
@ -4240,6 +4246,51 @@ class CpplintTest(CpplintTestBase):
self.assertEquals('CPPLINT_CPPLINT_TEST_HEADER_H_', self.assertEquals('CPPLINT_CPPLINT_TEST_HEADER_H_',
cpplint.GetHeaderGuardCPPVariable(file_path)) cpplint.GetHeaderGuardCPPVariable(file_path))
# prepend to the header guard by using a root dir that is more outer
# than the repo dir
# (using absolute paths)
this_files_path = os.path.dirname(os.path.abspath(__file__))
(styleguide_path, this_files_dir) = os.path.split(this_files_path)
(styleguide_parent_path, _) = os.path.split(styleguide_path)
# parent dir of styleguide
cpplint._root = styleguide_parent_path
self.assertIsNotNone(styleguide_parent_path)
# do not have 'styleguide' repo in '/'
self.assertEquals('STYLEGUIDE_CPPLINT_CPPLINT_TEST_HEADER_H_',
cpplint.GetHeaderGuardCPPVariable(file_path))
# (using relative paths)
styleguide_rel_path = os.path.relpath(styleguide_path, this_files_path)
# '..'
cpplint._root = styleguide_rel_path
self.assertEquals('CPPLINT_CPPLINT_TEST_HEADER_H_',
cpplint.GetHeaderGuardCPPVariable(file_path))
styleguide_rel_path = os.path.relpath(styleguide_parent_path,
this_files_path) # '../..'
cpplint._root = styleguide_rel_path
self.assertEquals('STYLEGUIDE_CPPLINT_CPPLINT_TEST_HEADER_H_',
cpplint.GetHeaderGuardCPPVariable(file_path))
cpplint._root = None
def testPathSplitToList(self):
self.assertEquals([''],
cpplint.PathSplitToList(os.path.join('')))
self.assertEquals(['.'],
cpplint.PathSplitToList(os.path.join('.')))
self.assertEquals(['..'],
cpplint.PathSplitToList(os.path.join('..')))
self.assertEquals(['..', 'a', 'b'],
cpplint.PathSplitToList(os.path.join('..', 'a', 'b')))
self.assertEquals(['a', 'b', 'c', 'd'],
cpplint.PathSplitToList(os.path.join('a', 'b', 'c', 'd')))
def testBuildInclude(self): def testBuildInclude(self):
# Test that include statements have slashes in them. # Test that include statements have slashes in them.
self.TestLint('#include "foo.h"', self.TestLint('#include "foo.h"',