Skip to content

Commit a7cdf72

Browse files
GrueMastertkruse
authored andcommitted
Fix cpplint#114: --exclude not work
1 parent 6cf0765 commit a7cdf72

4 files changed

Lines changed: 133 additions & 5 deletions

File tree

cpplint.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6565,15 +6565,35 @@ def _ExpandDirectories(filenames):
65656565
for filename in expanded:
65666566
if os.path.splitext(filename)[1][1:] in GetAllExtensions():
65676567
filtered.append(filename)
6568-
65696568
return filtered
65706569

6571-
def _FilterExcludedFiles(filenames):
6570+
def _FilterExcludedFiles(fnames):
65726571
"""Filters out files listed in the --exclude command line switch. File paths
65736572
in the switch are evaluated relative to the current working directory
65746573
"""
65756574
exclude_paths = [os.path.abspath(f) for f in _excludes]
6576-
return [f for f in filenames if os.path.abspath(f) not in exclude_paths]
6575+
# because globbing does not work recursively, exclude all subpath of all excluded entries
6576+
return [f for f in fnames
6577+
if not any(e for e in exclude_paths
6578+
if _IsParentOrSame(e, os.path.abspath(f)))]
6579+
6580+
def _IsParentOrSame(parent, child):
6581+
"""Return true if child is subdirectory of parent.
6582+
Assumes both paths are absolute and don't contain symlinks.
6583+
"""
6584+
parent = os.path.normpath(parent)
6585+
child = os.path.normpath(child)
6586+
if parent == child:
6587+
return True
6588+
6589+
prefix = os.path.commonprefix([parent, child])
6590+
if prefix != parent:
6591+
return False
6592+
# Note: os.path.commonprefix operates on character basis, so
6593+
# take extra care of situations like '/foo/ba' and '/foo/bar/baz'
6594+
child_suffix = child[len(prefix):]
6595+
child_suffix = child_suffix.lstrip(os.sep)
6596+
return child == os.path.join(prefix, child_suffix)
65776597

65786598
def main():
65796599
filenames = ParseArguments(sys.argv[1:])

cpplint_clitest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def testSillySample(self):
160160
self.checkAllInFolder('./samples/silly-sample', 2)
161161

162162
def testBoostSample(self):
163-
self.checkAllInFolder('./samples/boost-sample', 2)
163+
self.checkAllInFolder('./samples/boost-sample', 3)
164164

165165
def testProtobufSample(self):
166166
self.checkAllInFolder('./samples/protobuf-sample', 1)

cpplint_unittest.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4096,6 +4096,7 @@ def testRecursiveArgument(self):
40964096
os.chdir(temp_dir)
40974097
expected = ['one.cpp', os.path.join('src', 'two.cpp'),
40984098
os.path.join('src', 'nested', 'three.cpp')]
4099+
cpplint._excludes = None
40994100
actual = cpplint.ParseArguments(['--recursive', 'one.cpp', 'src'])
41004101
self.assertEquals(set(expected), set(actual))
41014102
finally:
@@ -4113,6 +4114,7 @@ def testRecursiveExcludeInvalidFileExtension(self):
41134114
open(os.path.join(src_dir, "three.cc"), 'w').close()
41144115
os.chdir(temp_dir)
41154116
expected = ['one.cpp', os.path.join('src', 'two.cpp')]
4117+
cpplint._excludes = None
41164118
actual = cpplint.ParseArguments(['--recursive', '--extensions=cpp',
41174119
'one.cpp', 'src'])
41184120
self.assertEquals(set(expected), set(actual))
@@ -4122,27 +4124,56 @@ def testRecursiveExcludeInvalidFileExtension(self):
41224124
cpplint._hpp_headers = set([])
41234125
cpplint._valid_extensions = set([])
41244126

