|
| 1 | +use crate::{args::TestArgs, langs::check_output}; |
| 2 | +use anyhow::{bail, Context, Ok, Result}; |
| 3 | +use std::{path::Path, process::Command}; |
| 4 | + |
| 5 | +/// Run tests. |
| 6 | +pub fn cmd_test(_args: &TestArgs) -> Result<()> { |
| 7 | + let venv_path = Path::new(".venv"); |
| 8 | + let bin_path = venv_path.join("bin"); |
| 9 | + let pytest_path = bin_path.join("pytest"); |
| 10 | + |
| 11 | + // Ensure venv exists. |
| 12 | + if !venv_path.is_dir() { |
| 13 | + println!("⏳️ creating venv..."); |
| 14 | + let mut cmd = Command::new("python3"); |
| 15 | + let cmd = cmd.args(["-m", "venv", ".venv"]); |
| 16 | + let output = cmd.output().context("create venv")?; |
| 17 | + check_output(&output).context("create venv")?; |
| 18 | + } |
| 19 | + |
| 20 | + // Ensure pytest and firefly-test are installed. |
| 21 | + if !pytest_path.is_file() { |
| 22 | + println!("⏳️ installing dependencies..."); |
| 23 | + let pip_path = bin_path.join("pip"); |
| 24 | + let mut cmd = Command::new(&pip_path); |
| 25 | + let cmd = cmd.args(["install", "pytest", "firefly-test"]); |
| 26 | + let output = cmd.output().context("install firefly-test")?; |
| 27 | + check_output(&output).context("install firefly-test")?; |
| 28 | + } |
| 29 | + |
| 30 | + // Run pytest |
| 31 | + println!("⏳️ running pytest..."); |
| 32 | + let mut cmd = Command::new(&pytest_path); |
| 33 | + let status = cmd.status().context("run pytest")?; |
| 34 | + if !status.success() { |
| 35 | + let code = status.code().unwrap_or(1); |
| 36 | + bail!("subprocess exited with status code {code}"); |
| 37 | + } |
| 38 | + |
| 39 | + Ok(()) |
| 40 | +} |
0 commit comments