Skip to content

Commit ebf77a2

Browse files
committed
Create TestContext
1 parent d145c19 commit ebf77a2

File tree

6 files changed

+85
-7
lines changed

6 files changed

+85
-7
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ark-testing/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
bitcoin.workspace = true
8+
log.workspace = true
9+
rand.workspace = true
710
tokio.workspace = true

ark-testing/src/constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod env {
2+
pub const TEST_LEAVE_INTACT : &str = "TEST_LEAVE_INTACT";
3+
}

ark-testing/src/context.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::path::PathBuf;
2+
use std::fs;
3+
4+
use crate::util::random_string;
5+
use crate::constants;
6+
7+
pub struct TestContext {
8+
#[allow(dead_code)]
9+
name: String,
10+
datadir: PathBuf
11+
}
12+
13+
impl TestContext {
14+
15+
pub fn new(name: String, base_path: PathBuf) -> Self {
16+
fs::create_dir_all(base_path.clone()).unwrap();
17+
TestContext { name, datadir: base_path}
18+
}
19+
20+
pub fn generate() -> Self {
21+
let name = random_string();
22+
let datadir = ["/tmp/ark-testing/", &name].iter().collect();
23+
Self::new(name, datadir)
24+
}
25+
}
26+
27+
impl Drop for TestContext {
28+
fn drop(&mut self) {
29+
// Remove the data-directory
30+
// If the user has set `LEAVE_INTACT` we don't delete any
31+
// test-data.
32+
33+
if std::env::var(constants::env::TEST_LEAVE_INTACT).is_ok() {
34+
return
35+
}
36+
if self.datadir.exists() {
37+
log::info!("Test clean-up");
38+
std::fs::remove_dir_all(self.datadir.clone()).unwrap();
39+
}
40+
}
41+
}
42+
43+
#[cfg(test)]
44+
mod test {
45+
use super::*;
46+
47+
#[test]
48+
fn context_creates_and_deletes_datadir() {
49+
let context = TestContext::generate();
50+
let base_path = context.datadir.clone();
51+
52+
// The base-path is created
53+
assert!(context.datadir.exists());
54+
drop(context);
55+
56+
// The test cleans up after itself
57+
match std::env::var(constants::env::TEST_LEAVE_INTACT) {
58+
Ok(_) => assert!(base_path.exists()),
59+
Err(_) => assert!(!base_path.exists())
60+
}
61+
}
62+
}

ark-testing/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
#[cfg(test)]
2-
mod tests {
3-
#[test]
4-
fn add_two() {
5-
assert_eq!(1+1, 2);
6-
}
7-
}
1+
mod context;
2+
mod constants;
3+
mod util;
4+
5+
pub use context::TestContext;

ark-testing/src/util.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use rand::RngCore;
2+
3+
pub fn random_string() -> String {
4+
// Generate a few random bytes and base58 encode them
5+
// The entropy should be sufficient to generate unique test-names
6+
let mut entropy :[u8; 8] = [0; 8];
7+
rand::thread_rng().fill_bytes(&mut entropy);
8+
bitcoin::base58::encode(&entropy)
9+
}

0 commit comments

Comments
 (0)