Skip to content

Commit 21e402b

Browse files
committed
Create primer test runner
1 parent 4d78e89 commit 21e402b

File tree

1 file changed

+105
-0
lines changed
  • pydocstringformatter/testutils/primer

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import subprocess
2+
import sys
3+
from pathlib import Path
4+
from typing import Dict, List
5+
6+
from pydocstringformatter.testutils.primer.const import DIFF_OUTPUT
7+
from pydocstringformatter.testutils.primer.packages import PACKAGES, _PackageToPrime
8+
9+
10+
def _fix_diff(output: str, package: _PackageToPrime) -> str:
11+
"""Make the diff more readable and useful."""
12+
new_output: List[str] = []
13+
14+
for index, line in enumerate(output.splitlines()):
15+
if line.startswith("--- "):
16+
if index:
17+
new_output.append("```\n")
18+
19+
link = line.replace("--- ../.pydocstringformatter_primer_tests/", "")
20+
file = "/".join(link.split("/")[2:])
21+
22+
new_output.append(f"{package.url}/blob/{package.branch}/{file}")
23+
new_output.append("```diff")
24+
25+
new_output.append(line)
26+
27+
return "\n".join(new_output)
28+
29+
30+
def _run_prepare() -> None:
31+
"""Prepare everything for the primer to be run.
32+
33+
This clones all packages that need to be 'primed' and
34+
does any other necessary setup.
35+
"""
36+
for package in PACKAGES.values():
37+
package.lazy_clone()
38+
39+
print("## Preparation of primer successful!")
40+
41+
42+
def _run_step_one() -> None:
43+
"""Run program over all packages in write mode.
44+
45+
Runs the program in write mode over all packages that need
46+
to be 'primed'. This should be run when the local repository
47+
is checked out to upstream/main.
48+
"""
49+
for package in PACKAGES.values():
50+
subprocess.run(
51+
[sys.executable, "-m", "pydocstringformatter", "-w"]
52+
+ package.paths_to_lint,
53+
cwd=Path(__file__).parent.parent.parent,
54+
capture_output=True,
55+
text=True,
56+
check=False,
57+
)
58+
59+
print("## Step one of primer successful!")
60+
61+
62+
def _run_step_two() -> None:
63+
"""Run program over all packages and store the diff.
64+
65+
This reiterates over all packages that need to be 'primed',
66+
runs the program in diff mode and stores the output to file.
67+
"""
68+
output: Dict[str, str] = {}
69+
70+
for name, package in PACKAGES.items():
71+
process = subprocess.run(
72+
[sys.executable, "-m", "pydocstringformatter"] + package.paths_to_lint,
73+
cwd=Path(__file__).parent.parent.parent,
74+
capture_output=True,
75+
text=True,
76+
check=False,
77+
)
78+
output[name] = _fix_diff(process.stdout, package)
79+
80+
final_output = ""
81+
for name, string in output.items():
82+
if string.startswith("Nothing to do!"):
83+
continue
84+
final_output += f"**{name}:**\n\n{string}\n\n"
85+
86+
with open(DIFF_OUTPUT, "w", encoding="utf-8") as file:
87+
file.write(final_output)
88+
89+
print("## Step two of primer successful!")
90+
91+
92+
def _run_primer() -> None:
93+
"""Run the primer test."""
94+
args = sys.argv[1:]
95+
96+
if "--prepare" in args:
97+
_run_prepare()
98+
elif "--step-one" in args:
99+
_run_step_one()
100+
elif "--step-two" in args:
101+
_run_step_two()
102+
103+
104+
if __name__ == "__main__":
105+
_run_primer()

0 commit comments

Comments
 (0)