use std::path::Path;
use std::process::Command;
pub(crate) fn detect(dir: &Path) -> bool {
dir.join("uv.lock").exists()
}
pub(crate) fn install_cmd(frozen: bool) -> Command {
let mut c = super::program::command("uv");
c.arg("sync");
if frozen {
c.arg("--frozen");
}
c
}
pub(crate) fn run_cmd(script: &str, args: &[String]) -> Command {
let mut c = super::program::command("uv");
c.arg("run").arg(script).args(args);
c
}
pub(crate) fn exec_cmd(args: &[String]) -> Command {
let mut c = super::program::command("uvx");
c.args(args);
c
}
pub(crate) fn run_file_cmd(file: &Path, args: &[String]) -> Command {
let mut c = super::program::command("uv");
c.arg("run").arg(file).args(args);
c
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::{exec_cmd, run_cmd, run_file_cmd};
#[test]
fn run_uses_uv_run_with_script_and_args() {
let built: Vec<_> = run_cmd("greenpy", &[String::from("--flag")])
.get_args()
.map(|arg| arg.to_string_lossy().into_owned())
.collect();
assert_eq!(built, ["run", "greenpy", "--flag"]);
}
#[test]
fn exec_uses_uvx_passthrough() {
let args = [String::from("ruff"), String::from("check")];
let built: Vec<_> = exec_cmd(&args)
.get_args()
.map(|arg| arg.to_string_lossy().into_owned())
.collect();
assert_eq!(built, ["ruff", "check"]);
}
#[test]
fn run_file_cmd_uses_uv_run_with_path() {
let cmd = run_file_cmd(Path::new("/abs/task.py"), &[String::from("--once")]);
let built: Vec<_> = cmd
.get_args()
.map(|arg| arg.to_string_lossy().into_owned())
.collect();
assert_eq!(cmd.get_program().to_string_lossy(), "uv");
assert_eq!(built, ["run", "/abs/task.py", "--once"]);
}
}