use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use super::SubCommand;
use crate::global::GlobalOpts;
use crate::spawn::ParameterizedSpawn;
#[cfg_attr(
feature = "lt2015_1",
doc = "`p4 [g-opts] dbverify [ -t db.tablename ] [-U]`"
)]
#[cfg_attr(
all(feature = "lt2016_1", not(feature = "lt2015_1")),
doc = "`p4 [g-opts] dbverify [-t db.tablename] [-U]`"
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "`p4 [g-opts] dbverify [-t db.tablename] [-U][-v]`"
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "",
doc = "Use [`Self::verbose`] to request verbose information on the verification."
)]
#[derive(Debug, Clone, Default)]
pub struct DBVerify {
bin: PathBuf,
global_opts: GlobalOpts,
table: Option<String>,
less_detailed: bool,
#[cfg(not(feature = "lt2016_1"))]
verbose: bool,
}
impl DBVerify {
pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
Self {
bin: bin.into(),
global_opts,
table: None,
less_detailed: false,
#[cfg(not(feature = "lt2016_1"))]
verbose: false,
}
}
pub fn get_bin(&self) -> &Path {
&self.bin
}
pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
self.bin = bin.into();
self
}
pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
self.set_bin(bin);
self
}
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn get_global_opts(&self) -> &GlobalOpts {
&self.global_opts
}
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
self.global_opts = v;
self
}
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn global_opts(mut self, v: GlobalOpts) -> Self {
self.global_opts = v;
self
}
#[cfg_attr(feature = "lt2015_1", doc = "-t tablename")]
#[cfg_attr(not(feature = "lt2015_1"), doc = "-t db.tablename")]
pub fn get_table(&self) -> Option<&str> {
self.table.as_deref()
}
#[cfg_attr(feature = "lt2015_1", doc = "-t tablename")]
#[cfg_attr(not(feature = "lt2015_1"), doc = "-t db.tablename")]
pub fn set_table(&mut self, v: impl Into<String>) -> &mut Self {
self.table = Some(v.into());
self
}
#[cfg_attr(feature = "lt2015_1", doc = "-t tablename")]
#[cfg_attr(not(feature = "lt2015_1"), doc = "-t db.tablename")]
pub fn table(mut self, v: impl Into<String>) -> Self {
self.set_table(v);
self
}
pub fn get_less_detailed(&self) -> bool {
self.less_detailed
}
pub fn set_less_detailed(&mut self, v: bool) -> &mut Self {
self.less_detailed = v;
self
}
pub fn less_detailed(mut self, v: bool) -> Self {
self.set_less_detailed(v);
self
}
#[cfg(not(feature = "lt2016_1"))]
pub fn get_verbose(&self) -> bool {
self.verbose
}
#[cfg(not(feature = "lt2016_1"))]
pub fn set_verbose(&mut self, v: bool) -> &mut Self {
self.verbose = v;
self
}
#[cfg(not(feature = "lt2016_1"))]
pub fn verbose(mut self, v: bool) -> Self {
self.set_verbose(v);
self
}
}
impl SubCommand for DBVerify {
fn name(&self) -> &str {
"dbverify"
}
fn inject_local_args(&self, command: &mut Command) {
if let Some(table) = &self.table {
command.arg("-t").arg(table);
}
if self.less_detailed {
command.arg("-U");
}
#[cfg(not(feature = "lt2016_1"))]
if self.verbose {
command.arg("-v");
}
}
fn global_opts(&self) -> Option<&GlobalOpts> {
Some(&self.global_opts)
}
}
impl ParameterizedSpawn<()> for DBVerify {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
self.setup_command(&self.bin)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cmd::args_of;
#[test]
fn bin_accessors_update_executable_path() {
let mut command = DBVerify::new("p4", GlobalOpts::new());
assert_eq!(command.get_bin(), Path::new("p4"));
command.set_bin("/usr/local/bin/p4");
assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));
let command = command.bin("p4.exe");
assert_eq!(command.get_bin(), Path::new("p4.exe"));
}
#[test]
fn without_options() {
let dbverify = DBVerify::new("p4", GlobalOpts::new());
assert_eq!(args_of(&dbverify.setup_command("p4")), ["dbverify"]);
}
#[test]
fn with_table() {
let dbverify = DBVerify::new("p4", GlobalOpts::new()).table("db.have");
assert_eq!(dbverify.get_table(), Some("db.have"));
assert_eq!(
args_of(&dbverify.setup_command("p4")),
["dbverify", "-t", "db.have"]
);
}
#[test]
fn less_detailed_flag() {
let dbverify = DBVerify::new("p4", GlobalOpts::new()).less_detailed(true);
assert!(dbverify.get_less_detailed());
assert_eq!(args_of(&dbverify.setup_command("p4")), ["dbverify", "-U"]);
}
#[test]
fn table_and_less_detailed_in_syntax_order() {
let mut dbverify = DBVerify::new("p4", GlobalOpts::new()).less_detailed(true);
dbverify.set_table("db.have");
assert_eq!(
args_of(&dbverify.setup_command("p4")),
["dbverify", "-t", "db.have", "-U"]
);
}
#[cfg(not(feature = "lt2016_1"))]
#[test]
fn verbose_flag() {
let dbverify = DBVerify::new("p4", GlobalOpts::new()).verbose(true);
assert!(dbverify.get_verbose());
assert_eq!(args_of(&dbverify.setup_command("p4")), ["dbverify", "-v"]);
}
#[cfg(not(feature = "lt2016_1"))]
#[test]
fn all_options_in_syntax_order() {
let dbverify = DBVerify::new("p4", GlobalOpts::new())
.table("db.have")
.less_detailed(true)
.verbose(true);
assert_eq!(
args_of(&dbverify.setup_command("p4")),
["dbverify", "-t", "db.have", "-U", "-v"]
);
}
#[test]
fn global_opts_are_injected_once() {
let dbverify = DBVerify::new(
"p4",
GlobalOpts::new().port("localhost:1666").quiet_mode(true),
)
.table("db.have");
assert_eq!(
args_of(&dbverify.setup_command("p4")),
["-p", "localhost:1666", "-q", "dbverify", "-t", "db.have"]
);
}
}