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 08aa53e
Show file tree
Hide file tree
Showing 4 changed files with 43 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.

2 changes: 2 additions & 0 deletions crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ bollard.workspace = true
futures-util.workspace = true
http.workspace = true
octez = { path = "../octez" }
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
29 changes: 29 additions & 0 deletions crates/jstzd/tests/octez_client_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use jstzd::task::{endpoint::Endpoint, octez_client::OctezClientBuilder};
use serde_json::Value;
use std::fs::{read_to_string, remove_file};
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());
}

0 comments on commit 08aa53e

Please sign in to comment.