Remove duplicate code in zebra_test::command
This commit is contained in:
parent
32bbc19c6b
commit
04ce907dbf
|
|
@ -2,16 +2,18 @@ use color_eyre::{
|
||||||
eyre::{eyre, Context, Report, Result},
|
eyre::{eyre, Context, Report, Result},
|
||||||
Help, SectionExt,
|
Help, SectionExt,
|
||||||
};
|
};
|
||||||
|
use tempdir::TempDir;
|
||||||
use tracing::instrument;
|
use tracing::instrument;
|
||||||
|
|
||||||
use std::convert::Infallible as NoDir;
|
use std::convert::Infallible as NoDir;
|
||||||
use std::fmt::Write as _;
|
use std::fmt::Write as _;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::os::unix::process::ExitStatusExt;
|
use std::os::unix::process::ExitStatusExt;
|
||||||
use std::path::Path;
|
|
||||||
use std::{
|
use std::{
|
||||||
|
borrow::Borrow,
|
||||||
io::{BufRead, BufReader, Lines, Read},
|
io::{BufRead, BufReader, Lines, Read},
|
||||||
process::{Child, ChildStdout, Command, ExitStatus, Output},
|
path::Path,
|
||||||
|
process::{Child, ChildStdout, Command, ExitStatus, Output, Stdio},
|
||||||
time::{Duration, Instant},
|
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<TempDir> + 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<TestChild<Self>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> TestDirExt for T
|
||||||
|
where
|
||||||
|
Self: Borrow<TempDir> + Sized,
|
||||||
|
{
|
||||||
|
fn spawn_child_with_command(self, cmd: &str, args: &[&str]) -> Result<TestChild<Self>> {
|
||||||
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct TestStatus {
|
pub struct TestStatus {
|
||||||
pub cmd: String,
|
pub cmd: String,
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ use tempdir::TempDir;
|
||||||
use std::{borrow::Borrow, fs, io::Write, time::Duration};
|
use std::{borrow::Borrow, fs, io::Write, time::Duration};
|
||||||
|
|
||||||
use zebra_chain::parameters::Network::{self, *};
|
use zebra_chain::parameters::Network::{self, *};
|
||||||
use zebra_test::prelude::*;
|
use zebra_test::{command::TestDirExt, prelude::*};
|
||||||
use zebrad::config::ZebradConfig;
|
use zebrad::config::ZebradConfig;
|
||||||
|
|
||||||
fn default_test_config() -> Result<ZebradConfig> {
|
fn default_test_config() -> Result<ZebradConfig> {
|
||||||
|
|
@ -61,24 +61,26 @@ where
|
||||||
|
|
||||||
impl<T> ZebradTestDirExt for T
|
impl<T> ZebradTestDirExt for T
|
||||||
where
|
where
|
||||||
Self: Borrow<TempDir> + Sized,
|
Self: TestDirExt + Borrow<TempDir> + Sized,
|
||||||
{
|
{
|
||||||
fn spawn_child(self, args: &[&str]) -> Result<TestChild<Self>> {
|
fn spawn_child(self, args: &[&str]) -> Result<TestChild<Self>> {
|
||||||
let tempdir = self.borrow();
|
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");
|
let default_config_path = tempdir.path().join("zebrad.toml");
|
||||||
|
|
||||||
if default_config_path.exists() {
|
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<Self> {
|
fn with_config(self, mut config: ZebradConfig) -> Result<Self> {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue