diff --git a/src/build_options.rs b/src/build_options.rs index 3ee86711b..d05bfa2a7 100644 --- a/src/build_options.rs +++ b/src/build_options.rs @@ -564,7 +564,14 @@ impl BuildOptions { } else { // User given list of interpreters let interpreter = if self.interpreter.is_empty() && !target.cross_compiling() { - vec![PathBuf::from("python3")] + if cfg!(test) { + match env::var_os("MATURIN_TEST_PYTHON") { + Some(python) => vec![python.into()], + None => vec![PathBuf::from("python3")], + } + } else { + vec![PathBuf::from("python3")] + } } else { self.interpreter.clone() }; diff --git a/tests/common/integration.rs b/tests/common/integration.rs index 85f10b0d1..91a0d6afd 100644 --- a/tests/common/integration.rs +++ b/tests/common/integration.rs @@ -74,7 +74,7 @@ pub fn test_integration( let interpreter = if build_context.interpreter.is_empty() { let error_message = "python3 should be a python interpreter"; let venv_interpreter = PythonInterpreter::check_executable( - "python3", + python_interp.as_deref().unwrap_or("python3"), &build_context.target, &build_context.bridge, ) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index cd85ee4bc..9b2fbb0ed 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -92,13 +92,30 @@ pub fn handle_result(result: Result) -> T { } } +/// Get Python implementation +pub fn get_python_implementation(python_interp: &Path) -> Result { + let code = "import sys; print(sys.implementation.name, end='')"; + let output = Command::new(python_interp).arg("-c").arg(code).output()?; + let python_impl = String::from_utf8(output.stdout).unwrap(); + Ok(python_impl) +} + /// Create virtualenv pub fn create_virtualenv(name: &str, python_interp: Option) -> Result<(PathBuf, PathBuf)> { + let interp = python_interp.or_else(|| test_python_path().map(PathBuf::from)); + let venv_interp = interp.clone().unwrap_or_else(|| { + let target = Target::from_target_triple(None).unwrap(); + target.get_python() + }); + let venv_name = match get_python_implementation(&venv_interp) { + Ok(python_impl) => format!("{}-{}", name, python_impl), + Err(_) => name.to_string(), + }; let venv_dir = PathBuf::from("test-crates") .normalize()? .into_path_buf() .join("venvs") - .join(name); + .join(venv_name); let target = Target::from_target_triple(None)?; if venv_dir.is_dir() { @@ -106,7 +123,6 @@ pub fn create_virtualenv(name: &str, python_interp: Option) -> Result<( } let mut cmd = Command::new("virtualenv"); - let interp = python_interp.or_else(|| test_python_path().map(PathBuf::from)); if let Some(interp) = interp { cmd.arg("-p").arg(interp); } diff --git a/tests/run.rs b/tests/run.rs index a8049df68..df190dcce 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -1,6 +1,11 @@ //! To speed up the tests, they are tests all collected in a single module -use common::{develop, editable, errors, handle_result, integration, other}; +use common::{ + develop, editable, errors, get_python_implementation, handle_result, integration, other, + test_python_path, +}; +use maturin::Target; +use std::path::PathBuf; mod common; @@ -145,22 +150,11 @@ fn editable_pyo3_ffi_pure() { #[test] fn integration_pyo3_bin() { - use common::test_python_path; - use maturin::Target; - use std::path::PathBuf; - use std::process::Command; - let python = test_python_path().map(PathBuf::from).unwrap_or_else(|| { let target = Target::from_target_triple(None).unwrap(); target.get_python() }); - let code = "import sys; print(sys.implementation.name, end='')"; - let output = Command::new(python) - .arg("-c") - .arg(code) - .output() - .expect("Failed to execute python"); - let python_implementation = String::from_utf8(output.stdout).unwrap(); + let python_implementation = get_python_implementation(&python).unwrap(); if python_implementation == "pypy" { // PyPy doesn't support the 'auto-initialize' feature of pyo3 return; @@ -322,10 +316,20 @@ fn integration_wasm_hello_world() { Some("wasm32-wasi"), )); + let python = test_python_path().map(PathBuf::from).unwrap_or_else(|| { + let target = Target::from_target_triple(None).unwrap(); + target.get_python() + }); + let python_implementation = get_python_implementation(&python).unwrap(); + let venv_name = format!( + "integration-wasm-hello-world-py3-wasm32-wasi-{}", + python_implementation + ); + // Make sure we're actually running wasm assert!(Path::new("test-crates") .join("venvs") - .join("integration-wasm-hello-world-py3-wasm32-wasi") + .join(venv_name) .join(if cfg!(target_os = "windows") { "Scripts" } else {