-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: improve commit and submodule testing
Two of the tests in radicle-git-ext and radicle-surf relied on using the Git repository itself. This can end up being an issue when sandboxing is involved. This improves the situation by being able to generate commit data to add to a temporary repository. It also adds a helper for creating a local, temporary submodule for testing the submodule interactions in the radicle-surf API. Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com> X-Clacks-Overhead: GNU Terry Pratchett
- Loading branch information
Showing
15 changed files
with
461 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::convert::Infallible; | ||
|
||
use proptest::strategy::Strategy; | ||
use radicle_git_ext::commit::{self, CommitData}; | ||
|
||
mod author; | ||
mod headers; | ||
mod trailers; | ||
|
||
pub use author::author; | ||
pub use headers::headers; | ||
pub use trailers::{trailer, trailers}; | ||
|
||
use super::alphanumeric; | ||
|
||
pub fn commit() -> impl Strategy<Value = CommitData<TreeData, Infallible>> { | ||
( | ||
TreeData::gen(), | ||
author(), | ||
author(), | ||
headers(), | ||
alphanumeric(), | ||
trailers(3), | ||
) | ||
.prop_map(|(tree, author, committer, headers, message, trailers)| { | ||
CommitData::new(tree, vec![], author, committer, headers, message, trailers) | ||
}) | ||
} | ||
|
||
pub fn write_commits( | ||
repo: &git2::Repository, | ||
linear: Vec<CommitData<TreeData, Infallible>>, | ||
) -> Result<Vec<git2::Oid>, commit::error::Write> { | ||
let mut parent = None; | ||
let mut commits = Vec::new(); | ||
for commit in linear { | ||
let commit = commit.map_tree(|tree| tree.write(repo))?; | ||
let commit = match parent { | ||
Some(parent) => commit | ||
.map_parents::<git2::Oid, Infallible, _>(|_| Ok(parent)) | ||
.unwrap(), | ||
None => commit | ||
.map_parents::<git2::Oid, Infallible, _>(|_| unreachable!("no parents")) | ||
.unwrap(), | ||
}; | ||
let oid = commit.write(repo)?; | ||
commits.push(oid); | ||
parent = Some(oid); | ||
} | ||
Ok(commits) | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum TreeData { | ||
Blob { name: String, data: String }, | ||
Tree { name: String, inner: Vec<TreeData> }, | ||
} | ||
|
||
impl TreeData { | ||
fn gen() -> impl Strategy<Value = Self> { | ||
let leaf = | ||
(alphanumeric(), alphanumeric()).prop_map(|(name, data)| Self::Blob { name, data }); | ||
leaf.prop_recursive(8, 16, 5, |inner| { | ||
(proptest::collection::vec(inner, 1..5), alphanumeric()) | ||
.prop_map(|(inner, name)| Self::Tree { name, inner }) | ||
}) | ||
} | ||
|
||
fn write(&self, repo: &git2::Repository) -> Result<git2::Oid, git2::Error> { | ||
let mut builder = repo.treebuilder(None)?; | ||
self.write_(repo, &mut builder)?; | ||
builder.write() | ||
} | ||
|
||
fn write_( | ||
&self, | ||
repo: &git2::Repository, | ||
builder: &mut git2::TreeBuilder, | ||
) -> Result<git2::Oid, git2::Error> { | ||
match self { | ||
Self::Blob { name, data } => { | ||
let oid = repo.blob(data.as_bytes())?; | ||
builder.insert(name, oid, git2::FileMode::Blob.into())?; | ||
} | ||
Self::Tree { name, inner } => { | ||
for data in inner { | ||
let oid = data.write_(repo, builder)?; | ||
builder.insert(name, oid, git2::FileMode::Tree.into())?; | ||
} | ||
} | ||
} | ||
builder.write() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use proptest::strategy::{Just, Strategy}; | ||
use radicle_git_ext::author::{Author, Time}; | ||
|
||
use crate::gen; | ||
|
||
pub fn author() -> impl Strategy<Value = Author> { | ||
gen::alphanumeric().prop_flat_map(move |name| { | ||
(Just(name), gen::alphanumeric()).prop_flat_map(|(name, domain)| { | ||
(Just(name), Just(domain), (0..1000i64)).prop_map(move |(name, domain, time)| { | ||
let email = format!("{name}@{domain}"); | ||
Author { | ||
name, | ||
email, | ||
time: Time::new(time, 0), | ||
} | ||
}) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.