use tempfile::TempDir;
use super::support::*;
use crate::languages::runner::*;
use crate::languages::spec::ToolSpec;
#[test]
fn repo_local_executable_is_preferred_over_path() {
let dir = TempDir::new().unwrap();
let bin = dir.path().join("mytool");
write_executable(&bin, "#!/bin/sh\n");
let spec = ToolSpec {
local_paths: &["mytool"],
command: &["mytool"],
..ToolSpec::default()
};
assert_eq!(resolve_tool(&spec, dir.path()), Some(bin));
}
#[test]
fn non_executable_repo_local_path_falls_through_to_path() {
let dir = TempDir::new().unwrap();
std::fs::write(dir.path().join("mytool"), "not executable").unwrap();
let path_dir = TempDir::new().unwrap();
let on_path = path_dir.path().join("mytool");
write_executable(&on_path, "#!/bin/sh\nexit 0\n");
let spec = ToolSpec {
local_paths: &["mytool"],
command: &["mytool"],
..ToolSpec::default()
};
let path = std::env::join_paths([path_dir.path()]).expect("joins");
assert_eq!(
resolve_tool_in(&spec, dir.path(), Some(path.as_os_str())),
Some(on_path),
"the non-executable local hit must be skipped and PATH consulted"
);
}
#[test]
fn resolve_tool_returns_none_when_neither_local_nor_path_has_it() {
let dir = TempDir::new().unwrap();
let spec = ToolSpec {
local_paths: &["definitely-not-installed-x9z"],
command: &["definitely-not-installed-x9z"],
..ToolSpec::default()
};
assert_eq!(resolve_tool(&spec, dir.path()), None);
}
#[test]
fn is_configured_true_when_any_config_file_exists() {
let dir = TempDir::new().unwrap();
std::fs::write(dir.path().join("pyproject.toml"), "").unwrap();
assert!(is_configured(&ruff_like_spec(), dir.path()));
}
#[test]
fn tool_status_skipped_when_not_configured_and_detail_lists_config() {
let dir = TempDir::new().unwrap();
let outcome = tool_status(&ruff_like_spec(), dir.path());
assert_eq!(outcome.status, ToolStatus::Skipped);
assert!(
outcome.detail.contains("pyproject.toml"),
"detail was {:?}",
outcome.detail
);
}
#[test]
fn tool_status_unavailable_when_configured_but_binary_missing() {
let dir = TempDir::new().unwrap();
std::fs::write(dir.path().join("pyproject.toml"), "").unwrap();
let spec = ToolSpec {
name: "ruff",
local_paths: &["no/such/ruff"],
command: &["definitely-not-installed-ruff-abc"],
config_files: &["pyproject.toml"],
output_format: "json",
diagnostics_stream: "stdout",
..ToolSpec::default()
};
let outcome = tool_status(&spec, dir.path());
assert_eq!(outcome.status, ToolStatus::Unavailable);
}
#[tokio::test]
async fn a_filename_that_looks_like_a_flag_is_passed_as_a_path() {
let dir = tempfile::tempdir().expect("tempdir");
let bin = dir.path().join("bin");
std::fs::create_dir_all(&bin).expect("mkdir bin");
let tool = bin.join("argvdump");
write_executable(
&tool,
format!(
"#!/bin/sh\nfor a in \"$@\"; do echo \"$a\"; done > {}/argv.txt\nexit 0\n",
dir.path().display()
),
);
std::fs::write(dir.path().join("pyproject.toml"), "").expect("config");
let spec = ToolSpec {
name: "argvdump",
command: &["argvdump"],
local_paths: &["bin/argvdump"],
config_files: &["pyproject.toml"],
output_format: "lines",
diagnostics_stream: "stdout",
..ToolSpec::default()
};
let _ = run_tool(&spec, dir.path(), &["--fix".to_owned()]).await;
let argv = std::fs::read_to_string(dir.path().join("argv.txt")).unwrap_or_default();
assert!(
argv.lines().any(|a| a == "./--fix"),
"a dash-leading filename must reach the tool as `./--fix`, got: {argv:?}"
);
assert!(
!argv.lines().any(|a| a == "--fix"),
"it must not reach the tool as a bare option: {argv:?}"
);
}