Esempio n. 1
0
def test_patch_unpatch():
    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
Esempio n. 2
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
Esempio n. 3
0
def test_unpatch():
    def sample():
        return 9001

    patchy.unpatch(
        sample, """\
        @@ -1,2 +1,2 @@
         def sample():
        -    return 1
        +    return 9001
        """)
    assert sample() == 1
Esempio n. 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
Esempio n. 5
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
Esempio n. 6
0
def test_unpatch_invalid_hunk():
    """
    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
Esempio n. 7
0
def fuzz_cachew_impl():
    """
    Insert random sleeps in cachew_impl to increase likelihood of concurrency issues
    """
    import patchy  # type: ignore[import]
    from .. import cachew_impl
    patch = '''\
@@ -47,6 +47,11 @@

                 logger.debug('old hash: %s', prev_hash)

+                from random import random
+                rs = random() * 2
+                print("sleeping for: ", rs)
+                from time import sleep; sleep(rs)
+
                 if h == prev_hash:
                     logger.debug('hash matched: loading from cache')
                     rows = conn.execute(values_table.select()
'''
    patchy.patch(cachew_impl, patch)
    yield
    patchy.unpatch(cachew_impl, patch)
Esempio n. 8
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