|
11 | 11 | # ANY KIND, either express or implied. See the License for the specific
|
12 | 12 | # language governing permissions and limitations under the License.
|
13 | 13 |
|
| 14 | +import contextlib |
| 15 | +import os |
| 16 | +import random |
| 17 | +import shutil |
| 18 | +import tempfile |
| 19 | +import time |
| 20 | +from unittest import mock # noqa: F401 |
| 21 | + |
| 22 | +import botocore.loaders |
14 | 23 | from botocore.compat import parse_qs, urlparse
|
15 | 24 |
|
| 25 | +import aiobotocore.session |
| 26 | + |
| 27 | +_LOADER = botocore.loaders.Loader() |
| 28 | + |
| 29 | + |
| 30 | +def create_session(**kwargs): |
| 31 | + # Create a Session object. By default, |
| 32 | + # the _LOADER object is used as the loader |
| 33 | + # so that we reused the same models across tests. |
| 34 | + session = aiobotocore.session.AioSession(**kwargs) |
| 35 | + session.register_component('data_loader', _LOADER) |
| 36 | + session.set_config_variable('credentials_file', 'noexist/foo/botocore') |
| 37 | + return session |
| 38 | + |
| 39 | + |
| 40 | +@contextlib.contextmanager |
| 41 | +def temporary_file(mode): |
| 42 | + """This is a cross platform temporary file creation. |
| 43 | +
|
| 44 | + tempfile.NamedTemporary file on windows creates a secure temp file |
| 45 | + that can't be read by other processes and can't be opened a second time. |
| 46 | +
|
| 47 | + For tests, we generally *want* them to be read multiple times. |
| 48 | + The test fixture writes the temp file contents, the test reads the |
| 49 | + temp file. |
| 50 | +
|
| 51 | + """ |
| 52 | + temporary_directory = tempfile.mkdtemp() |
| 53 | + basename = f'tmpfile-{int(time.time())}-{random.randint(1, 1000)}' |
| 54 | + full_filename = os.path.join(temporary_directory, basename) |
| 55 | + open(full_filename, 'w').close() |
| 56 | + try: |
| 57 | + with open(full_filename, mode) as f: |
| 58 | + yield f |
| 59 | + finally: |
| 60 | + shutil.rmtree(temporary_directory) |
| 61 | + |
16 | 62 |
|
17 | 63 | def _urlparse(url):
|
18 | 64 | if isinstance(url, bytes):
|
|
0 commit comments