-
Notifications
You must be signed in to change notification settings - Fork 22
/
test_filesystem_io.py
47 lines (35 loc) · 1.74 KB
/
test_filesystem_io.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
import pathlib
import pytest
from utils.filesystem import file_reader, search_files
@pytest.fixture
def mocker_file(mocker):
# Read a mocked /etc/release file
mocked_file_data = mocker.mock_open(read_data="file line 1\nfile line 2\n")
mocker.patch("builtins.open", mocked_file_data)
def test_check_solaris_version(mocker_file):
lines = file_reader(filepath='fakefile')
assert(len(lines) == 2)
assert(lines[0] == 'file line 1\n')
def test_filesystem_search_by_pattern(mocker):
mocker.patch("pathlib.Path.rglob", return_value=[
pathlib.Path('file.1'), pathlib.Path('file.2')])
mocker.patch("pathlib.Path.is_file", return_value=True)
res = search_files('some/path', ['some.pattern'])
assert(res == [pathlib.Path('file.1'), pathlib.Path('file.2')])
res = search_files('some/path', ['some.pattern', 'same.pattern'])
assert(res == [pathlib.Path('file.1'), pathlib.Path('file.2'),
pathlib.Path('file.1'), pathlib.Path('file.2')])
def test_filesystem_search_by_pattern2(mocker):
mocker.patch("pathlib.Path.rglob", return_value=[pathlib.Path('file.1')])
mocker.patch("pathlib.Path.is_file", return_value=True)
res = search_files('some/path', [])
assert(res == [])
res = search_files('some/path', ['some.pattern'])
assert(res == [pathlib.Path('file.1')])
res = search_files('some/path', ['some.pattern', 'same.pattern'])
assert(res == [pathlib.Path('file.1'), pathlib.Path('file.1')])
def test_filesystem_search_by_pattern3(mocker):
mocker.patch("pathlib.Path.rglob", return_value=[pathlib.Path('file.1')])
mocker.patch("pathlib.Path.is_file", return_value=False)
res = search_files('some/path', ['some.pattern', 'same.pattern'])
assert(res == [])