-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
executable file
·162 lines (143 loc) · 6.74 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
from unittest import TestCase, main
from unittest.mock import patch, MagicMock
from io import StringIO
class Qual:
def __init__(self, name, op, value, is_list=False):
self.field_name = name
self.operator = op
self.value = value
self.is_list_operator = is_list
class ForeignDataWrapper:
def __init__(self, options, columns):
pass
class multicorn:
ForeignDataWrapper = ForeignDataWrapper
import sys
sys.modules['multicorn'] = multicorn
from gnufind import FindWrapper
ID_BUILTIN = 0
ID_PATTERN = 1
ID_EXECUTABLE = 2
TEST_DIR = '/usr/share/example/'
TEST_ARGS = ['/usr/bin/find', '-O3', TEST_DIR,
'-regextype', 'posix-egrep', '-ignore_readdir_race']
class FindWrapperTests(TestCase):
def test_root_with_slash(self):
columns = { 'path': None, 'modified': None }
fw = FindWrapper({ 'root_directory': '/dir/no/slash/' }, columns)
self.assertEqual(fw._root, '/dir/no/slash/')
def test_root_without_slash(self):
columns = { 'path': None, 'modified': None }
fw = FindWrapper({ 'root_directory': '/dir/no/slash' }, columns)
self.assertEqual(fw._root, '/dir/no/slash/')
@patch('gnufind.Popen')
@patch('gnufind.PIPE')
def test_basic(self, mock_pipe, mock_popen):
process_mock = MagicMock()
process_attrs = { 'stdout': StringIO('example.txt\t2017-03-22+22:33:15.3646792370\n') }
process_mock.configure_mock(**process_attrs)
mock_popen.return_value = process_mock
columns = { 'path': None, 'modified': None }
fw = FindWrapper({ 'root_directory': TEST_DIR }, columns)
self.assertIn('path', fw._handlers)
self.assertEqual(fw._handlers['path'][0], ID_BUILTIN)
self.assertEqual(fw._handlers['path'][1], '%P')
self.assertIn('modified', fw._handlers)
self.assertEqual(fw._handlers['modified'][0], ID_BUILTIN)
self.assertEqual(fw._handlers['modified'][1], '%T+')
row = next(fw.execute([], columns))
expected = TEST_ARGS + [ '-printf', '%P\t%T+\n' ]
mock_popen.assert_called_with(expected, stdout=mock_pipe, universal_newlines=True)
self.assertEqual(row, { 'modified': '2017-03-22 22:33:15.3646792370', 'path': 'example.txt' })
@patch('gnufind.Popen')
@patch('gnufind.PIPE')
def test_multi(self, mock_pipe, mock_popen):
process_mock = MagicMock()
process_attrs = { 'stdout': StringIO('example1.txt\t2017-03-22+22:33:15.3646792370\n' +
'example2.txt\t2017-03-22+22:33:45.3646792370\n') }
process_mock.configure_mock(**process_attrs)
mock_popen.return_value = process_mock
columns = { 'path': None, 'modified': None }
fw = FindWrapper({ 'root_directory': TEST_DIR }, columns)
rows = fw.execute([], columns)
row = next(rows)
self.assertEqual(row, { 'modified': '2017-03-22 22:33:15.3646792370', 'path': 'example1.txt' })
row = next(rows)
self.assertEqual(row, { 'modified': '2017-03-22 22:33:45.3646792370', 'path': 'example2.txt' })
@patch('gnufind.Popen')
@patch('gnufind.PIPE')
def test_exec(self, mock_pipe, mock_popen):
process_mock = MagicMock()
process_attrs = { 'stdout': StringIO('example.txt\t2017-03-22+22:33:15.3646792370\n' +
'text/plain\n') }
process_mock.configure_mock(**process_attrs)
mock_popen.return_value = process_mock
columns = { 'path': None, 'modified': None, 'mime_type': None }
fw = FindWrapper({ 'root_directory': TEST_DIR,
'mime_type': '/usr/bin/file -b -i' }, columns)
row = next(fw.execute([], columns))
self.assertEqual(row, { 'modified': '2017-03-22 22:33:15.3646792370',
'path': 'example.txt',
'mime_type': 'text/plain' })
expected = TEST_ARGS + [ '-printf', '%P\t%T+\n',
'-exec', '/usr/bin/file', '-b', '-i', '{}', ';' ]
mock_popen.assert_called_with(expected, stdout=mock_pipe, universal_newlines=True)
@patch('gnufind.Popen')
@patch('gnufind.PIPE')
def test_alias(self, mock_pipe, mock_popen):
process_mock = MagicMock()
process_attrs = { 'stdout': StringIO('example.txt\t2017-03-22+22:33:15.3646792370\n') }
process_mock.configure_mock(**process_attrs)
mock_popen.return_value = process_mock
columns = { 'path': None, 'ts': None }
fw = FindWrapper({ 'root_directory': TEST_DIR,
'ts': 'modified' }, columns)
row = next(fw.execute([], columns))
self.assertEqual(row, { 'ts': '2017-03-22 22:33:15.3646792370',
'path': 'example.txt' })
expected = TEST_ARGS + [ '-printf', '%P\t%T+\n' ]
mock_popen.assert_called_with(expected, stdout=mock_pipe, universal_newlines=True)
@patch('gnufind.Popen')
@patch('gnufind.PIPE')
def test_quals(self, mock_pipe, mock_popen):
process_mock = MagicMock()
process_attrs = { 'stdout': StringIO('example.txt\n') }
process_mock.configure_mock(**process_attrs)
mock_popen.return_value = process_mock
columns = { 'path': None, 'depth': None, 'type': None }
fw = FindWrapper({ 'root_directory': TEST_DIR }, columns)
row = next(fw.execute([Qual('depth','=',2), Qual('type','=','f')], { 'path': None }))
self.assertEqual(row, { 'path': 'example.txt' })
expected = TEST_ARGS + [ '-mindepth', '2', '-maxdepth', '2', '-type', 'f', '-printf', '%P\n' ]
mock_popen.assert_called_with(expected, stdout=mock_pipe, universal_newlines=True)
@patch('gnufind.Popen')
@patch('gnufind.PIPE')
def test_pattern(self, mock_pipe, mock_popen):
process_mock = MagicMock()
process_attrs = { 'stdout': StringIO(TEST_DIR + 'example.txt\n') }
process_mock.configure_mock(**process_attrs)
mock_popen.return_value = process_mock
columns = { 'stem': None, 'extension': None }
fw = FindWrapper({ 'root_directory': TEST_DIR,
'stem': '(?P<stem>[^.]+)\\.(?P<extension>[^.]+)' }, columns)
row = next(fw.execute([], columns))
self.assertEqual(row, { 'extension': 'txt', 'stem': 'example' })
expected = TEST_ARGS + [ '-printf', '%P\n' ]
mock_popen.assert_called_with(expected, stdout=mock_pipe, universal_newlines=True)
@patch('gnufind.Popen')
@patch('gnufind.PIPE')
def test_anonymous_pattern(self, mock_pipe, mock_popen):
process_mock = MagicMock()
process_attrs = { 'stdout': StringIO(TEST_DIR + 'example.txt\n') }
process_mock.configure_mock(**process_attrs)
mock_popen.return_value = process_mock
columns = { 'stem': None }
fw = FindWrapper({ 'root_directory': TEST_DIR,
'stem': '([^.]+)\\.[^.]+' }, columns)
row = next(fw.execute([], columns))
self.assertEqual(row, { 'stem': 'example' })
expected = TEST_ARGS + [ '-printf', '%P\n' ]
mock_popen.assert_called_with(expected, stdout=mock_pipe, universal_newlines=True)
if __name__ == '__main__':
main()