Skip to content

Commit 6de717a

Browse files
[refactor] Permit to get the expected exit code in configuration test framework
1 parent 9fe8c89 commit 6de717a

File tree

6 files changed

+49
-15
lines changed

6 files changed

+49
-15
lines changed

pylint/testutils/configuration_test.py

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import logging
88
import unittest
99
from pathlib import Path
10-
from typing import Any, Dict, Tuple, Union
10+
from typing import Any, Dict, List, Tuple, Union
1111
from unittest.mock import Mock
1212

1313
from pylint.lint import Run
@@ -22,13 +22,9 @@ def get_expected_or_default(
2222
tested_configuration_file: str, suffix: str, default: ConfigurationValue
2323
) -> str:
2424
"""Return the expected value from the file if it exists, or the given default."""
25-
26-
def get_path_according_to_suffix() -> Path:
27-
path = Path(tested_configuration_file)
28-
return path.parent / f"{path.stem}.{suffix}"
29-
3025
expected = default
31-
expected_result_path = get_path_according_to_suffix()
26+
path = Path(tested_configuration_file)
27+
expected_result_path = path.parent / f"{path.stem}.{suffix}"
3228
if expected_result_path.exists():
3329
with open(expected_result_path, encoding="utf8") as f:
3430
expected = f.read()
@@ -74,18 +70,56 @@ def get_expected_output(
7470
configuration_path: str, user_specific_path: Path
7571
) -> Tuple[int, str]:
7672
"""Get the expected output of a functional test."""
77-
output = get_expected_or_default(configuration_path, suffix="out", default="")
78-
if output:
73+
74+
def get_related_files(
75+
tested_configuration_file: str, suffix_filter: str
76+
) -> List[Path]:
77+
path = Path(tested_configuration_file)
78+
return [
79+
p
80+
for p in path.parent.iterdir()
81+
if path.stem in str(p) and str(p).endswith(suffix_filter)
82+
]
83+
84+
exit_code = 0
85+
msg = (
86+
"we expect a single file of the form "
87+
"'filename_dot_expected_error_code_dot_out.32.out'"
88+
)
89+
possible_out_files = get_related_files(configuration_path, suffix_filter="out")
90+
if len(possible_out_files) > 1:
91+
logging.error(
92+
"Too much .out files for %s %s.",
93+
configuration_path,
94+
msg,
95+
)
96+
return -1, "out file is broken"
97+
if not possible_out_files:
7998
# logging is helpful to see what the expected exit code is and why.
8099
# The output of the program is checked during the test so printing
81100
# messes with the result.
82-
logging.info(
83-
"Output exists for %s so the expected exit code is 2", configuration_path
84-
)
85-
exit_code = 2
86-
else:
87101
logging.info(".out file does not exists, so the expected exit code is 0")
88-
exit_code = 0
102+
return 0, ""
103+
path = possible_out_files[0]
104+
try:
105+
exit_code = int(str(path.stem).rsplit(".", maxsplit=1)[-1])
106+
except Exception as e: # pylint: disable=broad-except
107+
logging.error(
108+
"Wrong format for .out file name for %s %s: %s",
109+
configuration_path,
110+
msg,
111+
e,
112+
)
113+
return -1, "out file is broken"
114+
115+
output = get_expected_or_default(
116+
configuration_path, suffix=f"{exit_code}.out", default=""
117+
)
118+
logging.info(
119+
"Output exists for %s so the expected exit code is %s",
120+
configuration_path,
121+
exit_code,
122+
)
89123
return exit_code, output.format(
90124
abspath=configuration_path,
91125
relpath=Path(configuration_path).relative_to(user_specific_path),

0 commit comments

Comments
 (0)