88
99import click
1010from plumbum import FG , RETCODE
11+ from requests_html import HTMLSession
1112
12- from clit .constants import PYCHARM_MACOS_APP_PATH
13+ from clit .constants import PYCHARM_MACOS_APP_PATH , TEST_NAMES_REGEX
1314from clit .files import shell
1415from clit .ui import prompt
1516
@@ -33,13 +34,19 @@ def pycharm_cli(files):
3334 call (command_line )
3435
3536
36- @click .command ()
37+ @click .group ()
38+ def extra_pytest ():
39+ """Extra commands for py.test."""
40+ pass
41+
42+
43+ @extra_pytest .command ()
3744@click .option ("--delete" , "-d" , default = False , is_flag = True , help = "Delete pytest directory first" )
3845@click .option ("--failed" , "-f" , default = False , is_flag = True , help = "Run only failed tests" )
3946@click .option ("--count" , "-c" , default = 0 , help = "Repeat the same test several times" )
4047@click .option ("--reruns" , "-r" , default = 0 , help = "Re-run a failed test several times" )
4148@click .argument ("class_names_or_args" , nargs = - 1 )
42- def pytest_run (delete : bool , failed : bool , count : int , reruns : int , class_names_or_args : Tuple [str ]):
49+ def run (delete : bool , failed : bool , count : int , reruns : int , class_names_or_args : Tuple [str ]):
4350 """Run pytest with some shortcut options."""
4451 # Import locally, so we get an error only in this function, and not in other functions of this module.
4552 from plumbum .cmd import time as time_cmd , rm
@@ -74,6 +81,32 @@ def pytest_run(delete: bool, failed: bool, count: int, reruns: int, class_names_
7481 exit (rv )
7582
7683
84+ @extra_pytest .command ()
85+ @click .option ("-f" , "--result-file" , type = click .File ())
86+ @click .option ("-j" , "--jenkins-url" , multiple = True )
87+ @click .option ("-s" , "dont_capture" , flag_value = "-s" , help = "Don't capture output" )
88+ @click .pass_context
89+ def results (ctx , result_file , jenkins_url : Tuple [str , ...], dont_capture ):
90+ """Parse a file with the output of failed tests, then re-run only those failed tests."""
91+ if result_file :
92+ contents = result_file .read ()
93+ elif jenkins_url :
94+ responses = []
95+ for url in set (jenkins_url ):
96+ request = HTMLSession ().get (url , auth = (os .environ ["JENKINS_USERNAME" ], os .environ ["JENKINS_PASSWORD" ]))
97+ responses .append (request .html .html )
98+ contents = "\n " .join (responses )
99+ else :
100+ click .echo (ctx .get_help ())
101+ return
102+
103+ all_tests = set (TEST_NAMES_REGEX .findall (contents ))
104+ expression = " or " .join (all_tests )
105+ if not dont_capture :
106+ dont_capture = ""
107+ shell (f"pytest -vv { dont_capture } -k '{ expression } '" )
108+
109+
77110class PyPICommands :
78111 """Commands executed by this helper script."""
79112
@@ -170,8 +203,14 @@ def changelog():
170203 shell (f"{ PyPICommands .CHANGELOG } -u | less" )
171204
172205
173- @click .command ()
174- def poetry_setup_py ():
206+ @click .group ()
207+ def extra_poetry ():
208+ """Extra commands for poetry."""
209+ pass
210+
211+
212+ @extra_poetry .command ()
213+ def setup_py ():
175214 """Use poetry to generate a setup.py file from pyproject.toml."""
176215 shell ("poetry build" )
177216 shell ("tar -xvzf dist/*.gz --strip-components 1 */setup.py" )
0 commit comments