| 1 |
use crate::common::{ |
| 2 |
check_installed, create_named_virtualenv, create_virtualenv, maybe_mock_cargo, test_python_path, |
| 3 |
}; |
| 4 |
use anyhow::{bail, Context, Result}; |
| 5 |
#[cfg(feature = "zig")] |
| 6 |
use cargo_zigbuild::Zig; |
| 7 |
use clap::Parser; |
| 8 |
use fs4::fs_err3::FileExt; |
| 9 |
use fs_err::File; |
| 10 |
use maturin::{BuildOptions, PlatformTag, PythonInterpreter, Target}; |
| 11 |
use normpath::PathExt; |
| 12 |
use std::collections::HashSet; |
| 13 |
use std::env; |
| 14 |
use std::path::{Path, PathBuf}; |
| 15 |
use std::process::Command; |
| 16 |
use std::str; |
| 17 |
|
| 18 |
/// For each installed python version, this builds a wheel, creates a virtualenv if it |
| 19 |
/// doesn't exist, installs the package and runs check_installed.py |
| 20 |
pub fn test_integration( |
| 21 |
package: impl AsRef<Path>, |
| 22 |
bindings: Option<String>, |
| 23 |
unique_name: &str, |
| 24 |
zig: bool, |
| 25 |
target: Option<&str>, |
| 26 |
) -> Result<()> { |
| 27 |
maybe_mock_cargo(); |
| 28 |
|
| 29 |
// Pass CARGO_BIN_EXE_maturin for testing purpose |
| 30 |
env::set_var( |
| 31 |
"CARGO_BIN_EXE_cargo-zigbuild", |
| 32 |
env!("CARGO_BIN_EXE_maturin"), |
| 33 |
); |
| 34 |
|
| 35 |
let package_string = package.as_ref().join("Cargo.toml").display().to_string(); |
| 36 |
|
| 37 |
// The first argument is ignored by clap |
| 38 |
let shed = format!("test-crates/wheels/{unique_name}"); |
| 39 |
let target_dir = format!("test-crates/targets/{unique_name}"); |
| 40 |
let python_interp = test_python_path(); |
| 41 |
let mut cli: Vec<std::ffi::OsString> = vec![ |
| 42 |
"build".into(), |
| 43 |
"--quiet".into(), |
| 44 |
"--manifest-path".into(), |
| 45 |
package_string.into(), |
| 46 |
"--target-dir".into(), |
| 47 |
target_dir.into(), |
| 48 |
"--out".into(), |
| 49 |
shed.into(), |
| 50 |
]; |
| 51 |
|
| 52 |
if let Some(ref bindings) = bindings { |
| 53 |
cli.push("--bindings".into()); |
| 54 |
cli.push(bindings.into()); |
| 55 |
} |
| 56 |
|
| 57 |
if let Some(target) = target { |
| 58 |
cli.push("--target".into()); |
| 59 |
cli.push(target.into()) |
| 60 |
} |
| 61 |
|
| 62 |
#[cfg(feature = "zig")] |
| 63 |
let zig_found = Zig::find_zig().is_ok(); |
| 64 |
#[cfg(not(feature = "zig"))] |
| 65 |
let zig_found = false; |
| 66 |
|
| 67 |
let test_zig = if zig && (env::var("GITHUB_ACTIONS").is_ok() || zig_found) { |
| 68 |
cli.push("--zig".into()); |
| 69 |
true |
| 70 |
} else { |
| 71 |
cli.push("--compatibility".into()); |
| 72 |
cli.push("linux".into()); |
| 73 |
false |
| 74 |
}; |
| 75 |
|
| 76 |
// One scope up to extend the lifetime |
| 77 |
let venvs_dir = Path::new("test-crates") |
| 78 |
.normalize()? |
| 79 |
.into_path_buf() |
| 80 |
.join("venvs"); |
| 81 |
fs_err::create_dir_all(&venvs_dir)?; |
| 82 |
let cffi_provider = "cffi-provider"; |
| 83 |
let cffi_venv = venvs_dir.join(cffi_provider); |
| 84 |
|
| 85 |
if let Some(interp) = python_interp.as_ref() { |
| 86 |
cli.push("--interpreter".into()); |
| 87 |
cli.push(interp.into()); |
| 88 |
} else { |
| 89 |
// Install cffi in a separate environment |
| 90 |
|
| 91 |
// All tests try to use this venv at the same time, so we need to make sure only one |
| 92 |
// modifies it at a time and that during that time, no other test reads it. |
| 93 |
let file = File::create(venvs_dir.join("cffi-provider.lock"))?; |
| 94 |
file.lock_exclusive()?; |
| 95 |
let python = if !cffi_venv.is_dir() { |
| 96 |
create_named_virtualenv(cffi_provider, python_interp.clone().map(PathBuf::from))?; |
| 97 |
let target_triple = Target::from_target_triple(None)?; |
| 98 |
let python = target_triple.get_venv_python(&cffi_venv); |
| 99 |
assert!(python.is_file(), "cffi venv not created correctly"); |
| 100 |
let pip_install_cffi = [ |
| 101 |
"-m", |
| 102 |
"pip", |
| 103 |
"--disable-pip-version-check", |
| 104 |
"--no-cache-dir", |
| 105 |
"install", |
| 106 |
"cffi", |
| 107 |
]; |
| 108 |
let output = Command::new(&python) |
| 109 |
.args(pip_install_cffi) |
| 110 |
.output() |
| 111 |
.with_context(|| format!("pip install cffi failed with {python:?}"))?; |
| 112 |
if !output.status.success() { |
| 113 |
let stderr = String::from_utf8_lossy(&output.stderr); |
| 114 |
let stdout = String::from_utf8_lossy(&output.stdout); |
| 115 |
bail!( |
| 116 |
"Installing cffi into {} failed.\nstdout: {}\nstderr: {}", |
| 117 |
cffi_venv.display(), |
| 118 |
stdout, |
| 119 |
stderr |
| 120 |
); |
| 121 |
} |
| 122 |
python |
| 123 |
} else { |
| 124 |
let target_triple = Target::from_target_triple(None)?; |
| 125 |
target_triple.get_venv_python(&cffi_venv) |
| 126 |
}; |
| 127 |
file.unlock()?; |
| 128 |
cli.push("--interpreter".into()); |
| 129 |
cli.push(python.as_os_str().to_owned()); |
| 130 |
} |
| 131 |
|
| 132 |
let options: BuildOptions = BuildOptions::try_parse_from(cli)?; |
| 133 |
let build_context = options |
| 134 |
.into_build_context() |
| 135 |
.release(false) |
| 136 |
.strip(cfg!(feature = "faster-tests")) |
| 137 |
.editable(false) |
| 138 |
.build()?; |
| 139 |
let wheels = build_context.build_wheels()?; |
| 140 |
|
| 141 |
// For abi3 on unix, we didn't use a python interpreter, but we need one here |
| 142 |
let interpreter = if build_context.interpreter.is_empty() { |
| 143 |
let error_message = "python3 should be a python interpreter"; |
| 144 |
let venv_interpreter = PythonInterpreter::check_executable( |
| 145 |
python_interp.as_deref().unwrap_or("python3"), |
| 146 |
&build_context.target, |
| 147 |
build_context.bridge(), |
| 148 |
) |
| 149 |
.context(error_message)? |
| 150 |
.context(error_message)?; |
| 151 |
vec![venv_interpreter] |
| 152 |
} else { |
| 153 |
build_context.interpreter |
| 154 |
}; |
| 155 |
// We can do this since we know that wheels are built and returned in the |
| 156 |
// order they are in the build context |
| 157 |
for ((filename, supported_version), python_interpreter) in wheels.iter().zip(interpreter) { |
| 158 |
check_for_duplicates(filename)?; |
| 159 |
if test_zig |
| 160 |
&& build_context.target.is_linux() |
| 161 |
&& !build_context.target.is_musl_libc() |
| 162 |
&& build_context.target.get_minimum_manylinux_tag() != PlatformTag::Linux |
| 163 |
{ |
| 164 |
let rustc_ver = rustc_version::version()?; |
| 165 |
let python_arch = build_context.target.get_python_arch(); |
| 166 |
let file_suffix = if rustc_ver >= semver::Version::new(1, 64, 0) { |
| 167 |
format!("manylinux_2_17_{python_arch}.manylinux2014_{python_arch}.whl") |
| 168 |
} else { |
| 169 |
format!("manylinux_2_12_{python_arch}.manylinux2010_{python_arch}.whl") |
| 170 |
}; |
| 171 |
assert!(filename.to_string_lossy().ends_with(&file_suffix)) |
| 172 |
} |
| 173 |
let mut venv_name = if supported_version == "py3" { |
| 174 |
format!("{unique_name}-py3") |
| 175 |
} else { |
| 176 |
format!( |
| 177 |
"{}-py{}.{}", |
| 178 |
unique_name, python_interpreter.major, python_interpreter.minor, |
| 179 |
) |
| 180 |
}; |
| 181 |
if let Some(target) = target { |
| 182 |
venv_name = format!("{venv_name}-{target}"); |
| 183 |
} |
| 184 |
let (venv_dir, python) = |
| 185 |
create_virtualenv(&venv_name, Some(python_interpreter.executable.clone()))?; |
| 186 |
|
| 187 |
let command = [ |
| 188 |
"-m", |
| 189 |
"pip", |
| 190 |
"--disable-pip-version-check", |
| 191 |
"--no-cache-dir", |
| 192 |
"install", |
| 193 |
"--force-reinstall", |
| 194 |
]; |
| 195 |
let output = Command::new(&python) |
| 196 |
.args(command) |
| 197 |
.arg(dunce::simplified(filename)) |
| 198 |
.output() |
| 199 |
.context(format!("pip install failed with {python:?}"))?; |
| 200 |
if !output.status.success() { |
| 201 |
let full_command = format!("{} {}", python.display(), command.join(" ")); |
| 202 |
bail!( |
| 203 |
"pip install in {} failed running {:?}: {}\n--- Stdout:\n{}\n--- Stderr:\n{}\n---\n", |
| 204 |
venv_dir.display(), |
| 205 |
full_command, |
| 206 |
output.status, |
| 207 |
str::from_utf8(&output.stdout)?.trim(), |
| 208 |
str::from_utf8(&output.stderr)?.trim(), |
| 209 |
); |
| 210 |
} |
| 211 |
if !output.stderr.is_empty() { |
| 212 |
bail!( |
| 213 |
"pip raised a warning running {:?}: {}\n--- Stdout:\n{}\n--- Stderr:\n{}\n---\n", |
| 214 |
&command, |
| 215 |
output.status, |
| 216 |
str::from_utf8(&output.stdout)?.trim(), |
| 217 |
str::from_utf8(&output.stderr)?.trim(), |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
check_installed(package.as_ref(), &python)?; |
| 222 |
} |
| 223 |
|
| 224 |
Ok(()) |
| 225 |
} |
| 226 |
|
| 227 |
pub fn test_integration_conda(package: impl AsRef<Path>, bindings: Option<String>) -> Result<()> { |
| 228 |
use crate::common::create_conda_env; |
| 229 |
use std::path::PathBuf; |
| 230 |
use std::process::Stdio; |
| 231 |
|
| 232 |
let package_string = package.as_ref().join("Cargo.toml").display().to_string(); |
| 233 |
|
| 234 |
// Create environments to build against, prepended with "A" to ensure that integration |
| 235 |
// tests are executed with these environments |
| 236 |
let mut interpreters = Vec::new(); |
| 237 |
for minor in 7..=10 { |
| 238 |
let (_, venv_python) = create_conda_env(&format!("A-maturin-env-3{minor}"), 3, minor)?; |
| 239 |
interpreters.push(venv_python); |
| 240 |
} |
| 241 |
|
| 242 |
// The first argument is ignored by clap |
| 243 |
let mut cli: Vec<std::ffi::OsString> = vec![ |
| 244 |
"build".into(), |
| 245 |
"--manifest-path".into(), |
| 246 |
package_string.into(), |
| 247 |
"--quiet".into(), |
| 248 |
"--interpreter".into(), |
| 249 |
]; |
| 250 |
for interp in &interpreters { |
| 251 |
cli.push(interp.to_str().unwrap().into()); |
| 252 |
} |
| 253 |
|
| 254 |
if let Some(ref bindings) = bindings { |
| 255 |
cli.push("--bindings".into()); |
| 256 |
cli.push(bindings.into()); |
| 257 |
} |
| 258 |
|
| 259 |
let options = BuildOptions::try_parse_from(cli)?; |
| 260 |
|
| 261 |
let build_context = options |
| 262 |
.into_build_context() |
| 263 |
.release(false) |
| 264 |
.strip(cfg!(feature = "faster-tests")) |
| 265 |
.editable(false) |
| 266 |
.build()?; |
| 267 |
let wheels = build_context.build_wheels()?; |
| 268 |
|
| 269 |
let mut conda_wheels: Vec<(PathBuf, PathBuf)> = vec![]; |
| 270 |
for ((filename, _), python_interpreter) in wheels.iter().zip(build_context.interpreter) { |
| 271 |
let executable = python_interpreter.executable; |
| 272 |
if executable.to_str().unwrap().contains("maturin-env-") { |
| 273 |
conda_wheels.push((filename.clone(), executable)) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
assert_eq!( |
| 278 |
interpreters.len(), |
| 279 |
conda_wheels.len(), |
| 280 |
"Error creating or detecting conda environments." |
| 281 |
); |
| 282 |
for (wheel_file, executable) in conda_wheels { |
| 283 |
let output = Command::new(&executable) |
| 284 |
.args([ |
| 285 |
"-m", |
| 286 |
"pip", |
| 287 |
"--disable-pip-version-check", |
| 288 |
"install", |
| 289 |
"--force-reinstall", |
| 290 |
]) |
| 291 |
.arg(dunce::simplified(&wheel_file)) |
| 292 |
.stderr(Stdio::inherit()) |
| 293 |
.output()?; |
| 294 |
if !output.status.success() { |
| 295 |
panic!(); |
| 296 |
} |
| 297 |
check_installed(package.as_ref(), &executable)?; |
| 298 |
} |
| 299 |
|
| 300 |
Ok(()) |
| 301 |
} |
| 302 |
|
| 303 |
/// See <https://github.com/PyO3/maturin/issues/2106> and |
| 304 |
/// <https://github.com/PyO3/maturin/issues/2066>. |
| 305 |
fn check_for_duplicates(wheel: &Path) -> Result<()> { |
| 306 |
let mut seen = HashSet::new(); |
| 307 |
let mut reader = File::open(wheel)?; |
| 308 |
// We have to use this API since `ZipArchive` deduplicates names. |
| 309 |
while let Some(file) = zip::read::read_zipfile_from_stream(&mut reader)? { |
| 310 |
if !seen.insert(file.name().to_string()) { |
| 311 |
bail!("Duplicate file: {}", file.name()); |
| 312 |
} |
| 313 |
} |
| 314 |
Ok(()) |
| 315 |
} |