summaryrefslogtreecommitdiff
path: root/tests/cli_test_utils/git.rs
blob: 961592ea4318a7e62b221c5a718874e3415d41f2 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::context::TestContext;
use anyhow::Result;
use assert_cmd::{assert::Assert, Command};
use std::{
    fs,
    path::{Path, PathBuf},
};

pub fn create_clone(c: &TestContext, repo_dir: &Path, relative_name: &str) -> PathBuf {
    Command::new("git")
        .arg("clone")
        .arg(repo_dir)
        .arg(relative_name)
        .current_dir(&c.workdir)
        .timeout(std::time::Duration::from_secs(3))
        .assert()
        .success();

    let repo_dir = c.workdir.as_ref().join(relative_name);

    // standardising the main branch of our clone simplifies things.
    Command::new("git")
        .args(["checkout", "-b", "main"])
        .current_dir(&repo_dir)
        .assert()
        .success();

    repo_dir
}

pub fn create_commit(repo_dir: &Path) -> Result<String> {
    Command::new("git")
        .args(["config", "user.email", "shukkie@example.com"])
        .current_dir(&repo_dir)
        .assert()
        .success();
    Command::new("git")
        .args(["config", "user.name", "Shackle Test User"])
        .current_dir(&repo_dir)
        .assert()
        .success();

    let test_file_name = repo_dir.join("some_file");
    fs::write(test_file_name, "Some content or something")?;
    Command::new("git")
        .args(["add", "--all"])
        .current_dir(&repo_dir)
        .timeout(std::time::Duration::from_secs(3))
        .assert()
        .success();
    Command::new("git")
        .args(["commit", "-m", "A commit message"])
        .current_dir(&repo_dir)
        .timeout(std::time::Duration::from_secs(3))
        .assert()
        .success();

    let commit_hash = String::from_utf8(
        Command::new("git")
            .args(["rev-parse", "HEAD"])
            .current_dir(&repo_dir)
            .timeout(std::time::Duration::from_secs(3))
            .output()?
            .stdout,
    )?;

    Ok(commit_hash.trim().to_owned())
}

pub fn push(local_repo_dir: &Path, refspec: &str) {
    Command::new("git")
        .args(["push", "origin", refspec])
        .current_dir(&local_repo_dir)
        .timeout(std::time::Duration::from_secs(3))
        .assert()
        .success();
}

pub fn verify_repo_exists(repo_dir: &Path) {
    Command::new("git")
        .arg("rev-list")
        .arg("--all")
        .current_dir(repo_dir)
        .assert()
        .success()
        .stdout("");
}

fn check_commit_exists(repo_dir: &Path, commit_hash: &str) -> Assert {
    Command::new("git")
        .arg("rev-list")
        .arg(commit_hash)
        .current_dir(repo_dir)
        .assert()
}

pub fn verify_commit_exists(repo_dir: &Path, commit_hash: &str) {
    check_commit_exists(repo_dir, commit_hash).success();
}

pub fn verify_commit_does_not_exist(repo_dir: &Path, commit_hash: &str) {
    check_commit_exists(repo_dir, commit_hash).failure();
}

pub fn verify_repo_does_not_exist(repo_dir: &Path) {
    assert!(!repo_dir.exists());
}

pub fn verify_current_branch(repo_dir: &Path, expected_ref: &str) {
    Command::new("git")
        .arg("symbolic-ref")
        .arg("HEAD")
        .current_dir(repo_dir)
        .assert()
        .success()
        .stdout(format!("{expected_ref}\n"));
}

pub fn verify_repo_config_value(repo_dir: &Path, config_key: &str, config_value: Option<&str>) {
    let assert = Command::new("git")
        .args(["config", "--local", config_key])
        .current_dir(repo_dir)
        .assert();
    match config_value {
        Some(value) => {
            assert.success().stdout(format!("{value}\n"));
        }
        None => {
            assert.failure().code(1);
        }
    }
}