4125-
def testExclude(self):
4127+
def testRecursiveExclude(self):
41264128
working_dir = os.getcwd()
41274129
temp_dir = tempfile.mkdtemp()
41284130
try:
41294131
src_dir = os.path.join(temp_dir, 'src')
4132+
src2_dir = os.path.join(temp_dir, 'src2')
41304133
os.makedirs(src_dir)
4134+
os.makedirs(src2_dir)
41314135
open(os.path.join(src_dir, 'one.cc'), 'w').close()
41324136
open(os.path.join(src_dir, 'two.cc'), 'w').close()
41334137
open(os.path.join(src_dir, 'three.cc'), 'w').close()
4138+
open(os.path.join(src2_dir, 'one.cc'), 'w').close()
4139+
open(os.path.join(src2_dir, 'two.cc'), 'w').close()
4140+
open(os.path.join(src2_dir, 'three.cc'), 'w').close()
41344141
os.chdir(temp_dir)
41354142

4143+
expected = [
4144+
os.path.join('src', 'one.cc'),
4145+
os.path.join('src', 'two.cc'),
4146+
os.path.join('src', 'three.cc')
4147+
]
4148+
cpplint._excludes = None
4149+
actual = cpplint.ParseArguments(['src'])
4150+
self.assertEquals(set(['src']), set(actual))
4151+
4152+
cpplint._excludes = None
4153+
actual = cpplint.ParseArguments(['--recursive', 'src'])
4154+
self.assertEquals(set(expected), set(actual))
4155+
41364156
expected = [os.path.join('src', 'one.cc')]
4157+
cpplint._excludes = None
41374158
actual = cpplint.ParseArguments(['--recursive',
41384159
'--exclude=src{0}t*'.format(os.sep), 'src'])
41394160
self.assertEquals(set(expected), set(actual))
41404161

41414162
expected = [os.path.join('src', 'one.cc')]
4163+
cpplint._excludes = None
41424164
actual = cpplint.ParseArguments(['--recursive',
41434165
'--exclude=src/two.cc', '--exclude=src/three.cc', 'src'])
41444166
self.assertEquals(set(expected), set(actual))
41454167

4168+
expected = set([
4169+
os.path.join('src2', 'one.cc'),
4170+
os.path.join('src2', 'two.cc'),
4171+
os.path.join('src2', 'three.cc')
4172+
])
4173+
cpplint._excludes = None
4174+
actual = cpplint.ParseArguments(['--recursive',
4175+
'--exclude=src', '.'])
4176+
self.assertEquals(expected, set(actual))
41464177
finally:
41474178
os.chdir(working_dir)
41484179
shutil.rmtree(temp_dir)
@@ -6328,6 +6359,10 @@ def testQuietWithoutErrors(self):
63286359
# Output with no errors must be completely blank!
63296360
self.assertEquals("", output)
63306361

