Skip to content

Commit

Permalink
feat(jstzd): add config init for client
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Sep 30, 2024
1 parent 18da236 commit a518733
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ bollard.workspace = true
futures-util.workspace = true
http.workspace = true
octez = { path = "../octez" }
reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
tempfile.workspace = true
tokio.workspace = true

Expand Down
13 changes: 10 additions & 3 deletions crates/jstzd/src/task/octez_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use super::{endpoint::Endpoint, octez_node::DEFAULT_RPC_ENDPOINT};
use anyhow::{anyhow, bail, Result};
use http::Uri;
use std::{ffi::OsStr, path::PathBuf, str::FromStr};
use std::{
ffi::OsStr,
path::{Path, PathBuf},
str::FromStr,
};
use tempfile::{tempdir, TempDir};
use tokio::process::{Child, Command};

Expand Down Expand Up @@ -100,15 +104,13 @@ impl Directory {
}
}

#[allow(dead_code)]
pub struct OctezClient {
binary_path: PathBuf,
base_dir: Directory,
endpoint: Endpoint,
disable_unsafe_disclaimer: bool,
}

#[allow(dead_code)]
impl OctezClient {
fn command<S: AsRef<OsStr>, I: IntoIterator<Item = S>>(
&self,
Expand All @@ -133,6 +135,11 @@ impl OctezClient {
let mut command = self.command(args)?;
Ok(command.spawn()?)
}

pub async fn config_init(&self, output_path: &Path) -> Result<Child> {
let output = output_path.to_str().ok_or(anyhow!("non utf-8 path"))?;
self.spawn_command(["config", "init", "--output", output])
}
}

#[cfg(test)]
Expand Down
44 changes: 44 additions & 0 deletions crates/jstzd/tests/octez_client_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use jstzd::task::{endpoint::Endpoint, octez_client::OctezClientBuilder};
use serde_json::Value;
use std::{
ffi::OsStr,
fs::{read_to_string, remove_file},
os::unix::ffi::OsStrExt,
path::PathBuf,
};
use tempfile::{NamedTempFile, TempDir};

#[tokio::test]
async fn config_init() {
let temp_dir = TempDir::new().unwrap();
let expected_base_dir = temp_dir.path().to_path_buf();
let expected_endpoint: Endpoint = Endpoint::localhost(3000);
let config_file = NamedTempFile::new().unwrap();
let _ = remove_file(config_file.path());
let octez_client = OctezClientBuilder::new()
.set_base_dir(expected_base_dir.clone())
.set_endpoint(expected_endpoint.clone())
.build()
.unwrap();
let mut res = octez_client.config_init(config_file.path()).await.unwrap();
let status = res.wait().await;
assert!(status.is_ok_and(|s| s.success()));
let actual: Value =
serde_json::from_str(&read_to_string(config_file).expect("Unable to read file"))
.expect("Unable to parse JSON");
assert_eq!(
actual["base_dir"],
expected_base_dir.to_str().unwrap().to_owned()
);
assert_eq!(actual["endpoint"], expected_endpoint.to_string());
}

#[tokio::test]
async fn command_fails_on_non_utf8_path() {
let invalid_bytes = b"/tmp/\xFF\xFE";
let invalid_file_path = PathBuf::from(OsStr::from_bytes(invalid_bytes));
let _ = remove_file(&invalid_file_path);
let octez_client = OctezClientBuilder::new().build().unwrap();
let res = octez_client.config_init(&invalid_file_path).await;
assert!(res.is_err_and(|e| { e.to_string().contains("non utf-8 path") }));
}

0 comments on commit a518733

Please sign in to comment.