mirror of
https://github.com/kbwo/testing-language-server.git
synced 2025-08-03 15:18:16 +00:00
prevent passing --test-kind argument to test command
This commit is contained in:
parent
7f27e6638c
commit
5e4f15cbd8
2 changed files with 36 additions and 25 deletions
|
@ -3,35 +3,45 @@ use crate::model::Runner;
|
|||
use clap::Parser;
|
||||
use std::str::FromStr;
|
||||
use testing_language_server::spec::AdapterCommands;
|
||||
use testing_language_server::spec::DetectWorkspaceRootArgs;
|
||||
use testing_language_server::spec::DiscoverArgs;
|
||||
use testing_language_server::spec::RunFileTestArgs;
|
||||
pub mod model;
|
||||
pub mod runner;
|
||||
|
||||
fn detect_test_from_extra(extra: &[String]) -> Result<AvailableTestKind, anyhow::Error> {
|
||||
let test_kind = extra
|
||||
fn pick_test_from_extra(
|
||||
extra: &mut [String],
|
||||
) -> Result<(Vec<String>, AvailableTestKind), anyhow::Error> {
|
||||
// extraから--test-kind=のものを取り出し、元の配列から`--test-kind=`のものは除外する
|
||||
let mut extra = extra.to_vec();
|
||||
let index = extra
|
||||
.iter()
|
||||
.find(|arg| arg.starts_with("--test-kind="))
|
||||
.ok_or_else(|| anyhow::anyhow!("test kind not found"))?;
|
||||
.position(|arg| arg.starts_with("--test-kind="))
|
||||
.unwrap();
|
||||
let test_kind = extra.remove(index);
|
||||
|
||||
let language = test_kind.replace("--test-kind=", "");
|
||||
AvailableTestKind::from_str(&language)
|
||||
Ok((extra, AvailableTestKind::from_str(&language)?))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = AdapterCommands::parse();
|
||||
match args {
|
||||
AdapterCommands::Discover(args) => {
|
||||
let extra = args.extra.clone();
|
||||
let test_kind = detect_test_from_extra(&extra).unwrap();
|
||||
test_kind.disover(args).unwrap();
|
||||
AdapterCommands::Discover(mut args) => {
|
||||
let (extra, test_kind) = pick_test_from_extra(&mut args.extra).unwrap();
|
||||
test_kind.disover(DiscoverArgs { extra, ..args }).unwrap();
|
||||
}
|
||||
AdapterCommands::RunFileTest(args) => {
|
||||
let extra = args.extra.clone();
|
||||
let test_kind = detect_test_from_extra(&extra).unwrap();
|
||||
test_kind.run_file_test(args).unwrap();
|
||||
AdapterCommands::RunFileTest(mut args) => {
|
||||
let (extra, test_kind) = pick_test_from_extra(&mut args.extra).unwrap();
|
||||
test_kind
|
||||
.run_file_test(RunFileTestArgs { extra, ..args })
|
||||
.unwrap();
|
||||
}
|
||||
AdapterCommands::DetectWorkspaceRoot(args) => {
|
||||
let extra = args.extra.clone();
|
||||
let test_kind = detect_test_from_extra(&extra).unwrap();
|
||||
test_kind.detect_workspaces_root(args).unwrap();
|
||||
AdapterCommands::DetectWorkspaceRoot(mut args) => {
|
||||
let (extra, test_kind) = pick_test_from_extra(&mut args.extra).unwrap();
|
||||
test_kind
|
||||
.detect_workspaces_root(DetectWorkspaceRootArgs { extra, ..args })
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -45,29 +55,29 @@ mod tests {
|
|||
#[test]
|
||||
// If `--test-kind=<value>` is not present, then return Err
|
||||
fn error_test_kind_detection() {
|
||||
let extra = vec![];
|
||||
detect_test_from_extra(&extra).unwrap_err();
|
||||
let extra = vec!["--foo=bar".to_string()];
|
||||
detect_test_from_extra(&extra).unwrap_err();
|
||||
let mut extra = vec![];
|
||||
pick_test_from_extra(&mut extra).unwrap_err();
|
||||
let mut extra = vec!["--foo=bar".to_string()];
|
||||
pick_test_from_extra(&mut extra).unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
// If `--test-kind=<value>` is present, then return Ok(value)
|
||||
fn test_kind_detection() {
|
||||
let extra = vec!["--test-kind=cargo-test".to_string()];
|
||||
let language = detect_test_from_extra(&extra).unwrap();
|
||||
let mut extra = vec!["--test-kind=cargo-test".to_string()];
|
||||
let (_, language) = pick_test_from_extra(&mut extra).unwrap();
|
||||
assert_eq!(language, AvailableTestKind::CargoTest(CargoTestRunner));
|
||||
}
|
||||
|
||||
#[test]
|
||||
// If multiple `--test-kind=<value>` are present, then return first one
|
||||
fn error_multiple_test_kind_detection() {
|
||||
let extra = vec![
|
||||
let mut extra = vec![
|
||||
"--test-kind=cargo-test".to_string(),
|
||||
"--test-kind=jest".to_string(),
|
||||
"--test-kind=foo".to_string(),
|
||||
];
|
||||
let test_kind = detect_test_from_extra(&extra).unwrap();
|
||||
let (_, test_kind) = pick_test_from_extra(&mut extra).unwrap();
|
||||
assert_eq!(test_kind, AvailableTestKind::Jest(JestRunner));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -245,6 +245,7 @@ impl Runner for CargoTestRunner {
|
|||
let test_result = std::process::Command::new("cargo")
|
||||
.current_dir(&workspace_root)
|
||||
.arg("test")
|
||||
.args(args.extra)
|
||||
.output()
|
||||
.unwrap();
|
||||
let test_result = String::from_utf8(test_result.stdout)?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue