#![allow(clippy::unwrap_used)]
use std::fs;
use std::process::Command;
fn mkit_bin() -> &'static str {
env!("CARGO_BIN_EXE_mkit")
}
fn write_editor_script(payload: &str) -> (tempfile::TempDir, std::path::PathBuf) {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("fake-editor.sh");
let script = format!("#!/bin/sh\nprintf '%s' \"{payload}\" > \"$1\"\n");
fs::write(&path, script).unwrap();
let mut perms = fs::metadata(&path).unwrap().permissions();
perms.set_mode(0o755);
fs::set_permissions(&path, perms).unwrap();
(dir, path)
}
#[test]
fn commit_without_message_honours_editor_env_var() {
if cfg!(windows) {
return;
}
let td = tempfile::tempdir().unwrap();
let xdg = tempfile::tempdir().unwrap();
Command::new(mkit_bin())
.args(["init"])
.current_dir(td.path())
.env("XDG_CONFIG_HOME", xdg.path())
.status()
.unwrap();
Command::new(mkit_bin())
.args(["keygen"])
.current_dir(td.path())
.env("XDG_CONFIG_HOME", xdg.path())
.status()
.unwrap();
fs::write(td.path().join("hello.txt"), b"hi\n").unwrap();
Command::new(mkit_bin())
.args(["add", "."])
.current_dir(td.path())
.env("XDG_CONFIG_HOME", xdg.path())
.status()
.unwrap();
let (_script_dir, script) = write_editor_script("commit-from-editor");
let out = Command::new(mkit_bin())
.args(["commit"])
.current_dir(td.path())
.env("XDG_CONFIG_HOME", xdg.path())
.env("EDITOR", script.as_os_str())
.env_remove("GIT_EDITOR")
.env_remove("VISUAL")
.output()
.expect("spawn mkit commit");
assert!(
out.status.success(),
"commit failed: stdout={}, stderr={}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("commit-from-editor"),
"commit message from $EDITOR not reflected in output, got: {stderr}"
);
}
#[test]
fn commit_without_message_and_empty_editor_output_aborts() {
if cfg!(windows) {
return;
}
let td = tempfile::tempdir().unwrap();
let xdg = tempfile::tempdir().unwrap();
Command::new(mkit_bin())
.args(["init"])
.current_dir(td.path())
.env("XDG_CONFIG_HOME", xdg.path())
.status()
.unwrap();
Command::new(mkit_bin())
.args(["keygen"])
.current_dir(td.path())
.env("XDG_CONFIG_HOME", xdg.path())
.status()
.unwrap();
fs::write(td.path().join("f.txt"), b"x").unwrap();
Command::new(mkit_bin())
.args(["add", "."])
.current_dir(td.path())
.env("XDG_CONFIG_HOME", xdg.path())
.status()
.unwrap();
let (_script_dir, script) = write_editor_script("");
let out = Command::new(mkit_bin())
.args(["commit"])
.current_dir(td.path())
.env("XDG_CONFIG_HOME", xdg.path())
.env("EDITOR", script.as_os_str())
.env_remove("GIT_EDITOR")
.env_remove("VISUAL")
.output()
.expect("spawn mkit commit");
assert!(
!out.status.success(),
"empty message must abort, but exit code was zero"
);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("empty") || stderr.contains("abort"),
"expected an 'empty' / 'abort' diagnostic, got: {stderr}"
);
}