示例#1
0
def test_patch_future_doesnt_inherit(tmpdir):
    # This test module has 'division' imported, test file doesn't
    assert division
    tmpdir.join('no_future_division.py').write(
        dedent("""\
        def sample():
            return 1 / 2
    """))
    sys.path.insert(0, six.text_type(tmpdir))

    try:
        from no_future_division import sample
    finally:
        sys.path.pop(0)

    assert sample() == 0

    patchy.patch(
        sample, """\
        @@ -1,2 +1,3 @@
         def sample():
        +    pass
             return 1 / 2
        """)

    assert sample() == 0
示例#2
0
def test_patch_future_python_3_6(tmpdir):
    tmpdir.join("future_user.py").write(
        dedent("""\
        from __future__ import generator_stop

        def f(x):
            raise StopIteration()


        def sample():
            return list(f(x) for x in range(10))
    """))
    sys.path.insert(0, str(tmpdir))

    try:
        from future_user import sample
    finally:
        sys.path.pop(0)

    with pytest.raises(RuntimeError):
        sample()

    patchy.patch(
        sample,
        """\
        @@ -1,2 +1,3 @@
         def sample():
        +    pass
             return list(f(x) for x in range(10))
        """,
    )

    with pytest.raises(RuntimeError):
        sample()
示例#3
0
def test_patch_future_twice(tmpdir):
    tmpdir.join('future_twice.py').write(dedent("""\
        from __future__ import unicode_literals

        def sample():
            return type('example string')
    """))
    sys.path.insert(0, six.text_type(tmpdir))

    try:
        from future_twice import sample
    finally:
        sys.path.pop(0)

    assert sample() is six.text_type

    patchy.patch(sample, """\
        @@ -1,2 +1,3 @@
         def sample():
        +    pass
             return type('example string 2')
        """)

    assert sample() is six.text_type

    patchy.patch(sample, """\
        @@ -1,3 +1,4 @@
         def sample():
             pass
        +    pass
             return type('example string 2')
        """)

    assert sample() is six.text_type
示例#4
0
def test_patch_future(tmpdir):
    tmpdir.join('future_user.py').write(
        dedent("""\
        from __future__ import unicode_literals

        def sample():
            return type('example string')
    """))
    sys.path.insert(0, six.text_type(tmpdir))

    try:
        from future_user import sample
    finally:
        sys.path.pop(0)

    assert sample() is six.text_type

    patchy.patch(
        sample, """\
        @@ -1,2 +1,3 @@
         def sample():
        +    pass
             return type('example string')
        """)

    assert sample() is six.text_type
示例#5
0
def test_patch_nonlocal_fails(tmpdir):
    # Put in separate file since it would SyntaxError on Python 2
    tmpdir.join('py3_nonlocal.py').write(dedent("""\
        variab = 20


        def get_function():
            variab = 15

            def sample():
                nonlocal variab
                multiple = 3
                return variab * multiple

            return sample

        sample = get_function()
    """))
    sys.path.insert(0, six.text_type(tmpdir))
    try:
        from py3_nonlocal import sample
    finally:
        sys.path.pop(0)

    assert sample() == 15 * 3

    patchy.patch(sample, """\
        @@ -2,3 +2,3 @@
             nonlocal variab
        -    multiple = 3
        +    multiple = 4
        """)

    assert sample() == 15 * 4
示例#6
0
def test_patch_freevars_nested():
    def free_func(v):
        return v + " on toast"

    def sample():
        filling = "Chalk"

        def _inner_func():
            return free_func(filling)

        return _inner_func

    patchy.patch(
        sample,
        """\
        @@ -1,2 +1,2 @@
         def sample():
        -    filling = "Chalk"
        +    filling = "Cheese"

             def _inner_func():
        """,
    )

    assert sample()() == "Cheese on toast"
