-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_filter.py
More file actions
143 lines (93 loc) · 4.72 KB
/
test_filter.py
File metadata and controls
143 lines (93 loc) · 4.72 KB
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
140
141
142
143
# Boost Software License - Version 1.0 - August 17th, 2003
#
# Permission is hereby granted, free of charge, to any person or organization
# obtaining a copy of the software and accompanying documentation covered by
# this license (the "Software") to use, reproduce, display, distribute,
# execute, and transmit the Software, and to prepare derivative works of the
# Software, and to permit third-parties to whom the Software is furnished to
# do so, all subject to the following:
#
# The copyright notices in the Software and this entire statement, including
# the above license grant, this restriction and the following disclaimer,
# must be included in all copies of the Software, in whole or in part, and
# all derivative works of the Software, unless such copies or derivative
# works are solely in the form of machine-executable object code generated by
# a source language processor.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import filter
import os.path
import pytest
def test_without_options_node_should_not_be_filtered(tester):
assert tester.test('path') is False
def test_library_path_should_be_filtered_always(tester):
assert tester.test(_create_library_path('path')) is True
def test_node_not_in_include_directory_should_be_filtered(tester):
assert tester.with_include_directory('a').test('b/file.cpp') is True
def test_node_in_include_directory_should_not_be_filtered(tester):
assert tester.with_include_directory('a').test('a/file.cpp') is False
def test_multiple_include_directories_should_be_considered(tester):
tester.with_include_directory('a').with_include_directory('b')
assert tester.test('b/file.cpp') is False
def test_exclude_directories_should_be_filtered(tester):
tester.with_include_directory('a').with_exclude_directory('a/b')
assert tester.test('a/b/file.cpp') is True
def test_exclude_directories_are_filtered_even_without_includes(tester):
tester.with_exclude_directory('a')
assert tester.test('a/file.cpp') is True
def test_directory_is_in_itself():
assert filter._is_in_directory('a', 'a') is True
def test_unrelated_path_is_not_in_directory():
assert filter._is_in_directory('b', 'a') is False
def test_path_in_root_is_in_directory():
assert filter._is_in_directory('a/file', 'a') is True
def test_path_in_sub_directory_is_in_directory():
assert filter._is_in_directory('a/b/c/d/e/f/file', 'a') is True
def test_path_is_in_sub_directory_when_common_multiple_levels():
assert filter._is_in_directory('a/b/c/d/e/f/file', 'a/b/c/d') is True
def test_forward_slashes_are_allowed_in_checking_directory():
assert filter._is_in_directory('a/file', 'a/') is True
def test_backward_slashes_are_allowed_in_checking_directory():
assert filter._is_in_directory('a\\file', 'a\\') is True
def test_mixed_slashes_are_allowed_in_checking_directory():
assert filter._is_in_directory('a\\b/file', 'a\\b/') is True
def test_directory_and_path_can_have_different_os_styles():
assert filter._is_in_directory('a\\b\\file', 'a/b/') is True
def test_absolute_directory_can_be_checked_with_relative_path():
directory = os.path.abspath('a/b')
assert filter._is_in_directory('a/b/file', directory) is True
def test_absolute_path_can_be_checked_with_relative_directory():
path = os.path.abspath('a/b/file')
assert filter._is_in_directory(path, 'a/b') is True
def test_relative_paths_are_normalized():
directory = '../check-name/a/b/file'
assert filter._is_in_directory(directory, 'a/../a/b') is True
@pytest.fixture
def tester(request):
return _Tester()
class _Tester:
def __init__(self):
self.include_directories = []
self.exclude_directories = []
def with_include_directory(self, directory):
self.include_directories.append(directory)
return self
def with_exclude_directory(self, directory):
self.exclude_directories.append(directory)
return self
def test(self, path):
filtering_options = (self.include_directories,
self.exclude_directories)
return filter.should_filter(filtering_options, path)
def _create_library_path(path):
return os.path.join(_library_root(), path)
def _library_root():
if os.name == 'posix':
return '/usr/lib'
assert False, 'Current operating system not yet supported'