|
| 1 | +use std::env; |
| 2 | +use std::path::PathBuf; |
| 3 | +use std::process::Command; |
| 4 | + |
| 5 | +#[test] |
| 6 | +#[ignore] |
| 7 | +fn run_python_tests() { |
| 8 | + let project_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 9 | + let venv_dir = project_root.join("target").join("pytest_venv"); |
| 10 | + let python_executable_path; |
| 11 | + let maturin_executable_path; |
| 12 | + let pytest_executable_path; |
| 13 | + |
| 14 | + if cfg!(windows) { |
| 15 | + python_executable_path = venv_dir.join("Scripts").join("python.exe"); |
| 16 | + maturin_executable_path = venv_dir.join("Scripts").join("maturin.exe"); |
| 17 | + pytest_executable_path = venv_dir.join("Scripts").join("pytest.exe"); |
| 18 | + } else { |
| 19 | + python_executable_path = venv_dir.join("bin").join("python"); |
| 20 | + maturin_executable_path = venv_dir.join("bin").join("maturin"); |
| 21 | + pytest_executable_path = venv_dir.join("bin").join("pytest"); |
| 22 | + } |
| 23 | + |
| 24 | + // Create virtual environment if it doesn't exist |
| 25 | + if !venv_dir.exists() { |
| 26 | + println!("--- Creating Python virtual environment in `target` directory ---"); |
| 27 | + let venv_cmd = Command::new("python3") |
| 28 | + .args(["-m", "venv", venv_dir.to_str().unwrap()]) |
| 29 | + .status() |
| 30 | + .expect("Failed to execute `python3 -m venv`. Is `python3` in your PATH?"); |
| 31 | + if !venv_cmd.success() { |
| 32 | + panic!("Failed to create python venv. Is `python3` and `venv` module installed?"); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Install dependencies into the virtual environment |
| 37 | + println!("--- Installing maturin and pytest using pip ---"); |
| 38 | + let pip_cmd = Command::new(python_executable_path.to_str().unwrap()) |
| 39 | + .args(["-m", "pip", "install", "-U", "pip", "maturin", "pytest"]) |
| 40 | + .status() |
| 41 | + .expect("Failed to run pip install. Is the venv corrupted?"); |
| 42 | + if !pip_cmd.success() { |
| 43 | + panic!("Failed to install maturin and pytest in the venv."); |
| 44 | + } |
| 45 | + |
| 46 | + // Build the python wheel |
| 47 | + println!("--- Building wheel with maturin ---"); |
| 48 | + let maturin_build_cmd = Command::new(maturin_executable_path.to_str().unwrap()) |
| 49 | + .args(["build", "--release", "--out", "target/wheels"]) |
| 50 | + .status() |
| 51 | + .expect("Failed to run `maturin build`."); |
| 52 | + if !maturin_build_cmd.success() { |
| 53 | + panic!("`maturin build` failed."); |
| 54 | + } |
| 55 | + |
| 56 | + // Install the generated Python wheel |
| 57 | + println!("--- Installing wheel with pip ---"); |
| 58 | + let wheel_path = std::fs::read_dir("target/wheels") |
| 59 | + .unwrap() |
| 60 | + .filter_map(|entry| entry.ok()) |
| 61 | + .find(|entry| { |
| 62 | + entry |
| 63 | + .path() |
| 64 | + .extension() |
| 65 | + .map_or_else(|| false, |ext| ext == "whl") |
| 66 | + }) |
| 67 | + .unwrap() |
| 68 | + .path(); |
| 69 | + |
| 70 | + let pip_install_wheel_cmd = Command::new(python_executable_path.to_str().unwrap()) |
| 71 | + .args([ |
| 72 | + "-m", |
| 73 | + "pip", |
| 74 | + "install", |
| 75 | + wheel_path.to_str().unwrap(), |
| 76 | + "--force-reinstall", |
| 77 | + ]) |
| 78 | + .status() |
| 79 | + .expect("Failed to install wheel."); |
| 80 | + if !pip_install_wheel_cmd.success() { |
| 81 | + panic!("Failed to install wheel."); |
| 82 | + } |
| 83 | + |
| 84 | + // Run pytest |
| 85 | + println!("--- Running pytest ---"); |
| 86 | + let pytest_cmd = Command::new(pytest_executable_path.to_str().unwrap()) |
| 87 | + .arg("tests/python/") |
| 88 | + .status() |
| 89 | + .expect("Failed to run `pytest`."); |
| 90 | + if !pytest_cmd.success() { |
| 91 | + panic!("Pytest failed."); |
| 92 | + } |
| 93 | +} |
| 94 | + |
0 commit comments