Skip to content

Commit fc48124

Browse files
committed
tests: add test for pkg config
Signed-off-by: Henry Schreiner <[email protected]>
1 parent c933d9b commit fc48124

File tree

2 files changed

+64
-59
lines changed

2 files changed

+64
-59
lines changed

noxfile.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def lint(session: nox.Session) -> None:
2727
Lint the codebase (except for clang-format/tidy).
2828
"""
2929
session.install("pre-commit")
30-
session.run("pre-commit", "run", "-a")
30+
session.run("pre-commit", "run", "-a", *session.posargs)
3131

3232

3333
@nox.session(python=PYTHON_VERSIONS)
@@ -58,7 +58,7 @@ def tests_packaging(session: nox.Session) -> None:
5858
"""
5959

6060
session.install("-r", "tests/requirements.txt", "--prefer-binary")
61-
session.run("pytest", "tests/extra_python_package")
61+
session.run("pytest", "tests/extra_python_package", *session.posargs)
6262

6363

6464
@nox.session(reuse_venv=True)

tests/extra_python_package/test_files.py

+62-57
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
DIR = os.path.abspath(os.path.dirname(__file__))
1313
MAIN_DIR = os.path.dirname(os.path.dirname(DIR))
1414

15+
PKGCONFIG = """\
16+
prefix=${{pcfiledir}}/../../
17+
includedir=${{prefix}}/include
18+
19+
Name: pybind11
20+
Description: Seamless operability between C++11 and Python
21+
Version: {VERSION}
22+
Cflags: -I${{includedir}}
23+
"""
24+
1525

1626
main_headers = {
1727
"include/pybind11/attr.h",
@@ -106,22 +116,25 @@
106116
}
107117

108118

119+
def read_tz_file(tar: tarfile.TarFile, name: str) -> bytes:
120+
start = tar.getnames()[0] + "/"
121+
inner_file = tar.extractfile(tar.getmember(f"{start}{name}"))
122+
assert inner_file
123+
with contextlib.closing(inner_file) as f:
124+
return f.read()
125+
126+
127+
def normalize_line_endings(value: bytes) -> bytes:
128+
return value.replace(os.linesep.encode("utf-8"), b"\n")
129+
130+
109131
def test_build_sdist(monkeypatch, tmpdir):
110132

111133
monkeypatch.chdir(MAIN_DIR)
112134

113-
out = subprocess.check_output(
114-
[
115-
sys.executable,
116-
"-m",
117-
"build",
118-
"--sdist",
119-
"--outdir",
120-
str(tmpdir),
121-
]
135+
subprocess.run(
136+
[sys.executable, "-m", "build", "--sdist", f"--outdir={tmpdir}"], check=True
122137
)
123-
if hasattr(out, "decode"):
124-
out = out.decode()
125138

126139
(sdist,) = tmpdir.visit("*.tar.gz")
127140

@@ -130,25 +143,17 @@ def test_build_sdist(monkeypatch, tmpdir):
130143
version = start[9:-1]
131144
simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
132145

133-
with contextlib.closing(
134-
tar.extractfile(tar.getmember(start + "setup.py"))
135-
) as f:
136-
setup_py = f.read()
137-
138-
with contextlib.closing(
139-
tar.extractfile(tar.getmember(start + "pyproject.toml"))
140-
) as f:
141-
pyproject_toml = f.read()
142-
143-
with contextlib.closing(
144-
tar.extractfile(
145-
tar.getmember(
146-
start + "pybind11/share/cmake/pybind11/pybind11Config.cmake"
147-
)
148-
)
149-
) as f:
150-
contents = f.read().decode("utf8")
151-
assert 'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")' in contents
146+
setup_py = read_tz_file(tar, "setup.py")
147+
pyproject_toml = read_tz_file(tar, "pyproject.toml")
148+
pkgconfig = read_tz_file(tar, "pybind11/share/pkgconfig/pybind11.pc")
149+
cmake_cfg = read_tz_file(
150+
tar, "pybind11/share/cmake/pybind11/pybind11Config.cmake"
151+
)
152+
153+
assert (
154+
'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")'
155+
in cmake_cfg.decode("utf-8")
156+
)
152157

153158
files = {f"pybind11/{n}" for n in all_files}
154159
files |= sdist_files
@@ -159,51 +164,47 @@ def test_build_sdist(monkeypatch, tmpdir):
159164

160165
with open(os.path.join(MAIN_DIR, "tools", "setup_main.py.in"), "rb") as f:
161166
contents = (
162-
string.Template(f.read().decode())
167+
string.Template(f.read().decode("utf-8"))
163168
.substitute(version=version, extra_cmd="")
164-
.encode()
169+
.encode("utf-8")
165170
)
166171
assert setup_py == contents
167172

168173
with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
169174
contents = f.read()
170175
assert pyproject_toml == contents
171176

177+
simple_version = ".".join(version.split(".")[:3])
178+
pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version).encode("utf-8")
179+
assert normalize_line_endings(pkgconfig) == pkgconfig_expected
180+
172181

