postgresql_commands 0.22.0

PostgreSQL commands for interacting with a PostgreSQL server.
Documentation
use crate::Settings;
use crate::traits::CommandBuilder;
use std::convert::AsRef;
use std::ffi::{OsStr, OsString};
use std::path::PathBuf;

/// `pg_test_fsync` command to determine fastest `wal_sync_method` for `PostgreSQL`
#[derive(Clone, Debug, Default)]
pub struct PgTestFsyncBuilder {
    program_dir: Option<PathBuf>,
    envs: Vec<(OsString, OsString)>,
    help: bool,
    version: bool,
    filename: Option<OsString>,
    secs_per_test: Option<usize>,
}

impl PgTestFsyncBuilder {
    /// Create a new [`PgTestFsyncBuilder`]
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a new [`PgTestFsyncBuilder`] from [Settings]
    pub fn from(settings: &dyn Settings) -> Self {
        Self::new().program_dir(settings.get_binary_dir())
    }

    /// Location of the program binary
    #[must_use]
    pub fn program_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.program_dir = Some(path.into());
        self
    }

    /// Set the filename
    #[must_use]
    pub fn filename<S: AsRef<OsStr>>(mut self, filename: S) -> Self {
        self.filename = Some(filename.as_ref().to_os_string());
        self
    }

    /// Set the seconds per test
    #[must_use]
    pub fn secs_per_test(mut self, secs: usize) -> Self {
        self.secs_per_test = Some(secs);
        self
    }

    /// Show help, then exit.
    #[must_use]
    pub fn help(mut self) -> Self {
        self.help = true;
        self
    }

    /// Show version information, then exit.
    #[must_use]
    pub fn version(mut self) -> Self {
        self.version = true;
        self
    }
}

impl CommandBuilder for PgTestFsyncBuilder {
    /// Get the program name
    fn get_program(&self) -> &'static OsStr {
        "pg_test_fsync".as_ref()
    }

    /// Location of the program binary
    fn get_program_dir(&self) -> &Option<PathBuf> {
        &self.program_dir
    }

    /// Get the arguments for the command
    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(filename) = &self.filename {
            args.push("-f".into());
            args.push(filename.into());
        }

        if let Some(secs) = &self.secs_per_test {
            args.push("-s".into());
            args.push(secs.to_string().into());
        }

        args
    }

    /// Get the environment variables for the command
    fn get_envs(&self) -> Vec<(OsString, OsString)> {
        self.envs.clone()
    }

    /// Set an environment variable for the command
    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, assert_args, binary_dir};
    use crate::traits::CommandToString;
    use test_log::test;

    #[test]
    fn test_builder_new() {
        let command = PgTestFsyncBuilder::new().program_dir(".").build();
        assert_eq!(
            PathBuf::from(".").join("pg_test_fsync"),
            PathBuf::from(command.to_command_string().replace('"', ""))
        );
    }

    #[test]
    fn test_builder_from() {
        let command = PgTestFsyncBuilder::from(&TestSettings).build();
        #[cfg(not(target_os = "windows"))]
        let command_prefix = r#""./pg_test_fsync""#;
        #[cfg(target_os = "windows")]
        let command_prefix = r#"".\\pg_test_fsync""#;

        assert_eq!(format!("{command_prefix}"), command.to_command_string());
    }

    #[test]
    fn test_builder() {
        let command = PgTestFsyncBuilder::new()
            .env("PGDATABASE", "database")
            .filename("filename")
            .secs_per_test(10)
            .help()
            .version()
            .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_test_fsync" "--version" "--help" "-f" "filename" "-s" "10""#
            ),
            command.to_command_string()
        );
    }

    #[test]
    fn test_options() {
        assert_args(PgTestFsyncBuilder::new().help(), &["--help"]);
        assert_args(PgTestFsyncBuilder::new().version(), &["--version"]);
    }

    #[test]
    fn test_informational_options_come_first_on_configured_builders() {
        let version = PgTestFsyncBuilder::from(&TestSettings).version().get_args();
        assert_eq!(version.first(), Some(&OsString::from("--version")));
        let help = PgTestFsyncBuilder::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(());
        };
        PgTestFsyncBuilder::new()
            .program_dir(&bin)
            .version()
            .build()
            .execute()?;
        PgTestFsyncBuilder::new()
            .program_dir(&bin)
            .help()
            .build()
            .execute()?;
        Ok(())
    }
}