Debian Codegraph @

test/unit/_cli/test_autocomplete_cache.py

raw
1 ######################################################################
2 #
3 # File: test/unit/_cli/test_autocomplete_cache.py
4 #
5 # Copyright 2023 Backblaze Inc. All Rights Reserved.
6 #
7 # License https://www.backblaze.com/using_b2_code.html
8 #
9 ######################################################################
10
11 # Most of the tests in this module are running in a forked process
12 # because argcomplete and autocomplete_cache mess with global state,
13 # making the argument parser unusable for other tests.
14
15 from __future__ import annotations
16
17 import contextlib
18 import importlib
19 import io
20 import os
21 import pickle
22 import sys
23 from typing import Any
24
25 import argcomplete
26 import pytest
27
28 import b2._internal._cli.argcompleters
29 import b2._internal.arg_parser
30 import b2._internal.console_tool
31 from b2._internal._cli import autocomplete_cache
32
33 # We can't use pytest.mark.skipif to skip forked tests because with pytest-forked,
34 # there is an attempt to fork even if the test is marked as skipped.
35 # See https://github.com/pytest-dev/pytest-forked/issues/44
36 if sys.platform == "win32":
37 forked = pytest.mark.skip(reason="Tests can't be run forked on windows")
38 else:
39 forked = pytest.mark.forked
40
41
42 class Exit:
43 """A mocked exit method callable. Instead of actually exiting,
44 it just stores the exit code and returns."""
45
46 code: int | None
47
48 @property
49 def success(self):
50 return self.code == 0
51
52 @property
53 def empty(self):
54 return self.code is None
55
56 def __init__(self):
57 self.code = None
58
59 def __call__(self, n: int):
60 self.code = n
61
62
63 @pytest.fixture
64 def autocomplete_runner(monkeypatch, b2_cli):
65 def fdopen(fd, *args, **kwargs):
66 # argcomplete package tries to open fd 9 for debugging which causes
67 # pytest to later raise errors about bad file descriptors.
68 if fd == 9:
69 return sys.stderr
70 return os.fdopen(fd, *args, **kwargs)
71
72 @contextlib.contextmanager
73 def runner(command: str):
74 with monkeypatch.context() as m:
75 m.setenv('COMP_LINE', command)
76 m.setenv('COMP_POINT', str(len(command)))
77 m.setenv('_ARGCOMPLETE_IFS', ' ')
78 m.setenv('_ARGCOMPLETE', '1')
79 m.setattr('os.fdopen', fdopen)
80
81 def _get_b2api_for_profile(profile: str):
82 return b2_cli.b2_api
83
84 m.setattr('b2._internal._cli.b2api._get_b2api_for_profile', _get_b2api_for_profile)
85 yield
86
87 return runner
88
89
90 def argcomplete_result():
91 parser = b2._internal.console_tool.B2.create_parser()
92 exit, output = Exit(), io.StringIO()
93 argcomplete.autocomplete(parser, exit_method=exit, output_stream=output)
94 return exit.code, output.getvalue()
95
96
97 def cached_complete_result(cache: autocomplete_cache.AutocompleteCache, raise_exc: bool = True):
98 exit, output = Exit(), io.StringIO()
99 cache.autocomplete_from_cache(
100 uncached_args={
101 'exit_method': exit,
102 'output_stream': output
103 }, raise_exc=raise_exc
104 )
105 return exit.code, output.getvalue()
106
107
108 def uncached_complete_result(cache: autocomplete_cache.AutocompleteCache):
109 exit, output = Exit(), io.StringIO()
110 parser = b2._internal.console_tool.B2.create_parser()
111 cache.cache_and_autocomplete(
112 parser, uncached_args={
113 'exit_method': exit,
114 'output_stream': output
115 }
116 )
117 return exit.code, output.getvalue()
118
119
120 @forked
121 def test_complete_main_command(autocomplete_runner, tmp_path):
122 cache = autocomplete_cache.AutocompleteCache(
123 tracker=autocomplete_cache.VersionTracker(),
124 store=autocomplete_cache.HomeCachePickleStore(tmp_path),
125 )
126 with autocomplete_runner('b2 '):
127 exit, argcomplete_output = argcomplete_result()
128 assert exit == 0
129 assert 'get-bucket' in argcomplete_output
130
131 with autocomplete_runner('b2 '):
132 exit, output = cached_complete_result(cache)
133 # Nothing has been cached yet, we expect simple return, not an exit
134 assert exit is None
135 assert not output
136
137 with autocomplete_runner('b2 '):
138 exit, output = uncached_complete_result(cache)
139 assert exit == 0
140 assert output == argcomplete_output
141
142 with autocomplete_runner('b2 '):
143 exit, output = cached_complete_result(cache)
144 assert exit == 0
145 assert output == argcomplete_output
146
147
148 @forked
149 def test_complete_with_bucket_suggestions(autocomplete_runner, tmp_path, bucket, authorized_b2_cli):
150 cache = autocomplete_cache.AutocompleteCache(
151 tracker=autocomplete_cache.VersionTracker(),
152 store=autocomplete_cache.HomeCachePickleStore(tmp_path),
153 )
154 with autocomplete_runner('b2 get-bucket '):
155 exit, argcomplete_output = argcomplete_result()
156 assert exit == 0
157 assert bucket in argcomplete_output
158
159 exit, output = uncached_complete_result(cache)
160 assert exit == 0
161 assert output == argcomplete_output
162
163 exit, output = cached_complete_result(cache)
164 assert exit == 0
165 assert output == argcomplete_output
166
167
168 @forked
169 def test_complete_with_escaped_control_characters(
170 autocomplete_runner, tmp_path, bucket, uploaded_file_with_control_chars, authorized_b2_cli
171 ):
172 cc_file_name = uploaded_file_with_control_chars['fileName']
173 escaped_cc_file_name = uploaded_file_with_control_chars['escapedFileName']
174 cache = autocomplete_cache.AutocompleteCache(
175 tracker=autocomplete_cache.VersionTracker(),
176 store=autocomplete_cache.HomeCachePickleStore(tmp_path),
177 )
178
179 with autocomplete_runner(f'b2 hide-file {bucket} '):
180 exit, argcomplete_output = argcomplete_result()
181 assert exit == 0
182 assert escaped_cc_file_name in argcomplete_output
183 assert cc_file_name not in argcomplete_output
184
185 exit, output = uncached_complete_result(cache)
186 assert exit == 0
187 assert output == argcomplete_output
188
189 exit, output = cached_complete_result(cache)
190 assert exit == 0
191 assert output == argcomplete_output
192
193
194 @forked
195 def test_complete_with_file_suggestions(
196 autocomplete_runner, tmp_path, bucket, uploaded_file, authorized_b2_cli
197 ):
198 file_name = uploaded_file['fileName']
199 cache = autocomplete_cache.AutocompleteCache(
200 tracker=autocomplete_cache.VersionTracker(),
201 store=autocomplete_cache.HomeCachePickleStore(tmp_path),
202 )
203 with autocomplete_runner(f'b2 hide-file {bucket} '):
204 exit, argcomplete_output = argcomplete_result()
205 assert exit == 0
206 assert file_name in argcomplete_output
207
208 exit, output = cached_complete_result(cache)
209 assert exit is None
210 assert output == ''
211
212 exit, output = uncached_complete_result(cache)
213 assert exit == 0
214 assert output == argcomplete_output
215
216 exit, output = cached_complete_result(cache)
217 assert exit == 0
218 assert output == argcomplete_output
219
220
221 @forked
222 def test_complete_with_file_uri_suggestions(
223 autocomplete_runner, tmp_path, bucket, uploaded_file, authorized_b2_cli
224 ):
225 file_name = uploaded_file['fileName']
226 cache = autocomplete_cache.AutocompleteCache(
227 tracker=autocomplete_cache.VersionTracker(),
228 store=autocomplete_cache.HomeCachePickleStore(tmp_path),
229 )
230 with autocomplete_runner(f'b2 download-file b2://{bucket}/'):
231 exit, argcomplete_output = argcomplete_result()
232 assert exit == 0
233 assert file_name in argcomplete_output
234
235 exit, output = uncached_complete_result(cache)
236 assert exit == 0
237 assert output == argcomplete_output
238
239 exit, output = cached_complete_result(cache)
240 assert exit == 0
241 assert output == argcomplete_output
242
243
244 def test_pickle_store(tmp_path):
245 dir = tmp_path
246 store = autocomplete_cache.HomeCachePickleStore(dir)
247
248 store.set_pickle('test_1', b'test_data_1')
249 assert store.get_pickle('test_1') == b'test_data_1'
250 assert store.get_pickle('test_2') is None
251 assert len(list(dir.glob('**'))) == 1
252
253 store.set_pickle('test_2', b'test_data_2')
254 assert store.get_pickle('test_2') == b'test_data_2'
255 assert store.get_pickle('test_1') is None
256 assert len(list(dir.glob('**'))) == 1
257
258
259 class Unpickler(pickle.Unpickler):
260 """This Unpickler will raise an exception if loading the pickled object
261 imports any b2sdk module."""
262
263 _modules_to_load: set[str]
264
265 def load(self):
266 self._modules_to_load = set()
267
268 b2_modules = [module for module in sys.modules if 'b2sdk' in module]
269 for key in b2_modules:
270 del sys.modules[key]
271
272 result = super().load()
273
274 for module in self._modules_to_load:
275 importlib.import_module(module)
276 importlib.reload(sys.modules[module])
277
278 if any('b2sdk' in module for module in sys.modules):
279 raise RuntimeError("Loading the pickled object imported b2sdk module")
280 return result
281
282 def find_class(self, module: str, name: str) -> Any:
283 self._modules_to_load.add(module)
284 return super().find_class(module, name)
285
286
287 def unpickle(data: bytes) -> Any:
288 """Unpickling function that raises RuntimeError if unpickled
289 object depends on b2sdk."""
290 return Unpickler(io.BytesIO(data)).load()
291
292
293 def test_unpickle():
294 """This tests ensures that Unpickler works as expected:
295 prevents successful unpickling of objects that depend on loading
296 modules from b2sdk."""
297 from .fixtures.module_loading_b2sdk import function
298 pickled = pickle.dumps(function)
299 with pytest.raises(RuntimeError):
300 unpickle(pickled)
301
302
303 @forked
304 def test_that_autocomplete_cache_loading_does_not_load_b2sdk(autocomplete_runner, tmp_path):
305 cache = autocomplete_cache.AutocompleteCache(
306 tracker=autocomplete_cache.VersionTracker(),
307 store=autocomplete_cache.HomeCachePickleStore(tmp_path),
308 unpickle=unpickle, # using our unpickling function that fails if b2sdk is loaded
309 )
310 with autocomplete_runner('b2 '):
311 exit, uncached_output = uncached_complete_result(cache)
312 assert exit == 0
313 assert 'get-bucket' in uncached_output
314
315 exit, output = cached_complete_result(cache, raise_exc=True)
316 assert (exit, output) == (0, uncached_output)