173182
def test_build_global_dist(monkeypatch, tmpdir):
174183

175184
monkeypatch.chdir(MAIN_DIR)
176185
monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
177-
out = subprocess.check_output(
178-
[
179-
sys.executable,
180-
"-m",
181-
"build",
182-
"--sdist",
183-
"--outdir",
184-
str(tmpdir),
185-
]
186+
subprocess.run(
187+
[sys.executable, "-m", "build", "--sdist", "--outdir", str(tmpdir)], check=True
186188
)
187189

188-
if hasattr(out, "decode"):
189-
out = out.decode()
190-
191190
(sdist,) = tmpdir.visit("*.tar.gz")
192191

193192
with tarfile.open(str(sdist), "r:gz") as tar:
194193
start = tar.getnames()[0] + "/"
195194
version = start[16:-1]
196195
simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]}
197196

198-
with contextlib.closing(
199-
tar.extractfile(tar.getmember(start + "setup.py"))
200-
) as f:
201-
setup_py = f.read()
197+
setup_py = read_tz_file(tar, "setup.py")
198+
pyproject_toml = read_tz_file(tar, "pyproject.toml")
199+
pkgconfig = read_tz_file(tar, "pybind11/share/pkgconfig/pybind11.pc")
200+
cmake_cfg = read_tz_file(
201+
tar, "pybind11/share/cmake/pybind11/pybind11Config.cmake"
202+
)
202203

203-
with contextlib.closing(
204-
tar.extractfile(tar.getmember(start + "pyproject.toml"))
205-
) as f:
206-
pyproject_toml = f.read()
204+
assert (
205+
'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")'
206+
in cmake_cfg.decode("utf-8")
207+
)
207208

208209
files = {f"pybind11/{n}" for n in all_files}
209210
files |= sdist_files
@@ -214,20 +215,24 @@ def test_build_global_dist(monkeypatch, tmpdir):
214215
contents = (
215216
string.Template(f.read().decode())
216217
.substitute(version=version, extra_cmd="")
217-
.encode()
218+
.encode("utf-8")
218219
)
219220
assert setup_py == contents
220221

221222
with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f:
222223
contents = f.read()
223224
assert pyproject_toml == contents
224225

226+
simple_version = ".".join(version.split(".")[:3])
227+
pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version).encode("utf-8")
228+
assert normalize_line_endings(pkgconfig) == pkgconfig_expected
229+
225230

226231
def tests_build_wheel(monkeypatch, tmpdir):
227232
monkeypatch.chdir(MAIN_DIR)
228233

229-
subprocess.check_output(
230-
[sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)]
234+
subprocess.run(
235+
[sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)], check=True
231236
)
232237

233238
(wheel,) = tmpdir.visit("*.whl")
@@ -254,8 +259,8 @@ def tests_build_global_wheel(monkeypatch, tmpdir):
254259
monkeypatch.chdir(MAIN_DIR)
255260
monkeypatch.setenv("PYBIND11_GLOBAL_SDIST", "1")
256261

257-
subprocess.check_output(
258-
[sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)]
262+
subprocess.run(
263+
[sys.executable, "-m", "pip", "wheel", ".", "-w", str(tmpdir)], check=True
259264
)
260265

261266
(wheel,) = tmpdir.visit("*.whl")

0 commit comments

Comments
 (0)