summary refs log tree commit diff
path: root/tests/test.rs
blob: 9edacff701b3db1486047fe674848cdbdd58d3f8 (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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2021 Alyssa Ross <hi@alyssa.is>

use std::{
    env::var_os,
    fs::create_dir,
    io::{stderr, Write},
    process::Command,
};

use git2::{Commit, IntoCString, Oid, Reference, Signature, Time, Tree};
use tempdir::TempDir;

struct Repo<'a> {
    inner: git2::Repository,
    signature: Signature<'a>,
}

impl Repo<'_> {
    fn new(inner: git2::Repository) -> Self {
        Self {
            inner,
            signature: Signature::new("git-girf test suite", "git-girf@test", &Time::new(0, 0))
                .unwrap(),
        }
    }

    fn commit(&self, files: &[(impl AsRef<[u8]>, impl AsRef<[u8]>)]) -> Oid {
        let mut treebuilder = self.inner.treebuilder(None).unwrap();

        for (path, data) in files {
            let blob = self.inner.blob(data.as_ref()).unwrap();
            treebuilder.insert(path.as_ref(), blob, 0o100644).unwrap();
        }

        let tree_oid = treebuilder.write().unwrap();
        let tree = self.inner.find_tree(tree_oid).unwrap();
        let parent_oids = self
            .inner
            .head()
            .into_iter()
            .map(|head| head.target().unwrap());
        let parents: Vec<_> = parent_oids
            .map(|oid| self.inner.find_commit(oid).unwrap())
            .collect();
        let borrowed_parents: Vec<_> = parents.iter().collect();
        dbg!(self.inner
            .commit(
                Some("HEAD"),
                &self.signature,
                &self.signature,
                "A commit",
                &tree,
                &borrowed_parents,
            )
            .unwrap())
    }
}

#[test]
fn happy() {
    let dir = TempDir::new("git-girf-tests").unwrap();

    let path = var_os("PATH").unwrap_or_else(|| "/usr/bin:/bin".into());

    let repo = Repo::new(git2::Repository::init(&dir).expect("opening repo"));

    // We're going to use `tail -n 1` as our filter command, meaning
    // commits shouldn't be printed if they change the last line of
    // any file.

    let mut expected = Vec::new();

    repo.commit(&[("a", "a\na\n")]);

    // This creates a file, so shouldn't be printed.
    repo.commit(&[("a", "a\na\n"), ("b", "b\nb\n")]);

    // This changes the first lines only, so should be printed.
    expected.push(repo.commit(&[("a", "A\na\n"), ("b", "B\nb\n")]));

    // This changes the last line, so shouldn't be printed.
    repo.commit(&[("a", "A\nA\n"), ("b", "b\nB\n")]);

    // This has lots of files, but it should be obvious that it
    // doesn't have to be printed after the first file.  We include it
    // to test that short circuiting doesn't panic the diff thread.
    repo.commit(&(b'a'..b'z').map(|c| ([c], [c, b'\n'])).collect::<Vec<_>>());

    let output = Command::new(env!("CARGO_BIN_EXE_git-girf"))
        .args(&["tail", "-n", "1"])
        .current_dir(&dir)
        .env_clear()
        .env("PATH", path)
        .output()
        .expect("spawn");

    let _ = stderr().write_all(&output.stderr);

    let len = expected.len();
    let mut expected_bytes = Vec::with_capacity(41 * expected.len());
    for oid in expected {
        expected_bytes.extend_from_slice(oid.to_string().as_bytes());
        expected_bytes.push(b'\n');
    }

    assert!(output.stderr.is_empty());
    assert_eq!(
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&expected_bytes)
    );
    assert!(output.status.success());
}

#[test]
fn no_repo() {
    let output = Command::new(env!("CARGO_BIN_EXE_git-girf"))
        .arg("cat")
        .current_dir("/var/empty")
        .env_clear()
        .output()
        .expect("spawn");

    assert_eq!(output.status.code(), Some(128));
    assert_eq!(
        output.stderr,
        b"fatal: could not find repository from '.'\n"
    );
    assert!(output.stdout.is_empty());
}

#[test]
fn no_args() {
    let output = Command::new(env!("CARGO_BIN_EXE_git-girf"))
        .current_dir("/var/empty")
        .env_clear()
        .output()
        .expect("spawn");

    assert_eq!(output.status.code(), Some(128));
    assert_eq!(output.stderr, b"fatal: no command given\n");
    assert!(output.stdout.is_empty());
}

#[test]
fn flag() {
    let output = Command::new(env!("CARGO_BIN_EXE_git-girf"))
        .arg("-Z")
        .current_dir("/var/empty")
        .env_clear()
        .output()
        .expect("spawn");

    assert_eq!(output.status.code(), Some(128));
    assert_eq!(output.stderr, b"fatal: unrecognized argument: -Z\n");
    assert!(output.stdout.is_empty());
}

#[test]
fn filter_sigpipe() {
    todo!();
}

#[test]
fn sigpipe() {
    todo!();
}