"""Shared fixtures and path constants for `deploy/scripts/tests/`."""
import os
from pathlib import Path


ROOT = Path(__file__).resolve().parents[3]
SCRIPTS = ROOT / "deploy" / "scripts"
LIBEXEC = SCRIPTS / "libexec"
SBIN = SCRIPTS / "sbin"

# `deploy/` is also the parent of the scripts/ tree.  The sudoers example
# lives at `deploy/files/etc/sudoers.d/left4me` and is the canonical
# statement of which paths sudo grants to the `left4me` uid.
# `deploy/scripts/tests/test_sudoers_grants.py` reads it from there.
DEPLOY = ROOT / "deploy"


def fake_command(tmp_path, command_name):
    """Drop a no-op stub of `command_name` into `tmp_path`. Returns the
    marker file the stub writes its args to, so tests can assert that the
    helper rejected bad input before invoking the real command.
    """
    marker = tmp_path / f"{command_name}.args"
    command = tmp_path / command_name
    command.write_text(f"#!/bin/sh\nprintf '%s\\n' \"$*\" > '{marker}'\nexit 0\n")
    command.chmod(0o755)
    return marker


def env_with_fake_commands(tmp_path):
    """Build an environment that prepends `tmp_path` onto PATH so helpers
    find the fake commands first.
    """
    env = os.environ.copy()
    env["PATH"] = f"{tmp_path}{os.pathsep}{env.get('PATH', '')}"
    return env
