use crate::error::{VBError, VBResult};
use crate::state::file;
use crate::value::VBVariant;
use vb6core::error::err_number;
pub fn kill(pathname: &VBVariant) -> VBResult<()> {
let path_str = match pathname {
VBVariant::String(s) => s.as_str().to_string(),
_ => {
return Err(VBError::with_description(
13, "Type mismatch in Kill",
));
}
};
file::remove_file(std::path::Path::new(&path_str)).map_err(|e| {
VBError::with_description(
match e.kind() {
std::io::ErrorKind::NotFound => err_number::FILE_NOT_FOUND, std::io::ErrorKind::PermissionDenied => err_number::PERMISSION_DENIED, _ => err_number::DEVICE_IO_ERROR, },
e.to_string(),
)
})?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::state::file::{self};
use vb6core::error::err_number;
#[test]
fn kill_deletes_file() {
let _guard = crate::state::test_support::lock_test();
let dir = tempfile::tempdir().unwrap();
file::set_root(dir.path());
std::fs::write(dir.path().join("test.txt"), "Hello").unwrap();
assert!(dir.path().join("test.txt").exists());
kill(&VBVariant::from_string("test.txt")).unwrap();
assert!(!dir.path().join("test.txt").exists());
}
#[test]
fn kill_rejects_nonexistent_file() {
let _guard = crate::state::test_support::lock_test();
let dir = tempfile::tempdir().unwrap();
file::set_root(dir.path());
let result = kill(&VBVariant::from_string("nonexistent.txt"));
assert!(result.is_err());
assert_eq!(result.unwrap_err().number, err_number::FILE_NOT_FOUND);
}
#[test]
fn kill_rejects_non_string() {
let _guard = crate::state::test_support::lock_test();
let result = kill(&VBVariant::Long(42));
assert!(result.is_err());
assert_eq!(result.unwrap_err().number, err_number::TYPE_MISMATCH);
}
}