Debian Codegraph @

tests/sources/patch.py

raw
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 #
14
15 # Pylint doesn't play well with fixtures and dependency injection from pytest
16 # pylint: disable=redefined-outer-name
17
18 import os
19 import socket
20 import pytest
21
22 from buildstream import _yaml
23
24 from buildstream.exceptions import ErrorDomain, LoadErrorReason
25 from buildstream._testing import cli # pylint: disable=unused-import
26
27 DATA_DIR = os.path.join(
28 os.path.dirname(os.path.realpath(__file__)),
29 "patch",
30 )
31
32
33 # generate_file_types()
34 #
35 # Generator that creates a regular file directory, symbolic link, fifo
36 # and socket at the specified path.
37 #
38 # Args:
39 # path: (str) path where to create each different type of file
40 #
41 def generate_file_types(path):
42 def clean():
43 if os.path.exists(path):
44 if os.path.isdir(path):
45 os.rmdir(path)
46 else:
47 os.remove(path)
48
49 clean()
50
51 with open(path, "w", encoding="utf-8"):
52 pass
53 yield
54 clean()
55
56 os.makedirs(path)
57 yield
58 clean()
59
60 os.symlink("project.conf", path)
61 yield
62 clean()
63
64 os.mkfifo(path)
65 yield
66 clean()
67
68 # Change directory because the full path may be longer than the ~100
69 # characters permitted for a unix socket
70 old_dir = os.getcwd()
71 parent, child = os.path.split(path)
72 os.chdir(parent)
73
74 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
75
76 try:
77 s.bind(child)
78 os.chdir(old_dir)
79 yield
80 finally:
81 s.close()
82
83 clean()
84
85
86 @pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
87 def test_missing_patch(cli, datafiles):
88 project = str(datafiles)
89
90 # Removing the local file causes preflight to fail
91 localfile = os.path.join(project, "file_1.patch")
92 os.remove(localfile)
93
94 result = cli.run(project=project, args=["show", "target.bst"])
95 result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.MISSING_FILE)
96
97
98 @pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
99 def test_non_regular_file_patch(cli, datafiles):
100 project = str(datafiles)
101
102 patch_path = os.path.join(project, "irregular_file.patch")
103 for _file_type in generate_file_types(patch_path):
104 result = cli.run(project=project, args=["show", "irregular.bst"])
105 if os.path.isfile(patch_path) and not os.path.islink(patch_path):
106 result.assert_success()
107 else:
108 result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.PROJ_PATH_INVALID_KIND)
109
110
111 @pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
112 def test_invalid_absolute_path(cli, datafiles):
113 project = str(datafiles)
114
115 with open(os.path.join(project, "target.bst"), "r", encoding="utf-8") as f:
116 old_yaml = f.read()
117 new_yaml = old_yaml.replace("file_1.patch", os.path.join(project, "file_1.patch"))
118 assert old_yaml != new_yaml
119
120 with open(os.path.join(project, "target.bst"), "w", encoding="utf-8") as f:
121 f.write(new_yaml)
122
123 result = cli.run(project=project, args=["show", "target.bst"])
124 result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.PROJ_PATH_INVALID)
125
126
127 @pytest.mark.datafiles(os.path.join(DATA_DIR, "invalid-relative-path"))
128 def test_invalid_relative_path(cli, datafiles):
129 project = str(datafiles)
130
131 result = cli.run(project=project, args=["show", "irregular.bst"])
132 result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.PROJ_PATH_INVALID)
133
134
135 @pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
136 def test_stage_and_patch(cli, tmpdir, datafiles):
137 project = str(datafiles)
138 checkoutdir = os.path.join(str(tmpdir), "checkout")
139
140 # Build, checkout
141 result = cli.run(project=project, args=["build", "target.bst"])
142 result.assert_success()
143 result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir])
144 result.assert_success()
145
146 # Test the file.txt was patched and changed
147 with open(os.path.join(checkoutdir, "file.txt"), encoding="utf-8") as f:
148 assert f.read() == "This is text file with superpowers\n"
149
150
151 @pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
152 def test_stage_file_nonexistent_dir(cli, datafiles):
153 project = str(datafiles)
154
155 # Fails at build time because it tries to patch into a non-existing directory
156 result = cli.run(project=project, args=["build", "failure-nonexistent-dir.bst"])
157 result.assert_main_error(ErrorDomain.STREAM, None)
158 result.assert_task_error(ErrorDomain.SOURCE, "patch-no-files")
159
160
161 @pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
162 def test_stage_file_empty_dir(cli, datafiles):
163 project = str(datafiles)
164
165 # Fails at build time because it tries to patch with nothing else staged
166 result = cli.run(project=project, args=["build", "failure-empty-dir.bst"])
167 result.assert_main_error(ErrorDomain.STREAM, None)
168 result.assert_task_error(ErrorDomain.SOURCE, "patch-no-files")
169
170
171 @pytest.mark.datafiles(os.path.join(DATA_DIR, "separate-patch-dir"))
172 def test_stage_separate_patch_dir(cli, tmpdir, datafiles):
173 project = str(datafiles)
174 checkoutdir = os.path.join(str(tmpdir), "checkout")
175
176 # Track, fetch, build, checkout
177 result = cli.run(project=project, args=["build", "target.bst"])
178 result.assert_success()
179 result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir])
180 result.assert_success()
181
182 # Test the file.txt was patched and changed
183 with open(os.path.join(checkoutdir, "test-dir", "file.txt"), encoding="utf-8") as f:
184 assert f.read() == "This is text file in a directory with superpowers\n"
185
186
187 @pytest.mark.datafiles(os.path.join(DATA_DIR, "multiple-patches"))
188 def test_stage_multiple_patches(cli, tmpdir, datafiles):
189 project = str(datafiles)
190 checkoutdir = os.path.join(str(tmpdir), "checkout")
191
192 # Track, fetch, build, checkout
193 result = cli.run(project=project, args=["build", "target.bst"])
194 result.assert_success()
195 result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir])
196 result.assert_success()
197
198 # Test the file.txt was patched and changed
199 with open(os.path.join(checkoutdir, "file.txt"), encoding="utf-8") as f:
200 assert f.read() == "This is text file with more superpowers\n"
201
202
203 @pytest.mark.datafiles(os.path.join(DATA_DIR, "different-strip-level"))
204 def test_patch_strip_level(cli, tmpdir, datafiles):
205 project = str(datafiles)
206 checkoutdir = os.path.join(str(tmpdir), "checkout")
207
208 # Track, fetch, build, checkout
209 result = cli.run(project=project, args=["build", "target.bst"])
210 result.assert_success()
211 result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir])
212 result.assert_success()
213
214 # Test the file.txt was patched and changed
215 with open(os.path.join(checkoutdir, "file.txt"), encoding="utf-8") as f:
216 assert f.read() == "This is text file with superpowers\n"
217
218
219 @pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
220 def test_show_source_info(cli, tmpdir, datafiles):
221 # Get the source info
222 project = str(datafiles)
223 result = cli.run(project=project, args=["show", "--format", "%{name}:\n%{source-info}", "target.bst"])
224 result.assert_success()
225
226 # Check the results of the patch source, which will be second in the list after the local file
227 loaded = _yaml.load_data(result.output)
228 sources = loaded.get_sequence("target.bst")
229 source_info = sources.mapping_at(1)
230
231 assert source_info.get_str("kind") == "patch"
232 assert source_info.get_str("url") == "file_1.patch"
233 assert source_info.get_str("medium") == "local"
234 assert source_info.get_str("version-type") == "sha256"
235 assert source_info.get_str("version") == "063fbc138d30a2f2ea8f3cd53810824520ff3971d752235c70f17c67fefca0f1"
236 assert source_info.get_str("version-guess", None) is None