Created
March 13, 2023 14:49
-
-
Save brouberol/62cb62b8068ff0fbc19c4f7655fb1388 to your computer and use it in GitHub Desktop.
Fetches failed test info from circleCI and run the tests locally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
import os | |
import sys | |
import difflib | |
import subprocess | |
from pathlib import Path | |
def is_camel_case(s): | |
return s != s.lower() and s != s.upper() and "_" not in s | |
def join_overlapping_path(p1, p2): | |
sm = difflib.SequenceMatcher(None, p1, p2) | |
p1i, p2i, size = sm.get_matching_blocks()[0] | |
if not p1i or not p2i: | |
None | |
p1, p2 = (p1, p2) if p2i == 0 else (p2, p1) | |
size = sm.get_matching_blocks()[0].size | |
return p1 + p2[size:] | |
job_id = sys.argv[1] | |
cwd = Path.cwd() | |
tests_data = requests.get( | |
f"https://circleci.com/api/v2/project/github/alan-eu/alan-backend/{job_id}/tests", | |
headers={"Circle-Token": os.environ["CIRCLE_TOKEN"]}, | |
).json() | |
failed_tests = [test for test in tests_data["items"] if test["result"] == "failure"] | |
command = ["poetry", "run", "pytest"] | |
for failed_test in failed_tests: | |
classname = failed_test["classname"].split(".")[-1] | |
test_file = Path( | |
join_overlapping_path(str(cwd), str(failed_test["file"])) | |
).relative_to(cwd) | |
if is_camel_case(classname): | |
# unittest | |
test_path = f"{test_file}::{classname}::{failed_test['name']}" | |
else: | |
# pytest | |
test_path = f"{test_file}::{failed_test['name']}" | |
command.append(test_path) | |
print(" ".join(command)) | |
subprocess.run(command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment