Skip to content

Commit 7eb2630

Browse files
committed
add cargo test support
1 parent 1179383 commit 7eb2630

File tree

5 files changed

+97
-6
lines changed

5 files changed

+97
-6
lines changed

CHANGES.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
CHANGES
22
=======
33

4-
0.5.1 (2017-04-xx)
4+
0.5.1 (2017-05-xx)
55
------------------
66

7+
- Added support for "cargo test"
8+
79
- Fixed unbound method type error #4
810

911

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup
22

3-
version = '0.5.0'
3+
version = '0.5.1'
44

55

66
setup(
@@ -37,5 +37,6 @@
3737
clean_rust=setuptools_rust:clean_rust
3838
build_ext=setuptools_rust:build_ext
3939
build_rust=setuptools_rust:build_rust
40+
test_rust=setuptools_rust:test_rust
4041
"""
4142
)

setuptools_rust/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
from .build_ext import build_ext
66
from .check import check_rust
77
from .clean import clean_rust
8+
from .test import test_rust
89
from .extension import RustExtension
910

1011
__all__ = ('RustExtension',
11-
'check_rust', 'clean_rust', 'build_ext', 'build_rust')
12+
'check_rust', 'clean_rust', 'build_ext', 'build_rust', 'test_rust')
1213

1314

1415
patch.monkey_patch_dist(build_ext)

setuptools_rust/test.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from __future__ import print_function, absolute_import
2+
import os
3+
import sys
4+
import subprocess
5+
from distutils.cmd import Command
6+
from distutils.errors import (
7+
CompileError, DistutilsFileError, DistutilsExecError)
8+
9+
import semantic_version
10+
11+
from .extension import RustExtension
12+
from .utils import cpython_feature, get_rust_version
13+
14+
MIN_VERSION = semantic_version.Spec('>=1.15')
15+
16+
17+
class test_rust(Command):
18+
""" Run cargo test"""
19+
20+
description = "test rust extensions"
21+
22+
user_options = []
23+
24+
def initialize_options(self):
25+
self.extensions = ()
26+
27+
def finalize_options(self):
28+
self.extensions = [ext for ext in self.distribution.rust_extensions
29+
if isinstance(ext, RustExtension)]
30+
31+
def run(self):
32+
if not self.extensions:
33+
return
34+
35+
version = get_rust_version()
36+
if version not in MIN_VERSION:
37+
print('Rust version mismatch: required rust%s got rust%s' % (
38+
MIN_VERSION, version))
39+
return
40+
41+
# Make sure that if pythonXX-sys is used, it builds against the current
42+
# executing python interpreter.
43+
bindir = os.path.dirname(sys.executable)
44+
45+
env = os.environ.copy()
46+
env.update({
47+
# disables rust's pkg-config seeking for specified packages,
48+
# which causes pythonXX-sys to fall back to detecting the
49+
# interpreter from the path.
50+
"PYTHON_2.7_NO_PKG_CONFIG": "1",
51+
"PATH": bindir + os.pathsep + os.environ.get("PATH", "")
52+
})
53+
54+
for ext in self.extensions:
55+
if not os.path.exists(ext.path):
56+
raise DistutilsFileError(
57+
"Can not file rust extension project file: %s" % ext.path)
58+
59+
features = set(ext.features)
60+
features.update(cpython_feature(ext=False))
61+
62+
# build cargo command
63+
args = (["cargo", "test", "--manifest-path", ext.path,
64+
"--features", " ".join(features)]
65+
+ list(ext.args or []))
66+
67+
# Execute cargo command
68+
print(' '.join(args))
69+
try:
70+
subprocess.check_output(args)
71+
except subprocess.CalledProcessError as e:
72+
raise CompileError(
73+
"cargo failed with code: %d\n%s" % (
74+
e.returncode, e.output.decode("utf-8")))
75+
except OSError:
76+
raise DistutilsExecError(
77+
"Unable to execute 'cargo' - this package "
78+
"requires rust to be installed and "
79+
"cargo to be on the PATH")
80+
else:
81+
print("Test has been completed for '%s' extension" % ext.name)

setuptools_rust/utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
import semantic_version
77

88

9-
def cpython_feature():
9+
def cpython_feature(ext=True):
1010
version = sys.version_info
1111
if (2, 7) < version < (2, 8):
12-
return ("cpython/python27-sys", "cpython/extension-module-2-7")
12+
if ext:
13+
return ("cpython/python27-sys", "cpython/extension-module-2-7")
14+
else:
15+
return ("cpython/python27-sys",)
1316
elif (3, 3) < version:
14-
return ("cpython/python3-sys", "cpython/extension-module")
17+
if ext:
18+
return ("cpython/python3-sys", "cpython/extension-module")
19+
else:
20+
return ("cpython/python3-sys",)
1521
else:
1622
raise DistutilsPlatformError(
1723
"Unsupported python version: %s" % sys.version)

0 commit comments

Comments
 (0)