From 04ce907dbff6caa0c48a577ac8405d709ad94bf9 Mon Sep 17 00:00:00 2001 From: teor Date: Fri, 9 Oct 2020 23:07:55 +1000 Subject: [PATCH] Remove duplicate code in zebra_test::command --- zebra-test/src/command.rs | 35 +++++++++++++++++++++++++++++++++-- zebrad/tests/acceptance.rs | 26 ++++++++++++++------------ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/zebra-test/src/command.rs b/zebra-test/src/command.rs index ced8f81f..fa9b6e1a 100644 --- a/zebra-test/src/command.rs +++ b/zebra-test/src/command.rs @@ -2,16 +2,18 @@ use color_eyre::{ eyre::{eyre, Context, Report, Result}, Help, SectionExt, }; +use tempdir::TempDir; use tracing::instrument; use std::convert::Infallible as NoDir; use std::fmt::Write as _; #[cfg(unix)] use std::os::unix::process::ExitStatusExt; -use std::path::Path; use std::{ + borrow::Borrow, io::{BufRead, BufReader, Lines, Read}, - process::{Child, ChildStdout, Command, ExitStatus, Output}, + path::Path, + process::{Child, ChildStdout, Command, ExitStatus, Output, Stdio}, time::{Duration, Instant}, }; @@ -89,6 +91,35 @@ impl CommandExt for Command { } } +/// Extension trait for methods on `tempdir::TempDir` for using it as a test +/// directory with an arbitrary command. +pub trait TestDirExt +where + Self: Borrow + Sized, +{ + /// Spawn `cmd` with `args` as a child process in this test directory, + /// potentially taking ownership of the tempdir for the duration of the + /// child process. + fn spawn_child_with_command(self, cmd: &str, args: &[&str]) -> Result>; +} + +impl TestDirExt for T +where + Self: Borrow + Sized, +{ + fn spawn_child_with_command(self, cmd: &str, args: &[&str]) -> Result> { + let tempdir = self.borrow(); + let mut cmd = test_cmd(cmd, tempdir.path())?; + + Ok(cmd + .args(args) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn2(self) + .unwrap()) + } +} + #[derive(Debug)] pub struct TestStatus { pub cmd: String, diff --git a/zebrad/tests/acceptance.rs b/zebrad/tests/acceptance.rs index 770c6293..19291365 100644 --- a/zebrad/tests/acceptance.rs +++ b/zebrad/tests/acceptance.rs @@ -21,7 +21,7 @@ use tempdir::TempDir; use std::{borrow::Borrow, fs, io::Write, time::Duration}; use zebra_chain::parameters::Network::{self, *}; -use zebra_test::prelude::*; +use zebra_test::{command::TestDirExt, prelude::*}; use zebrad::config::ZebradConfig; fn default_test_config() -> Result { @@ -61,24 +61,26 @@ where impl ZebradTestDirExt for T where - Self: Borrow + Sized, + Self: TestDirExt + Borrow + Sized, { fn spawn_child(self, args: &[&str]) -> Result> { let tempdir = self.borrow(); - let mut cmd = test_cmd(env!("CARGO_BIN_EXE_zebrad"), tempdir.path())?; - let default_config_path = tempdir.path().join("zebrad.toml"); if default_config_path.exists() { - cmd.arg("-c").arg(default_config_path); + let mut extra_args: Vec<_> = Vec::new(); + extra_args.push("-c"); + extra_args.push( + default_config_path + .as_path() + .to_str() + .expect("Path is valid Unicode"), + ); + extra_args.extend_from_slice(args); + self.spawn_child_with_command(env!("CARGO_BIN_EXE_zebrad"), &extra_args) + } else { + self.spawn_child_with_command(env!("CARGO_BIN_EXE_zebrad"), args) } - - Ok(cmd - .args(args) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn2(self) - .unwrap()) } fn with_config(self, mut config: ZebradConfig) -> Result {