netsuke-build 0.1.0-beta2

A YAML-powered Ninja/Jinja hybrid build system.
//! Shared `#[cfg(test)]` helpers for the command module test suites.

use std::sync::Arc;

use anyhow::{Context, Result, anyhow};
use camino::Utf8PathBuf;
use cap_std::{ambient_authority, fs_utf8::Dir};
use tempfile::tempdir;

use super::{CommandConfig, CommandConfigInit};
use crate::stdlib::{DEFAULT_COMMAND_MAX_OUTPUT_BYTES, DEFAULT_COMMAND_MAX_STREAM_BYTES};

/// Build a [`CommandConfig`] rooted at a fresh temporary workspace using the
/// default output and stream byte budgets. The returned [`tempfile::TempDir`]
/// guard must be kept alive for the workspace directory to persist.
pub(super) fn test_command_config() -> Result<(tempfile::TempDir, CommandConfig)> {
    let temp = tempdir().context("create command temp workspace")?;
    let path = Utf8PathBuf::from_path_buf(temp.path().to_path_buf())
        .map_err(|path| anyhow!("temp workspace should be valid UTF-8: {path:?}"))?;
    let dir =
        Dir::open_ambient_dir(&path, ambient_authority()).context("open temp workspace dir")?;
    let config = CommandConfig::new(CommandConfigInit {
        max_capture_bytes: DEFAULT_COMMAND_MAX_OUTPUT_BYTES,
        max_stream_bytes: DEFAULT_COMMAND_MAX_STREAM_BYTES,
        workspace_root: Arc::new(dir),
        workspace_root_path: Some(Arc::new(path)),
        command_path_override: None,
    });
    Ok((temp, config))
}