use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;
fn setup_test_home() -> TempDir {
TempDir::new().expect("Failed to create temp dir")
}
#[test]
fn test_global_command_without_installation() {
let temp_home = setup_test_home();
let mut cmd = Command::cargo_bin("kopi").unwrap();
cmd.env("KOPI_HOME", temp_home.path())
.env("KOPI_AUTO_INSTALL__ENABLED", "false")
.arg("global")
.arg("21")
.assert()
.failure()
.stderr(predicate::str::contains("not installed"));
}
#[test]
fn test_global_command_creates_version_file() {
let mut cmd = Command::cargo_bin("kopi").unwrap();
cmd.arg("global")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains(
"Set the global default JDK version",
));
}
#[test]
fn test_default_alias_works() {
let mut cmd = Command::cargo_bin("kopi").unwrap();
cmd.arg("default")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains(
"Set the global default JDK version",
));
}
#[test]
fn test_global_command_invalid_version() {
let temp_home = setup_test_home();
let mut cmd = Command::cargo_bin("kopi").unwrap();
cmd.env("KOPI_HOME", temp_home.path())
.arg("global")
.arg("invalid-version")
.assert()
.failure()
.stderr(predicate::str::contains("Invalid version"));
}
#[test]
fn test_global_with_distribution() {
let temp_home = setup_test_home();
let mut cmd = Command::cargo_bin("kopi").unwrap();
cmd.env("KOPI_HOME", temp_home.path())
.env("KOPI_AUTO_INSTALL__ENABLED", "false")
.arg("global")
.arg("temurin@21")
.assert()
.failure()
.stderr(predicate::str::contains("not installed"));
}
#[test]
fn test_version_file_format() {
let temp_dir = TempDir::new().unwrap();
let version_file = temp_dir.path().join("version");
fs::write(&version_file, "temurin@21").unwrap();
let content = fs::read_to_string(&version_file).unwrap();
assert_eq!(content, "temurin@21");
fs::write(&version_file, "17").unwrap();
let content = fs::read_to_string(&version_file).unwrap();
assert_eq!(content, "17");
}