Skip to content

Commit a355f0b

Browse files
committed
Fixes small issues and code style, added unit tests.
1 parent ea2c066 commit a355f0b

7 files changed

+85
-17
lines changed

.cico.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,5 @@
7878
git_username: openscap
7979
git_repo: scap-security-guide
8080
ci_project: '{name}'
81-
ci_cmd: "yum -y install install cmake openscap-utils git PyYAML python-jinja2 && cd build && cmake ../ && make -j4 && CTEST_OUTPUT_ON_FAILURE=1 ctest -j4"
81+
ci_cmd: "yum -y install install cmake openscap-utils git PyYAML python-jinja2 python-pytest && cd build && cmake ../ && make -j4 && CTEST_OUTPUT_ON_FAILURE=1 ctest -j4"
8282
timeout: '20m'

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ enable_testing()
178178

179179
include(SSGCommon)
180180

181+
ssg_python_unit_tests("misc")
182+
ssg_python_unit_tests("utils")
183+
181184
# Targets 'stats', 'profile-stats' and 'zipfile' need to be added
182185
# before any product because they will receive dependencies from products added
183186

cmake/SSGCommon.cmake

+16
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ if (SSG_SHELLCHECK_BASH_FIXES_VALIDATION_ENABLED AND SHELLCHECK_EXECUTABLE AND "
219219
endif()
220220
endmacro()
221221

222+
223+
if (ENABLE_PYTHON_COVERAGE)
224+
set(PYTEST_COVERAGE_OPTIONS --cov-append --cov-report=xml --cov "${CMAKE_SOURCE_DIR}/shared/")
225+
endif()
226+
227+
macro(ssg_python_unit_tests PYTHON_COMPONENT)
228+
add_test(
229+
NAME "python-unit-${PYTHON_COMPONENT}"
230+
COMMAND "${PYTHON_EXECUTABLE}" -m pytest ${PYTEST_COVERAGE_OPTIONS} "${CMAKE_SOURCE_DIR}/tests/unit/${PYTHON_COMPONENT}"
231+
)
232+
set_tests_properties ("python-unit-${PYTHON_COMPONENT}" PROPERTIES ENVIRONMENT
233+
"PYTHONPATH=${CMAKE_SOURCE_DIR}/shared/${PYTHON_COMPONENT}"
234+
)
235+
endmacro()
236+
237+
222238
macro(ssg_build_remediations PRODUCT)
223239
message(STATUS "Scanning for dependencies of ${PRODUCT} fixes (bash, ansible, puppet and anaconda)...")
224240
_ssg_build_remediations_for_language(${PRODUCT} "bash")

shared/misc/generate-contributors.py shared/misc/generate_contributors.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import collections
1010

1111

12-
MANUAL_EDIT_WARNING = \
13-
"""
14-
This file is generated using the %s script. DO NOT MANUALLY EDIT!!!!
12+
MANUAL_EDIT_WARNING = """This file is generated using the %s script. DO NOT MANUALLY EDIT!!!!
1513
Last Modified: %s
1614
""" % (os.path.basename(__file__), datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
1715

@@ -68,22 +66,21 @@
6866
}
6967

7068

71-
def _get_contributions_by_email(output):
69+
def _get_contributions_by_canonical_email(output):
7270
contributions_by_email = collections.defaultdict(list)
7371
for line in output.split("\n"):
74-
match = re.match(r"[\s]*([0-9]+)[\s+](.+)[\s]+\<(.+)\>", line)
72+
match = re.match(r"[\s]*([0-9]+)\s+(.+)\s+\<(.+)\>", line)
7573
if match is None:
7674
continue
7775

78-
commits, name, email = match.groups()
76+
commits_count, author_name, email = match.groups()
7977

80-
if email in email_mappings:
81-
email = email_mappings[email]
78+
canonical_email = email_mappings.get(email, email)
8279

83-
if email == "":
80+
if canonical_email == "":
8481
continue # ignored
8582

86-
contributions_by_email[email].append((int(commits), name))
83+
contributions_by_email[canonical_email].append((int(commits_count), author_name))
8784
return contributions_by_email
8885

8986

@@ -94,23 +91,21 @@ def _get_name_used_most_in_contributions(contribution_sets):
9491

9592
def _get_contributor_email_mapping(contributions_by_email):
9693
contributors = {}
97-
# We will use the most used full name
9894
for email in contributions_by_email:
9995
name_used_most = _get_name_used_most_in_contributions(contributions_by_email[email])
100-
if name_used_most in name_mappings:
101-
name_used_most = name_mappings[name_used_most]
96+
canonical_name_used_most = name_mappings.get(name_used_most, name_used_most)
10297

103-
contributors[name_used_most] = email
98+
contributors[canonical_name_used_most] = email
10499
return contributors
105100

106101

107102
def _names_sorted_by_last_name(names):
108-
return sorted(names, key=lambda x: x.split(" ")[-1].upper())
103+
return sorted(names, key=lambda x: tuple(n.upper() for n in x.split(" "))[::-1])
109104

110105

111106
def main():
112107
output = subprocess.check_output(["git", "shortlog", "-se"]).decode("utf-8")
113-
contributions_by_email = _get_contributions_by_email(output)
108+
contributions_by_email = _get_contributions_by_canonical_email(output)
114109
contributors = _get_contributor_email_mapping(contributions_by_email)
115110

116111
contributors_md = "<!---%s--->\n\n" % MANUAL_EDIT_WARNING
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pytest
2+
3+
import generate_contributors
4+
5+
6+
INPUT = """
7+
1 David Smith <[email protected]>
8+
170 David Smith <[email protected]>
9+
19 Dave Smith <[email protected]>
10+
2 Deric Crago abc <[email protected]>
11+
1 Deric Crago aaa <[email protected]>
12+
1 Aeric Erago abc <[email protected]>
13+
1 Aeric Drago abc <[email protected]>
14+
14 nobodyl <[email protected]>
15+
"""
16+
17+
18+
@pytest.fixture()
19+
def emails():
20+
return generate_contributors._get_contributions_by_canonical_email(INPUT)
21+
22+
23+
@pytest.fixture()
24+
def authors(emails):
25+
return generate_contributors._get_contributor_email_mapping(emails)
26+
27+
28+
def test_contributions_aggregation(emails):
29+
assert "[email protected]" in emails
30+
assert "[email protected]" not in emails # <-- Not the canonical email
31+
assert "[email protected]" not in emails # <-- emails is supposed to be ignored
32+
33+
34+
def test_contributors_aggregation(authors):
35+
assert "Dave Smith" not in authors
36+
assert "David Smith" in authors
37+
38+
39+
def test_name_sorting(authors):
40+
authors_names = list(authors.keys())
41+
sorted_names = generate_contributors._names_sorted_by_last_name(authors_names)
42+
assert sorted_names[0] == "Deric Crago aaa"
43+
assert sorted_names[1] == "Deric Crago abc"
44+
assert sorted_names[2] == "Aeric Drago abc"
45+
assert sorted_names[3] == "Aeric Erago abc"

tests/unit/modules/test_ssgcommon.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
4+
def test_nothing():
5+
pass

tests/unit/utils/test_relabel_ids.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
def test_nothing():
4+
pass

0 commit comments

Comments
 (0)