//! Integration tests for the `refs` command. use assert_cmd::Command; use predicates::prelude::*; use std::path::PathBuf; fn cargo_command() -> Command { Command::cargo_bin("refs").expect("binary") } fn fixture_path(relative: &str) -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("..") .join("..") .join("fixtures") .join("polyglot") .join("repo") .join(relative) } fn pwsh_command(script: impl AsRef) -> Command { let mut command = Command::new("pwsh"); command .arg("-NoProfile") .arg("-Command") .arg(script.as_ref()); command } #[test] fn emits_exact_use_sites_as_json() { let mut command = cargo_command(); command .arg("--json") .arg("helper") .arg(fixture_path("")) .assert() .success() .stdout(predicate::str::contains( "\"enclosing_qualified_name\":\"call_helper\"", )) .stdout(predicate::str::contains("\"engine\":\"codeindex\"")); } #[test] fn groups_hits_by_callers() { let mut command = cargo_command(); command .args(["--callers", "helper"]) .arg(fixture_path("")) .assert() .success() .stdout(predicate::str::contains("caller=call_helper")) .stdout(predicate::str::contains("hits=2")); } #[test] fn finds_powershell_callers_in_auto_mode() { let mut command = cargo_command(); command .args(["--callers", "--lang", "powershell", "Invoke-Helper"]) .arg(fixture_path("scripts/tools.ps1")) .assert() .success() .stdout(predicate::str::contains("caller=Invoke-Formatting")); } #[test] fn supports_anchor_resolution() { let mut command = cargo_command(); command .args(["--callers", "--at"]) .arg(format!("{}:22", fixture_path("src/lib.rs").display())) .arg(fixture_path("")) .assert() .success() .stdout(predicate::str::contains("caller=call_helper")); } #[test] fn supports_powershell_pipeline_roots() { let binary = assert_cmd::cargo::cargo_bin("refs"); let root = fixture_path(""); let script = format!( "'{}' | & '{}' helper --json | ConvertFrom-Json | Select-Object -First 1 -ExpandProperty name", root.display(), binary.display() ); let mut command = pwsh_command(script); command .assert() .success() .stdout(predicate::str::contains("helper")); } #[test] fn accepts_jsonl_roots_from_stdin() { let root = fixture_path(""); let mut command = cargo_command(); command .args(["--json", "--input-format", "jsonl", "helper"]) .write_stdin(format!( "{}\n", serde_json::json!({ "path": root.display().to_string() }) )) .assert() .success() .stdout(predicate::str::contains("\"name\":\"helper\"")) .stdout(predicate::str::contains("\"path\":\"")); } #[test] fn reports_no_results_and_quiet_paths_in_text_mode() { let root = fixture_path(""); let mut command = cargo_command(); command .arg("totally_absent_symbol") .arg(&root) .assert() .failure() .stdout(predicate::str::contains("0 references")); let mut callers = cargo_command(); callers .args(["--callers", "--quiet", "totally_absent_symbol"]) .arg(root) .assert() .failure() .stdout(predicate::str::is_empty()); } #[test] fn applies_language_filters_and_limits_to_json_output() { let root = fixture_path(""); let mut command = cargo_command(); let assert = command .args([ "--json", "--lang", "python,typescript", "--limit", "1", "helper", ]) .arg(root) .assert() .success(); let stdout = String::from_utf8(assert.get_output().stdout.clone()).expect("utf8"); let uses = serde_json::from_str::>(&stdout).expect("json"); assert_eq!(uses.len(), 1); let path = uses[0] .get("path") .and_then(serde_json::Value::as_str) .expect("path"); assert!(path.ends_with("worker.py") || path.ends_with("app.ts")); assert!(!path.ends_with("lib.rs")); assert!(!path.ends_with("app.js")); } #[test] fn text_engine_reports_text_use_sites() { let mut command = cargo_command(); command .args(["--json", "--engine", "text", "helper"]) .arg(fixture_path("src/lib.rs")) .assert() .success() .stdout(predicate::str::contains("\"engine\":\"text\"")) .stdout(predicate::str::contains("\"name\":\"helper\"")); } #[test] fn help_includes_examples_and_pipeline_usage() { let mut command = cargo_command(); command .arg("--help") .assert() .success() .stdout(predicate::str::contains("--callers")) .stdout(predicate::str::contains("ConvertFrom-Json")) .stdout(predicate::str::contains("refs helper")); }