示例#7
0
def test_patch_invalid():
    """
    We need to balk on empty patches
    """
    def sample():
        return 1

    bad_patch = """garbage
        """
    with pytest.raises(ValueError) as excinfo:
        patchy.patch(sample, bad_patch)

    msg = str(excinfo.value)
    expected = dedent("""\
        Could not apply the patch to 'sample'. The message from `patch` was:

        patch: **** Only garbage was found in the patch input.

        The code to patch was:
        def sample():
            return 1

        The patch was:
        garbage
    """)
    assert msg == expected
    assert sample() == 1
示例#8
0
def test_patch_invalid_hunk_2():
    """
    We need to balk on patches that fail on application
    """
    def sample():
        if True:
            print("yes")
        if False:
            print("no")
        return 1

    bad_patch = """\
        @@ -1,2 +1,2 @@
         def sample():
        -    if True:
        +    if False:
        @@ -3,5 +3,5 @@
                 print("yes")
        -    if Falsy:
        +    if Truey:
                 print("no")
        """
    with pytest.raises(ValueError) as excinfo:
        patchy.patch(sample, bad_patch)

    assert "Hunk #2 FAILED" in str(excinfo.value)
    assert sample() == 1
示例#9
0
def test_patch_freevars_order():
    def tastes_good(v):
        return v + ' tastes good'

    def tastes_bad(v):
        return v + ' tastes bad'

    def sample():
        return ', '.join([
            tastes_good('Cheese'),
            tastes_bad('Chalk'),
        ])

    patchy.patch(
        sample, """\
        @@ -1,4 +1,4 @@
         def sample():
             return ', '.join([
        -        tastes_good('Cheese'),
        -        tastes_bad('Chalk'),
        +        tastes_bad('Chalk'),
        +        tastes_good('Cheese'),
             )]
        """)

    assert sample() == 'Chalk tastes bad, Cheese tastes good'
示例#10
0
def test_patch_invalid_hunk_2():
    """
    We need to balk on patches that fail on application
    """
    def sample():
        if True:
            print("yes")
        if False:
            print("no")
        return 1

    bad_patch = """\
        @@ -1,2 +1,2 @@
         def sample():
        -    if True:
        +    if False:
        @@ -3,5 +3,5 @@
                 print("yes")
        -    if Falsy:
        +    if Truey:
                 print("no")
        """
    with pytest.raises(ValueError) as excinfo:
        patchy.patch(sample, bad_patch)

    assert "Hunk #2 FAILED" in str(excinfo.value)
    assert sample() == 1
示例#11
0
def test_patch_freevars_order():
    def tastes_good(v):
        return v + ' tastes good'

    def tastes_bad(v):
        return v + ' tastes bad'

    def sample():
        return ', '.join([
            tastes_good('Cheese'),
            tastes_bad('Chalk'),
        ])

    patchy.patch(sample, """\
        @@ -1,4 +1,4 @@
         def sample():
             return ', '.join([
        -        tastes_good('Cheese'),
        -        tastes_bad('Chalk'),
        +        tastes_bad('Chalk'),
        +        tastes_good('Cheese'),
             )]
        """)

    assert sample() == 'Chalk tastes bad, Cheese tastes good'
示例#12
0
def test_patch_invalid():
    """
    We need to balk on empty patches
    """
    def sample():
        return 1

    bad_patch = """garbage
        """
    with pytest.raises(ValueError) as excinfo:
        patchy.patch(sample, bad_patch)

    msg = str(excinfo.value)
    expected = dedent("""\
        Could not apply the patch to 'sample'. The message from `patch` was:

        patch: **** Only garbage was found in the patch input.

        The code to patch was:
        def sample():
            return 1

        The patch was:
        garbage
    """)
    assert msg == expected
    assert sample() == 1
示例#13
0
def test_patch_simple_no_newline():
    def sample():
        return 1

    patchy.patch(sample, """\
        @@ -2,2 +2,2 @@
        -    return 1
        +    return 2""")
    assert sample() == 2
示例#14
0
def test_patch_simple_no_newline():
    def sample():
        return 1

    patchy.patch(
        sample, """\
        @@ -2,2 +2,2 @@
        -    return 1
        +    return 2""")
    assert sample() == 2
