rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use pretty_assertions::assert_eq;

use super::*;
use crate::app::daemon_host::machine_test_support::{git, repository};

/// What `repos` read of `folder` through git alone, as before it read files.
async fn git_says(folder: &Path, environment: &ClientEnvironment) -> Option<Checkout> {
    let top = super::super::toplevel(folder, environment).await.unwrap()?;
    let repo = super::super::read_repo(folder, environment).await;
    Some(Checkout { top, repo })
}

/// What the file read should say of a folder.
#[derive(Clone, Copy, Debug)]
enum Expect {
    /// What git says.
    AsGit,
    /// That git must decide.
    AskGit,
}

// Covers: reading a folder's `.git` files answers what git running there
// answers (top, `HEAD`, `origin`), for a checkout's subfolder, a linked
// worktree, a detached one, an unborn branch with no remote, a config git
// reads loosely, a submodule, a bare repository's worktree, a plain folder
// and a missing one; and defers to git for a bare repository, a git dir, a
// config with an include, a `HEAD` naming a ref git refuses, a `.git` file
// git refuses for its padding, a branch symlinked to another, which git
// follows, an empty branch file, which git fails on, a config line git
// refuses, and an environment that moves git's search.
// Owner: daemon host calls.
// Ownership (`safe.directory`) and mount crossings need another user or a
// mount, which a test cannot make.
#[cfg(unix)]
#[tokio::test]
async fn the_files_answer_as_git_or_defer_to_it() {
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    let home = root.join("home");
    std::fs::create_dir(&home).unwrap();
    let environment: ClientEnvironment = [
        ("PATH", std::env::var("PATH").unwrap()),
        ("HOME", home.to_str().unwrap().to_owned()),
        ("GIT_CONFIG_NOSYSTEM", "1".to_owned()),
    ]
    .into_iter()
    .map(|(name, value)| (name.to_owned(), value))
    .collect();

    let main = root.join("main");
    repository(&main, &[("sub/a.txt", "a")]);
    git(
        &main,
        &["remote", "add", "origin", "git@example.com:me/main.git"],
    );
    git(
        &main,
        &["worktree", "add", "-q", "-b", "feature", "../linked"],
    );
    git(&main, &["worktree", "add", "-q", "--detach", "../detached"]);
    let fresh = root.join("fresh");
    std::fs::create_dir(&fresh).unwrap();
    git(&fresh, &["init", "-q"]);
    git(&fresh, &["symbolic-ref", "HEAD", "refs/heads/trunk"]);
    let loose = root.join("loose");
    repository(&loose, &[("a", "a")]);
    append(
        &loose.join(".git/config"),
        "[Remote \"origin\"] ; a comment\n\tURL=ssh://example.com/a  b # a comment\n\
         [CORE]\n\trepositoryFormatVersion = 1\n",
    );
    let included = root.join("included");
    repository(&included, &[("a", "a")]);
    append(&included.join(".git/config"), "[include]\n\tpath = more\n");
    let outer = root.join("outer");
    repository(&outer, &[("a", "a")]);
    git(
        &outer,
        &[
            "-c",
            "protocol.file.allow=always",
            "submodule",
            "add",
            "-q",
            main.to_str().unwrap(),
            "inner",
        ],
    );
    git(root, &["clone", "-q", "--bare", "main", "bare.git"]);
    git(
        &root.join("bare.git"),
        &["worktree", "add", "-q", "../bare-tree", "main"],
    );
    git(&main, &["worktree", "add", "-q", "-b", "pad", "../padded"]);
    let padded = std::fs::read_to_string(root.join("padded/.git")).unwrap();
    std::fs::write(
        root.join("padded/.git"),
        format!("{}  \n", padded.trim_end()),
    )
    .unwrap();
    let linked_ref = root.join("linked-ref");
    repository(&linked_ref, &[("a", "a")]);
    std::os::unix::fs::symlink("refs/heads/main", linked_ref.join(".git/refs/heads/link")).unwrap();
    std::fs::write(linked_ref.join(".git/HEAD"), "ref: refs/heads/link\n").unwrap();
    let empty_ref = root.join("empty-ref");
    repository(&empty_ref, &[("a", "a")]);
    std::fs::write(empty_ref.join(".git/refs/heads/main"), "").unwrap();
    let bad_line = root.join("bad-line");
    repository(&bad_line, &[("a", "a")]);
    append(&bad_line.join(".git/config"), "[core]\n\tfoo # a comment\n");
    let bad_head = root.join("bad-head");
    repository(&bad_head, &[("a", "a")]);
    std::fs::write(bad_head.join(".git/HEAD"), "ref: refs/heads/a..b\n").unwrap();
    std::fs::create_dir(root.join("plain")).unwrap();
    let cases = [
        ("main/sub", Expect::AsGit),
        ("linked", Expect::AsGit),
        ("detached", Expect::AsGit),
        ("fresh", Expect::AsGit),
        ("loose", Expect::AsGit),
        ("outer/inner", Expect::AsGit),
        ("bare-tree", Expect::AsGit),
        ("plain", Expect::AsGit),
        ("missing", Expect::AsGit),
        ("bare.git", Expect::AskGit),
        ("main/.git/refs", Expect::AskGit),
        ("included", Expect::AskGit),
        ("bad-head", Expect::AskGit),
        ("padded", Expect::AskGit),
        ("linked-ref", Expect::AskGit),
        ("empty-ref", Expect::AskGit),
        ("bad-line", Expect::AskGit),
    ];

    let files = Files::for_environment(&environment).unwrap();
    let mut read_here = Vec::new();
    let mut expected = Vec::new();
    for (folder, expect) in cases {
        let path = root.join(folder);
        read_here.push((folder, files.read(&path)));
        let answer = match expect {
            Expect::AsGit => Ok(git_says(&path, &environment).await),
            Expect::AskGit => Err(AskGit),
        };
        expected.push((folder, answer));
    }
    let mut ceiling = environment.clone().into_iter().collect::<Vec<_>>();
    ceiling.push((
        "GIT_CEILING_DIRECTORIES".to_owned(),
        root.display().to_string(),
    ));
    let ceiling = ceiling.into_iter().collect();

    assert_eq!(read_here, expected);
    assert!(Files::for_environment(&ceiling).is_none());
}

fn append(path: &Path, text: &str) {
    use std::io::Write as _;
    let mut file = std::fs::OpenOptions::new().append(true).open(path).unwrap();
    file.write_all(text.as_bytes()).unwrap();
}