/
test_patches.py
157 lines (122 loc) · 5.44 KB
/
test_patches.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import patch_ng
import pytest
from conan.tools.files import patch, apply_conandata_patches
from conans.errors import ConanException
from conans.test.utils.mocks import ConanFileMock
class MockPatchset:
filename = None
string = None
apply_args = None
def apply(self, root, strip, fuzz):
self.apply_args = (root, strip, fuzz)
return True
@pytest.fixture
def mock_patch_ng(monkeypatch):
mock = MockPatchset()
def mock_fromfile(filename):
mock.filename = filename
return mock
def mock_fromstring(string):
mock.string = string
return mock
monkeypatch.setattr(patch_ng, "fromfile", mock_fromfile)
monkeypatch.setattr(patch_ng, "fromstring", mock_fromstring)
return mock
def test_single_patch_file(mock_patch_ng):
conanfile = ConanFileMock()
conanfile.display_name = 'mocked/ref'
patch(conanfile, patch_file='patch-file')
assert mock_patch_ng.filename == 'patch-file'
assert mock_patch_ng.string is None
assert mock_patch_ng.apply_args == (None, 0, False)
assert len(str(conanfile.output)) == 0
def test_single_patch_string(mock_patch_ng):
conanfile = ConanFileMock()
conanfile.display_name = 'mocked/ref'
patch(conanfile, patch_string='patch_string')
assert mock_patch_ng.string == b'patch_string'
assert mock_patch_ng.filename is None
assert mock_patch_ng.apply_args == (None, 0, False)
assert len(str(conanfile.output)) == 0
def test_single_patch_arguments(mock_patch_ng):
conanfile = ConanFileMock()
conanfile.display_name = 'mocked/ref'
patch(conanfile, patch_file='patch-file', base_path='root', strip=23, fuzz=True)
assert mock_patch_ng.filename == 'patch-file'
assert mock_patch_ng.apply_args == ('root', 23, True)
assert len(str(conanfile.output)) == 0
def test_single_patch_type(mock_patch_ng):
conanfile = ConanFileMock()
conanfile.display_name = 'mocked/ref'
patch(conanfile, patch_file='patch-file', patch_type='patch_type')
assert 'Apply patch (patch_type)\n' == str(conanfile.output)
def test_single_patch_description(mock_patch_ng):
conanfile = ConanFileMock()
conanfile.display_name = 'mocked/ref'
patch(conanfile, patch_file='patch-file', patch_description='patch_description')
assert 'Apply patch: patch_description\n' == str(conanfile.output)
def test_single_patch_extra_fields(mock_patch_ng):
conanfile = ConanFileMock()
conanfile.display_name = 'mocked/ref'
patch(conanfile, patch_file='patch-file', patch_type='patch_type',
patch_description='patch_description')
assert 'Apply patch (patch_type): patch_description\n' == str(conanfile.output)
def test_single_no_patchset(monkeypatch):
monkeypatch.setattr(patch_ng, "fromfile", lambda _: None)
conanfile = ConanFileMock()
conanfile.display_name = 'mocked/ref'
with pytest.raises(ConanException) as excinfo:
patch(conanfile, patch_file='patch-file-failed')
assert 'Failed to parse patch: patch-file-failed' == str(excinfo.value)
def test_single_apply_fail(monkeypatch):
class MockedApply:
def apply(self, *args, **kwargs):
return False
monkeypatch.setattr(patch_ng, "fromfile", lambda _: MockedApply())
conanfile = ConanFileMock()
conanfile.display_name = 'mocked/ref'
with pytest.raises(ConanException) as excinfo:
patch(conanfile, patch_file='patch-file-failed')
assert 'Failed to apply patch: patch-file-failed' == str(excinfo.value)
def test_multiple_no_version(mock_patch_ng):
conanfile = ConanFileMock()
conanfile.display_name = 'mocked/ref'
conanfile.conan_data = {'patches': [
{'patch_file': 'patches/0001-buildflatbuffers-cmake.patch',
'base_path': 'source_subfolder', },
{'patch_file': 'patches/0002-implicit-copy-constructor.patch',
'base_path': 'source_subfolder',
'patch_type': 'backport',
'patch_source': 'https://github.com/google/flatbuffers/pull/5650',
'patch_description': 'Needed to build with modern clang compilers.'}
]}
apply_conandata_patches(conanfile)
assert 'Apply patch (backport): Needed to build with modern clang compilers.\n' \
== str(conanfile.output)
def test_multiple_with_version(mock_patch_ng):
conanfile = ConanFileMock()
conanfile.display_name = 'mocked/ref'
conanfile.conan_data = {'patches': {
"1.11.0": [
{'patch_file': 'patches/0001-buildflatbuffers-cmake.patch',
'base_path': 'source_subfolder', },
{'patch_file': 'patches/0002-implicit-copy-constructor.patch',
'base_path': 'source_subfolder',
'patch_type': 'backport',
'patch_source': 'https://github.com/google/flatbuffers/pull/5650',
'patch_description': 'Needed to build with modern clang compilers.'}
],
"1.12.0": [
{'patch_file': 'patches/0001-buildflatbuffers-cmake.patch',
'base_path': 'source_subfolder', },
]}}
with pytest.raises(AssertionError) as excinfo:
apply_conandata_patches(conanfile)
assert 'Can only be applied if conanfile.version is already defined' == str(excinfo.value)
conanfile.version = "1.2.11"
apply_conandata_patches(conanfile)
assert len(str(conanfile.output)) == 0
conanfile.version = "1.11.0"
apply_conandata_patches(conanfile)
assert 'Apply patch (backport): Needed to build with modern clang compilers.\n' \
== str(conanfile.output)