if_changed/
testing.rs

1#[macro_export]
2macro_rules! git_test {
3    ($($name:literal: [$($path:literal => $content:expr),*])* $(staged: [$($spath:literal => $scontent:expr),*])? $(working: [$($wdpath:literal => $wdcontent:expr),*])?) => {{
4        let tempdir = ::tempfile::tempdir().unwrap();
5        let repo = ::git2::Repository::init(tempdir.path()).unwrap();
6        #[allow(unused_variables)]
7        let signature = ::git2::Signature::new("Example User", "test@example.com", &::git2::Time::new(0, 0)).unwrap();
8        #[allow(unused_variables, unused_mut)]
9        let mut index = repo.index().unwrap();
10        $({
11            $({
12                let path = tempdir.path().join($path);
13                ::std::fs::create_dir_all(path.parent().unwrap()).unwrap();
14                ::std::fs::write(path, $content).unwrap();
15            })*
16
17            index
18                .add_all(["."].iter(), git2::IndexAddOption::DEFAULT, None)
19                .unwrap();
20            index.write().unwrap();
21
22            let oid = index.write_tree().unwrap();
23            let tree = repo.find_tree(oid).unwrap();
24            let parents = if let Ok(Ok(parent_commit)) = repo.head().map(|head| head.peel_to_commit()) {
25                vec![parent_commit]
26            } else {
27                vec![]
28            };
29            repo.commit(
30                Some("HEAD"),
31                &signature,
32                &signature,
33                $name,
34                &tree,
35                &parents.iter().collect::<Vec<_>>(),
36            ).unwrap();
37        })*
38        $($({
39            let path = tempdir.path().join($spath);
40            ::std::fs::create_dir_all(path.parent().unwrap()).unwrap();
41            ::std::fs::write(path, $scontent).unwrap();
42
43            index
44                .add_all(["."].iter(), git2::IndexAddOption::DEFAULT, None)
45                .unwrap();
46            index.write().unwrap();
47        })*)?
48        $($({
49            let path = tempdir.path().join($wdpath);
50            ::std::fs::create_dir_all(path.parent().unwrap()).unwrap();
51            ::std::fs::write(path, $wdcontent).unwrap();
52        })*)?
53        (tempdir, repo)
54    }}
55}
56
57pub use git_test;