File: test_main.py

package info (click to toggle)
modernize 0.9-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 416 kB
  • sloc: python: 2,016; makefile: 145
file content (60 lines) | stat: -rw-r--r-- 1,306 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import generator_stop

import contextlib
import io

from utils import check_on_input

from modernize.__main__ import main as modernize_main


def test_list_fixers():
    stdout = io.StringIO()
    with contextlib.redirect_stdout(stdout):
        returncode = modernize_main(["-l"])
    assert returncode == 0
    assert "xrange_six" in stdout.getvalue()


def test_nofix_fixers(tmp_path):
    stdout = io.StringIO()
    stderr = io.StringIO()
    with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
        returncode = modernize_main(["--nofix=ham", str(tmp_path)])

    assert returncode == 2
    assert stderr.getvalue() == "Error: fix 'ham' was not found\n"
    assert stdout.getvalue() == ""


NO_SIX_SAMPLE = """\
a = range(10)

class B(object):
    __metaclass__ = Meta
"""

EXPECTED_SIX_RESULT = """\
from __future__ import absolute_import
import six
from six.moves import range
a = list(range(10))

class B(six.with_metaclass(Meta, object)):
    pass
"""


def test_no_six():
    check_on_input(
        NO_SIX_SAMPLE, NO_SIX_SAMPLE, extra_flags=["--no-six"], expected_return_code=0
    )


def test_enforce():
    check_on_input(
        NO_SIX_SAMPLE,
        EXPECTED_SIX_RESULT,
        extra_flags=["--enforce"],
        expected_return_code=2,
    )