Skip to content

Commit

Permalink
Add support for combining pathspec objects via addition operator (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhhollander committed Nov 7, 2020
1 parent 3aee306 commit f1399b1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pathspec/pathspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ def __len__(self):
"""
return len(self.patterns)

def __add__(self, other):
"""
Combines the :attr:`Pathspec.patterns` patterns from two
:class:`PathSpec` instances.
"""
if isinstance(other, PathSpec):
return PathSpec(self.patterns + other.patterns)
else:
return NotImplemented

def __iadd__(self, other):
"""
Adds the :attr:`Pathspec.patterns` patterns from one :class:`PathSpec`
instance to this instance.
"""
if isinstance(other, PathSpec):
self.patterns += other.patterns
return self
else:
return NotImplemented

@classmethod
def from_lines(cls, pattern_factory, lines):
"""
Expand Down
51 changes: 51 additions & 0 deletions pathspec/tests/test_pathspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,54 @@ def test_02_ne(self):
'!*.txt',
])
self.assertNotEqual(first_spec, second_spec)

def test_01_addition(self):
"""
Test pattern addition using + operator
"""
first_spec = pathspec.PathSpec.from_lines('gitwildmatch', [
'test.txt',
'test.png'
])
second_spec = pathspec.PathSpec.from_lines('gitwildmatch', [
'test.html',
'test.jpg'
])
combined_spec = first_spec + second_spec
results = set(combined_spec.match_files([
'test.txt',
'test.png',
'test.html',
'test.jpg'
], separators=('\\',)))
self.assertEqual(results, {
'test.txt',
'test.png',
'test.html',
'test.jpg'
})

def test_02_addition(self):
"""
Test pattern addition using += operator
"""
spec = pathspec.PathSpec.from_lines('gitwildmatch', [
'test.txt',
'test.png'
])
spec += pathspec.PathSpec.from_lines('gitwildmatch', [
'test.html',
'test.jpg'
])
results = set(spec.match_files([
'test.txt',
'test.png',
'test.html',
'test.jpg'
], separators=('\\',)))
self.assertEqual(results, {
'test.txt',
'test.png',
'test.html',
'test.jpg'
})

0 comments on commit f1399b1

Please sign in to comment.