1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
use anyhow::{Context, Result};
use clap::Parser;
use expect_test::Expect;
use flate2::read::GzDecoder;
use fs_err::File;
use maturin::pyproject_toml::{SdistGenerator, ToolMaturin};
use maturin::{BuildOptions, CargoOptions, PlatformTag};
use pretty_assertions::assert_eq;
use std::collections::BTreeSet;
use std::io::Read;
use std::path::{Path, PathBuf};
use tar::Archive;
use time::OffsetDateTime;
use zip::ZipArchive;
/// Tries to compile a sample crate (pyo3-pure) for musl,
/// given that rustup and the the musl target are installed
///
/// The bool in the Ok() response says whether the test was actually run
pub fn test_musl() -> Result<bool> {
use anyhow::bail;
use fs_err as fs;
use fs_err::File;
use goblin::elf::Elf;
use std::io::ErrorKind;
use std::process::Command;
let get_target_list = Command::new("rustup")
.args(["target", "list", "--installed"])
.output();
match get_target_list {
Ok(output) => {
if output.status.success() {
let has_musl = String::from_utf8_lossy(&output.stdout)
.lines()
.any(|line| line.trim() == "x86_64-unknown-linux-musl");
if !has_musl {
return Ok(false);
}
} else {
bail!(
"`rustup target list --installed` failed with status {}",
output.status
)
}
}
// Ignore installations without rustup
Err(err) if err.kind() == ErrorKind::NotFound => return Ok(false),
Err(err) => return Err(err.into()),
};
// The first arg gets ignored
let options: BuildOptions = BuildOptions::try_parse_from([
"build",
"--manifest-path",
"test-crates/hello-world/Cargo.toml",
"--interpreter",
"python3",
"--target",
"x86_64-unknown-linux-musl",
"--compatibility",
"linux",
"--quiet",
"--target-dir",
"test-crates/targets/test_musl",
"--out",
"test-crates/wheels/test_musl",
])?;
let build_context = options
.into_build_context()
.release(false)
.strip(cfg!(feature = "faster-tests"))
.editable(false)
.build()?;
let built_lib =
PathBuf::from("test-crates/targets/test_musl/x86_64-unknown-linux-musl/debug/hello-world");
if built_lib.is_file() {
fs::remove_file(&built_lib)?;
}
let wheels = build_context.build_wheels()?;
assert_eq!(wheels.len(), 1);
// Ensure that we've actually built for musl
assert!(built_lib.is_file());
let mut file = File::open(built_lib)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
let elf = Elf::parse(&buffer)?;
assert_eq!(elf.libraries, Vec::<&str>::new());
Ok(true)
}
/// Test that we ignore non-existent Cargo.lock file listed by `cargo package --list`,
/// which seems to only occur with workspaces.
/// See https://github.com/rust-lang/cargo/issues/7938#issuecomment-593280660 and
/// https://github.com/PyO3/maturin/issues/449
pub fn test_workspace_cargo_lock() -> Result<()> {
// The first arg gets ignored
let options: BuildOptions = BuildOptions::try_parse_from([
"build",
"--manifest-path",
"test-crates/workspace/py/Cargo.toml",
"--compatibility",
"linux",
"--quiet",
"--target-dir",
"test-crates/targets/test_workspace_cargo_lock",
"--out",
"test-crates/wheels/test_workspace_cargo_lock",
])?;
let build_context = options
.into_build_context()
.release(false)
.strip(false)
.editable(false)
.build()?;
let source_distribution = build_context.build_source_distribution()?;
assert!(source_distribution.is_some());
Ok(())
}
pub fn test_source_distribution(
package: impl AsRef<Path>,
sdist_generator: SdistGenerator,
expected_files: Expect,
expected_cargo_toml: Option<(&Path, Expect)>,
unique_name: &str,
) -> Result<()> {
let manifest_path = package.as_ref().join("Cargo.toml");
let sdist_directory = Path::new("test-crates").join("wheels").join(unique_name);
let build_options = BuildOptions {
out: Some(sdist_directory),
cargo: CargoOptions {
manifest_path: Some(manifest_path),
quiet: true,
target_dir: Some(PathBuf::from(
"test-crates/targets/test_workspace_cargo_lock",
)),
..Default::default()
},
..Default::default()
};
let mut build_context = build_options
.into_build_context()
.release(false)
.strip(false)
.editable(false)
.sdist_only(true)
.build()?;
// Override the sdist generator for testing
let mut pyproject_toml = build_context.pyproject_toml.take().unwrap();
let mut tool = pyproject_toml.tool.clone().unwrap_or_default();
if let Some(ref mut tool_maturin) = tool.maturin {
tool_maturin.sdist_generator = sdist_generator;
} else {
tool.maturin = Some(ToolMaturin {
sdist_generator,
..Default::default()
});
}
pyproject_toml.tool = Some(tool);
build_context.pyproject_toml = Some(pyproject_toml);
let (path, _) = build_context
.build_source_distribution()?
.context("Failed to build source distribution")?;
let tar_gz = fs_err::File::open(path)?;
let tar = GzDecoder::new(tar_gz);
let mut archive = Archive::new(tar);
let mut files = BTreeSet::new();
let mut file_count = 0;
let mut cargo_toml = None;
for entry in archive.entries()? {
let mut entry = entry?;
files.insert(format!("{}", entry.path()?.display()));
file_count += 1;
if let Some(cargo_toml_path) = expected_cargo_toml.as_ref().map(|(p, _)| *p) {
if entry.path()? == cargo_toml_path {
let mut contents = String::new();
entry.read_to_string(&mut contents)?;
cargo_toml = Some(contents);
}
}
}
expected_files.assert_debug_eq(&files);
assert_eq!(
file_count,
files.len(),
"duplicated files found in sdist: {:?}",
files
);
if let Some((cargo_toml_path, expected)) = expected_cargo_toml {
let cargo_toml = cargo_toml
.with_context(|| format!("{} not found in sdist", cargo_toml_path.display()))?;
expected.assert_eq(&cargo_toml.replace("\r\n", "\n"));
}
Ok(())
}
fn build_wheel_files(package: impl AsRef<Path>, unique_name: &str) -> Result<ZipArchive<File>> {
let manifest_path = package.as_ref().join("Cargo.toml");
let wheel_directory = Path::new("test-crates").join("wheels").join(unique_name);
let build_options = BuildOptions {
out: Some(wheel_directory),
cargo: CargoOptions {
manifest_path: Some(manifest_path),
quiet: true,
target_dir: Some(PathBuf::from(format!("test-crates/targets/{unique_name}"))),
..Default::default()
},
platform_tag: vec![PlatformTag::Linux],
..Default::default()
};
let build_context = build_options
.into_build_context()
.release(false)
.strip(false)
.editable(false)
.build()?;
let wheels = build_context
.build_wheels()
.context("Failed to build wheels")?;
assert!(!wheels.is_empty());
let (wheel_path, _) = &wheels[0];
let wheel = ZipArchive::new(File::open(wheel_path)?)?;
Ok(wheel)
}
pub fn check_wheel_mtimes(
package: impl AsRef<Path>,
expected_mtime: Vec<OffsetDateTime>,
unique_name: &str,
) -> Result<()> {
let mut wheel = build_wheel_files(package, unique_name)?;
let mut mtimes = BTreeSet::<OffsetDateTime>::new();
for idx in 0..wheel.len() {
let mtime = wheel.by_index(idx)?.last_modified().unwrap().try_into()?;
mtimes.insert(mtime);
}
assert_eq!(mtimes, expected_mtime.into_iter().collect::<BTreeSet<_>>());
Ok(())
}
pub fn check_wheel_files(
package: impl AsRef<Path>,
expected_files: Vec<&str>,
unique_name: &str,
) -> Result<()> {
let wheel = build_wheel_files(package, unique_name)?;
let drop_platform_specific_files = |file: &&str| -> bool {
!matches!(Path::new(file).extension(), Some(ext) if ext == "pyc" || ext == "pyd" || ext == "so")
};
assert_eq!(
wheel
.file_names()
.filter(drop_platform_specific_files)
.collect::<BTreeSet<_>>(),
expected_files.into_iter().collect::<BTreeSet<_>>()
);
Ok(())
}
pub fn abi3_python_interpreter_args() -> Result<()> {
// Case 1: maturin build without `-i`, should work
let options = BuildOptions::try_parse_from(vec![
"build",
"--manifest-path",
"test-crates/pyo3-pure/Cargo.toml",
"--quiet",
])?;
let result = options
.into_build_context()
.release(false)
.strip(cfg!(feature = "faster-tests"))
.editable(false)
.build();
assert!(result.is_ok());
// Case 2: maturin build -i python3.10, should work because python3.10 is in bundled sysconfigs
let options = BuildOptions::try_parse_from(vec![
"build",
"--manifest-path",
"test-crates/pyo3-pure/Cargo.toml",
"--quiet",
"-i",
"python3.10",
])?;
let result = options
.into_build_context()
.release(false)
.strip(cfg!(feature = "faster-tests"))
.editable(false)
.build();
assert!(result.is_ok());
// Windows is a bit different so we exclude it from case 3 & 4
// Case 3: maturin build -i python2.7, errors because python2.7 is supported
#[cfg(not(windows))]
{
let options = BuildOptions::try_parse_from(vec![
"build",
"--manifest-path",
"test-crates/pyo3-pure/Cargo.toml",
"--quiet",
"-i",
"python2.7",
])?;
let result = options
.into_build_context()
.release(false)
.strip(cfg!(feature = "faster-tests"))
.editable(false)
.build();
assert!(result.is_err());
// Case 4: maturin build -i python-does-not-exists, errors because python executable is not found
let options = BuildOptions::try_parse_from(vec![
"build",
"--manifest-path",
"test-crates/pyo3-pure/Cargo.toml",
"--quiet",
"-i",
"python-does-not-exists",
])?;
let result = options
.into_build_context()
.release(false)
.strip(cfg!(feature = "faster-tests"))
.editable(false)
.build();
assert!(result.is_err());
}
Ok(())
}
pub fn abi3_without_version() -> Result<()> {
// The first argument is ignored by clap
let cli = vec![
"build",
"--manifest-path",
"test-crates/pyo3-abi3-without-version/Cargo.toml",
"--quiet",
"--interpreter",
"python3",
"--target-dir",
"test-targets/wheels/abi3_without_version",
];
let options = BuildOptions::try_parse_from(cli)?;
let result = options
.into_build_context()
.release(false)
.strip(cfg!(feature = "faster-tests"))
.editable(false)
.build();
assert!(result.is_ok());
Ok(())
}
|