-
-
Notifications
You must be signed in to change notification settings - Fork 8
Add primer tests #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add primer tests #78
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4052831
Add primer directory to gitignore
DanielNoord 2f9881d
Add gitpython as dependency
DanielNoord 477ea25
Create primer testutil package
DanielNoord 3a2bac5
Set up constants for primer
DanielNoord 282d0ec
Set up ``_PackageToPrime``
DanielNoord c71f75a
Create primer test runner
DanielNoord 180af64
Set up primer workflow
DanielNoord 57788a4
Write basic documentation about the primer
DanielNoord File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Most of this is inspired by the mypy primer | ||
|
||
name: Primer | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
primer-run: | ||
name: Run | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out working version | ||
uses: actions/[email protected] | ||
with: | ||
fetch-depth: 2 | ||
- name: Check out changeable version | ||
uses: actions/[email protected] | ||
with: | ||
path: program_to_test | ||
fetch-depth: 0 | ||
- name: Set up Python 3.10 | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: "3.10" | ||
- name: Install dependencies | ||
run: pip install -U -r requirements-test.txt | ||
- name: Run primer | ||
shell: bash | ||
run: | | ||
cd program_to_test | ||
echo "new commit" | ||
git rev-list --format=%s --max-count=1 $GITHUB_SHA | ||
|
||
MERGE_BASE=$(git merge-base $GITHUB_SHA origin/main) | ||
git checkout -b base_commit $MERGE_BASE | ||
echo "base commit" | ||
git rev-list --format=%s --max-count=1 base_commit | ||
|
||
echo "installing changeable version" | ||
pip install -e . | ||
cd .. | ||
|
||
python -m pydocstringformatter.testutils.primer.primer --prepare | ||
python -m pydocstringformatter.testutils.primer.primer --step-one | ||
|
||
cd program_to_test | ||
git checkout $GITHUB_SHA | ||
cd .. | ||
python -m pydocstringformatter.testutils.primer.primer --step-two | ||
- name: Post comment | ||
id: post-comment | ||
uses: actions/github-script@v3 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const fs = require('fs') | ||
const data = fs.readFileSync('.pydocstringformatter_primer_tests/fulldiff.txt', { encoding: 'utf8' }) | ||
console.log("Diff from primer:") | ||
console.log(data) | ||
let body | ||
if (data.trim()) { | ||
body = 'Diff from the primer, showing the effect of this PR on open source code:\n' + data | ||
} else { | ||
body = 'According to the primer, this change has no effect on the checked open source code. 🤖🎉' | ||
} | ||
await github.issues.createComment({ | ||
issue_number: ${{ github.event.pull_request.number }}, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body | ||
}) | ||
- name: Hide old comments | ||
# Taken from mypy primer | ||
# v0.3.0 | ||
uses: kanga333/comment-hider@bbdf5b562fbec24e6f60572d8f712017428b92e0 | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
leave_visible: 1 | ||
issue_number: ${{ steps.post-comment.outputs.result }} |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.pydocstringformatter_primer_tests | ||
pydocstringformatter.egg-info | ||
dist/ | ||
docs/_build |
This file contains hidden or 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
Empty file.
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from pathlib import Path | ||
|
||
PRIMER_DIRECTORY_PATH = ( | ||
Path(__file__).parent.parent.parent.parent / ".pydocstringformatter_primer_tests" | ||
) | ||
"""Directory to store anything primer related in.""" | ||
|
||
DIFF_OUTPUT = PRIMER_DIRECTORY_PATH / "fulldiff.txt" | ||
"""Diff output file location.""" |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import logging | ||
import shutil | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import Dict, List, Union | ||
|
||
import git | ||
|
||
from pydocstringformatter.testutils.primer.const import PRIMER_DIRECTORY_PATH | ||
|
||
|
||
@dataclass | ||
class _PackageToPrime: | ||
"""Represents data about a package to be tested during primer tests.""" | ||
|
||
url: str | ||
"""URL of the repository to clone.""" | ||
|
||
branch: str | ||
"""Branch of the repository to clone.""" | ||
|
||
directories: List[str] | ||
"""Directories within the repository to run the program over.""" | ||
|
||
@property | ||
def clone_directory(self) -> Path: | ||
"""Directory to clone repository into.""" | ||
clone_name = "/".join(self.url.split("/")[-2:]).replace(".git", "") | ||
return PRIMER_DIRECTORY_PATH / clone_name | ||
|
||
@property | ||
def paths_to_lint(self) -> List[str]: | ||
"""The paths we need to run against.""" | ||
return [str(self.clone_directory / path) for path in self.directories] | ||
|
||
def lazy_clone(self) -> None: | ||
"""Clone the repo or pull any new commits. | ||
|
||
# TODO(#80): Allow re-using an already cloned repistory instead of removing it | ||
Currently this is therefore not really 'lazy'. | ||
""" | ||
logging.info("Lazy cloning %s", self.url) | ||
|
||
if self.clone_directory.exists(): | ||
shutil.rmtree(self.clone_directory) | ||
|
||
options: Dict[str, Union[str, int]] = { | ||
"url": self.url, | ||
"to_path": str(self.clone_directory), | ||
"branch": self.branch, | ||
"depth": 1, | ||
} | ||
git.Repo.clone_from(**options) | ||
|
||
|
||
PACKAGES = { | ||
"pylint": _PackageToPrime( | ||
"https://github.com/PyCQA/pylint", | ||
"main", | ||
["pylint"], | ||
), | ||
"pydocstringformatter": _PackageToPrime( | ||
"https://github.com/DanielNoord/pydocstringformatter", | ||
"main", | ||
["pydocstringformatter"], | ||
), | ||
} |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
from typing import Dict, List | ||
|
||
from pydocstringformatter.testutils.primer.const import DIFF_OUTPUT | ||
from pydocstringformatter.testutils.primer.packages import PACKAGES, _PackageToPrime | ||
|
||
|
||
def _fix_diff(output: str, package: _PackageToPrime) -> str: | ||
"""Make the diff more readable and useful.""" | ||
new_output: List[str] = [] | ||
|
||
for index, line in enumerate(output.splitlines()): | ||
if line.startswith("--- "): | ||
if index: | ||
new_output.append("```\n") | ||
|
||
link = line.replace("--- ../.pydocstringformatter_primer_tests/", "") | ||
file = "/".join(link.split("/")[2:]) | ||
|
||
new_output.append(f"{package.url}/blob/{package.branch}/{file}") | ||
new_output.append("```diff") | ||
|
||
new_output.append(line) | ||
|
||
return "\n".join(new_output) | ||
|
||
|
||
def _run_prepare() -> None: | ||
"""Prepare everything for the primer to be run. | ||
|
||
This clones all packages that need to be 'primed' and | ||
does any other necessary setup. | ||
""" | ||
for package in PACKAGES.values(): | ||
package.lazy_clone() | ||
|
||
print("## Preparation of primer successful!") | ||
|
||
|
||
def _run_step_one() -> None: | ||
"""Run program over all packages in write mode. | ||
|
||
Runs the program in write mode over all packages that need | ||
to be 'primed'. This should be run when the local repository | ||
is checked out to upstream/main. | ||
""" | ||
for package in PACKAGES.values(): | ||
subprocess.run( | ||
[sys.executable, "-m", "pydocstringformatter", "-w"] | ||
+ package.paths_to_lint, | ||
cwd=Path(__file__).parent.parent.parent, | ||
capture_output=True, | ||
text=True, | ||
check=False, | ||
) | ||
|
||
print("## Step one of primer successful!") | ||
|
||
|
||
def _run_step_two() -> None: | ||
"""Run program over all packages and store the diff. | ||
|
||
This reiterates over all packages that need to be 'primed', | ||
runs the program in diff mode and stores the output to file. | ||
""" | ||
output: Dict[str, str] = {} | ||
|
||
for name, package in PACKAGES.items(): | ||
process = subprocess.run( | ||
[sys.executable, "-m", "pydocstringformatter"] + package.paths_to_lint, | ||
cwd=Path(__file__).parent.parent.parent, | ||
capture_output=True, | ||
text=True, | ||
check=False, | ||
) | ||
output[name] = _fix_diff(process.stdout, package) | ||
|
||
final_output = "" | ||
for name, string in output.items(): | ||
if string.startswith("Nothing to do!"): | ||
continue | ||
final_output += f"**{name}:**\n\n{string}\n\n" | ||
|
||
with open(DIFF_OUTPUT, "w", encoding="utf-8") as file: | ||
file.write(final_output) | ||
|
||
print("## Step two of primer successful!") | ||
|
||
|
||
def _run_primer() -> None: | ||
"""Run the primer test.""" | ||
args = sys.argv[1:] | ||
|
||
if "--prepare" in args: | ||
_run_prepare() | ||
elif "--step-one" in args: | ||
_run_step_one() | ||
elif "--step-two" in args: | ||
_run_step_two() | ||
|
||
|
||
if __name__ == "__main__": | ||
_run_primer() |
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could create a test with pytest for that ? It seem automatable even if not very convenient because you need to duplicate the git repository.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think it does not offer much: when would the test pass or fail?
At some point I could explore trying to create a script that does the same and handles cloning the repository, but for now I think this is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha right it makes no sense as a test. A script to set it up is more like it.