use std::fs;
use std::io;
use std::path::{Path, PathBuf};
#[cfg(any(test, rudzio_test))]
use rudzio::common::context::{Suite, Test};
#[cfg(any(test, rudzio_test))]
use rudzio::runtime::futures::ThreadPool;
#[cfg(all(any(test, rudzio_test), any(target_os = "linux", target_os = "macos"),))]
use rudzio::runtime::monoio;
#[cfg(any(test, rudzio_test))]
use rudzio::runtime::tokio::{CurrentThread, Local, Multithread};
#[cfg(any(test, rudzio_test))]
use rudzio::runtime::{async_std, compio, embassy, smol};
pub const SUFFIX: &str = ".backup_before_migration_to_rudzio";
#[derive(Debug)]
#[non_exhaustive]
pub enum Outcome {
AlreadyExists(PathBuf),
Created(PathBuf),
}
impl Outcome {
#[inline]
#[must_use]
pub fn path(&self) -> &Path {
match self {
Self::AlreadyExists(path) | Self::Created(path) => path,
}
}
}
#[inline]
#[must_use]
pub fn path_for(original: &Path) -> PathBuf {
let mut bytes = original.as_os_str().to_owned();
bytes.push(SUFFIX);
PathBuf::from(bytes)
}
#[inline]
pub fn copy_before_write(original: &Path) -> io::Result<Outcome> {
let dest = path_for(original);
if dest.exists() {
return Ok(Outcome::AlreadyExists(dest));
}
let _bytes_copied = fs::copy(original, &dest)?;
Ok(Outcome::Created(dest))
}
#[rudzio::suite([
(runtime = Multithread::new, suite = Suite, test = Test),
(runtime = CurrentThread::new, suite = Suite, test = Test),
(runtime = Local::new, suite = Suite, test = Test),
(runtime = compio::Runtime::new, suite = Suite, test = Test),
(runtime = embassy::Runtime::new, suite = Suite, test = Test),
(runtime = ThreadPool::new, suite = Suite, test = Test),
(runtime = async_std::Runtime::new, suite = Suite, test = Test),
(runtime = smol::Runtime::new, suite = Suite, test = Test),
#[cfg(any(target_os = "linux", target_os = "macos"))]
(runtime = monoio::Runtime::new, suite = Suite, test = Test),
])]
#[cfg(any(test, rudzio_test))]
mod tests {
use super::{Path, Test, path_for};
#[rudzio::test]
async fn backup_suffix_appends_to_path(_ctx: &Test) -> anyhow::Result<()> {
let path = Path::new("/tmp/foo/bar.rs");
anyhow::ensure!(
path_for(path).as_path()
== Path::new("/tmp/foo/bar.rs.backup_before_migration_to_rudzio"),
"path_for did not append the expected suffix to {}",
path.display(),
);
Ok(())
}
#[rudzio::test]
async fn backup_suffix_applies_to_cargo_toml(_ctx: &Test) -> anyhow::Result<()> {
let path = Path::new("/tmp/foo/Cargo.toml");
anyhow::ensure!(
path_for(path).as_path()
== Path::new("/tmp/foo/Cargo.toml.backup_before_migration_to_rudzio"),
"path_for did not append the expected suffix to {}",
path.display(),
);
Ok(())
}
}