Skip to content

Commit

Permalink
feat(jstzd): add command for client
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Oct 1, 2024
1 parent 0069139 commit 5fb136e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
40 changes: 40 additions & 0 deletions crates/jstzd/src/task/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,30 @@ impl TryFrom<PathBuf> for Directory {
}
}

impl TryFrom<&Directory> for String {
type Error = anyhow::Error;
fn try_from(dir: &Directory) -> Result<Self> {
match dir {
Directory::TempDir(temp_dir) => temp_dir
.path()
.to_str()
.map(|s| s.to_string())
.ok_or_else(|| {
anyhow::anyhow!("directory path must be a valid utf-8 path")
}),
Directory::Path(path) => {
path.to_str().map(|s| s.to_string()).ok_or_else(|| {
anyhow::anyhow!("directory path must be a valid utf-8 path")
})
}
}
}
}

#[cfg(test)]
mod test {
use std::{ffi::OsStr, os::unix::ffi::OsStrExt, path::PathBuf};

use tempfile::{NamedTempFile, TempDir};

use super::Directory;
Expand All @@ -57,4 +79,22 @@ mod test {
matches!(Directory::try_from(dir_path.clone()).unwrap(), Directory::Path(d) if d == dir_path),
);
}

#[test]
fn test_string_try_from_directory() {
let temp_dir = TempDir::new().unwrap();
let dir_path = temp_dir.path().to_path_buf();
let directory = Directory::Path(dir_path.clone());
let res: Result<String, anyhow::Error> = (&directory).try_into();
assert!(res.is_ok_and(|s| s == dir_path.to_string_lossy()));
}

#[test]
fn test_string_try_from_directory_fails_on_invalid_path() {
let invalid_bytes = b"/tmp/\xFF\xFE";
let invalid_file_path = PathBuf::from(OsStr::from_bytes(invalid_bytes));
let directory = Directory::Path(invalid_file_path);
let res: Result<String, anyhow::Error> = (&directory).try_into();
assert!(res.is_err_and(|e| { e.to_string().contains("valid utf-8 path") }));
}
}
12 changes: 12 additions & 0 deletions crates/jstzd/src/task/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ impl Endpoint {
}
}

impl ToString for Endpoint {
fn to_string(&self) -> String {
format!("{}://{}:{}", self.scheme, self.host, self.port)
}
}

impl TryFrom<Uri> for Endpoint {
type Error = anyhow::Error;
fn try_from(value: Uri) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -83,4 +89,10 @@ mod tests {
let err = Endpoint::try_from(uri).unwrap_err();
assert_eq!(err.to_string(), "No host part in URI 'http://:9999/abc'");
}

#[test]
fn test_to_string() {
let endpoint = Endpoint::localhost(8765);
assert!(endpoint.to_string().contains("http://localhost:8765"));
}
}
63 changes: 61 additions & 2 deletions crates/jstzd/src/task/octez_client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::{directory::Directory, endpoint::Endpoint, octez_node::DEFAULT_RPC_ENDPOINT};
use anyhow::{bail, Result};
use anyhow::{anyhow, bail, Result};
use http::Uri;
use std::{path::PathBuf, str::FromStr};
use std::{ffi::OsStr, path::PathBuf, str::FromStr};
use tempfile::tempdir;
use tokio::process::{Child, Command};

const DEFAULT_BINARY_PATH: &str = "octez-client";

Expand Down Expand Up @@ -83,6 +84,36 @@ pub struct OctezClient {
disable_unsafe_disclaimer: bool,
}

#[allow(dead_code)]
impl OctezClient {
fn command<S: AsRef<OsStr>, I: IntoIterator<Item = S>>(
&self,
args: I,
) -> Result<Command> {
let binary_path = self
.binary_path
.to_str()
.ok_or(anyhow!("binary path must be a valid utf-8 path"))?;
let mut command = Command::new(binary_path);
let base_dir: String = (&self.base_dir).try_into()?;
command.args(["--base-dir", &base_dir]);
command.args(["--endpoint", &self.endpoint.to_string()]);
if self.disable_unsafe_disclaimer {
command.env("TEZOS_CLIENT_UNSAFE_DISABLE_DISCLAIMER", "Y");
}
command.args(args);
Ok(command)
}

fn spawn_command<S: AsRef<OsStr>, I: IntoIterator<Item = S>>(
&self,
args: I,
) -> Result<Child> {
let mut command = self.command(args)?;
Ok(command.spawn()?)
}
}

#[cfg(test)]
mod test {
use tempfile::{NamedTempFile, TempDir};
Expand Down Expand Up @@ -186,4 +217,32 @@ mod test {
octez_client.is_err_and(|e| &e.to_string() == "Binary path is not a file")
);
}

#[test]
fn commands_are_created() {
let temp_dir = TempDir::new().unwrap();
let base_dir = temp_dir.path().to_path_buf();
let octez_client = OctezClientBuilder::new()
.set_base_dir(base_dir.clone())
.build()
.unwrap();
let actual = octez_client.command(["some", "command"]).unwrap();
let actual_program = actual.as_std().get_program().to_str().unwrap();
let actual_args = actual
.as_std()
.get_args()
.map(|arg| arg.to_str().unwrap())
.collect::<Vec<&str>>();
let expected_program = DEFAULT_BINARY_PATH;
let expected_args: Vec<&str> = vec![
"--base-dir",
base_dir.to_str().unwrap(),
"--endpoint",
"http://localhost:8732",
"some",
"command",
];
assert_eq!(actual_program, expected_program);
assert_eq!(actual_args, expected_args);
}
}

0 comments on commit 5fb136e

Please sign in to comment.