#[path = "common/mod.rs"]
mod common;
use assert_cmd::Command;
use common::TestHomeGuard;
use predicates::prelude::*;
use std::fs;
use std::path::Path;
fn get_test_command(kopi_home: &Path) -> Command {
let mut cmd = Command::cargo_bin("kopi").unwrap();
cmd.env("KOPI_HOME", kopi_home.to_str().unwrap());
let parent = kopi_home.parent().unwrap();
cmd.env("HOME", parent);
#[cfg(windows)]
cmd.env("USERPROFILE", parent);
cmd
}
#[test]
fn test_env_basic_bash() {
let test_home = TestHomeGuard::new();
test_home.setup_kopi_structure();
let kopi_home = test_home.kopi_home();
let jdk_path = kopi_home.join("jdks").join("temurin-21.0.1");
let bin_dir = jdk_path.join("bin");
fs::create_dir_all(&bin_dir).unwrap();
let exe_ext = if cfg!(windows) { ".exe" } else { "" };
fs::write(bin_dir.join(format!("java{exe_ext}")), "mock java").unwrap();
fs::write(bin_dir.join(format!("javac{exe_ext}")), "mock javac").unwrap();
let mut cmd = get_test_command(&kopi_home);
cmd.env("KOPI_JAVA_VERSION", "temurin@21.0.1");
cmd.arg("env").arg("--shell").arg("bash");
cmd.assert()
.success()
.stdout(predicate::str::contains("export JAVA_HOME="))
.stdout(predicate::str::contains("temurin-21.0.1"));
}
#[test]
fn test_env_jdk_not_installed() {
let test_home = TestHomeGuard::new();
test_home.setup_kopi_structure();
let kopi_home = test_home.kopi_home();
let mut cmd = get_test_command(&kopi_home);
cmd.env("KOPI_JAVA_VERSION", "temurin@21.0.1");
cmd.arg("env");
cmd.assert().failure().stderr(predicate::str::contains(
"Error: JDK 'temurin@21.0.1' is not installed",
));
}