Skip to content

Commit

Permalink
Merge pull request #1220 from messense/pypy-test
Browse files Browse the repository at this point in the history
Create different venv name for different Python implementation in tests
  • Loading branch information
messense authored Oct 27, 2022
2 parents bf7f753 + c8b45f3 commit 7081eaf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
9 changes: 8 additions & 1 deletion src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
Expand Down
2 changes: 1 addition & 1 deletion tests/common/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
20 changes: 18 additions & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,37 @@ pub fn handle_result<T>(result: Result<T>) -> T {
}
}

/// Get Python implementation
pub fn get_python_implementation(python_interp: &Path) -> Result<String> {
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<PathBuf>) -> 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() {
fs::remove_dir_all(&venv_dir)?;
}

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);
}
Expand Down
32 changes: 18 additions & 14 deletions tests/run.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7081eaf

Please sign in to comment.