Skip to content

Commit

Permalink
test: add test_develop and check_wheel_files testing for `--with-…
Browse files Browse the repository at this point in the history
…debuginfo`
  • Loading branch information
WSH032 committed Sep 16, 2024
1 parent 1a85cb5 commit b9a034f
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 3 deletions.
2 changes: 2 additions & 0 deletions test-crates/pyo3-bin-msvc-debuginfo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.py[cdo]
*.pdb
7 changes: 7 additions & 0 deletions test-crates/pyo3-bin-msvc-debuginfo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test-crates/pyo3-bin-msvc-debuginfo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "pyo3-mixed"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]]
name = "pyo3-bin" # keep the name with a dash to test `.pdb` file name
path = "src/main.rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sys
import platform
from pathlib import Path


class MaturinTestError(Exception):
pass


def main():
# e.g. `venv\Scripts`
scripts_dir = Path(sys.executable).parent

# set by `bin.name = "pyo3-bin"` in `Cargo.toml`
exe_binding_path = scripts_dir / "pyo3-bin.exe"
if not exe_binding_path.exists():
raise MaturinTestError(f"{exe_binding_path} does not exist")

# the pdb file of `foo-bar.exe` is `foo_bar.pdb`
exe_binding_debuginfo_path = scripts_dir / "pyo3_bin.pdb"
if not exe_binding_debuginfo_path.exists():
raise MaturinTestError(f"{exe_binding_debuginfo_path} does not exist")


if __name__ == "__main__":
if not platform.system() == "Windows":
raise MaturinTestError("This test is only supported on MSVC")

main()

print("SUCCESS")
16 changes: 16 additions & 0 deletions test-crates/pyo3-bin-msvc-debuginfo/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[build-system]
requires = ["maturin>=1.7,<2.0"]
build-backend = "maturin"

[project]
name = "pyo3-mixed"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dynamic = ["version"]

[tool.maturin]
bindings = "bin"
3 changes: 3 additions & 0 deletions test-crates/pyo3-bin-msvc-debuginfo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
2 changes: 2 additions & 0 deletions test-crates/pyo3-mixed-msvc-debuginfo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.py[cdo]
*.pdb
171 changes: 171 additions & 0 deletions test-crates/pyo3-mixed-msvc-debuginfo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions test-crates/pyo3-mixed-msvc-debuginfo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "pyo3-mixed"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "pyo3_mixed"
crate-type = ["cdylib"]

[dependencies]
pyo3 = "0.22.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import platform
from pathlib import Path
import sysconfig

import pyo3_mixed


class MaturinTestError(Exception):
pass


def main():
# e.g. `...\test-crates\pyo3-mixed-msvc-debuginfo\python\pyo3_mixed\__init__.py`
init_py_path = Path(pyo3_mixed.__file__)

ext_suffix = sysconfig.get_config_var("EXT_SUFFIX")

# set by `module-name = "pyo3_mixed._pyo3_lib"` in `pyproject.toml`
# e.g. `_pyo3_lib.cp310-win_amd64.pyd`
lib_pyd_path = init_py_path.with_name(f"_pyo3_lib{ext_suffix}")
if not lib_pyd_path.exists():
raise MaturinTestError(f"{lib_pyd_path} does not exist")

# set by `lib.name = "pyo3_mixed"` in `Cargo.toml`
lib_debuginfo_path = init_py_path.with_name("pyo3_mixed.pdb")
if not lib_debuginfo_path.exists():
raise MaturinTestError(f"{lib_debuginfo_path} does not exist")


if __name__ == "__main__":
if not platform.system() == "Windows":
raise MaturinTestError("This test is only supported on MSVC")

main()

print("SUCCESS")
19 changes: 19 additions & 0 deletions test-crates/pyo3-mixed-msvc-debuginfo/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[build-system]
requires = ["maturin>=1.7,<2.0"]
build-backend = "maturin"

[project]
name = "pyo3-mixed"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dynamic = ["version"]

[tool.maturin]
python-source = "python"
# keep the pyd name different from the rust lib name to test `.pdb` file name
module-name = "pyo3_mixed._pyo3_lib"
features = ["pyo3/extension-module"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from ._pyo3_lib import sum_as_string
from . import _pyo3_lib

__all__ = ["sum_as_string"]

__doc__ = _pyo3_lib.__doc__
if hasattr(_pyo3_lib, "__all__"):
__all__ = _pyo3_lib.__all__
15 changes: 15 additions & 0 deletions test-crates/pyo3-mixed-msvc-debuginfo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use pyo3::prelude::*;

/// A Python module implemented in Rust.
#[pymodule]
// keep the name the same as `module-name = "pyo3_mixed._pyo3_lib"` in `pyproject.toml`
#[pyo3(name = "_pyo3_lib")]
mod pyo3_lib {
use super::*;

/// Formats the sum of two numbers as string.
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}
}
12 changes: 9 additions & 3 deletions tests/common/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ pub fn test_source_distribution(
Ok(())
}

fn build_wheel_files(package: impl AsRef<Path>, unique_name: &str) -> Result<ZipArchive<File>> {
fn build_wheel_files(
package: impl AsRef<Path>,
unique_name: &str,
with_debuginfo: bool,
) -> Result<ZipArchive<File>> {
let manifest_path = package.as_ref().join("Cargo.toml");
let wheel_directory = Path::new("test-crates").join("wheels").join(unique_name);

Expand All @@ -204,6 +208,7 @@ fn build_wheel_files(package: impl AsRef<Path>, unique_name: &str) -> Result<Zip
..Default::default()
},
platform_tag: vec![PlatformTag::Linux],
with_debuginfo,
..Default::default()
};

Expand All @@ -223,7 +228,7 @@ pub fn check_wheel_mtimes(
expected_mtime: Vec<OffsetDateTime>,
unique_name: &str,
) -> Result<()> {
let mut wheel = build_wheel_files(package, unique_name)?;
let mut wheel = build_wheel_files(package, unique_name, false)?;
let mut mtimes = BTreeSet::<OffsetDateTime>::new();

for idx in 0..wheel.len() {
Expand All @@ -240,8 +245,9 @@ pub fn check_wheel_files(
package: impl AsRef<Path>,
expected_files: Vec<&str>,
unique_name: &str,
with_debuginfo: bool,
) -> Result<()> {
let wheel = build_wheel_files(package, unique_name)?;
let wheel = build_wheel_files(package, unique_name, with_debuginfo)?;
let drop_platform_specific_files = |file: &&str| -> bool {
!matches!(Path::new(file).extension(), Some(ext) if ext == "pyc" || ext == "pyd" || ext == "so")
};
Expand Down
Loading

0 comments on commit b9a034f

Please sign in to comment.