|
1 | | -import os |
2 | | -import shutil |
| 1 | +from unittest import mock |
3 | 2 | from clang_tools.util import check_install_os, download_file, unpack_file |
4 | 3 |
|
5 | | -TEST_REPO = "https://github.com/shenxianpeng/clang-tools-pip" |
6 | | - |
7 | 4 |
|
8 | 5 | def test_check_install_os(): |
9 | 6 | install_os = check_install_os() |
10 | 7 | assert install_os in ("linux", "windos", "macosx") |
11 | 8 |
|
12 | 9 |
|
13 | | -def test_download_file(): |
14 | | - url = f"{TEST_REPO}/blob/master/README.md" |
15 | | - file_name = "test_file" |
16 | | - download_file(url, file_name) |
17 | | - assert os.path.exists(file_name) |
18 | | - os.remove(file_name) |
19 | | - assert not os.path.exists(file_name) |
| 10 | +@mock.patch('clang_tools.util.urllib.request.urlretrieve') |
| 11 | +def test_fail_download_file(mock_request): |
| 12 | + mock_result = mock.MagicMock() |
| 13 | + attrs = {'mock_result.return_value': 'file.tar.gz'} |
| 14 | + mock_result.configure_mock(**attrs) |
| 15 | + mock_request.return_value = mock_result |
| 16 | + file_name = download_file('https://www.google.com', 'file.tar.gz') |
| 17 | + assert file_name is None |
20 | 18 |
|
21 | 19 |
|
22 | | -def test_unpack_file(): |
23 | | - url = f"{TEST_REPO}/archive/refs/tags/v0.1.0.tar.gz" |
24 | | - file_name = "test_file.tar.gz" |
25 | | - download_file(url, file_name) |
26 | | - status = unpack_file(file_name) |
27 | | - assert status == 0 |
28 | | - os.remove(file_name) |
29 | | - shutil.rmtree("clang-tools-pip-0.1.0") |
| 20 | +@mock.patch('clang_tools.util.subprocess.run') |
| 21 | +def test_unpack_file(mock_run): |
| 22 | + mock_stdout = mock.Mock() |
| 23 | + mock_stdout.configure_mock(**{"returncode": '0'}) |
| 24 | + mock_run.return_value = mock_stdout |
| 25 | + result = unpack_file('file.tar.gz') |
| 26 | + assert result == '0' |
0 commit comments