fastmash 0.7.1

Fast command-line statistics on text data, compatible with GNU datamash
#![forbid(unsafe_code)]
//! Fresh-process fixture setup and isolated executable copies.
use std::{
    ffi::OsStr,
    path::{Path, PathBuf},
    process::Command,
};

#[allow(dead_code)] // Only the standalone-executable suites need a copy.
pub fn copy_executable(source: impl AsRef<Path>, destination: impl AsRef<Path>) {
    // Parallel forks must not inherit a writable descriptor for this executable.
    // The fresh copier opens it after exec and exits before this function returns.
    let output = bounded("--copy-executable", 15)
        .arg(source.as_ref())
        .arg(destination.as_ref())
        .env_clear()
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "copy {:?} to {:?}: {output:?}",
        source.as_ref(),
        destination.as_ref(),
    );
}

pub fn bounded(binary: impl AsRef<OsStr>, seconds: u32) -> Command {
    assert!((1..=60).contains(&seconds));
    let launcher = std::env::var_os("FASTMASH_TEST_PROCESS")
        .map(PathBuf::from)
        .unwrap_or_else(|| {
            Path::new(env!("CARGO_BIN_EXE_fastmash")).with_file_name("fastmash-test-process")
        });
    assert!(
        launcher.is_file(),
        "prepare fastmash-test-process before native command tests"
    );
    let mut command = Command::new(launcher);
    command.arg(seconds.to_string()).arg(binary);
    command
}

/// Preserve ignored SIGXFSZ across the fresh launcher and target execs.
/// Set the selected file-size limit after the caller's env_clear().
#[allow(dead_code)] // Other suites use only the ordinary alarm launcher.
pub fn ignoring_file_size_signal(binary: impl AsRef<OsStr>, seconds: u32) -> Command {
    let launcher = bounded(binary, seconds);
    let mut command = Command::new("/bin/sh");
    command
        .args(["-c", r#"trap '' XFSZ && exec "$@""#, "fastmash-file-size"])
        .arg(launcher.get_program())
        .args(launcher.get_args());
    command
}