mod common;
use std::path::Path;
use std::process::{Command, Output};
fn pets() -> Command {
Command::new(env!("CARGO_BIN_EXE_pets"))
}
fn toml_path(p: &Path) -> String {
p.to_string_lossy().replace('\\', "/")
}
fn base_config(root: &Path) -> String {
format!(
r#"
[project]
name = "MusKitty"
root = "{root}"
crates_dir = "{root}/crates"
[project.commands]
hello = "echo hello-from-pets"
[main_repo]
name = "MusKitty"
dir = "{root}"
url = "https://github.com/Ink-dark/MusKitty.git"
token_key = "main"
"#,
root = toml_path(root),
)
}
fn stdout_plus_stderr(out: &Output) -> String {
String::from_utf8_lossy(&out.stdout).to_string() + &String::from_utf8_lossy(&out.stderr)
}
fn git(dir: &Path, args: &[&str]) {
let out = Command::new("git")
.args(args)
.current_dir(dir)
.output()
.expect("git should run");
assert!(
out.status.success(),
"git {:?} failed: {}",
args,
String::from_utf8_lossy(&out.stderr)
);
}
#[test]
fn help_lists_all_subcommands() {
let out = pets().arg("--help").output().unwrap();
assert!(out.status.success());
let text = String::from_utf8_lossy(&out.stdout);
for name in [
"clone", "pull", "setup", "build", "test", "push", "status", "publish", "wpt", "fmt",
"new", "run", "list",
] {
assert!(text.contains(name), "help is missing subcommand '{}'", name);
}
}
#[test]
fn list_shows_project_commands() {
let dir = common::temp_dir("list");
let cfg = dir.join("repos.toml");
std::fs::write(&cfg, base_config(&dir)).unwrap();
let out = pets()
.args(["list", "MusKitty", "--config"])
.arg(&cfg)
.output()
.unwrap();
assert!(out.status.success());
let text = stdout_plus_stderr(&out);
assert!(text.contains("hello"));
assert!(text.contains("echo hello-from-pets"));
common::cleanup(&dir);
}
#[test]
fn run_executes_project_script() {
let dir = common::temp_dir("run");
let cfg = dir.join("repos.toml");
std::fs::write(&cfg, base_config(&dir)).unwrap();
let out = pets()
.args(["run", "MusKitty", "--config"])
.arg(&cfg)
.args(["hello"])
.output()
.unwrap();
assert!(out.status.success());
let text = stdout_plus_stderr(&out);
assert!(text.contains("hello-from-pets"));
common::cleanup(&dir);
}
#[test]
fn run_missing_script_fails_with_hint() {
let dir = common::temp_dir("runmiss");
let cfg = dir.join("repos.toml");
std::fs::write(&cfg, base_config(&dir)).unwrap();
let out = pets()
.args(["run", "MusKitty", "--config"])
.arg(&cfg)
.args(["nope"])
.output()
.unwrap();
assert!(!out.status.success());
let text = stdout_plus_stderr(&out);
assert!(text.contains("no script 'nope'"));
assert!(text.contains("hello"));
common::cleanup(&dir);
}
#[test]
fn run_unknown_repo_fails() {
let dir = common::temp_dir("runrepo");
let cfg = dir.join("repos.toml");
std::fs::write(&cfg, base_config(&dir)).unwrap();
let out = pets()
.args(["run", "MusKitty", "hello", "--repo", "nope", "--config"])
.arg(&cfg)
.output()
.unwrap();
assert!(!out.status.success());
let text = stdout_plus_stderr(&out);
assert!(text.contains("unknown repo 'nope'"));
common::cleanup(&dir);
}
#[test]
fn missing_config_fails() {
let out = pets()
.args([
"status",
"MusKitty",
"--config",
"C:/definitely/not/here.toml",
])
.output()
.unwrap();
assert!(!out.status.success());
}
#[test]
fn clone_then_status_roundtrip() {
let dir = common::temp_dir("roundtrip");
let src = dir.join("src");
std::fs::create_dir_all(&src).unwrap();
git(&src, &["init", "-q"]);
git(&src, &["config", "user.email", "pets-it@example.com"]);
git(&src, &["config", "user.name", "Pets IT"]);
git(&src, &["config", "core.autocrlf", "false"]);
std::fs::write(src.join("main.txt"), "x\n").unwrap();
git(&src, &["add", "main.txt"]);
git(&src, &["commit", "-q", "-m", "initial"]);
let bare = dir.join("remote.git");
let out = Command::new("git")
.args(["clone", "--bare", "-q"])
.arg(&src)
.arg(&bare)
.output()
.expect("git clone --bare should run");
assert!(
out.status.success(),
"bare clone failed: {}",
String::from_utf8_lossy(&out.stderr)
);
let work = dir.join("work");
let cfg = dir.join("repos.toml");
let cfg_text = format!(
r#"
[project]
name = "MusKitty"
root = "{root}"
[main_repo]
name = "MusKitty"
dir = "{work}"
url = "{bare}"
token_key = "main"
"#,
root = toml_path(&dir),
work = toml_path(&work),
bare = toml_path(&bare),
);
std::fs::write(&cfg, cfg_text).unwrap();
let out = pets()
.args(["clone", "MusKitty", "--config"])
.arg(&cfg)
.output()
.unwrap();
assert!(
out.status.success(),
"clone failed: {}",
String::from_utf8_lossy(&out.stderr)
);
assert!(work.join(".git").is_dir());
let out = pets()
.args(["status", "MusKitty", "--config"])
.arg(&cfg)
.output()
.unwrap();
assert!(out.status.success());
let text = stdout_plus_stderr(&out);
assert!(text.contains("MusKitty"));
assert!(text.contains("clean"));
common::cleanup(&dir);
}