6362+
#class FileFilterTest(unittest.TestCase):
6363+
# def testFilterExcludedFiles(self):
6364+
# self.assertEquals([], _FilterExcludedFiles([]))
6365+
63316366
# pylint: disable=C6409
63326367
def setUp():
63336368
"""Runs before all tests are executed.

samples/boost-sample/exclude.def

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--recursive --exclude=headers/* src
2+
1
3+
3
4+
Done processing src/tr1/c_policy.hpp
5+
Total errors found: 66
6+
7+
src/tr1/c_policy.hpp:0: No #ifndef header guard found, suggested CPP variable is: SAMPLES_BOOST_SAMPLE_SRC_TR1_C_POLICY_HPP_ [build/header_guard] [5]
8+
src/tr1/c_policy.hpp:9: Missing space before { [whitespace/braces] [5]
9+
src/tr1/c_policy.hpp:13: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
10+
src/tr1/c_policy.hpp:14: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
11+
src/tr1/c_policy.hpp:15: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
12+
src/tr1/c_policy.hpp:16: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
13+
src/tr1/c_policy.hpp:17: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
14+
src/tr1/c_policy.hpp:18: Lines should be <= 80 characters long [whitespace/line_length] [2]
15+
src/tr1/c_policy.hpp:19: { should almost always be at the end of the previous line [whitespace/braces] [4]
16+
src/tr1/c_policy.hpp:20: public: should be indented +1 space inside struct policy [whitespace/indent] [3]
17+
src/tr1/c_policy.hpp:21: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
18+
src/tr1/c_policy.hpp:22: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
19+
src/tr1/c_policy.hpp:23: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
20+
src/tr1/c_policy.hpp:24: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
21+
src/tr1/c_policy.hpp:25: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
22+
src/tr1/c_policy.hpp:26: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
23+
src/tr1/c_policy.hpp:27: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
24+
src/tr1/c_policy.hpp:28: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
25+
src/tr1/c_policy.hpp:30: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
26+
src/tr1/c_policy.hpp:32: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
27+
src/tr1/c_policy.hpp:34: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
28+
src/tr1/c_policy.hpp:35: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
29+
src/tr1/c_policy.hpp:36: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
30+
src/tr1/c_policy.hpp:37: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
31+
src/tr1/c_policy.hpp:38: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
32+
src/tr1/c_policy.hpp:39: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
33+
src/tr1/c_policy.hpp:44: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
34+
src/tr1/c_policy.hpp:45: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
35+
src/tr1/c_policy.hpp:46: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
36+
src/tr1/c_policy.hpp:47: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
37+
src/tr1/c_policy.hpp:48: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
38+
src/tr1/c_policy.hpp:49: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
39+
src/tr1/c_policy.hpp:50: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
40+
src/tr1/c_policy.hpp:51: Lines should be <= 80 characters long [whitespace/line_length] [2]
41+
src/tr1/c_policy.hpp:52: { should almost always be at the end of the previous line [whitespace/braces] [4]
42+
src/tr1/c_policy.hpp:53: public: should be indented +1 space inside struct policy [whitespace/indent] [3]
43+
src/tr1/c_policy.hpp:54: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
44+
src/tr1/c_policy.hpp:55: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
45+
src/tr1/c_policy.hpp:56: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
46+
src/tr1/c_policy.hpp:57: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
47+
src/tr1/c_policy.hpp:58: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
48+
src/tr1/c_policy.hpp:59: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
49+
src/tr1/c_policy.hpp:60: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
50+
src/tr1/c_policy.hpp:61: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
51+
src/tr1/c_policy.hpp:63: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
52+
src/tr1/c_policy.hpp:65: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
53+
src/tr1/c_policy.hpp:67: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
54+
src/tr1/c_policy.hpp:68: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
55+
src/tr1/c_policy.hpp:69: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
56+
src/tr1/c_policy.hpp:70: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
57+
src/tr1/c_policy.hpp:71: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
58+
src/tr1/c_policy.hpp:72: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
59+
src/tr1/c_policy.hpp:76: Lines should be <= 80 characters long [whitespace/line_length] [2]
60+
src/tr1/c_policy.hpp:88: { should almost always be at the end of the previous line [whitespace/braces] [4]
61+
src/tr1/c_policy.hpp:89: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
62+
src/tr1/c_policy.hpp:89: Lines should be <= 80 characters long [whitespace/line_length] [2]
63+
src/tr1/c_policy.hpp:93: Lines should be <= 80 characters long [whitespace/line_length] [2]
64+
src/tr1/c_policy.hpp:105: { should almost always be at the end of the previous line [whitespace/braces] [4]
65+
src/tr1/c_policy.hpp:106: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
66+
src/tr1/c_policy.hpp:106: Lines should be <= 80 characters long [whitespace/line_length] [2]
67+
src/tr1/c_policy.hpp:109: Namespace should be terminated with "// namespace policies" [readability/namespace] [5]
68+
src/tr1/c_policy.hpp:109: Namespace should be terminated with "// namespace math" [readability/namespace] [5]
69+
src/tr1/c_policy.hpp:109: Namespace should be terminated with "// namespace boost" [readability/namespace] [5]
70+
src/tr1/c_policy.hpp:109: At least two spaces is best between code and comments [whitespace/comments] [2]
71+
src/tr1/c_policy.hpp:111: Missing space before { [whitespace/braces] [5]
72+
src/tr1/c_policy.hpp:131: Namespace should be terminated with "// namespace c_policies" [readability/namespace] [5]
73+

0 commit comments

Comments
 (0)