File: test_filter.py

package info (click to toggle)
python-pygit2 1.18.2-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 2,804 kB
  • sloc: ansic: 12,348; python: 11,309; sh: 205; makefile: 26
file content (138 lines) | stat: -rw-r--r-- 4,377 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import codecs
from collections.abc import Callable, Generator
from io import BytesIO

import pytest

import pygit2
from pygit2 import Blob, Filter, FilterSource, Repository
from pygit2.enums import BlobFilter
from pygit2.errors import Passthrough


def _rot13(data: bytes) -> bytes:
    return codecs.encode(data.decode('utf-8'), 'rot_13').encode('utf-8')


class _Rot13Filter(pygit2.Filter):
    attributes = 'text'

    def write(
        self,
        data: bytes,
        src: FilterSource,
        write_next: Callable[[bytes], None],
    ) -> None:
        return super().write(_rot13(data), src, write_next)


class _BufferedFilter(pygit2.Filter):
    attributes = 'text'

    def __init__(self) -> None:
        super().__init__()
        self.buf = BytesIO()

    def write(
        self,
        data: bytes,
        src: FilterSource,
        write_next: Callable[[bytes], None],
    ) -> None:
        self.buf.write(data)

    def close(self, write_next: Callable[[bytes], None]) -> None:
        write_next(_rot13(self.buf.getvalue()))


class _PassthroughFilter(_Rot13Filter):
    def check(self, src: FilterSource, attr_values: list[str | None]) -> None:
        assert attr_values == [None]
        assert src.repo
        raise Passthrough


class _UnmatchedFilter(_Rot13Filter):
    attributes = 'filter=rot13'


@pytest.fixture
def rot13_filter() -> Generator[None, None, None]:
    pygit2.filter_register('rot13', _Rot13Filter)
    yield
    pygit2.filter_unregister('rot13')


@pytest.fixture
def passthrough_filter() -> Generator[None, None, None]:
    pygit2.filter_register('passthrough-rot13', _PassthroughFilter)
    yield
    pygit2.filter_unregister('passthrough-rot13')


@pytest.fixture
def buffered_filter() -> Generator[None, None, None]:
    pygit2.filter_register('buffered-rot13', _BufferedFilter)
    yield
    pygit2.filter_unregister('buffered-rot13')


@pytest.fixture
def unmatched_filter() -> Generator[None, None, None]:
    pygit2.filter_register('unmatched-rot13', _UnmatchedFilter)
    yield
    pygit2.filter_unregister('unmatched-rot13')


def test_filter(testrepo: Repository, rot13_filter: Filter) -> None:
    blob_oid = testrepo.create_blob_fromworkdir('bye.txt')
    blob = testrepo[blob_oid]
    assert isinstance(blob, Blob)
    flags = BlobFilter.CHECK_FOR_BINARY | BlobFilter.ATTRIBUTES_FROM_HEAD
    assert b'olr jbeyq\n' == blob.data
    with pygit2.BlobIO(blob) as reader:
        assert b'olr jbeyq\n' == reader.read()
    with pygit2.BlobIO(blob, as_path='bye.txt', flags=flags) as reader:
        assert b'bye world\n' == reader.read()


def test_filter_buffered(testrepo: Repository, buffered_filter: Filter) -> None:
    blob_oid = testrepo.create_blob_fromworkdir('bye.txt')
    blob = testrepo[blob_oid]
    assert isinstance(blob, Blob)
    flags = BlobFilter.CHECK_FOR_BINARY | BlobFilter.ATTRIBUTES_FROM_HEAD
    assert b'olr jbeyq\n' == blob.data
    with pygit2.BlobIO(blob) as reader:
        assert b'olr jbeyq\n' == reader.read()
    with pygit2.BlobIO(blob, 'bye.txt', flags=flags) as reader:
        assert b'bye world\n' == reader.read()


def test_filter_passthrough(testrepo: Repository, passthrough_filter: Filter) -> None:
    blob_oid = testrepo.create_blob_fromworkdir('bye.txt')
    blob = testrepo[blob_oid]
    assert isinstance(blob, Blob)
    flags = BlobFilter.CHECK_FOR_BINARY | BlobFilter.ATTRIBUTES_FROM_HEAD
    assert b'bye world\n' == blob.data
    with pygit2.BlobIO(blob) as reader:
        assert b'bye world\n' == reader.read()
    with pygit2.BlobIO(blob, 'bye.txt', flags=flags) as reader:
        assert b'bye world\n' == reader.read()


def test_filter_unmatched(testrepo: Repository, unmatched_filter: Filter) -> None:
    blob_oid = testrepo.create_blob_fromworkdir('bye.txt')
    blob = testrepo[blob_oid]
    assert isinstance(blob, Blob)
    flags = BlobFilter.CHECK_FOR_BINARY | BlobFilter.ATTRIBUTES_FROM_HEAD
    assert b'bye world\n' == blob.data
    with pygit2.BlobIO(blob) as reader:
        assert b'bye world\n' == reader.read()
    with pygit2.BlobIO(blob, as_path='bye.txt', flags=flags) as reader:
        assert b'bye world\n' == reader.read()


def test_filter_cleanup(dirtyrepo: Repository, rot13_filter: Filter) -> None:
    # Indirectly test that pygit2_filter_cleanup has the GIL
    # before calling pygit2_filter_payload_free.
    dirtyrepo.diff()