forked from mherrmann/gitignore_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
139 lines (122 loc) · 5.87 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from unittest.mock import patch, mock_open
from pathlib import Path
from gitignore_parser import parse_gitignore
from unittest import TestCase
class Test(TestCase):
def test_simple(self):
matches = _parse_gitignore_string(
'__pycache__/\n'
'*.py[cod]',
fake_base_dir='/home/michael'
)
self.assertFalse(matches('/home/michael/main.py'))
self.assertTrue(matches('/home/michael/main.pyc'))
self.assertTrue(matches('/home/michael/dir/main.pyc'))
self.assertTrue(matches('/home/michael/__pycache__'))
def test_wildcard(self):
matches = _parse_gitignore_string(
'hello.*',
fake_base_dir='/home/michael'
)
self.assertTrue(matches('/home/michael/hello.txt'))
self.assertTrue(matches('/home/michael/hello.foobar/'))
self.assertTrue(matches('/home/michael/dir/hello.txt'))
self.assertTrue(matches('/home/michael/hello.'))
self.assertFalse(matches('/home/michael/hello'))
self.assertFalse(matches('/home/michael/helloX'))
def test_anchored_wildcard(self):
matches = _parse_gitignore_string(
'/hello.*',
fake_base_dir='/home/michael'
)
self.assertTrue(matches('/home/michael/hello.txt'))
self.assertTrue(matches('/home/michael/hello.c'))
self.assertFalse(matches('/home/michael/a/hello.java'))
def test_trailingspaces(self):
matches = _parse_gitignore_string(
'ignoretrailingspace \n'
'notignoredspace\\ \n'
'partiallyignoredspace\\ \n'
'partiallyignoredspace2 \\ \n'
'notignoredmultiplespace\\ \\ \\ ',
fake_base_dir='/home/michael'
)
self.assertTrue(matches('/home/michael/ignoretrailingspace'))
self.assertFalse(matches('/home/michael/ignoretrailingspace '))
self.assertTrue(matches('/home/michael/partiallyignoredspace '))
self.assertFalse(matches('/home/michael/partiallyignoredspace '))
self.assertFalse(matches('/home/michael/partiallyignoredspace'))
self.assertTrue(matches('/home/michael/partiallyignoredspace2 '))
self.assertFalse(matches('/home/michael/partiallyignoredspace2 '))
self.assertFalse(matches('/home/michael/partiallyignoredspace2 '))
self.assertFalse(matches('/home/michael/partiallyignoredspace2'))
self.assertTrue(matches('/home/michael/notignoredspace '))
self.assertFalse(matches('/home/michael/notignoredspace'))
self.assertTrue(matches('/home/michael/notignoredmultiplespace '))
self.assertFalse(matches('/home/michael/notignoredmultiplespace'))
def test_comment(self):
matches = _parse_gitignore_string(
'somematch\n'
'#realcomment\n'
'othermatch\n'
'\\#imnocomment',
fake_base_dir='/home/michael'
)
self.assertTrue(matches('/home/michael/somematch'))
self.assertFalse(matches('/home/michael/#realcomment'))
self.assertTrue(matches('/home/michael/othermatch'))
self.assertTrue(matches('/home/michael/#imnocomment'))
def test_ignore_directory(self):
matches = _parse_gitignore_string('.venv/', fake_base_dir='/home/michael')
self.assertTrue(matches('/home/michael/.venv'))
self.assertTrue(matches('/home/michael/.venv/folder'))
self.assertTrue(matches('/home/michael/.venv/file.txt'))
def test_ignore_directory_asterisk(self):
matches = _parse_gitignore_string('.venv/*', fake_base_dir='/home/michael')
self.assertFalse(matches('/home/michael/.venv'))
self.assertTrue(matches('/home/michael/.venv/folder'))
self.assertTrue(matches('/home/michael/.venv/file.txt'))
def test_negation(self):
matches = _parse_gitignore_string(
'''
*.ignore
!keep.ignore
''',
fake_base_dir='/home/michael'
)
self.assertTrue(matches('/home/michael/trash.ignore'))
self.assertFalse(matches('/home/michael/keep.ignore'))
self.assertTrue(matches('/home/michael/waste.ignore'))
def test_double_asterisks(self):
matches = _parse_gitignore_string('foo/**/Bar', fake_base_dir='/home/michael')
self.assertTrue(matches('/home/michael/foo/hello/Bar'))
self.assertTrue(matches('/home/michael/foo/world/Bar'))
self.assertTrue(matches('/home/michael/foo/Bar'))
def test_directory_only_negation(self):
matches = _parse_gitignore_string('''
data/**
!data/**/
!.gitkeep
!data/01_raw/*
''',
fake_base_dir='/home/michael'
)
self.assertFalse(matches('/home/michael/data/01_raw/'))
self.assertFalse(matches('/home/michael/data/01_raw/.gitkeep'))
self.assertFalse(matches('/home/michael/data/01_raw/raw_file.csv'))
self.assertFalse(matches('/home/michael/data/02_processed/'))
self.assertFalse(matches('/home/michael/data/02_processed/.gitkeep'))
self.assertTrue(matches('/home/michael/data/02_processed/processed_file.csv'))
def test_single_asterisk(self):
matches = _parse_gitignore_string('*', fake_base_dir='/home/michael')
self.assertTrue(matches('/home/michael/file.txt'))
self.assertTrue(matches('/home/michael/directory'))
self.assertTrue(matches('/home/michael/directory-trailing/'))
def test_supports_path_type_argument(self):
matches = _parse_gitignore_string('file1\n!file2', fake_base_dir='/home/michael')
self.assertTrue(matches(Path('/home/michael/file1')))
self.assertFalse(matches(Path('/home/michael/file2')))
def _parse_gitignore_string(data: str, fake_base_dir: str = None):
with patch('builtins.open', mock_open(read_data=data)):
success = parse_gitignore(f'{fake_base_dir}/.gitignore', fake_base_dir)
return success