def test_no_change():
    patterns = get_patterns(dbl_quote_docstring)
    text = """
    def derp():
        '''inside\"\"\"str'''
"""
    assert process(patterns, text) == (text)
Example #2
0
def test_nl_at_eof():
    patterns = get_patterns(nl_at_eof)
    text = """
herp derp"""
    assert process(patterns, text) == ("""
herp derp
""")
Example #3
0
def test_function_used():
    patterns = get_patterns(remove_unused_import)
    text = """
from function_lives_here import function

derp herp function(args) herp derp
"""
    assert process(patterns, text) == text
def test_file_without_unicode_literals():
    patterns = get_patterns(remove_needless_u_specifier)
    text = """
from __future__ import print_statement, absolute_import

derp herp u"some string" herp derp
"""
    assert process(patterns, text) == text
def test_dbl_quote_docstring():
    patterns = get_patterns(dbl_quote_docstring)
    text = """
    def derp():
        '''doc'''
"""
    assert process(patterns, text) == ('''
    def derp():
        """doc"""
''')
Example #6
0
def test_match_method_but_not_import():
    patterns = get_patterns(method_to_function)
    text = """
from function_lives_here import function
obj.method().derp
"""
    assert process(patterns, text) == ("""
from function_lives_here import function
function(obj).derp
""")
Example #7
0
def test_exec_function():
    patterns = get_patterns(exec_function)
    text = """
        exec stra in globals()
        exec strb in globals(), locals()
"""
    assert process(patterns, text) == ("""
        exec(stra, globals())
        exec(strb, globals(), locals())
""")
def test_file_with_unicode_literals():
    patterns = get_patterns(remove_needless_u_specifier)
    text = """
from __future__ import unicode_literals

derp herp u"some string" herp derp
"""
    assert process(patterns, text) == ("""
from __future__ import unicode_literals

derp herp "some string" herp derp
""")
Example #9
0
def test_attribute_to_function():
    patterns = get_patterns(attribute_to_function)
    text = """
        obj.attribute
        h(obj.a.b(c).d[e].attribute)
"""
    assert process(patterns,
                   text) == ("""from function_lives_here import function

        function(obj)
        h(function(obj.a.b(c).d[e]))
""")
Example #10
0
def _process_file(patterns, text_file):
    log.info('undebting {}'.format(text_file))

    text = _load_text(text_file)

    try:
        result_text = process(patterns, text)
    except Exception:
        log.exception(traceback.format_exc())
        return False
    else:
        _write_result_text(result_text, text_file)
        return True
Example #11
0
def process(patterns, text_file, dry_run):
    log.info('undebting {}'.format(text_file))

    text = _load_text(text_file)

    try:
        result_text = logic.process(patterns, text)
    except Exception:
        log.exception(traceback.format_exc())
        return False
    else:
        if result_text != text:
            _write_result_text(result_text, text_file, dry_run)
        return True
def test_ignore_u_at_end_of_string():
    patterns = get_patterns(remove_needless_u_specifier)
    text = """
from __future__ import unicode_literals

subprocess.check_call(('mysql',
                       '-u', 'test_user',))
TITLE_LABELS = {
    "en": ("Subject", ),
    "ru": ("Тема", ),
    "fr": ("Objet", "Sujet"),
}
"""
    assert process(patterns, text) == text
Example #13
0
def test_hex_to_bitshift():
    patterns = get_patterns(hex_to_bitshift)
    text = """
0x001
0x002
0x003
0x004
"""
    assert process(patterns, text) == ("""
1 << 0
1 << 1
0x003
1 << 2
""")
Example #14
0
def test_sqla_count():
    patterns = get_patterns(sqla_count)
    text = """
    session.query(
        derp,
    ).filter(
        herp,
    ).count()
"""
    assert process(patterns, text) == (
        """import sqlalchemy

    session.query(
        sqlalchemy.func.count(),
    ).filter(
        herp,
    ).scalar()
""")
Example #15
0
def test_no_match():
    patterns = get_patterns(attribute_to_function)
    text = """
    derp.herp
    """
    assert process(patterns, text) == text
Example #16
0
def test_no_import():
    patterns = get_patterns(remove_unused_import)
    text = """
derp herp function(args) herp derp
"""
    assert process(patterns, text) == text
Example #17
0
def test_contextlib_nested():
    patterns = get_patterns(contextlib_nested)
    text = """
import mock
import contextlib
from contextlib import nested


with contextlib.nested(
        mock.patch(
            'os.path.join'),
        mock.patch('os.mkdir'),
        mock.patch('os.chdir'),
):
    pass


with nested(
        mock.patch(
            'os.path.join'
        ),
        mock.patch('os.mkdir'),
        mock.patch('os.chdir'),
):
    pass


def a_function():
    with contextlib.nested(
            mock.patch(
                'lots of stuff',
                'in this mock',
                return_value=None
            ), mock.patch(
                'reformatting really stinks'
            ),
    ):
        return None

with contextlib.nested(a, b, c) as (x, y, z):
    pass
"""
    assert process(patterns, text) == ("""
import mock
import contextlib
from contextlib import nested


with mock.patch(
            'os.path.join'), \\
    mock.patch('os.mkdir'), \\
    mock.patch('os.chdir'):
    pass


with mock.patch(
            'os.path.join'
        ), \\
    mock.patch('os.mkdir'), \\
    mock.patch('os.chdir'):
    pass


def a_function():
    with mock.patch(
                'lots of stuff',
                'in this mock',
                return_value=None
            ), \\
        mock.patch(
                'reformatting really stinks'
            ):
        return None

with a as x, \\
    b as y, \\
    c as z:
    pass
""")