Skip to content

Commit

Permalink
Add test-harness fs helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed Aug 28, 2023
1 parent 7dd0e79 commit 8857f8c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

members = [
"ghcid-ng",
"test-harness",
]

resolver = "2"
10 changes: 10 additions & 0 deletions test-harness/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "test-harness"
version = "0.1.0"
edition = "2021"
description = "Test harness for ghcid-ng"
publish = false

[dependencies]
miette = { version = "5.9.0", features = ["fancy"] }
tokio = { version = "1.28.2", features = ["full", "tracing"] }
56 changes: 56 additions & 0 deletions test-harness/src/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::fmt::Display;
use std::path::Path;
use std::time::Duration;

use backoff::backoff::Backoff;
use backoff::ExponentialBackoff;
use miette::Context;
use miette::IntoDiagnostic;
use tokio::fs::File;
use tokio::fs::OpenOptions;
use tokio::io::AsyncWriteExt;

/// Touch a path.
pub async fn touch(path: impl AsRef<Path>) -> miette::Result<()> {
let path = path.as_ref();
OpenOptions::new()
.create(true)
.write(true)
.open(path)
.await
.into_diagnostic()
.wrap_err_with(|| format!("Failed to touch {path:?}"))
.map(|_| ())
}

/// Append some data to a path.
pub async fn append(path: impl AsRef<Path>, data: impl Display) -> miette::Result<()> {
let path = path.as_ref();
let mut file = OpenOptions::new()
.append(true)
.open(path)
.await
.into_diagnostic()
.wrap_err_with(|| format!("Failed to open {path:?}"))?;
file.write_all(data.to_string().as_bytes())
.await
.into_diagnostic()?;
Ok(())
}

/// Wait for a path to be created.
///
/// This should generaly be run under a [`tokio::time::timeout`].
pub async fn wait_for_path(path: &Path) {
let mut backoff = ExponentialBackoff {
max_interval: Duration::from_secs(1),
..Default::default()
};
while let Some(duration) = backoff.next_backoff() {
if (File::open(path).await).is_ok() {
break;
}
tracing::debug!("Waiting {duration:?} before retrying");
tokio::time::sleep(duration).await;
}
}
1 change: 1 addition & 0 deletions test-harness/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod fs;

0 comments on commit 8857f8c

Please sign in to comment.