From 0e3afd43827958cc081e27544a43a190bc9cfecc Mon Sep 17 00:00:00 2001 From: ljedrz Date: Mon, 7 Sep 2020 17:13:47 +0200 Subject: [PATCH] chore: move the interop objects into separate modules Signed-off-by: ljedrz --- tests/common/interop.rs | 180 ++++++++++++++++++++-------------------- tests/kademlia.rs | 5 +- 2 files changed, 92 insertions(+), 93 deletions(-) diff --git a/tests/common/interop.rs b/tests/common/interop.rs index 87537b51..f275f3d2 100644 --- a/tests/common/interop.rs +++ b/tests/common/interop.rs @@ -1,110 +1,106 @@ #[cfg(feature = "test_go_interop")] -use libp2p::{Multiaddr, PeerId}; -#[cfg(feature = "test_go_interop")] -use rand::prelude::*; -#[cfg(feature = "test_go_interop")] -use serde::Deserialize; -#[cfg(feature = "test_go_interop")] -use std::time::Duration; -#[cfg(feature = "test_go_interop")] -use std::{ - env, fs, - path::PathBuf, - process::{Child, Command, Stdio}, - thread, -}; +pub mod go { + use libp2p::{Multiaddr, PeerId}; + use rand::prelude::*; + use serde::Deserialize; + use std::time::Duration; + use std::{ + env, fs, + path::PathBuf, + process::{Child, Command, Stdio}, + thread, + }; -#[cfg(feature = "test_go_interop")] -#[derive(Deserialize, Debug)] -#[serde(rename_all = "PascalCase")] -pub struct GoNodeId { - #[serde(rename = "ID")] - pub id: String, - #[serde(skip)] - pub public_key: String, - pub addresses: Vec, - #[serde(skip)] - agent_version: String, - #[serde(skip)] - protocol_version: String, -} + #[derive(Deserialize, Debug)] + #[serde(rename_all = "PascalCase")] + pub struct GoNodeId { + #[serde(rename = "ID")] + pub id: String, + #[serde(skip)] + pub public_key: String, + pub addresses: Vec, + #[serde(skip)] + agent_version: String, + #[serde(skip)] + protocol_version: String, + } -#[cfg(feature = "test_go_interop")] -pub struct ForeignNode { - dir: PathBuf, - daemon: Child, - pub id: PeerId, - pub addrs: Vec, -} + pub struct ForeignNode { + dir: PathBuf, + daemon: Child, + pub id: PeerId, + pub addrs: Vec, + } -impl ForeignNode { - #[cfg(feature = "test_go_interop")] - pub fn new() -> ForeignNode { - // GO_IPFS_PATH should point to the location of the go-ipfs binary - let go_ipfs_path = env::vars() - .find(|(key, _val)| key == "GO_IPFS_PATH") - .expect("the GO_IPFS_PATH environment variable was not found") - .1; + impl ForeignNode { + pub fn new() -> ForeignNode { + // GO_IPFS_PATH should point to the location of the go-ipfs binary + let go_ipfs_path = env::vars() + .find(|(key, _val)| key == "GO_IPFS_PATH") + .expect("the GO_IPFS_PATH environment variable was not found") + .1; - let mut tmp_dir = env::temp_dir(); - let mut rng = rand::thread_rng(); - tmp_dir.push(&format!("ipfs_test_{}", rng.gen::())); - let _ = fs::create_dir(&tmp_dir); + let mut tmp_dir = env::temp_dir(); + let mut rng = rand::thread_rng(); + tmp_dir.push(&format!("ipfs_test_{}", rng.gen::())); + let _ = fs::create_dir(&tmp_dir); - Command::new(&go_ipfs_path) - .env("IPFS_PATH", &tmp_dir) - .arg("init") - .arg("-p") - .arg("test") - .arg("--bits") - .arg("2048") - .stdout(Stdio::null()) - .status() - .unwrap(); + Command::new(&go_ipfs_path) + .env("IPFS_PATH", &tmp_dir) + .arg("init") + .arg("-p") + .arg("test") + .arg("--bits") + .arg("2048") + .stdout(Stdio::null()) + .status() + .unwrap(); - let daemon = Command::new(&go_ipfs_path) - .env("IPFS_PATH", &tmp_dir) - .arg("daemon") - .stdout(Stdio::null()) - .spawn() - .unwrap(); + let daemon = Command::new(&go_ipfs_path) + .env("IPFS_PATH", &tmp_dir) + .arg("daemon") + .stdout(Stdio::null()) + .spawn() + .unwrap(); - // give the go-ipfs daemon a little bit of time to start - thread::sleep(Duration::from_secs(1)); + // give the go-ipfs daemon a little bit of time to start + thread::sleep(Duration::from_secs(1)); - let go_id = Command::new(&go_ipfs_path) - .env("IPFS_PATH", &tmp_dir) - .arg("id") - .output() - .unwrap() - .stdout; + let go_id = Command::new(&go_ipfs_path) + .env("IPFS_PATH", &tmp_dir) + .arg("id") + .output() + .unwrap() + .stdout; - let go_id_stdout = String::from_utf8_lossy(&go_id); - let go_id: GoNodeId = serde_json::de::from_str(&go_id_stdout).unwrap(); + let go_id_stdout = String::from_utf8_lossy(&go_id); + let go_id: GoNodeId = serde_json::de::from_str(&go_id_stdout).unwrap(); - let id = go_id.id.parse().unwrap(); - let addrs = go_id - .addresses - .into_iter() - .map(|a| a.parse().unwrap()) - .collect(); + let id = go_id.id.parse().unwrap(); + let addrs = go_id + .addresses + .into_iter() + .map(|a| a.parse().unwrap()) + .collect(); - ForeignNode { - dir: tmp_dir, - daemon, - id, - addrs, + ForeignNode { + dir: tmp_dir, + daemon, + id, + addrs, + } + } + } + + impl Drop for ForeignNode { + fn drop(&mut self) { + let _ = self.daemon.kill(); + let _ = fs::remove_dir_all(&self.dir); } } } #[cfg(not(feature = "test_go_interop"))] -pub struct ForeignNode; - -#[cfg(feature = "test_go_interop")] -impl Drop for ForeignNode { - fn drop(&mut self) { - let _ = self.daemon.kill(); - let _ = fs::remove_dir_all(&self.dir); - } +pub mod none { + pub struct ForeignNode; } diff --git a/tests/kademlia.rs b/tests/kademlia.rs index c86886c9..a73b461c 100644 --- a/tests/kademlia.rs +++ b/tests/kademlia.rs @@ -7,7 +7,10 @@ use tokio::time::timeout; use std::{convert::TryInto, time::Duration}; mod common; -use common::interop::ForeignNode; +#[cfg(feature = "test_go_interop")] +use common::interop::go::ForeignNode; +#[cfg(not(feature = "test_go_interop"))] +use common::interop::none::ForeignNode; fn strip_peer_id(addr: Multiaddr) -> Multiaddr { let MultiaddrWithPeerId { multiaddr, .. } = addr.try_into().unwrap();