diff options
Diffstat (limited to 'test_utils/src/git.rs')
| -rw-r--r-- | test_utils/src/git.rs | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/test_utils/src/git.rs b/test_utils/src/git.rs new file mode 100644 index 0000000..166693d --- /dev/null +++ b/test_utils/src/git.rs | |||
| @@ -0,0 +1,123 @@ | |||
| 1 | //create | ||
| 2 | |||
| 3 | // implement drop? | ||
| 4 | use std::{env::current_dir, fs, path::PathBuf}; | ||
| 5 | |||
| 6 | use anyhow::Result; | ||
| 7 | use git2::{Oid, RepositoryInitOptions, Signature, Time}; | ||
| 8 | |||
| 9 | pub struct GitTestRepo { | ||
| 10 | pub dir: PathBuf, | ||
| 11 | pub git_repo: git2::Repository, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl Default for GitTestRepo { | ||
| 15 | fn default() -> Self { | ||
| 16 | Self::new("main").unwrap() | ||
| 17 | } | ||
| 18 | } | ||
| 19 | impl GitTestRepo { | ||
| 20 | pub fn new(main_branch_name: &str) -> Result<Self> { | ||
| 21 | let path = current_dir()?.join(format!("tmpgit-{}", rand::random::<u64>())); | ||
| 22 | let git_repo = git2::Repository::init_opts( | ||
| 23 | &path, | ||
| 24 | RepositoryInitOptions::new() | ||
| 25 | .initial_head(main_branch_name) | ||
| 26 | .mkpath(true), | ||
| 27 | )?; | ||
| 28 | Ok(Self { | ||
| 29 | dir: path, | ||
| 30 | git_repo, | ||
| 31 | }) | ||
| 32 | } | ||
| 33 | |||
| 34 | pub fn initial_commit(&self) -> Result<Oid> { | ||
| 35 | let oid = self.git_repo.index()?.write_tree()?; | ||
| 36 | let tree = self.git_repo.find_tree(oid)?; | ||
| 37 | let commit_oid = self.git_repo.commit( | ||
| 38 | Some("HEAD"), | ||
| 39 | &joe_signature(), | ||
| 40 | &joe_signature(), | ||
| 41 | "Initial commit", | ||
| 42 | &tree, | ||
| 43 | &[], | ||
| 44 | )?; | ||
| 45 | Ok(commit_oid) | ||
| 46 | } | ||
| 47 | |||
| 48 | pub fn populate(&self) -> Result<Oid> { | ||
| 49 | self.initial_commit()?; | ||
| 50 | fs::write(self.dir.join("t1.md"), "some content")?; | ||
| 51 | self.stage_and_commit("add t1.md")?; | ||
| 52 | fs::write(self.dir.join("t2.md"), "some content1")?; | ||
| 53 | self.stage_and_commit("add t2.md") | ||
| 54 | } | ||
| 55 | |||
| 56 | pub fn stage_and_commit(&self, message: &str) -> Result<Oid> { | ||
| 57 | let prev_oid = self.git_repo.head().unwrap().peel_to_commit()?; | ||
| 58 | |||
| 59 | let mut index = self.git_repo.index()?; | ||
| 60 | index.add_all(["."], git2::IndexAddOption::DEFAULT, None)?; | ||
| 61 | index.write()?; | ||
| 62 | |||
| 63 | let oid = self.git_repo.commit( | ||
| 64 | Some("HEAD"), | ||
| 65 | &joe_signature(), | ||
| 66 | &joe_signature(), | ||
| 67 | message, | ||
| 68 | &self.git_repo.find_tree(index.write_tree()?)?, | ||
| 69 | &[&prev_oid], | ||
| 70 | )?; | ||
| 71 | |||
| 72 | Ok(oid) | ||
| 73 | } | ||
| 74 | |||
| 75 | pub fn create_branch(&self, branch_name: &str) -> Result<()> { | ||
| 76 | self.git_repo | ||
| 77 | .branch(branch_name, &self.git_repo.head()?.peel_to_commit()?, false)?; | ||
| 78 | Ok(()) | ||
| 79 | } | ||
| 80 | |||
| 81 | pub fn checkout(&self, ref_name: &str) -> Result<Oid> { | ||
| 82 | let (object, reference) = self.git_repo.revparse_ext(ref_name)?; | ||
| 83 | |||
| 84 | self.git_repo.checkout_tree(&object, None)?; | ||
| 85 | |||
| 86 | match reference { | ||
| 87 | // gref is an actual reference like branches or tags | ||
| 88 | Some(gref) => self.git_repo.set_head(gref.name().unwrap()), | ||
| 89 | // this is a commit, not a reference | ||
| 90 | None => self.git_repo.set_head_detached(object.id()), | ||
| 91 | }?; | ||
| 92 | let oid = self.git_repo.head()?.peel_to_commit()?.id(); | ||
| 93 | Ok(oid) | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | impl Drop for GitTestRepo { | ||
| 98 | fn drop(&mut self) { | ||
| 99 | let _ = fs::remove_dir_all(&self.dir); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | pub fn joe_signature() -> Signature<'static> { | ||
| 103 | Signature::new("Joe Bloggs", "joe.bloggs@pm.me", &Time::new(0, 0)).unwrap() | ||
| 104 | } | ||
| 105 | |||
| 106 | #[cfg(test)] | ||
| 107 | mod tests { | ||
| 108 | |||
| 109 | use super::*; | ||
| 110 | |||
| 111 | #[test] | ||
| 112 | fn methods_do_not_throw() -> Result<()> { | ||
| 113 | let repo = GitTestRepo::new("main")?; | ||
| 114 | |||
| 115 | repo.populate()?; | ||
| 116 | repo.create_branch("feature")?; | ||
| 117 | repo.checkout("feature")?; | ||
| 118 | fs::write(repo.dir.join("t3.md"), "some content")?; | ||
| 119 | repo.stage_and_commit("add t3.md")?; | ||
| 120 | |||
| 121 | Ok(()) | ||
| 122 | } | ||
| 123 | } | ||