Skip to content

Commit 790eb44

Browse files
authored
Add more tests for #1 and fix #7 (#8)
* Add test_main.py #1 * Fixed #7 and add tests for #1 * Update README.md * Add unitests * Update README.md
1 parent 3a23574 commit 790eb44

File tree

7 files changed

+93
-45
lines changed

7 files changed

+93
-45
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# clang-tools
22

3-
[![PyPI](https://img.shields.io/pypi/v/clang-tools)](https://pypi.org/project/clang-tools/) [![Python test](https://github.com/shenxianpeng/clang-tools-pip/actions/workflows/python-test.yml/badge.svg)](https://github.com/shenxianpeng/clang-tools-pip/actions/workflows/python-test.yml) [![codecov](https://codecov.io/gh/shenxianpeng/clang-tools-pip/branch/master/graph/badge.svg?token=40G5ZOIRRR)](https://codecov.io/gh/shenxianpeng/clang-tools-pip) [![Platfrom](https://img.shields.io/badge/platform-linux--64%20%7C%20win--64%20%7C%20osx--64%20-blue)](https://pypi.org/project/clang-tools/)
3+
[![PyPI](https://img.shields.io/pypi/v/clang-tools)](https://pypi.org/project/clang-tools/) [![Python test](https://github.com/shenxianpeng/clang-tools-pip/actions/workflows/python-test.yml/badge.svg)](https://github.com/shenxianpeng/clang-tools-pip/actions/workflows/python-test.yml) [![codecov](https://codecov.io/gh/shenxianpeng/clang-tools-pip/branch/master/graph/badge.svg?token=40G5ZOIRRR)](https://codecov.io/gh/shenxianpeng/clang-tools-pip) [![Platfrom](https://img.shields.io/badge/platform-linux--64%20%7C%20win--64%20%7C%20osx--64%20-blue)](https://pypi.org/project/clang-tools/) ![PyPI - Downloads](https://img.shields.io/pypi/dw/clang-tools)
4+
45

56
Install clang-tools binaries (clang-format, clang-tidy) with pip.
67

@@ -45,7 +46,21 @@ LLVM (http://llvm.org/):
4546

4647
## Supported versions
4748

48-
| verions | 14 | 13 | 12.0.1 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3.9 |
49-
|:------------:|:--:|:--:|:------:|:--:|:--:|:--:|:-:|:-:|:-:|:-:|:-:|:-:|:---:|
50-
| clang-format | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
51-
| clang-tidy || ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
49+
### clang-format
50+
| Version | 14 | 13 | 12.0.1 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3.9 |
51+
|:-------:|:--:|:--:|:------:|:--:|:--:|:--:|:-:|:-:|:-:|:-:|:-:|:-:|:---:|
52+
| Linux | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
53+
| Windows | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
54+
| macOS | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
55+
56+
### clang-tidy
57+
58+
| Version | 14 | 13 | 12.0.1 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3.9 |
59+
|:-------:|:--:|:--:|:------:|:--:|:--:|:--:|:-:|:-:|:-:|:-:|:-:|:-:|:---:|
60+
| Linux || ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
61+
| Windows | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
62+
| macOS | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
63+
64+
Know issue: clang-tidy version 14 has Segmentation fault (core dumped).
65+
66+
Thanks to the project [clang-tools-static-binaries](https://github.com/muttleyxd/clang-tools-static-binaries) for all the binaries.

clang_tools/install.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ def move_and_chmod_binary(old_file_name, new_file_name, directory) -> None:
6767
if directory:
6868
install_dir = directory
6969
else:
70-
install_os = check_install_os()
71-
if install_os not in ['linux', 'macosx', 'windows']:
72-
raise SystemExit(f"Not support {install_os}")
7370
install_dir = os.path.dirname(sys.executable)
7471
try:
7572
if not os.path.isdir(install_dir):
7673
os.makedirs(install_dir)
7774
shutil.move(old_file_name, f"{install_dir}/{new_file_name}")
7875
os.chmod(os.path.join(install_dir, new_file_name), 0o755)
7976
except PermissionError:
80-
raise SystemExit("You don't have permission. Try to run with the appropriate permissions.")
77+
raise SystemExit(
78+
f"Don't have permission to install {new_file_name} to {install_dir}. \
79+
Try to run with the appropriate permissions."
80+
)
8181

8282

8383
def install_clang_tools(version, directory) -> None:

clang_tools/main.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import sys
12
import argparse
23
from clang_tools.install import install_clang_tools
34

45

5-
def main() -> int:
6+
def parse_args(args):
67
parser = argparse.ArgumentParser(prog='clang-tools')
78

89
parser.add_argument(
910
"-i",
1011
"--install",
11-
default="12",
12-
help="Install clang-tools with specific version. default is 12.",
12+
default="13",
13+
help="Install clang-tools with specific version. default is 13.",
1314
)
1415

1516
parser.add_argument(
@@ -18,13 +19,16 @@ def main() -> int:
1819
default="",
1920
help="The directory where is the clang-tools install.",
2021
)
21-
args = parser.parse_args()
22+
return parser.parse_args()
23+
24+
25+
def main():
26+
args = parse_args(sys.argv[1:])
2227

2328
version = args.install
2429
directory = args.directory
25-
2630
install_clang_tools(version, directory)
2731

2832

2933
if __name__ == '__main__':
30-
raise SystemExit(main())
34+
main()

clang_tools/util.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import string
22
import subprocess
3-
from urllib.error import HTTPError
43
import urllib.request
54
import platform
65

@@ -9,23 +8,20 @@ def check_install_os() -> string:
98
os = platform.system().lower()
109
if os == "darwin":
1110
os = "macosx"
11+
if os not in ['linux', 'macosx', 'windows']:
12+
raise SystemExit(f"Not support {os}")
1213
return os
1314

1415

15-
def download_file(url, file_name) -> None:
16+
def download_file(url, file_name) -> string:
1617
try:
17-
urllib.request.urlretrieve(url, file_name)
18-
except HTTPError:
19-
raise SystemExit(f"Not found {file_name}, exit!")
18+
file, _ = urllib.request.urlretrieve(url, file_name)
19+
except ValueError:
20+
return None
21+
return file
2022

2123

2224
def unpack_file(file_name) -> int:
2325
command = ["tar", "-xvf", file_name]
2426
result = subprocess.run(command, stdout=subprocess.PIPE)
2527
return result.returncode
26-
27-
28-
def cmake_and_build():
29-
command = []
30-
result = subprocess.run(command, stdout=subprocess.PIPE)
31-
print(result.stdout.decode("utf-8"))

tests/test_install.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
from clang_tools.install import check_install_os
3+
from clang_tools.install import clang_format_exist
4+
from clang_tools.install import clang_tidy_exist
5+
from clang_tools.install import clang_tools_binary_url
6+
7+
8+
@pytest.mark.parametrize(('version', 'expected'), (('12', True), ('100', False)))
9+
def test_clang_tools_exist(version, expected):
10+
assert clang_format_exist(version) == expected
11+
assert clang_tidy_exist(version) == expected
12+
13+
14+
def test_clang_tools_binary_url():
15+
tools = ['clang-format', 'clang-tidy']
16+
version = 12
17+
os = check_install_os()
18+
for tool in tools:
19+
ret = clang_tools_binary_url(tool, version)
20+
if os == 'windows':
21+
assert(f"{tool}-{version}_{os}-amd64.exe" in ret)
22+
else:
23+
assert(f"{tool}-{version}_{os}-amd64" in ret)

tests/test_main.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
from unittest import mock
3+
from clang_tools.main import main
4+
5+
6+
@pytest.mark.parametrize("argv, called, response", [(['-i'], True, False), (['-d'], True, False)])
7+
def test_main_install(argv, called, response):
8+
with mock.patch('sys.argv', [''] + argv):
9+
if called and not response:
10+
with pytest.raises(SystemExit):
11+
main()
12+
else:
13+
main()

tests/test_util.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
import os
2-
import shutil
1+
from unittest import mock
32
from clang_tools.util import check_install_os, download_file, unpack_file
43

5-
TEST_REPO = "https://github.com/shenxianpeng/clang-tools-pip"
6-
74

85
def test_check_install_os():
96
install_os = check_install_os()
107
assert install_os in ("linux", "windos", "macosx")
118

129

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
2018

2119

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

Comments
 (0)