summaryrefslogtreecommitdiff
path: root/tests/cli_test_utils/git.rs
blob: fe6e1bd81f863898f67433695e598339e41ae6c8 (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
use crate::context::TestContext;
use anyhow::Result;
use assert_cmd::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();
    c.workdir.as_ref().join(relative_name)
}

pub fn create_commit(repo_dir: &Path) -> Result<String> {
    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)
}

pub fn push(local_repo_dir: &Path, branch: &str) {
    Command::new("git")
        .args(["push", "origin", branch])
        .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("");
}

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);
        }
    }
}