aboutsummaryrefslogtreecommitdiffstats
path: root/tests/git.rs
blob: 7a28076b8e96318dd77795c10bf49e1557642ebf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * This file is part of agent686
 * Copyright (C) 2026 Salmonik <salmon@2137697.xyz>
 * SPDX-License-Identifier: GPL-3.0-or-later
 * https://git.2137697.xyz/agent686
 */

use std::fs;
use std::path::PathBuf;
use std::process::Command;

use agent686::git;

fn temp_dir(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("agent686-git-{tag}-{}", std::process::id()));
    let _ = fs::remove_dir_all(&dir);
    fs::create_dir_all(&dir).unwrap();
    dir
}

fn repo(tag: &str) -> PathBuf {
    let dir = temp_dir(tag);
    for args in [
        &["init", "-q"][..],
        &["config", "user.email", "agent686@test"][..],
        &["config", "user.name", "agent686"][..],
    ] {
        assert!(Command::new("git")
            .arg("-C")
            .arg(&dir)
            .args(args)
            .status()
            .unwrap()
            .success());
    }
    dir
}

#[test]
fn checkpoint_then_undo_restores_the_tree() {
    let dir = repo("roundtrip");
    fs::write(dir.join("a.txt"), "original\n").unwrap();

    let sha = git::checkpoint(&dir).expect("in a repo");
    assert!(!sha.is_empty());

    // The turn mutates the tree.
    fs::write(dir.join("a.txt"), "changed\n").unwrap();
    fs::write(dir.join("b.txt"), "untracked\n").unwrap();

    git::undo(&dir, &sha);

    assert_eq!(fs::read_to_string(dir.join("a.txt")).unwrap(), "original\n");
    assert!(
        !dir.join("b.txt").exists(),
        "untracked files must be removed"
    );
}

#[test]
fn checkpoint_is_none_outside_a_repo() {
    let dir = temp_dir("norepo");
    assert!(git::checkpoint(&dir).is_none());
}