use crate::core::util::close::CloseableWrite;
use crate::core::util::error::lucene_error::{LuceneError, LuceneErrorKind, Result};
use crate::core::util::io_utils::IOUtils;
use crate::test_framework::core::util::lucene_test_case::create_temp_dir;
use std::fs::{self, File, create_dir_all};
use std::io::Write;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
#[allow(dead_code)] struct TestIOUtils;
#[test]
fn test_delete_file_ignoring_exceptions() -> Result<()> {
let dir = create_temp_dir()?;
let file1 = dir.path().join("file1");
File::create(&file1)?;
IOUtils::delete_paths_ignoring_exceptions([Some(&file1)]);
assert!(!file1.exists());
Ok(())
}
#[test]
fn test_dont_delete_file_ignoring_exceptions() -> Result<()> {
let dir = create_temp_dir()?;
let file1 = dir.path().join("file1");
IOUtils::delete_paths_ignoring_exceptions([Some(&file1)]);
Ok(())
}
#[test]
fn test_delete_two_files_ignoring_exceptions() -> Result<()> {
let dir = create_temp_dir()?;
let file1 = dir.path().join("file1");
let file2 = dir.path().join("file2");
File::create(&file2)?;
IOUtils::delete_paths_ignoring_exceptions([Some(&file1), Some(&file2)]);
assert!(!file2.exists());
Ok(())
}
#[test]
fn test_delete_file_if_exists() -> Result<()> {
let dir = create_temp_dir()?;
let file1 = dir.path().join("file1");
File::create(&file1)?;
IOUtils::delete_files_if_exist([Some(&file1)])?;
assert!(!file1.exists());
Ok(())
}
#[test]
fn test_dont_delete_doesnt_exist() -> Result<()> {
let dir = create_temp_dir()?;
let file1 = dir.path().join("file1");
IOUtils::delete_files_if_exist([Some(&file1)])?;
Ok(())
}
#[test]
fn test_delete_two_files_if_exist() -> Result<()> {
let dir = create_temp_dir()?;
let file1 = dir.path().join("file1");
let file2 = dir.path().join("file2");
File::create(&file2)?;
IOUtils::delete_files_if_exist([Some(&file1), Some(&file2)])?;
assert!(!file2.exists());
Ok(())
}
#[test]
fn test_fsync_directory() -> Result<()> {
let dir = create_temp_dir()?;
let dir_path = fs::canonicalize(dir.path())?;
let dev_dir = dir_path.join("dev");
create_dir_all(&dev_dir)?;
IOUtils::fsync(&dev_dir, true)?;
Ok(())
}
#[test]
fn test_fsync_access_denied_opening_directory() -> Result<()> {
let dir = create_temp_dir()?;
let path = fs::canonicalize(dir.path())?;
#[cfg(windows)]
IOUtils::fsync(&path, true)?;
#[cfg(unix)]
{
let original_permissions = fs::metadata(&path)?.permissions();
let mut denied_permissions = original_permissions.clone();
denied_permissions.set_mode(0o0);
fs::set_permissions(&path, denied_permissions)?;
let result = IOUtils::fsync(&path, true);
fs::set_permissions(&path, original_permissions)?;
if unsafe { libc::geteuid() } != 0 {
assert!(
matches!(result.as_ref().map_err(LuceneError::kind), Err(LuceneErrorKind::IoWithPath { source, .. })
if source.kind() == std::io::ErrorKind::PermissionDenied)
);
}
}
Ok(())
}
#[test]
fn test_fsync_non_existent_directory() -> Result<()> {
let dir = create_temp_dir()?;
let dir_path = fs::canonicalize(dir.path())?;
let non_existent_dir = dir_path.join("non-existent");
assert!(matches!(
(IOUtils::fsync(&non_existent_dir, true))
.as_ref()
.map_err(LuceneError::kind),
Err(LuceneErrorKind::NoSuchFile(_))
));
Ok(())
}
#[test]
fn test_fsync_file() -> Result<()> {
let dir = create_temp_dir()?;
let dir_path = fs::canonicalize(dir.path())?;
let dev_dir = dir_path.join("dev");
create_dir_all(&dev_dir)?;
let file_path = dev_dir.join("somefile");
let mut output = File::create(&file_path)?;
output.write_all(b"0\n")?;
output.flush()?;
output.close()?;
IOUtils::fsync(&file_path, false)?;
Ok(())
}
#[test]
fn test_apply_to_all() -> Result<()> {
let mut closed = Vec::new();
let values = [1, 2];
let error = IOUtils::apply_to_all(values.iter().map(Some), |value| {
closed.push(*value);
Err(LuceneError::illegal_state(value.to_string()))
})
.unwrap_err();
assert_eq!("1", error.to_string());
let suppressed = error
.get_suppressed()?
.expect("the second failure must be suppressed");
assert_eq!("2", suppressed.to_string());
assert!(suppressed.get_suppressed()?.is_none());
assert_eq!(&[1, 2][..], closed.as_slice());
Ok(())
}