Debian Codegraph @

extra_tests/cffi_tests/cffi1/test_pkgconfig.py

raw symbols SCIP
1 # Generated by pypy/tool/import_cffi.py
2 import sys
3 import subprocess
4 import pytest
5 import cffi.pkgconfig as pkgconfig
6 from cffi import PkgConfigError
7
8
9 def mock_call(libname, flag):
10 assert libname=="foobarbaz"
11 flags = {
12 "--cflags": "-I/usr/include/python3.6m -DABCD -DCFFI_TEST=1 -O42\n",
13 "--libs": "-L/usr/lib64 -lpython3.6 -shared\n",
14 }
15 return flags[flag]
16
17
18 def test_merge_flags():
19 d1 = {"ham": [1, 2, 3], "spam" : ["a", "b", "c"], "foo" : []}
20 d2 = {"spam" : ["spam", "spam", "spam"], "bar" : ["b", "a", "z"]}
21
22 pkgconfig.merge_flags(d1, d2)
23 assert d1 == {
24 "ham": [1, 2, 3],
25 "spam" : ["a", "b", "c", "spam", "spam", "spam"],
26 "bar" : ["b", "a", "z"],
27 "foo" : []}
28
29
30 def test_pkgconfig():
31 assert pkgconfig.flags_from_pkgconfig([]) == {}
32
33 saved = pkgconfig.call
34 try:
35 pkgconfig.call = mock_call
36 flags = pkgconfig.flags_from_pkgconfig(["foobarbaz"])
37 finally:
38 pkgconfig.call = saved
39 assert flags == {
40 'include_dirs': ['/usr/include/python3.6m'],
41 'library_dirs': ['/usr/lib64'],
42 'libraries': ['python3.6'],
43 'define_macros': [('ABCD', None), ('CFFI_TEST', '1')],
44 'extra_compile_args': ['-O42'],
45 'extra_link_args': ['-shared']
46 }
47
48 class mock_subprocess:
49 PIPE = Ellipsis
50 class Popen:
51 def __init__(self, cmd, stdout, stderr):
52 if mock_subprocess.RESULT is None:
53 raise OSError("oops can't run")
54 assert cmd == ['pkg-config', '--print-errors', '--cflags', 'libfoo']
55 def communicate(self):
56 bout, berr, rc = mock_subprocess.RESULT
57 self.returncode = rc
58 return bout, berr
59
60 def test_call():
61 saved = pkgconfig.subprocess
62 try:
63 pkgconfig.subprocess = mock_subprocess
64
65 mock_subprocess.RESULT = None
66 e = pytest.raises(PkgConfigError, pkgconfig.call, "libfoo", "--cflags")
67 assert str(e.value) == "cannot run pkg-config: oops can't run"
68
69 mock_subprocess.RESULT = b"", "Foo error!\n", 1
70 e = pytest.raises(PkgConfigError, pkgconfig.call, "libfoo", "--cflags")
71 assert str(e.value) == "Foo error!"
72
73 mock_subprocess.RESULT = b"abc\\def\n", "", 0
74 e = pytest.raises(PkgConfigError, pkgconfig.call, "libfoo", "--cflags")
75 assert str(e.value).startswith("pkg-config --cflags libfoo returned an "
76 "unsupported backslash-escaped output:")
77
78 mock_subprocess.RESULT = b"abc def\n", "", 0
79 result = pkgconfig.call("libfoo", "--cflags")
80 assert result == "abc def\n"
81
82 mock_subprocess.RESULT = b"abc def\n", "", 0
83 result = pkgconfig.call("libfoo", "--cflags")
84 assert result == "abc def\n"
85
86 if sys.version_info >= (3,):
87 mock_subprocess.RESULT = b"\xff\n", "", 0
88 e = pytest.raises(PkgConfigError, pkgconfig.call,
89 "libfoo", "--cflags", encoding="utf-8")
90 assert str(e.value) == (
91 "pkg-config --cflags libfoo returned bytes that cannot be "
92 "decoded with encoding 'utf-8':\nb'\\xff\\n'")
93
94 finally:
95 pkgconfig.subprocess = saved