-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod async_octez_node; | ||
pub mod endpoint; | ||
pub mod octez_client; | ||
pub mod octez_node; | ||
|
||
use anyhow::Result; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
use super::{endpoint::Endpoint, octez_node::DEFAULT_RPC_ENDPOINT}; | ||
use anyhow::{bail, Result}; | ||
use http::Uri; | ||
use std::{path::PathBuf, str::FromStr}; | ||
use tempfile::{tempdir, TempDir}; | ||
|
||
const DEFAULT_BINARY_PATH: &str = "octez-client"; | ||
#[derive(Default)] | ||
pub struct OctezClientBuilder { | ||
// if None, use the binary in $PATH | ||
binary_path: Option<PathBuf>, | ||
// if None, use temp directory | ||
base_dir: Option<PathBuf>, | ||
// if None, use localhost:8732 (the default endpoint for octez-node) | ||
endpoint: Option<Endpoint>, | ||
disable_unsafe_disclaimer: bool, | ||
} | ||
|
||
impl OctezClientBuilder { | ||
pub fn new() -> Self { | ||
OctezClientBuilder::default() | ||
} | ||
pub fn set_path(mut self, path: PathBuf) -> Self { | ||
self.binary_path = Some(path); | ||
self | ||
} | ||
pub fn set_base_dir(mut self, base_dir: PathBuf) -> Self { | ||
self.base_dir = Some(base_dir); | ||
self | ||
} | ||
pub fn set_endpoint(mut self, endpoint: Endpoint) -> Self { | ||
self.endpoint = Some(endpoint); | ||
self | ||
} | ||
pub fn set_disable_unsafe_disclaimer( | ||
mut self, | ||
disable_unsafe_disclaimer: bool, | ||
) -> Self { | ||
self.disable_unsafe_disclaimer = disable_unsafe_disclaimer; | ||
self | ||
} | ||
pub fn build(self) -> Result<OctezClient> { | ||
self.validate_binary_path()?; | ||
self.validate_base_dir()?; | ||
let node_default_endpoint = format!("http://{}", DEFAULT_RPC_ENDPOINT); | ||
Ok(OctezClient { | ||
binary_path: self.binary_path.unwrap_or(DEFAULT_BINARY_PATH.into()), | ||
base_dir: self | ||
.base_dir | ||
.map_or(Directory::TempDir(tempdir()?), Directory::Path), | ||
endpoint: self | ||
.endpoint | ||
.unwrap_or(Endpoint::try_from(Uri::from_str(&node_default_endpoint)?)?), | ||
disable_unsafe_disclaimer: self.disable_unsafe_disclaimer, | ||
}) | ||
} | ||
fn validate_binary_path(&self) -> Result<()> { | ||
if let Some(binary_path) = &self.binary_path { | ||
if !binary_path.exists() { | ||
bail!("Binary path does not exist"); | ||
} | ||
if !binary_path.is_file() { | ||
bail!("Binary path is not a file"); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
fn validate_base_dir(&self) -> Result<()> { | ||
if let Some(base_dir) = &self.base_dir { | ||
if !base_dir.exists() { | ||
bail!("Base directory does not exist"); | ||
} | ||
if !base_dir.is_dir() { | ||
bail!("Base directory is not a directory"); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
// TODO: remove later | ||
#[allow(dead_code)] | ||
enum Directory { | ||
TempDir(TempDir), | ||
Path(PathBuf), | ||
} | ||
|
||
// TODO: remove later | ||
#[allow(dead_code)] | ||
pub struct OctezClient { | ||
binary_path: PathBuf, | ||
base_dir: Directory, | ||
endpoint: Endpoint, | ||
disable_unsafe_disclaimer: bool, | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
#[test] | ||
fn builds_default_octez_client() { | ||
let octez_client = OctezClientBuilder::new().build().unwrap(); | ||
assert_eq!( | ||
octez_client.binary_path.to_str().unwrap(), | ||
DEFAULT_BINARY_PATH | ||
); | ||
assert!(matches!(octez_client.base_dir, Directory::TempDir(_))); | ||
assert!(!octez_client.disable_unsafe_disclaimer); | ||
assert_eq!(octez_client.endpoint, Endpoint::localhost(8732)) | ||
} | ||
|
||
#[test] | ||
fn temp_dir_is_created_by_default() { | ||
let octez_client = OctezClientBuilder::new().build().unwrap(); | ||
match octez_client.base_dir { | ||
Directory::TempDir(temp_dir) => assert!(temp_dir.path().exists()), | ||
_ => panic!("Expected TempDir"), | ||
}; | ||
} | ||
|
||
#[test] | ||
fn temp_dir_is_removed_on_drop() { | ||
let octez_client = OctezClientBuilder::new().build().unwrap(); | ||
match &octez_client.base_dir { | ||
Directory::TempDir(temp_dir) => { | ||
let temp_dir_path = temp_dir.path().to_path_buf(); | ||
drop(octez_client); | ||
assert!(!temp_dir_path.exists()); | ||
} | ||
_ => panic!("Expected TempDir"), | ||
}; | ||
} | ||
|
||
#[test] | ||
fn sets_custom_binary_and_dir_path() { | ||
let binary_path = PathBuf::from("/tmp/binary1"); | ||
let dir_path = PathBuf::from("/tmp/dir1"); | ||
let _ = std::fs::remove_dir(&binary_path); | ||
let _ = std::fs::remove_file(&binary_path); | ||
let _ = std::fs::remove_dir(&dir_path); | ||
let _ = std::fs::remove_file(&dir_path); | ||
std::fs::File::create(&binary_path).unwrap(); | ||
std::fs::create_dir(&dir_path).unwrap(); | ||
let octez_client = OctezClientBuilder::new() | ||
.set_path(binary_path.clone()) | ||
.set_base_dir(dir_path.clone()) | ||
.build() | ||
.unwrap(); | ||
assert_eq!(octez_client.binary_path, binary_path); | ||
match octez_client.base_dir { | ||
Directory::Path(path) => { | ||
assert_eq!(path, dir_path) | ||
} | ||
_ => panic!("Expected Path"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn validates_base_dir() { | ||
let dir_path = PathBuf::from("/tmp/dir2"); | ||
let _ = std::fs::remove_dir_all(&dir_path); | ||
let _ = std::fs::remove_file(&dir_path); | ||
let octez_client = OctezClientBuilder::new() | ||
.set_base_dir(dir_path.clone()) | ||
.build(); | ||
assert!(octez_client.is_err()); | ||
assert!(octez_client | ||
.is_err_and(|e| &e.to_string() == "Base directory does not exist")); | ||
std::fs::File::create(&dir_path).unwrap(); | ||
let octez_client = OctezClientBuilder::new() | ||
.set_base_dir(dir_path.clone()) | ||
.build(); | ||
assert!(octez_client.is_err()); | ||
assert!(octez_client | ||
.is_err_and(|e| &e.to_string() == "Base directory is not a directory")); | ||
} | ||
|
||
#[test] | ||
fn validates_binary_path() { | ||
let binary_path = PathBuf::from("/tmp/binary3"); | ||
let _ = std::fs::remove_dir_all(&binary_path); | ||
let _ = std::fs::remove_file(&binary_path); | ||
let octez_client = OctezClientBuilder::new() | ||
.set_path(binary_path.clone()) | ||
.build(); | ||
assert!(octez_client.is_err()); | ||
assert!( | ||
octez_client.is_err_and(|e| &e.to_string() == "Binary path does not exist") | ||
); | ||
std::fs::create_dir(&binary_path).unwrap(); | ||
let octez_client = OctezClientBuilder::new() | ||
.set_path(binary_path.clone()) | ||
.build(); | ||
assert!(octez_client.is_err()); | ||
assert!( | ||
octez_client.is_err_and(|e| &e.to_string() == "Binary path is not a file") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters