Skip to content

Commit 9ce3b43

Browse files
committed
feat: add xpytest script
1 parent 1154a3c commit 9ce3b43

5 files changed

Lines changed: 230 additions & 19 deletions

File tree

clit/constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
"""Constants."""
2+
import re
23
from pathlib import Path
34

45
SECTION_SYMLINKS_FILES = "symlinks/files"
56
SECTION_SYMLINKS_DIRS = "symlinks/dirs"
67
PYCHARM_MACOS_APP_PATH = Path("/Applications/PyCharm.app/Contents/MacOS/pycharm")
78
CONFIG_DIR = Path("~/.config/dotfiles/").expanduser()
9+
10+
# Possible formats for tests:
11+
# ___ test_name ___
12+
# ___ Error on setup of test_name ___
13+
# ___ test_name[Parameter] ___
14+
TEST_NAMES_REGEX = re.compile(r"___ .*(test[^\[\] ]+)[\[\]A-Za-z]* ___")

clit/dev.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
import click
1010
from 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
1314
from clit.files import shell
1415
from 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+
77110
class 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

Comments
 (0)