示例#15
0
def test_mc_patchface():
    def sample():
        return 1

    patchy.mc_patchface(sample, """\
        @@ -2,2 +2,2 @@
        -    return 1
        +    return 2
        """)
    assert sample() == 2
示例#16
0
def test_mc_patchface():
    def sample():
        return 1

    patchy.mc_patchface(
        sample, """\
        @@ -2,2 +2,2 @@
        -    return 1
        +    return 2
        """)
    assert sample() == 2
示例#17
0
def test_patch():
    def sample():
        return 1

    patchy.patch(sample, """\
        @@ -1,2 +1,2 @@
         def sample():
        -    return 1
        +    return 9001
        """)
    assert sample() == 9001
示例#18
0
def test_patch():
    def sample():
        return 1

    patchy.patch(
        sample, """\
        @@ -1,2 +1,2 @@
         def sample():
        -    return 1
        +    return 9001
        """)
    assert sample() == 9001
示例#19
0
def test_patch_nonlocal_fails(tmpdir):
    # Put in separate file since it would SyntaxError on Python 2
    tmpdir.join("py3_nonlocal.py").write(
        dedent("""\
        variab = 20


        def get_function():
            variab = 15

            def sample():
                nonlocal variab
                multiple = 3
                return variab * multiple

            return sample

        sample = get_function()
    """))
    sys.path.insert(0, str(tmpdir))
    try:
        from py3_nonlocal import sample
    finally:
        sys.path.pop(0)

    assert sample() == 15 * 3

    patchy.patch(
        sample,
        """\
        @@ -2,3 +2,3 @@
             nonlocal variab
        -    multiple = 3
        +    multiple = 4
        """,
    )

    assert sample() == 15 * 4
示例#20
0
def test_patch_future_doesnt_inherit(tmpdir):
    # This test module has 'division' imported, test file doesn't
    assert division
    tmpdir.join('no_future_division.py').write(dedent("""\
        def sample():
            return 1 / 2
    """))
    sys.path.insert(0, six.text_type(tmpdir))

    try:
        from no_future_division import sample
    finally:
        sys.path.pop(0)

    assert sample() == 0

    patchy.patch(sample, """\
        @@ -1,2 +1,3 @@
         def sample():
        +    pass
             return 1 / 2
        """)

    assert sample() == 0
示例#21
0
def test_patch_invalid():
    """
    We need to balk on empty patches
    """
    def sample():
        return 1

    bad_patch = """
        """
    with pytest.raises(ValueError) as excinfo:
        patchy.patch(sample, bad_patch)

    msg = str(excinfo.value)
    assert msg.startswith("Could not apply the patch to 'sample'.")
    assert "Only garbage was found in the patch input."
    assert sample() == 1
示例#22
0
def test_patch_invalid():
    """
    We need to balk on empty patches
    """
    def sample():
        return 1

    bad_patch = """
        """
    with pytest.raises(ValueError) as excinfo:
        patchy.patch(sample, bad_patch)

    msg = str(excinfo.value)
    assert msg.startswith("Could not apply the patch to 'sample'.")
    assert "Only garbage was found in the patch input."
    assert sample() == 1
示例#23
0
def test_patch_invalid_hunk():
    """
    We need to balk on patches that fail on application
    """
    def sample():
        return 1

    bad_patch = """\
        @@ -1,2 +1,2 @@
         def sample():
        -    return 2
        +    return 23"""
    with pytest.raises(ValueError) as excinfo:
        patchy.patch(sample, bad_patch)

    assert "Hunk #1 FAILED" in str(excinfo.value)
    assert sample() == 1
示例#24
0
def test_patch_invalid_hunk():
    """
    We need to balk on patches that fail on application
    """
    def sample():
        return 1

    bad_patch = """\
        @@ -1,2 +1,2 @@
         def sample():
        -    return 2
        +    return 23"""
    with pytest.raises(ValueError) as excinfo:
        patchy.patch(sample, bad_patch)

    assert "Hunk #1 FAILED" in str(excinfo.value)
    assert sample() == 1
