Beispiel #1
0
    def test_patch_future(self):
        from python2_future import sample

        assert sample() is unicode

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

        assert sample() is unicode
Beispiel #2
0
    def test_context_manager(self):
        def sample():
            return 1234

        patch_text = """\
            @@ -1,2 +1,2 @@
             def sample():
            -    return 1234
            +    return 5678
            """

        assert sample() == 1234
        with patchy.temp_patch(sample, patch_text):
            assert sample() == 5678
        assert sample() == 1234
Beispiel #3
0
    def test_patch_invalid_hunk_2(self):
        """
        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)

        print(excinfo.value)
        assert "Hunk #2 FAILED" in str(excinfo.value)
        assert sample() == 1
Beispiel #4
0
    def test_patch_unpatch(self):
        def sample():
            return 1

        patch_text = """\
            @@ -1,2 +1,2 @@
             def sample():
            -    return 1
            +    return 9001
            """

        patchy.patch(sample, patch_text)
        assert sample() == 9001

        patchy.unpatch(sample, patch_text)
        assert sample() == 1
Beispiel #5
0
    def test_decorator(self):
        def sample():
            return 3456

        patch_text = """\
            @@ -1,2 +1,2 @@
             def sample():
            -    return 3456
            +    return 7890
            """

        @patchy.temp_patch(sample, patch_text)
        def decorated():
            assert sample() == 7890

        assert sample() == 3456
        decorated()
        assert sample() == 3456
Beispiel #6
0
    def test_patch_simple_no_newline(self):
        def sample():
            return 1

        patchy.patch(sample, """\
            @@ -2,2 +2,2 @@
            -    return 1
            +    return 2""")
        assert sample() == 2
Beispiel #7
0
    def test_mc_patchface(self):
        def sample():
            return 1

        patchy.mc_patchface(sample, """\
            @@ -2,2 +2,2 @@
            -    return 1
            +    return 2
            """)
        assert sample() == 2
Beispiel #8
0
    def test_unpatch(self):
        def sample():
            return 9001

        patchy.unpatch(sample, """\
            @@ -1,2 +1,2 @@
             def sample():
            -    return 1
            +    return 9001
            """)
        assert sample() == 1
Beispiel #9
0
    def test_patch_unpatch(self):
        def sample():
            return 1

        patch_text = """\
            @@ -1,2 +1,2 @@
             def sample():
            -    return 1
            +    return 9001
            """

        patchy.patch(sample, patch_text)
        assert sample() == 9001

        # Check that we use the cache
        orig_mkdtemp = patchy.api.mkdtemp

        def mkdtemp(*args, **kwargs):
            raise AssertionError(
                "mkdtemp should not be called, the unpatch should be cached."
            )

        try:
            patchy.api.mkdtemp = mkdtemp
            patchy.unpatch(sample, patch_text)
        finally:
            patchy.api.mkdtemp = orig_mkdtemp
        assert sample() == 1

        # Check that we use the cache going forwards again
        try:
            patchy.api.mkdtemp = mkdtemp
            patchy.patch(sample, patch_text)
        finally:
            patchy.api.mkdtemp = orig_mkdtemp
        assert sample() == 9001
Beispiel #10
0
    def test_patch_invalid(self):
        """
        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
Beispiel #11
0
    def test_patch_invalid_hunk(self):
        """
        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
Beispiel #12
0
    def test_patch_twice(self):
        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
Beispiel #13
0
    def test_unpatch_invalid_hunk(self):
        """
        We need to balk on patches that fail on application
        """
        def sample():
            return 1

        # This patch would make sense forwards but doesn't backwards
        bad_patch = """\
            @@ -1,2 +1,2 @@
             def sample():
            -    return 3
            +    return 2"""
        with pytest.raises(ValueError) as excinfo:
            patchy.unpatch(sample, bad_patch)

        assert "Hunk #1 FAILED" in str(excinfo.value)
        assert sample() == 1
Beispiel #14
0
 def decorated():
     assert sample() == 7890