Skip to content

Commit 6c541ab

Browse files
authored
Merge pull request #98 from graingert/pre-commit
Pre commit
2 parents 93d9bd4 + 0f2ae8c commit 6c541ab

8 files changed

Lines changed: 269 additions & 199 deletions

File tree

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.15.9
4+
hooks:
5+
- id: ruff-check
6+
args: [--fix]
7+
- id: ruff-format
8+
- repo: https://github.com/pre-commit/pre-commit-hooks
9+
rev: v6.0.0
10+
hooks:
11+
- id: trailing-whitespace
12+
- id: end-of-file-fixer
13+
- id: check-yaml

pyproject.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,15 @@ push-changes = false
5151
tag-format = "v{version}"
5252
tag-message = "unittest2pytest {version}"
5353
tag-signing = true
54+
55+
[tool.ruff]
56+
target-version = "py39"
57+
extend-exclude = ["tests/fixtures"]
58+
59+
[tool.ruff.lint]
60+
select = ["E", "F", "I", "W", "UP"]
61+
ignore = ["E501", "E701", "E741", "UP031"]
62+
63+
[tool.ruff.lint.per-file-ignores]
64+
"unittest2pytest/fixes/fix_remove_class.py" = ["W291"]
65+
"unittest2pytest/fixes/fix_self_assert.py" = ["F841"]

tests/test_all_fixes.py

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,43 @@
2121
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
#
2323

24-
2524
__author__ = "Hartmut Goebel <h.goebel@crazy-compilers.com>"
2625
__copyright__ = "Copyright 2015-2019 by Hartmut Goebel"
2726
__licence__ = "GNU General Public License version 3 or later (GPLv3+)"
2827

2928

30-
import pytest
31-
32-
29+
import glob
30+
import logging
3331
import os
34-
from os.path import join, abspath
3532
import re
36-
import glob
37-
import shutil
38-
from difflib import unified_diff
3933
import unittest
40-
import logging
34+
from difflib import unified_diff
35+
from os.path import abspath, join
4136

37+
import pytest
4238
from fissix.main import main
4339

4440
# make logging less verbose
45-
logging.getLogger('fissix.main').setLevel(logging.WARN)
46-
logging.getLogger('RefactoringTool').setLevel(logging.WARN)
41+
logging.getLogger("fissix.main").setLevel(logging.WARN)
42+
logging.getLogger("RefactoringTool").setLevel(logging.WARN)
43+
44+
FIXTURE_PATH = os.path.join(os.path.dirname(__file__), "fixtures")
4745

48-
FIXTURE_PATH = os.path.join(os.path.dirname(__file__), 'fixtures')
4946

5047
def requiredTestMethod(name):
5148
# skip if TestCase does not have this method
5249
is_missing = getattr(unittest.TestCase, name, None) is None
53-
return pytest.mark.skipif(is_missing,
54-
reason="unittest does not have TestCase.%s " % name)
50+
return pytest.mark.skipif(
51+
is_missing, reason="unittest does not have TestCase.%s " % name
52+
)
5553

5654

5755
def _collect_in_files_from_directory(directory):
58-
fixture_files = glob.glob(abspath(join(directory, '*_in.py')))
56+
fixture_files = glob.glob(abspath(join(directory, "*_in.py")))
5957
for fixture_file in fixture_files:
6058
with open(fixture_file) as fh:
6159
text = fh.read(200)
62-
l = re.findall(r'^# required-method: (\S+)', text)
60+
l = re.findall(r"^# required-method: (\S+)", text)
6361
method = l[0] if l else None
6462
yield fixture_file, method
6563

@@ -70,7 +68,7 @@ def collect_all_test_fixtures():
7068
# subdirectory, only run the fixer of the subdirectory name, else run
7169
# all fixers.
7270
for in_file, method in _collect_in_files_from_directory(root):
73-
fixer_to_run = root[len(FIXTURE_PATH)+1:] or None
71+
fixer_to_run = root[len(FIXTURE_PATH) + 1 :] or None
7472
marks = []
7573
if method:
7674
marks.append(requiredTestMethod(method))
@@ -82,20 +80,39 @@ def _get_id(argvalue):
8280
return os.path.basename(argvalue).replace("_in.py", "")
8381

8482

