Skip to content

Commit 8667623

Browse files
authored
Merge pull request google#292 from iam/root_cpplint_cfg
CPPLINT.cfg root=setting is now relative to CFG file.
2 parents d9e5e4d + 8a87a46 commit 8667623

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

cpplint/cpplint.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@
114114
ignored.
115115
116116
Examples:
117-
Assuming that src/.git exists, the header guard CPP variables for
118-
src/chrome/browser/ui/browser.h are:
117+
Assuming that top/src/.git exists (and cwd=top/src), the header guard
118+
CPP variables for top/src/chrome/browser/ui/browser.h are:
119119
120120
No flag => CHROME_BROWSER_UI_BROWSER_H_
121121
--root=chrome => BROWSER_UI_BROWSER_H_
122122
--root=chrome/browser => UI_BROWSER_H_
123+
--root=.. => SRC_CHROME_BROWSER_UI_BROWSER_H_
123124
124125
linelength=digits
125126
This is the allowed line length for the project. The default value is
@@ -168,9 +169,9 @@
168169
"linelength" allows to specify the allowed line length for the project.
169170
170171
The "root" option is similar in function to the --root flag (see example
171-
above).
172-
173-
The "headers" option is similar in function to the --headers flag
172+
above). Paths are relative to the directory of the CPPLINT.cfg.
173+
174+
The "headers" option is similar in function to the --headers flag
174175
(see example above).
175176
176177
CPPLINT.cfg has an effect on files in the same directory and all
@@ -539,6 +540,7 @@
539540
# The root directory used for deriving header guard CPP variable.
540541
# This is set by --root flag.
541542
_root = None
543+
_root_debug = False
542544

543545
# The allowed line length of files.
544546
# This is set by --linelength flag.
@@ -1802,8 +1804,14 @@ def GetHeaderGuardCPPVariable(filename):
18021804
file_path_from_root = fileinfo.RepositoryName()
18031805

18041806
def FixupPathFromRoot():
1807+
if _root_debug:
1808+
sys.stderr.write("\n_root fixup, _root = '%s', repository name = '%s'\n"
1809+
%(_root, fileinfo.RepositoryName()))
1810+
18051811
# Process the file path with the --root flag if it was set.
18061812
if not _root:
1813+
if _root_debug:
1814+
sys.stderr.write("_root unspecified\n")
18071815
return file_path_from_root
18081816

18091817
def StripListPrefix(lst, prefix):
@@ -1817,6 +1825,11 @@ def StripListPrefix(lst, prefix):
18171825
# --root=subdir , lstrips subdir from the header guard
18181826
maybe_path = StripListPrefix(PathSplitToList(file_path_from_root),
18191827
PathSplitToList(_root))
1828+
1829+
if _root_debug:
1830+
sys.stderr.write("_root lstrip (maybe_path=%s, file_path_from_root=%s," +
1831+
" _root=%s)\n" %(maybe_path, file_path_from_root, _root))
1832+
18201833
if maybe_path:
18211834
return os.path.join(*maybe_path)
18221835

@@ -1826,9 +1839,17 @@ def StripListPrefix(lst, prefix):
18261839

18271840
maybe_path = StripListPrefix(PathSplitToList(full_path),
18281841
PathSplitToList(root_abspath))
1842+
1843+
if _root_debug:
1844+
sys.stderr.write("_root prepend (maybe_path=%s, full_path=%s, " +
1845+
"root_abspath=%s)\n" %(maybe_path, full_path, root_abspath))
1846+
18291847
if maybe_path:
18301848
return os.path.join(*maybe_path)
18311849

1850+
if _root_debug:
1851+
sys.stderr.write("_root ignore, returning %s\n" %(file_path_from_root))
1852+
18321853
# --root=FAKE_DIR is ignored
18331854
return file_path_from_root
18341855

@@ -5947,7 +5968,8 @@ def ProcessConfigOverrides(filename):
59475968
sys.stderr.write('Line length must be numeric.')
59485969
elif name == 'root':
59495970
global _root
5950-
_root = val
5971+
# root directories are specified relative to CPPLINT.cfg dir.
5972+
_root = os.path.join(os.path.dirname(cfg_file), val)
59515973
elif name == 'headers':
59525974
ProcessHppHeadersOption(val)
59535975
else:

cpplint/cpplint_unittest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,6 +4218,8 @@ def testBuildHeaderGuard(self):
42184218
error_collector.ResultList())
42194219

42204220
def testBuildHeaderGuardWithRoot(self):
4221+
# note: Tested file paths must be real, otherwise
4222+
# the repository name lookup will fail.
42214223
file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
42224224
'cpplint_test_header.h')
42234225
file_info = cpplint.FileInfo(file_path)
@@ -4238,9 +4240,33 @@ def testBuildHeaderGuardWithRoot(self):
42384240
#
42394241

42404242
# left-strip the header guard by using a root dir inside of the repo dir.
4243+
# relative directory
42414244
cpplint._root = 'cpplint'
42424245
self.assertEquals('CPPLINT_TEST_HEADER_H_',
42434246
cpplint.GetHeaderGuardCPPVariable(file_path))
4247+
4248+
nested_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
4249+
os.path.join('nested',
4250+
'cpplint_test_header.h'))
4251+
cpplint._root = os.path.join('cpplint', 'nested')
4252+
actual = cpplint.GetHeaderGuardCPPVariable(nested_file_path)
4253+
self.assertEquals('CPPLINT_TEST_HEADER_H_',
4254+
actual)
4255+
4256+
# absolute directory
4257+
# (note that CPPLINT.cfg root=setting is always made absolute)
4258+
cpplint._root = os.path.join(os.path.dirname(os.path.abspath(__file__)))
4259+
self.assertEquals('CPPLINT_TEST_HEADER_H_',
4260+
cpplint.GetHeaderGuardCPPVariable(file_path))
4261+
4262+
nested_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
4263+
os.path.join('nested',
4264+
'cpplint_test_header.h'))
4265+
cpplint._root = os.path.join(os.path.dirname(os.path.abspath(__file__)),
4266+
'nested')
4267+
self.assertEquals('CPPLINT_TEST_HEADER_H_',
4268+
cpplint.GetHeaderGuardCPPVariable(nested_file_path))
4269+
42444270
# --root flag is ignored if an non-existent directory is specified.
42454271
cpplint._root = 'NON_EXISTENT_DIR'
42464272
self.assertEquals('CPPLINT_CPPLINT_TEST_HEADER_H_',
@@ -4250,6 +4276,7 @@ def testBuildHeaderGuardWithRoot(self):
42504276
# than the repo dir
42514277

42524278
# (using absolute paths)
4279+
# (note that CPPLINT.cfg root=setting is always made absolute)
42534280
this_files_path = os.path.dirname(os.path.abspath(__file__))
42544281
(styleguide_path, this_files_dir) = os.path.split(this_files_path)
42554282
(styleguide_parent_path, _) = os.path.split(styleguide_path)
@@ -4260,6 +4287,10 @@ def testBuildHeaderGuardWithRoot(self):
42604287
self.assertEquals('STYLEGUIDE_CPPLINT_CPPLINT_TEST_HEADER_H_',
42614288
cpplint.GetHeaderGuardCPPVariable(file_path))
42624289

4290+
# To run the 'relative path' tests, we must be in the directory of this test file.
4291+
cur_dir = os.getcwd()
4292+
os.chdir(this_files_path)
4293+
42634294
# (using relative paths)
42644295
styleguide_rel_path = os.path.relpath(styleguide_path, this_files_path)
42654296
# '..'
@@ -4275,6 +4306,9 @@ def testBuildHeaderGuardWithRoot(self):
42754306

42764307
cpplint._root = None
42774308

4309+
# Restore previous CWD.
4310+
os.chdir(cur_dir)
4311+
42784312
def testPathSplitToList(self):
42794313
self.assertEquals([''],
42804314
cpplint.PathSplitToList(os.path.join('')))

cpplint/nested/cpplint_test_header.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// A test header for cpplint_unittest.py.

0 commit comments

Comments
 (0)