cpplint --root: support non-subdirectories

Using cpplint.py --root with directories at a more outer level
will now prepend the header guard with all the directories from the
root to the file.

For example given

  ls /a/b/c # /a/b/c/.git /a/b/c/filename.h
  cpplint.py --root=/a/b /a/b/c/filename.h   # C_FILENAME_H_

  # no root behavior:
  cpplint.py /a/b/c/filename.h               # FILENAME_H_

Also supports relative paths:

  cd /a/b/c
  cpplint.py --root=.. filename.h            # C_FILENAME_H_

Note that the old usage is still supported:

  cd /a/b/c
  mkdir -p d/e/f
  touch /a/b/c/d/e/f/filename.h
  cpplint.py --root=d/e/f d/e/f/filename.h   # FILENAME_H_

which would "strip" the prefix rather than prepend an extra prefix.

(Invalid root prefixes are as before also ignored)
This commit is contained in:
Igor Murashkin
2017-11-08 14:33:52 -08:00
parent ec88ff999b
commit e7ddd2af62
2 changed files with 108 additions and 7 deletions
+51
View File
@@ -4232,6 +4232,12 @@ class CpplintTest(CpplintTestBase):
self.assertEquals('CPPLINT_CPPLINT_TEST_HEADER_H_',
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'
self.assertEquals('CPPLINT_TEST_HEADER_H_',
cpplint.GetHeaderGuardCPPVariable(file_path))
@@ -4240,6 +4246,51 @@ class CpplintTest(CpplintTestBase):
self.assertEquals('CPPLINT_CPPLINT_TEST_HEADER_H_',
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):
# Test that include statements have slashes in them.
self.TestLint('#include "foo.h"',