diff --git a/tests/common/mod.rs b/tests/common/mod.rs index a8a275919..bad0647d6 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -222,13 +222,3 @@ pub fn create_conda_env(name: &str, major: usize, minor: usize) -> Result<(PathB pub fn test_python_path() -> Option { env::var("MATURIN_TEST_PYTHON").ok() } - -/// Find index of subslice in a larger slice -pub fn find_subslice(haystack: &[T], needle: &[T]) -> Option { - if needle.is_empty() { - return Some(0); - } - haystack - .windows(needle.len()) - .position(|window| window == needle) -} diff --git a/tests/common/pep517.rs b/tests/common/pep517.rs index c5d8f951d..4f826b7f7 100644 --- a/tests/common/pep517.rs +++ b/tests/common/pep517.rs @@ -4,6 +4,13 @@ use std::path::{Path, PathBuf}; use std::process::{Command, Output}; use std::str; +pub fn target_dir(unique_name: &str) -> String { + format!( + "{}/test-crates/targets/{unique_name}", + env!("CARGO_MANIFEST_DIR") + ) +} + /// Creates a virtualenv and activates it, checks that the package isn't installed, uses /// pip install to install it and checks it is working pub fn test_pep517( @@ -47,13 +54,8 @@ pub fn test_pep517( cmd.arg(package.to_str().expect("package is utf8 path")); - cmd.env( - "CARGO_TARGET_DIR", - format!( - "{}/test-crates/targets/{unique_name}", - env!("CARGO_MANIFEST_DIR") - ), - ); + let target_dir = target_dir(unique_name); + cmd.env("CARGO_TARGET_DIR", target_dir); // Building with `--no-build-isolation` means that `maturin` needs to be on PATH _and_ // importable @@ -91,3 +93,19 @@ fn insert_path(env_var: &str, new_path: &Path) -> String { .into_string() .expect("PATH is not valid utf8") } + +/// Whether cargo built the shared library for the specified cargo profile in the test target +/// directory. +pub fn target_has_profile(unique_name: &str, profile: &str) -> bool { + let shared_library = if cfg!(windows) { + "pyo3_pure.dll" + } else if cfg!(target_os = "macos") { + "libpyo3_pure.dylib" + } else { + "libpyo3_pure.so" + }; + PathBuf::from(target_dir(unique_name)) + .join(profile) + .join(shared_library) + .is_file() +} diff --git a/tests/run.rs b/tests/run.rs index b5873804f..c678bf88a 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -1,5 +1,6 @@ //! To speed up the tests, they are tests all collected in a single module +use common::pep517::{target_has_profile, test_pep517}; use common::{ TestInstallBackend, develop, errors, handle_result, integration, other, test_python_implementation, @@ -14,8 +15,6 @@ use std::time::Duration; use time::macros::datetime; use which::which; -use crate::common::{find_subslice, pep517}; - mod common; #[test] @@ -1004,32 +1003,28 @@ fn sdist_source_date_epoch() { #[test] fn pep517_default_profile() { - let output = handle_result(pep517::test_pep517( + let unique_name = "pep517-pyo3-pure"; + handle_result(test_pep517( "test-crates/pyo3-pure", - "pep517-pyo3-pure", + unique_name, false, false, )); - assert!( - find_subslice(&output.stderr, b"`release` profile [optimized]").is_some(), - "Output was: {}", - std::str::from_utf8(&output.stderr).unwrap() - ); + assert!(target_has_profile(unique_name, "release")); + assert!(!target_has_profile(unique_name, "debug")); } #[test] fn pep517_editable_profile() { - let output = handle_result(pep517::test_pep517( + let unique_name = "pep517-pyo3-pure-editable"; + handle_result(test_pep517( "test-crates/pyo3-pure", - "pep517-pyo3-pure-editable", + unique_name, false, true, )); - assert!( - find_subslice(&output.stderr, b"`dev` profile [unoptimized + debuginfo]").is_some(), - "Output was: {}", - std::str::from_utf8(&output.stderr).unwrap() - ); + assert!(!target_has_profile(unique_name, "release")); + assert!(target_has_profile(unique_name, "debug")); }