Skip to content

Commit aa0f3c0

Browse files
author
Jan Musial
committed
Add tests for opencas.py
Signed-off-by: Jan Musial <[email protected]>
1 parent d773a81 commit aa0f3c0

File tree

9 files changed

+1250
-8
lines changed

9 files changed

+1250
-8
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ Module.symvers
1313
Module.markers
1414
*.mod.c
1515
modules.order
16+
__pycache__/
17+
*.py[cod]
18+
*$py.class

test/utils_tests/opencas-py-tests/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# Copyright(c) 2012-2019 Intel Corporation
3+
# SPDX-License-Identifier: BSD-3-Clause-Clear
4+
#
5+
6+
import sys
7+
8+
9+
def pytest_configure(config):
10+
try:
11+
import helpers
12+
except ImportError:
13+
raise Exception("Couldn't import helpers")
14+
15+
sys.path.append(helpers.find_repo_root() + "/utils")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#
2+
# Copyright(c) 2012-2019 Intel Corporation
3+
# SPDX-License-Identifier: BSD-3-Clause-Clear
4+
#
5+
6+
import mock
7+
import re
8+
import os
9+
from io import StringIO
10+
from textwrap import dedent
11+
12+
13+
def find_repo_root():
14+
path = os.getcwd()
15+
16+
while os.path.realpath(path) != "/":
17+
if ".git" in os.listdir(path):
18+
return path
19+
20+
path = os.path.dirname(path)
21+
22+
raise Exception(
23+
"Couldn't find repository root - unable to locate opencas.py"
24+
)
25+
26+
27+
def get_process_mock(return_value, stdout, stderr):
28+
process_mock = mock.Mock()
29+
attrs = {
30+
"wait.return_value": return_value,
31+
"communicate.return_value": (stdout, stderr),
32+
}
33+
process_mock.configure_mock(**attrs)
34+
35+
return process_mock
36+
37+
38+
def get_mock_os_exists(existing_files):
39+
return lambda x: x in existing_files
40+
41+
42+
def get_hashed_config_list(conf):
43+
"""
44+
Convert list of config lines to list of config lines hashes,
45+
drop empty lines
46+
"""
47+
hashed_conf = [get_conf_line_hash(x) for x in conf]
48+
49+
return [x for x in hashed_conf if x]
50+
51+
52+
def get_conf_line_hash(line):
53+
"""
54+
Removes whitespace, lowercases, comments and sorts cache params if present.
55+
Returns empty line for comment-only lines
56+
57+
We don't care about order of params and kinds of whitespace in config lines
58+
so normalize it to compare. We do care about case in paths, but to simplify
59+
testing we pretend we don't.
60+
"""
61+
62+
def sort_cache_params(params):
63+
return ",".join(sorted(params.split(",")))
64+
65+
line = line.split("#")[0]
66+
67+
cache_params_pattern = re.compile(r"(.*?\s)(\S+=\S+)")
68+
match = cache_params_pattern.search(line)
69+
if match:
70+
sorted_params = sort_cache_params(match.group(2))
71+
line = match.group(1) + sorted_params
72+
73+
return "".join(line.lower().split())
74+
75+
76+
class MockConfigFile(object):
77+
def __init__(self, buffer=""):
78+
self.set_contents(buffer)
79+
80+
def __enter__(self):
81+
return self.buffer
82+
83+
def __exit__(self, *args, **kwargs):
84+
self.set_contents(self.buffer.getvalue())
85+
86+
def __call__(self, path, mode):
87+
if mode == "w":
88+
self.buffer = StringIO()
89+
90+
return self
91+
92+
def read(self):
93+
return self.buffer.read()
94+
95+
def write(self, str):
96+
return self.buffer.write(str)
97+
98+
def close(self):
99+
self.set_contents(self.buffer.getvalue())
100+
101+
def readline(self):
102+
return self.buffer.readline()
103+
104+
def __next__(self):
105+
return self.buffer.__next__()
106+
107+
def __iter__(self):
108+
return self
109+
110+
def set_contents(self, buffer):
111+
self.buffer = StringIO(dedent(buffer).strip())
112+
113+
114+
class CopyableMock(mock.Mock):
115+
def __init__(self, *args, **kwargs):
116+
super(CopyableMock, self).__init__(*args, **kwargs)
117+
self.copies = []
118+
119+
def __deepcopy__(self, memo):
120+
copy = mock.Mock(spec=self)
121+
self.copies += [copy]
122+
return copy

0 commit comments

Comments
 (0)