Skip to content

Commit 0dfa274

Browse files
authored
add diff style check test (AtsushiSakai#617)
* add diff style check test * add diff style check test * add diff style check test * add diff style check test * add license * add license
1 parent 79f7107 commit 0dfa274

File tree

5 files changed

+120
-18
lines changed

5 files changed

+120
-18
lines changed

.github/workflows/Linux_CI.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ jobs:
3232
run: pip install coverage
3333
- name: install mypy
3434
run: pip install mypy
35-
- name: install pycodestyle
36-
run: pip install pycodestyle
37-
- name: install pytest-xdist
38-
run: pip install pytest-xdist
3935
- name: mypy check
4036
run: |
4137
mypy -p AerialNavigation
@@ -47,8 +43,6 @@ jobs:
4743
mypy -p PathPlanning
4844
mypy -p PathTracking
4945
mypy -p SLAM
50-
- name: do diff style check
51-
run: bash rundiffstylecheck.sh
5246
- name: do all unit tests
5347
run: bash runtests.sh
5448

.github/workflows/MacOS_CI.yml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ jobs:
3939
run: pip install coverage
4040
- name: install mypy
4141
run: pip install mypy
42-
- name: install pycodestyle
43-
run: pip install pycodestyle
44-
- name: install pytest-xdist
45-
run: pip install pytest-xdist
4642
- name: mypy check
4743
run: |
4844
mypy -p AerialNavigation
@@ -54,12 +50,5 @@ jobs:
5450
mypy -p PathPlanning
5551
mypy -p PathTracking
5652
mypy -p SLAM
57-
- name: do diff style check
58-
run: bash rundiffstylecheck.sh
59-
6053
- name: do all unit tests
61-
run: bash runtests.sh
62-
63-
64-
65-
54+
run: bash runtests.sh

docs/how_to_contribute_main.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ At the least, try to run the example code without animation in the unit test.
5050

5151
If you want to run the test suites locally, you can use the `runtests.sh` script by just executing it.
5252

53+
The `test_diff_codestyle.py`_ check code style for your PR's codes.
54+
5355

5456
.. _`how to write doc`:
5557

@@ -153,6 +155,7 @@ Sponsors
153155
.. _`reStructuredText`: https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html
154156
.. _`doc modules dir`: https://github.com/AtsushiSakai/PythonRobotics/tree/master/docs/modules
155157
.. _`doc README`: https://github.com/AtsushiSakai/PythonRobotics/blob/master/docs/README.md
158+
.. _`test_diff_codestyle.py`: https://github.com/AtsushiSakai/PythonRobotics/blob/master/tests/test_diff_codestyle.py
156159
.. _`JetBrains`: https://www.jetbrains.com/
157160
.. _`Sponsor @AtsushiSakai on GitHub Sponsors`: https://github.com/sponsors/AtsushiSakai
158161
.. _`Become a backer or sponsor on Patreon`: https://www.patreon.com/myenigma

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ matplotlib == 3.5.1
55
cvxpy == 1.1.18
66
pytest == 6.2.5 # For unit test
77
pytest-xdist == 2.5.0 # For unit test
8+
flake8 == 4.0.1 # For unit test
89
sphinx == 4.3.2 # For sphinx documentation
910
sphinx_rtd_theme == 1.0.0
1011
IPython == 7.30.1 # For sphinx documentation

tests/test_diff_codestyle.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
"""
2+
Diff based code style checker with flake8
3+
4+
This code come from:
5+
https://github.com/scipy/scipy/blob/main/tools/lint_diff.py
6+
7+
Scipy's licence: https://github.com/scipy/scipy/blob/main/LICENSE.txt
8+
Copyright (c) 2001-2002 Enthought, Inc. 2003-2022, SciPy Developers.
9+
All rights reserved.
10+
11+
Redistribution and use in source and binary forms, with or without
12+
modification, are permitted provided that the following conditions
13+
are met:
14+
15+
1. Redistributions of source code must retain the above copyright
16+
notice, this list of conditions and the following disclaimer.
17+
18+
2. Redistributions in binary form must reproduce the above
19+
copyright notice, this list of conditions and the following
20+
disclaimer in the documentation and/or other materials provided
21+
with the distribution.
22+
23+
3. Neither the name of the copyright holder nor the names of its
24+
contributors may be used to endorse or promote products derived
25+
from this software without specific prior written permission.
26+
27+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38+
"""
39+
import conftest
40+
import subprocess
41+
42+
43+
def rev_list(branch, num_commits):
44+
"""List commits in reverse chronological order.
45+
Only the first `num_commits` are shown.
46+
"""
47+
res = subprocess.run(
48+
[
49+
'git',
50+
'rev-list',
51+
'--max-count',
52+
f'{num_commits}',
53+
'--first-parent',
54+
branch
55+
],
56+
stdout=subprocess.PIPE,
57+
encoding='utf-8',
58+
)
59+
res.check_returncode()
60+
return res.stdout.rstrip('\n').split('\n')
61+
62+
63+
def find_branch_point(branch):
64+
"""Find when the current branch split off from the given branch.
65+
It is based off of this Stackoverflow post:
66+
https://stackoverflow.com/questions/1527234/finding-a-branch-point-with-git#4991675
67+
"""
68+
branch_commits = rev_list('HEAD', 1000)
69+
main_commits = set(rev_list(branch, 1000))
70+
for branch_commit in branch_commits:
71+
if branch_commit in main_commits:
72+
return branch_commit
73+
74+
# If a branch split off over 1000 commits ago we will fail to find
75+
# the ancestor.
76+
raise RuntimeError(
77+
'Failed to find a common ancestor in the last 1000 commits')
78+
79+
80+
def find_diff(sha):
81+
"""Find the diff since the given sha."""
82+
files = ['*.py']
83+
res = subprocess.run(
84+
['git', 'diff', '--unified=0', sha, '--'] + files,
85+
stdout=subprocess.PIPE,
86+
encoding='utf-8'
87+
)
88+
res.check_returncode()
89+
return res.stdout
90+
91+
92+
def run_flake8(diff):
93+
"""Run flake8 on the given diff."""
94+
res = subprocess.run(
95+
['flake8', '--diff'],
96+
input=diff,
97+
stdout=subprocess.PIPE,
98+
encoding='utf-8',
99+
)
100+
return res.returncode, res.stdout
101+
102+
103+
def test():
104+
branch_commit = find_branch_point("origin/master")
105+
diff = find_diff(branch_commit)
106+
rc, errors = run_flake8(diff)
107+
if errors:
108+
print(errors)
109+
else:
110+
print("No lint errors found.")
111+
assert rc == 0
112+
113+
114+
if __name__ == '__main__':
115+
conftest.run_this_test(__file__)

0 commit comments

Comments
 (0)