Skip to content

Commit 488d55f

Browse files
committed
feat: first ffi interface
1 parent 2d7f943 commit 488d55f

10 files changed

+60
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@ Please read [CONTRIBUTING.md](https://github.com/pact-foundation/pact-python/blo
435435
To setup a development environment:
436436

437437
1. If you want to run tests for all Python versions, install 2.7, 3.3, 3.4, 3.5, and 3.6 from source or using a tool like [pyenv]
438-
2. Its recommended to create a Python [virtualenv] for the project
438+
2. Its recommended to create a Python [virtualenv] for the project.
439+
3. We are now using FFI bindings. For mac you might want to read these [setup FFI](https://cffi.readthedocs.io/en/latest/installation.html)
439440

440441
The setup the environment, run tests, and package the application, run:
441442
`make release`

libpact_ffi-osx-x86_64.dylib

14.3 MB
Binary file not shown.

pact/ffi/ffi_verifier.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Wrapper to pact reference dynamic libraries using FFI."""
2+
from cffi import FFI
3+
import platform
4+
5+
class FFIVerify(object):
6+
"""A Pact Verifier Wrapper."""
7+
8+
def version(self):
9+
"""Publish version info."""
10+
ffi = FFI()
11+
ffi.cdef("""
12+
char *pactffi_version(void);
13+
""")
14+
lib = self._load_ffi_library(ffi)
15+
result = lib.pactffi_version()
16+
return ffi.string(result).decode('utf-8')
17+
18+
def _load_ffi_library(self, ffi):
19+
"""Load the right library."""
20+
target_platform = platform.platform().lower()
21+
22+
if 'darwin' in target_platform or 'macos' in target_platform:
23+
libname = "libpact_ffi-osx-x86_64.dylib"
24+
elif 'linux' in target_platform:
25+
libname = "libpact_ffi-linux-x86_64.so"
26+
elif 'windows' in target_platform:
27+
libname = "libpact_ffi-osx-x86_64.dylib"
28+
else:
29+
msg = ('Unfortunately, {} is not a supported platform. Only Linux,'
30+
' Windows, and OSX are currently supported.').format(
31+
platform.platform())
32+
raise Exception(msg)
33+
34+
return ffi.dlopen(libname)

pact/verifier.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def __str__(self):
2626
"""
2727
return 'Verifier for {} with url {}'.format(self.provider, self.provider_base_url)
2828

29+
def version(self):
30+
"""Return version info."""
31+
return VerifyWrapper().version()
32+
2933
def validate_publish(self, **kwargs):
3034
"""Validate publish has a version."""
3135
if ((kwargs.get('publish') is not None) and (kwargs.get('publish_version') is None)):

pact/verify_wrapper.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,7 @@ def publish_results(self, provider_app_version, command):
205205
command.extend(["--provider-app-version",
206206
provider_app_version,
207207
"--publish-verification-results"])
208+
209+
def version(self):
210+
"""Publish version info."""
211+
return '0.0.0'

requirements_dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ pytest==5.4.1
1212
pytest-cov==2.11.1
1313
tox-travis==0.8
1414
wheel==0.24.0
15-
psutil>=2.0.0
1615
requests>=2.5.0
1716
six>=1.9.0
17+
cffi==1.14.6

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,15 @@ def install_rust_app(bin_path):
9898

9999

100100
def fetch_lib(bin_path, suffix, type):
101-
102-
"""[summary]
103-
Fetches rust binaries to the bin_path based on a suffix which specifies the platform
104-
and the type of libary.
101+
"""
102+
Fetch rust binaries to the bin_path.
105103
106104
:param bin_path: The path where binaries should be installed.
107105
:param suffix: The suffix filenamne unique to this platform (e.g. libpact_ffi-osx-x86_64).
108106
"param type: The type of library (e.g. so|a|dll|dylib)
109107
Raises:
110108
RuntimeError: [description]
109+
111110
"""
112111
dest = os.path.join(bin_path, f'{suffix}.{type}')
113112
zipped = os.path.join(bin_path, f'{suffix}.{type}.gz')
@@ -182,6 +181,7 @@ def read(filename):
182181
'psutil>=2.0.0',
183182
'requests>=2.5.0',
184183
'six>=1.9.0',
184+
'cffi==1.14.6'
185185
]
186186

187187
if __name__ == '__main__':

tests/ffi/test_ffi_verifier.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from pact.ffi.ffi_verifier import FFIVerify
2+
3+
def test_version():
4+
assert FFIVerify().version() == "0.0.0"

tests/test_verifier.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def setUp(self):
2424
self.mock_wrapper = patch.object(
2525
VerifyWrapper, 'call_verify').start()
2626

27+
def test_version(self):
28+
self.assertEqual(self.verifier.version(), "0.0.0")
29+
2730
@patch("pact.verify_wrapper.VerifyWrapper.call_verify")
2831
@patch('pact.verifier.path_exists', return_value=True)
2932
def test_verifier_with_provider_and_files(self, mock_path_exists, mock_wrapper):

tests/test_verify_wrapper.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def assertProcess(self, *expected):
6969
self.mock_rerun_command.return_value)
7070
self.assertTrue(self.mock_Popen.called)
7171

72+
def test_version(self):
73+
wrapper = VerifyWrapper()
74+
self.assertEqual(wrapper.version(), "0.0.0")
75+
7276
def test_pact_urls_or_broker_required(self):
7377
self.mock_Popen.return_value.returncode = 2
7478
wrapper = VerifyWrapper()

0 commit comments

Comments
 (0)