#![allow(clippy::unwrap_used, clippy::expect_used)]
#![cfg(feature = "localfs")]
mod common;
use common::{kernel_at, run};
use kaish_kernel::ExecuteOptions;
#[cfg(unix)]
#[tokio::test]
async fn tee_append_succeeds_on_a_write_only_file() {
use std::os::unix::fs::PermissionsExt;
let tmp = tempfile::tempdir().unwrap();
let kernel = kernel_at(tmp.path());
let file = tmp.path().join("existing.txt");
std::fs::write(&file, b"original content\n").unwrap();
std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o200)).unwrap();
let read_still_works = std::fs::read(&file).is_ok();
let result = kernel.execute("echo appended | tee -a existing.txt").await;
std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o644)).unwrap();
if read_still_works {
eprintln!(
"skipping: mode-000 did not deny read (likely running as root); \
fixture cannot exercise the write-only-file path"
);
return;
}
let result = result.expect("kernel execute");
assert_eq!(
result.code, 0,
"tee -a on a write-only file must succeed (no read permission needed \
for a real append): {}",
result.text_out()
);
let on_disk = std::fs::read(&file).unwrap();
assert_eq!(
on_disk, b"original content\nappended\n",
"tee -a must append to, not replace, a write-only file's content; got {on_disk:?}"
);
}
#[tokio::test]
async fn tee_append_still_creates_a_missing_file() {
let tmp = tempfile::tempdir().unwrap();
let kernel = kernel_at(tmp.path());
let (out, code) = run(&kernel, "echo hello | tee -a newfile.txt").await;
assert_eq!(code, 0, "tee -a on a missing file must succeed: {out}");
let on_disk = std::fs::read(tmp.path().join("newfile.txt")).unwrap();
assert_eq!(on_disk, b"hello\n");
}
#[tokio::test]
async fn tee_append_still_appends_to_an_existing_file() {
let tmp = tempfile::tempdir().unwrap();
let kernel = kernel_at(tmp.path());
let file = tmp.path().join("existing.txt");
std::fs::write(&file, b"original content\n").unwrap();
let (out, code) = run(&kernel, "echo appended | tee -a existing.txt").await;
assert_eq!(code, 0, "tee -a on an existing, readable file must succeed: {out}");
let on_disk = std::fs::read(&file).unwrap();
assert_eq!(on_disk, b"original content\nappended\n");
}
#[tokio::test]
async fn tee_without_append_still_overwrites() {
let tmp = tempfile::tempdir().unwrap();
let kernel = kernel_at(tmp.path());
let file = tmp.path().join("existing.txt");
std::fs::write(&file, b"original content\n").unwrap();
let (out, code) = run(&kernel, "echo replaced | tee existing.txt").await;
assert_eq!(code, 0, "tee (no -a) on an existing file must succeed: {out}");
let on_disk = std::fs::read(&file).unwrap();
assert_eq!(on_disk, b"replaced\n");
}
#[tokio::test]
async fn tee_append_passes_binary_content_byte_exact() {
let tmp = tempfile::tempdir().unwrap();
let kernel = kernel_at(tmp.path());
let file = tmp.path().join("existing.bin");
std::fs::write(&file, [0xffu8, 0xfe, 0x00, 0x01]).unwrap();
let stdin: Vec<u8> = vec![0xdeu8, 0xad, 0xbe, 0xef];
let result = kernel
.execute_with_options("tee -a existing.bin", ExecuteOptions::new().with_stdin(stdin))
.await
.expect("kernel execute");
assert_eq!(result.code, 0, "tee -a on binary content must succeed: {}", result.text_out());
let on_disk = std::fs::read(&file).unwrap();
assert_eq!(on_disk, [0xff, 0xfe, 0x00, 0x01, 0xde, 0xad, 0xbe, 0xef]);
}
#[cfg(unix)]
#[tokio::test]
async fn redirect_append_succeeds_on_a_write_only_file() {
use std::os::unix::fs::PermissionsExt;
let tmp = tempfile::tempdir().unwrap();
let kernel = kernel_at(tmp.path());
let file = tmp.path().join("existing.txt");
std::fs::write(&file, b"original content\n").unwrap();
std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o200)).unwrap();
let read_still_works = std::fs::read(&file).is_ok();
let result = kernel.execute("echo appended >> existing.txt").await;
std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o644)).unwrap();
if read_still_works {
eprintln!(
"skipping: mode-000 did not deny read (likely running as root); \
fixture cannot exercise the write-only-file path"
);
return;
}
let result = result.expect("kernel execute");
assert_eq!(
result.code, 0,
">> on a write-only file must succeed (same append primitive as tee -a): {}",
result.text_out()
);
let on_disk = std::fs::read(&file).unwrap();
assert_eq!(
on_disk, b"original content\nappended\n",
">> must append to, not replace, a write-only file's content; got {on_disk:?}"
);
}