Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 2 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """Common functions for testing""" |
| 6 | |
| 7 | from __future__ import print_function |
Kuang-che Wu | e2563ea | 2018-01-05 20:30:28 +0800 | [diff] [blame] | 8 | import contextlib |
Kuang-che Wu | 3d37dbf | 2020-04-15 00:22:01 +0800 | [diff] [blame^] | 9 | import os |
| 10 | import shutil |
| 11 | import tempfile |
| 12 | |
Kuang-che Wu | 23192ad | 2020-03-11 18:12:46 +0800 | [diff] [blame] | 13 | from unittest import mock |
Kuang-che Wu | e2563ea | 2018-01-05 20:30:28 +0800 | [diff] [blame] | 14 | |
Kuang-che Wu | 3d37dbf | 2020-04-15 00:22:01 +0800 | [diff] [blame^] | 15 | from bisect_kit import common |
| 16 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 17 | |
| 18 | def get_testdata_path(filename=None): |
Kuang-che Wu | e2563ea | 2018-01-05 20:30:28 +0800 | [diff] [blame] | 19 | """Gets path to test data |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 20 | |
| 21 | Args: |
| 22 | filename: path relative to the test data folder. |
| 23 | |
| 24 | Returns: |
| 25 | path to the file inside test data folder. If filename is None, the path of |
| 26 | test data folder is returned. |
| 27 | """ |
| 28 | path = os.path.join(os.path.dirname(__file__), '..', 'testdata') |
Kuang-che Wu | e2563ea | 2018-01-05 20:30:28 +0800 | [diff] [blame] | 29 | assert os.path.exists(path) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 30 | if filename: |
| 31 | path = os.path.join(path, filename) |
| 32 | return path |
Kuang-che Wu | e2563ea | 2018-01-05 20:30:28 +0800 | [diff] [blame] | 33 | |
| 34 | |
| 35 | @contextlib.contextmanager |
| 36 | def mock_function_by_file(module, name, filename): |
| 37 | """Context manager to mock function by test data |
| 38 | |
| 39 | Args: |
| 40 | module: module of function to mock |
| 41 | name: function name to mock |
| 42 | filename: path relative to the test data folder |
| 43 | |
| 44 | Returns: |
| 45 | context manager, which mocks the said function within its scope. |
| 46 | """ |
Kuang-che Wu | a572349 | 2019-11-25 20:59:34 +0800 | [diff] [blame] | 47 | with open(get_testdata_path(filename)) as f: |
| 48 | data = f.read() |
Kuang-che Wu | e2563ea | 2018-01-05 20:30:28 +0800 | [diff] [blame] | 49 | with mock.patch.object(module, name, return_value=data): |
| 50 | yield |
Kuang-che Wu | 3d37dbf | 2020-04-15 00:22:01 +0800 | [diff] [blame^] | 51 | |
| 52 | |
| 53 | class mock_session_base(contextlib.ContextDecorator): |
| 54 | """Context decorator to mock session base and clean up.""" |
| 55 | |
| 56 | def __init__(self): |
| 57 | self.session_base_bak = None |
| 58 | self.session_base = tempfile.mkdtemp() |
| 59 | |
| 60 | def __enter__(self): |
| 61 | self.session_base_bak = common.DEFAULT_SESSION_BASE |
| 62 | common.DEFAULT_SESSION_BASE = self.session_base |
| 63 | |
| 64 | def __exit__(self, *exc): |
| 65 | common.DEFAULT_SESSION_BASE = self.session_base_bak |
| 66 | shutil.rmtree(self.session_base) |