Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.15.9
hooks:
- id: ruff-check
args: [--fix]
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
12 changes: 12 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ push-changes = false
tag-format = "v{version}"
tag-message = "unittest2pytest {version}"
tag-signing = true

[tool.ruff]
target-version = "py39"
extend-exclude = ["tests/fixtures"]

[tool.ruff.lint]
select = ["E", "F", "I", "W", "UP"]
ignore = ["E501", "E701", "E741", "UP031"]

[tool.ruff.lint.per-file-ignores]
"unittest2pytest/fixes/fix_remove_class.py" = ["W291"]
"unittest2pytest/fixes/fix_self_assert.py" = ["F841"]
81 changes: 50 additions & 31 deletions tests/test_all_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,43 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#


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


import pytest


import glob
import logging
import os
from os.path import join, abspath
import re
import glob
import shutil
from difflib import unified_diff
import unittest
import logging
from difflib import unified_diff
from os.path import abspath, join

import pytest
from fissix.main import main

# make logging less verbose
logging.getLogger('fissix.main').setLevel(logging.WARN)
logging.getLogger('RefactoringTool').setLevel(logging.WARN)
logging.getLogger("fissix.main").setLevel(logging.WARN)
logging.getLogger("RefactoringTool").setLevel(logging.WARN)

FIXTURE_PATH = os.path.join(os.path.dirname(__file__), "fixtures")

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

def requiredTestMethod(name):
# skip if TestCase does not have this method
is_missing = getattr(unittest.TestCase, name, None) is None
return pytest.mark.skipif(is_missing,
reason="unittest does not have TestCase.%s " % name)
return pytest.mark.skipif(
is_missing, reason="unittest does not have TestCase.%s " % name
)


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

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


@pytest.mark.parametrize("fixer, in_file",
collect_all_test_fixtures(), ids=_get_id)
@pytest.mark.parametrize("fixer, in_file", collect_all_test_fixtures(), ids=_get_id)
def test_check_fixture(in_file, fixer, tmpdir):
if fixer:
main("unittest2pytest.fixes",
args=['--no-diffs', '--fix', fixer, '-w', in_file,
'--nobackups', '--output-dir', str(tmpdir)])
main(
"unittest2pytest.fixes",
args=[
"--no-diffs",
"--fix",
fixer,
"-w",
in_file,
"--nobackups",
"--output-dir",
str(tmpdir),
],
)
else:
main("unittest2pytest.fixes",
args=['--no-diffs', '--fix', 'all', '-w', in_file,
'--nobackups', '--output-dir', str(tmpdir)])
main(
"unittest2pytest.fixes",
args=[
"--no-diffs",
"--fix",
"all",
"-w",
in_file,
"--nobackups",
"--output-dir",
str(tmpdir),
],
)

result_file_name = tmpdir.join(os.path.basename(in_file))
assert result_file_name.exists(), '%s is missing' % result_file_name
assert result_file_name.exists(), "%s is missing" % result_file_name
result_file_contents = result_file_name.readlines()

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

# ensure the expected code is actually correct and compiles
try:
compile(''.join(expected_contents), expected_file, 'exec')
compile("".join(expected_contents), expected_file, "exec")
except Exception as e:
pytest.fail(f"FATAL: {expected_file} does not compile: {e}",
False)
pytest.fail(f"FATAL: {expected_file} does not compile: {e}", False)

if result_file_contents != expected_contents:
text = "Refactured code doesn't match expected outcome\n"
text += ''.join(unified_diff(expected_contents, result_file_contents,
'expected', 'refactured result'))
text += "".join(
unified_diff(
expected_contents, result_file_contents, "expected", "refactured result"
)
)
pytest.fail(text, False)
4 changes: 2 additions & 2 deletions unittest2pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
__licence__ = "GNU General Public License version 3 or later (GPLv3+)"


__title__ = 'unittest2pytest'
__version__ = '0.6.dev0'
__title__ = "unittest2pytest"
__version__ = "0.6.dev0"
5 changes: 4 additions & 1 deletion unittest2pytest/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@


import fissix.main

from . import fixes


def main():
raise SystemExit(fissix.main.main(fixes.__name__))

if __name__ == '__main__':

if __name__ == "__main__":
main()
7 changes: 3 additions & 4 deletions unittest2pytest/fixes/fix_remove_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


from fissix.fixer_base import BaseFix
from fissix.fixer_util import token, find_indentation
from fissix.fixer_util import find_indentation, token

"""
Node(classdef,
Expand All @@ -47,8 +47,8 @@
Leaf(6, '')])])
"""

class FixRemoveClass(BaseFix):

class FixRemoveClass(BaseFix):
PATTERN = """
classdef< 'class' name=any '(' 'TestCase' ')' ':'
suite=suite
Expand All @@ -67,10 +67,9 @@ def dedent(self, suite, dedent):
# todo: handle tabs
if len(kid.prefix) > len(self.current_indent):
kid.prefix = self.current_indent


def transform(self, node, results):
suite = results['suite'].clone()
suite = results["suite"].clone()
# todo: handle tabs
dedent = len(find_indentation(suite)) - len(find_indentation(node))
self.dedent(suite, dedent)
Expand Down
Loading
Loading