Skip to content

Commit 7afd7d0

Browse files
author
moshemorad
authored
Add import tests (#483)
1 parent df17896 commit 7afd7d0

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

enforcer/enforcer_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import os
33

4-
# Add parent directory to Python path so we can import enforcer modules
4+
# Add parent directory to Python path so we can import enforcer modules
55
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
66

77
from enforcer.utils import add_custom_certificate

tests/test_app_imports.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
import pytest
3+
4+
KRR_EXPECTED_LINES = [
5+
"import os\n",
6+
"\n",
7+
"from robusta_krr.common.ssl_utils import add_custom_certificate\n",
8+
"\n",
9+
'ADDITIONAL_CERTIFICATE: str = os.environ.get("CERTIFICATE", "")\n',
10+
"\n",
11+
"if add_custom_certificate(ADDITIONAL_CERTIFICATE):\n",
12+
' print("added custom certificate")\n',
13+
"\n",
14+
"# DO NOT ADD ANY CODE ABOVE THIS\n",
15+
"# ADDING IMPORTS BEFORE ADDING THE CUSTOM CERTS MIGHT INIT HTTP CLIENTS THAT DOESN'T RESPECT THE CUSTOM CERT\n",
16+
]
17+
18+
ENFORCER_EXPECTED_LINES = [
19+
"import sys\n",
20+
"import os\n",
21+
"\n",
22+
"# Add parent directory to Python path so we can import enforcer modules\n",
23+
"sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))\n",
24+
"\n",
25+
"from enforcer.utils import add_custom_certificate\n",
26+
"\n",
27+
'ADDITIONAL_CERTIFICATE: str = os.environ.get("CERTIFICATE", "")\n',
28+
"\n",
29+
"if add_custom_certificate(ADDITIONAL_CERTIFICATE):\n",
30+
' print("added custom certificate")\n',
31+
"\n",
32+
"# DO NOT ADD ANY CODE ABOVE THIS\n",
33+
"# ADDING IMPORTS BEFORE ADDING THE CUSTOM CERTS MIGHT INIT HTTP CLIENTS THAT DOESN'T RESPECT THE CUSTOM CERT\n",
34+
]
35+
@pytest.mark.parametrize(
36+
"file_path,file_name,expected_lines",
37+
[
38+
("krr.py", "krr.py", KRR_EXPECTED_LINES),
39+
("enforcer/enforcer_main.py", "enforcer_main.py", ENFORCER_EXPECTED_LINES),
40+
],
41+
)
42+
def test_app_files_have_correct_initial_lines(file_path, file_name, expected_lines):
43+
"""Test that app files start with the required certificate handling code."""
44+
full_path = os.path.join(os.path.dirname(__file__), "..", file_path)
45+
46+
with open(full_path, "r") as f:
47+
lines = f.readlines()
48+
49+
for i, expected_line in enumerate(expected_lines):
50+
assert (
51+
lines[i] == expected_line
52+
), f"Line {i + 1} should be: {expected_line.strip()!r}, but got: {lines[i].strip()!r}. This tests make sure the import order in {file_name} file is correct, if you see this, go to {file_name} file and move your imports code to lower lines."

0 commit comments

Comments
 (0)