7
7
import logging
8
8
import unittest
9
9
from pathlib import Path
10
- from typing import Any , Dict , Tuple , Union
10
+ from typing import Any , Dict , List , Tuple , Union
11
11
from unittest .mock import Mock
12
12
13
13
from pylint .lint import Run
@@ -22,13 +22,9 @@ def get_expected_or_default(
22
22
tested_configuration_file : str , suffix : str , default : ConfigurationValue
23
23
) -> str :
24
24
"""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
-
30
25
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 } "
32
28
if expected_result_path .exists ():
33
29
with open (expected_result_path , encoding = "utf8" ) as f :
34
30
expected = f .read ()
@@ -74,18 +70,56 @@ def get_expected_output(
74
70
configuration_path : str , user_specific_path : Path
75
71
) -> Tuple [int , str ]:
76
72
"""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 :
79
98
# logging is helpful to see what the expected exit code is and why.
80
99
# The output of the program is checked during the test so printing
81
100
# 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 :
87
101
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
+ )
89
123
return exit_code , output .format (
90
124
abspath = configuration_path ,
91
125
relpath = Path (configuration_path ).relative_to (user_specific_path ),
0 commit comments