示例#25
0
def test_patch_twice():
    def sample():
        return 1

    patchy.patch(sample, """\
        @@ -1,2 +1,2 @@
         def sample():
        -    return 1
        +    return 2""")
    patchy.patch(sample, """\
        @@ -1,2 +1,2 @@
         def sample():
        -    return 2
        +    return 3
        """)

    assert sample() == 3
示例#26
0
def test_patch_freevars():
    def free_func(v):
        return v + ' on toast'

    def sample():
        filling = 'Chalk'
        return free_func(filling)

    patchy.patch(sample, """\
        @@ -1,2 +1,2 @@
         def method():
        -    filling = 'Chalk'
        +    filling = 'Cheese'
             return free_func(filling)
        """)

    assert sample() == "Cheese on toast"
示例#27
0
def test_patch_freevars():
    def free_func(v):
        return v + ' on toast'

    def sample():
        filling = 'Chalk'
        return free_func(filling)

    patchy.patch(
        sample, """\
        @@ -1,2 +1,2 @@
         def method():
        -    filling = 'Chalk'
        +    filling = 'Cheese'
             return free_func(filling)
        """)

    assert sample() == "Cheese on toast"
示例#28
0
def test_patch_twice():
    def sample():
        return 1

    patchy.patch(
        sample, """\
        @@ -1,2 +1,2 @@
         def sample():
        -    return 1
        +    return 2""")
    patchy.patch(
        sample, """\
        @@ -1,2 +1,2 @@
         def sample():
        -    return 2
        +    return 3
        """)

    assert sample() == 3
示例#29
0
def test_patch_freevars():
    def free_func(v):
        return v + " on toast"

    def sample():
        filling = "Chalk"
        return free_func(filling)

    patchy.patch(
        sample,
        """\
        @@ -1,2 +1,2 @@
         def method():
        -    filling = "Chalk"
        +    filling = "Cheese"
             return free_func(filling)
        """,
    )

    assert sample() == "Cheese on toast"
示例#30
0
def test_patch_freevars_re_close():
    def nasty_filling(v):
        return 'Chalk'

    def nice_filling(v):
        return 'Cheese'

    def sample():
        filling = nasty_filling()
        return filling + ' on toast'

    patchy.patch(sample, """\
        @@ -1,2 +1,2 @@
         def sample():
        -    filling = nasty_filling()
        +    filling = nice_filling()
             return filling + ' on toast'
        """)

    assert sample() == "Cheese on toast"
示例#31
0
def test_patch_freevars_re_close():
    def nasty_filling(v):
        return 'Chalk'

    def nice_filling(v):
        return 'Cheese'

    def sample():
        filling = nasty_filling()
        return filling + ' on toast'

    patchy.patch(
        sample, """\
        @@ -1,2 +1,2 @@
         def sample():
        -    filling = nasty_filling()
        +    filling = nice_filling()
             return filling + ' on toast'
        """)

    assert sample() == "Cheese on toast"
示例#32
0
def test_patch_freevars_remove():
    def tastes_good(v):
        return v + " tastes good"

    def tastes_bad(v):
        return v + " tastes bad"

    def sample():
        return ", ".join([tastes_bad("Chalk"), tastes_good("Cheese")])

    patchy.patch(
        sample,
        """\
        @@ -1,2 +1,2 @@
         def sample():
        -    return ", ".join([tastes_bad("Chalk"), tastes_good("Cheese")])
        +    return ", ".join([tastes_good("Cheese")])
        """,
    )

    assert sample() == "Cheese tastes good"
示例#33
0
def test_patch_freevars_nested():
    def free_func(v):
        return v + ' on toast'

    def sample():
        filling = 'Chalk'

        def _inner_func():
            return free_func(filling)

        return _inner_func

    patchy.patch(sample, """\
        @@ -1,2 +1,2 @@
         def sample():
        -    filling = 'Chalk'
        +    filling = 'Cheese'

             def _inner_func():
        """)

    assert sample()() == "Cheese on toast"