85-
@pytest.mark.parametrize("fixer, in_file",
86-
collect_all_test_fixtures(), ids=_get_id)
83+
@pytest.mark.parametrize("fixer, in_file", collect_all_test_fixtures(), ids=_get_id)
8784
def test_check_fixture(in_file, fixer, tmpdir):
8885
if fixer:
89-
main("unittest2pytest.fixes",
90-
args=['--no-diffs', '--fix', fixer, '-w', in_file,
91-
'--nobackups', '--output-dir', str(tmpdir)])
86+
main(
87+
"unittest2pytest.fixes",
88+
args=[
89+
"--no-diffs",
90+
"--fix",
91+
fixer,
92+
"-w",
93+
in_file,
94+
"--nobackups",
95+
"--output-dir",
96+
str(tmpdir),
97+
],
98+
)
9299
else:
93-
main("unittest2pytest.fixes",
94-
args=['--no-diffs', '--fix', 'all', '-w', in_file,
95-
'--nobackups', '--output-dir', str(tmpdir)])
100+
main(
101+
"unittest2pytest.fixes",
102+
args=[
103+
"--no-diffs",
104+
"--fix",
105+
"all",
106+
"-w",
107+
in_file,
108+
"--nobackups",
109+
"--output-dir",
110+
str(tmpdir),
111+
],
112+
)
96113

97114
result_file_name = tmpdir.join(os.path.basename(in_file))
98-
assert result_file_name.exists(), '%s is missing' % result_file_name
115+
assert result_file_name.exists(), "%s is missing" % result_file_name
99116
result_file_contents = result_file_name.readlines()
100117

101118
expected_file = in_file.replace("_in.py", "_out.py")
@@ -104,13 +121,15 @@ def test_check_fixture(in_file, fixer, tmpdir):
104121

105122
# ensure the expected code is actually correct and compiles
106123
try:
107-
compile(''.join(expected_contents), expected_file, 'exec')
124+
compile("".join(expected_contents), expected_file, "exec")
108125
except Exception as e:
109-
pytest.fail(f"FATAL: {expected_file} does not compile: {e}",
110-
False)
126+
pytest.fail(f"FATAL: {expected_file} does not compile: {e}", False)
111127

112128
if result_file_contents != expected_contents:
113129
text = "Refactured code doesn't match expected outcome\n"
114-
text += ''.join(unified_diff(expected_contents, result_file_contents,
115-
'expected', 'refactured result'))
130+
text += "".join(
131+
unified_diff(
132+
expected_contents, result_file_contents, "expected", "refactured result"
133+
)
134+
)
116135
pytest.fail(text, False)

unittest2pytest/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
__licence__ = "GNU General Public License version 3 or later (GPLv3+)"
2323

2424

25-
__title__ = 'unittest2pytest'
26-
__version__ = '0.6.dev0'
25+
__title__ = "unittest2pytest"
26+
__version__ = "0.6.dev0"

unittest2pytest/__main__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323

2424

2525
import fissix.main
26+
2627
from . import fixes
2728

29+
2830
def main():
2931
raise SystemExit(fissix.main.main(fixes.__name__))
3032

31-
if __name__ == '__main__':
33+
34+
if __name__ == "__main__":
3235
main()

unittest2pytest/fixes/fix_remove_class.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
from fissix.fixer_base import BaseFix
30-
from fissix.fixer_util import token, find_indentation
30+
from fissix.fixer_util import find_indentation, token
3131

3232
"""
3333
Node(classdef,
@@ -47,8 +47,8 @@
4747
Leaf(6, '')])])
4848
"""
4949

50-
class FixRemoveClass(BaseFix):
5150

51+
class FixRemoveClass(BaseFix):
5252
PATTERN = """
5353
classdef< 'class' name=any '(' 'TestCase' ')' ':'
5454
suite=suite
@@ -67,10 +67,9 @@ def dedent(self, suite, dedent):
6767
# todo: handle tabs
6868
if len(kid.prefix) > len(self.current_indent):
6969
kid.prefix = self.current_indent
70-
7170

7271
def transform(self, node, results):
73-
suite = results['suite'].clone()
72+
suite = results["suite"].clone()
7473
# todo: handle tabs
7574
dedent = len(find_indentation(suite)) - len(find_indentation(node))
7675
self.dedent(suite, dedent)

0 commit comments

Comments
 (0)