use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
use tempfile::tempdir;
#[test]
fn cli_build_produces_dot_and_json_and_hubs_query_works() {
let dir = tempdir().unwrap();
let root = dir.path();
let src = root.join("src");
fs::create_dir_all(&src).unwrap();
write_file(
&src.join("lib.rs"),
r#"
pub mod a;
pub fn top() {}
"#,
);
write_file(
&src.join("a.rs"),
r#"
use crate::top;
pub fn child() { top(); }
"#,
);
let mut cmd = Command::cargo_bin("rust-relations-explorer").unwrap();
cmd.arg("build")
.arg("--path")
.arg(root)
.arg("--json")
.arg(root.join("graph.json"))
.arg("--dot")
.arg(root.join("graph.dot"));
cmd.assert().success();
let json_path = root.join("graph.json");
let dot_path = root.join("graph.dot");
assert!(json_path.exists());
assert!(dot_path.exists());
assert!(fs::metadata(&json_path).unwrap().len() > 0);
assert!(fs::metadata(&dot_path).unwrap().len() > 0);
let mut q = Command::cargo_bin("rust-relations-explorer").unwrap();
q.arg("query")
.arg("hubs")
.arg("--graph")
.arg(&json_path)
.arg("--metric")
.arg("total")
.arg("--top")
.arg("5")
.arg("--format")
.arg("json");
q.assert().success().stdout(predicate::str::contains("["));
}
fn write_file(path: &PathBuf, content: &str) {
let mut f = fs::File::create(path).unwrap();
f.write_all(content.as_bytes()).unwrap();
}