use crate::Settings;
use crate::traits::CommandBuilder;
use std::ffi::{OsStr, OsString};
use std::path::PathBuf;
#[derive(Clone, Debug, Default)]
pub struct PgControlDataBuilder {
program_dir: Option<PathBuf>,
envs: Vec<(OsString, OsString)>,
pgdata: Option<PathBuf>,
version: bool,
help: bool,
}
impl PgControlDataBuilder {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn from(settings: &dyn Settings) -> Self {
Self::new().program_dir(settings.get_binary_dir())
}
#[must_use]
pub fn program_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.program_dir = Some(path.into());
self
}
#[must_use]
pub fn pgdata<P: Into<PathBuf>>(mut self, pgdata: P) -> Self {
self.pgdata = Some(pgdata.into());
self
}
#[must_use]
pub fn version(mut self) -> Self {
self.version = true;
self
}
#[must_use]
pub fn help(mut self) -> Self {
self.help = true;
self
}
}
impl CommandBuilder for PgControlDataBuilder {
fn get_program(&self) -> &'static OsStr {
"pg_controldata".as_ref()
}
fn get_program_dir(&self) -> &Option<PathBuf> {
&self.program_dir
}
fn get_args(&self) -> Vec<OsString> {
let mut args: Vec<OsString> = Vec::new();
if self.version {
args.push("--version".into());
}
if self.help {
args.push("--help".into());
}
if let Some(pgdata) = &self.pgdata {
args.push("--pgdata".into());
args.push(pgdata.into());
}
args
}
fn get_envs(&self) -> Vec<(OsString, OsString)> {
self.envs.clone()
}
fn env<S: AsRef<OsStr>>(mut self, key: S, value: S) -> Self {
self.envs
.push((key.as_ref().to_os_string(), value.as_ref().to_os_string()));
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::CommandExecutor;
use crate::TestSettings;
use crate::test_utils::{TestResult, binary_dir};
use crate::traits::CommandToString;
use test_log::test;
#[test]
fn test_builder_new() {
let command = PgControlDataBuilder::new().program_dir(".").build();
assert_eq!(
PathBuf::from(".").join("pg_controldata"),
PathBuf::from(command.to_command_string().replace('"', ""))
);
}
#[test]
fn test_builder_from() {
let command = PgControlDataBuilder::from(&TestSettings).build();
#[cfg(not(target_os = "windows"))]
let command_prefix = r#""./pg_controldata""#;
#[cfg(target_os = "windows")]
let command_prefix = r#"".\\pg_controldata""#;
assert_eq!(format!("{command_prefix}"), command.to_command_string());
}
#[test]
fn test_builder() {
let command = PgControlDataBuilder::new()
.env("PGDATABASE", "database")
.pgdata("pgdata")
.version()
.help()
.build();
#[cfg(not(target_os = "windows"))]
let command_prefix = r#"PGDATABASE="database" "#;
#[cfg(target_os = "windows")]
let command_prefix = String::new();
assert_eq!(
format!(r#"{command_prefix}"pg_controldata" "--version" "--help" "--pgdata" "pgdata""#),
command.to_command_string()
);
}
#[test]
fn test_informational_options_come_first_on_configured_builders() {
let version = PgControlDataBuilder::from(&TestSettings)
.version()
.get_args();
assert_eq!(version.first(), Some(&OsString::from("--version")));
let help = PgControlDataBuilder::from(&TestSettings).help().get_args();
assert_eq!(help.first(), Some(&OsString::from("--help")));
}
#[test]
fn test_cli_help_and_version() -> TestResult {
let Some(bin) = binary_dir() else {
return Ok(());
};
PgControlDataBuilder::new()
.program_dir(&bin)
.version()
.build()
.execute()?;
PgControlDataBuilder::new()
.program_dir(&bin)
.help()
.build()
.execute()?;
Ok(())
}
}