Merge pull request #292 from iam/root_cpplint_cfg

CPPLINT.cfg root=setting is now relative to CFG file.
This commit is contained in:
Mark Mentovai 2017-11-09 23:10:53 -05:00 committed by GitHub
commit 8667623865
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 6 deletions

34
cpplint/cpplint.py vendored
View File

@ -114,12 +114,13 @@ Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
ignored. ignored.
Examples: Examples:
Assuming that src/.git exists, the header guard CPP variables for Assuming that top/src/.git exists (and cwd=top/src), the header guard
src/chrome/browser/ui/browser.h are: CPP variables for top/src/chrome/browser/ui/browser.h are:
No flag => CHROME_BROWSER_UI_BROWSER_H_ No flag => CHROME_BROWSER_UI_BROWSER_H_
--root=chrome => BROWSER_UI_BROWSER_H_ --root=chrome => BROWSER_UI_BROWSER_H_
--root=chrome/browser => UI_BROWSER_H_ --root=chrome/browser => UI_BROWSER_H_
--root=.. => SRC_CHROME_BROWSER_UI_BROWSER_H_
linelength=digits linelength=digits
This is the allowed line length for the project. The default value is This is the allowed line length for the project. The default value is
@ -168,9 +169,9 @@ Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
"linelength" allows to specify the allowed line length for the project. "linelength" allows to specify the allowed line length for the project.
The "root" option is similar in function to the --root flag (see example The "root" option is similar in function to the --root flag (see example
above). above). Paths are relative to the directory of the CPPLINT.cfg.
The "headers" option is similar in function to the --headers flag The "headers" option is similar in function to the --headers flag
(see example above). (see example above).
CPPLINT.cfg has an effect on files in the same directory and all CPPLINT.cfg has an effect on files in the same directory and all
@ -539,6 +540,7 @@ _error_suppressions = {}
# The root directory used for deriving header guard CPP variable. # The root directory used for deriving header guard CPP variable.
# This is set by --root flag. # This is set by --root flag.
_root = None _root = None
_root_debug = False
# The allowed line length of files. # The allowed line length of files.
# This is set by --linelength flag. # This is set by --linelength flag.
@ -1802,8 +1804,14 @@ def GetHeaderGuardCPPVariable(filename):
file_path_from_root = fileinfo.RepositoryName() file_path_from_root = fileinfo.RepositoryName()
def FixupPathFromRoot(): def FixupPathFromRoot():
if _root_debug:
sys.stderr.write("\n_root fixup, _root = '%s', repository name = '%s'\n"
%(_root, fileinfo.RepositoryName()))
# Process the file path with the --root flag if it was set. # Process the file path with the --root flag if it was set.
if not _root: if not _root:
if _root_debug:
sys.stderr.write("_root unspecified\n")
return file_path_from_root return file_path_from_root
def StripListPrefix(lst, prefix): def StripListPrefix(lst, prefix):
@ -1817,6 +1825,11 @@ def GetHeaderGuardCPPVariable(filename):
# --root=subdir , lstrips subdir from the header guard # --root=subdir , lstrips subdir from the header guard
maybe_path = StripListPrefix(PathSplitToList(file_path_from_root), maybe_path = StripListPrefix(PathSplitToList(file_path_from_root),
PathSplitToList(_root)) PathSplitToList(_root))
if _root_debug:
sys.stderr.write("_root lstrip (maybe_path=%s, file_path_from_root=%s," +
" _root=%s)\n" %(maybe_path, file_path_from_root, _root))
if maybe_path: if maybe_path:
return os.path.join(*maybe_path) return os.path.join(*maybe_path)
@ -1826,9 +1839,17 @@ def GetHeaderGuardCPPVariable(filename):
maybe_path = StripListPrefix(PathSplitToList(full_path), maybe_path = StripListPrefix(PathSplitToList(full_path),
PathSplitToList(root_abspath)) PathSplitToList(root_abspath))
if _root_debug:
sys.stderr.write("_root prepend (maybe_path=%s, full_path=%s, " +
"root_abspath=%s)\n" %(maybe_path, full_path, root_abspath))
if maybe_path: if maybe_path:
return os.path.join(*maybe_path) return os.path.join(*maybe_path)
if _root_debug:
sys.stderr.write("_root ignore, returning %s\n" %(file_path_from_root))
# --root=FAKE_DIR is ignored # --root=FAKE_DIR is ignored
return file_path_from_root return file_path_from_root
@ -5947,7 +5968,8 @@ def ProcessConfigOverrides(filename):
sys.stderr.write('Line length must be numeric.') sys.stderr.write('Line length must be numeric.')
elif name == 'root': elif name == 'root':
global _root global _root
_root = val # root directories are specified relative to CPPLINT.cfg dir.
_root = os.path.join(os.path.dirname(cfg_file), val)
elif name == 'headers': elif name == 'headers':
ProcessHppHeadersOption(val) ProcessHppHeadersOption(val)
else: else:

View File

@ -4218,6 +4218,8 @@ class CpplintTest(CpplintTestBase):
error_collector.ResultList()) error_collector.ResultList())
def testBuildHeaderGuardWithRoot(self): def testBuildHeaderGuardWithRoot(self):
# note: Tested file paths must be real, otherwise
# the repository name lookup will fail.
file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'cpplint_test_header.h') 'cpplint_test_header.h')
file_info = cpplint.FileInfo(file_path) file_info = cpplint.FileInfo(file_path)
@ -4238,9 +4240,33 @@ class CpplintTest(CpplintTestBase):
# #
# left-strip the header guard by using a root dir inside of the repo dir. # left-strip the header guard by using a root dir inside of the repo dir.
# relative directory
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))
nested_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
os.path.join('nested',
'cpplint_test_header.h'))
cpplint._root = os.path.join('cpplint', 'nested')
actual = cpplint.GetHeaderGuardCPPVariable(nested_file_path)
self.assertEquals('CPPLINT_TEST_HEADER_H_',
actual)
# absolute directory
# (note that CPPLINT.cfg root=setting is always made absolute)
cpplint._root = os.path.join(os.path.dirname(os.path.abspath(__file__)))
self.assertEquals('CPPLINT_TEST_HEADER_H_',
cpplint.GetHeaderGuardCPPVariable(file_path))
nested_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
os.path.join('nested',
'cpplint_test_header.h'))
cpplint._root = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'nested')
self.assertEquals('CPPLINT_TEST_HEADER_H_',
cpplint.GetHeaderGuardCPPVariable(nested_file_path))
# --root flag is ignored if an non-existent directory is specified. # --root flag is ignored if an non-existent directory is specified.
cpplint._root = 'NON_EXISTENT_DIR' cpplint._root = 'NON_EXISTENT_DIR'
self.assertEquals('CPPLINT_CPPLINT_TEST_HEADER_H_', self.assertEquals('CPPLINT_CPPLINT_TEST_HEADER_H_',
@ -4250,6 +4276,7 @@ class CpplintTest(CpplintTestBase):
# than the repo dir # than the repo dir
# (using absolute paths) # (using absolute paths)
# (note that CPPLINT.cfg root=setting is always made absolute)
this_files_path = os.path.dirname(os.path.abspath(__file__)) this_files_path = os.path.dirname(os.path.abspath(__file__))
(styleguide_path, this_files_dir) = os.path.split(this_files_path) (styleguide_path, this_files_dir) = os.path.split(this_files_path)
(styleguide_parent_path, _) = os.path.split(styleguide_path) (styleguide_parent_path, _) = os.path.split(styleguide_path)
@ -4260,6 +4287,10 @@ class CpplintTest(CpplintTestBase):
self.assertEquals('STYLEGUIDE_CPPLINT_CPPLINT_TEST_HEADER_H_', self.assertEquals('STYLEGUIDE_CPPLINT_CPPLINT_TEST_HEADER_H_',
cpplint.GetHeaderGuardCPPVariable(file_path)) cpplint.GetHeaderGuardCPPVariable(file_path))
# To run the 'relative path' tests, we must be in the directory of this test file.
cur_dir = os.getcwd()
os.chdir(this_files_path)
# (using relative paths) # (using relative paths)
styleguide_rel_path = os.path.relpath(styleguide_path, this_files_path) styleguide_rel_path = os.path.relpath(styleguide_path, this_files_path)
# '..' # '..'
@ -4275,6 +4306,9 @@ class CpplintTest(CpplintTestBase):
cpplint._root = None cpplint._root = None
# Restore previous CWD.
os.chdir(cur_dir)
def testPathSplitToList(self): def testPathSplitToList(self):
self.assertEquals([''], self.assertEquals([''],
cpplint.PathSplitToList(os.path.join(''))) cpplint.PathSplitToList(os.path.join('')))

View File

@ -0,0 +1 @@
// A test header for cpplint_unittest.py.