Skip to content

Commit

Permalink
git: add tests to git parsing function (note: squash)
Browse files Browse the repository at this point in the history
when the solution to the pruning problem is solved, this can be squashed
  • Loading branch information
bsdinis committed Jan 11, 2025
1 parent b85e3cc commit fd77709
Showing 1 changed file with 79 additions and 11 deletions.
90 changes: 79 additions & 11 deletions lib/src/git_subprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ pub enum GitSubprocessError {
Spawn(std::io::Error),
#[error("Failed to wait for the git process")]
Wait(std::io::Error),
#[error("Failed to canonicalize path: {0}")]
PathCanonicalization(std::path::PathBuf, std::io::Error),
#[error("Git process failed: {0}")]
External(String),
}
Expand Down Expand Up @@ -60,11 +58,6 @@ pub(crate) struct GitSubprocessContext<'a> {
git_path: &'a Path,
}

fn canonicalize(path: impl AsRef<Path>) -> Result<PathBuf, GitSubprocessError> {
dunce::canonicalize(&path)
.map_err(|e| GitSubprocessError::PathCanonicalization(path.as_ref().to_path_buf(), e))
}

impl<'a> GitSubprocessContext<'a> {
pub(crate) fn new(git_dir: impl Into<PathBuf>, git_path: &'a Path) -> Self {
GitSubprocessContext {
Expand All @@ -77,7 +70,7 @@ impl<'a> GitSubprocessContext<'a> {
git_repo: &git2::Repository,
git_path: &'a Path,
) -> Result<Self, GitError> {
let git_dir = canonicalize(git_repo.path())?;
let git_dir = git_repo.path();

Ok(Self::new(git_dir, git_path))
}
Expand Down Expand Up @@ -324,7 +317,7 @@ fn parse_no_remote_tracking_branch(stderr: &[u8]) -> bool {
/// we just look at the last line to report
/// whether this was the problem or not.
/// It is up to the caller to know which ref it was trying to push
fn parse_force_with_lease_error(stderr: &[u8]) -> bool {
fn parse_force_with_lease(stderr: &[u8]) -> bool {
stderr
.lines()
.last()
Expand Down Expand Up @@ -370,7 +363,7 @@ fn parse_git_push_output(output: Output) -> Result<bool, GitError> {
}

parse_no_such_remote(&output.stderr).map_err(GitError::NoSuchRemote)?;
if parse_force_with_lease_error(&output.stderr) {
if parse_force_with_lease(&output.stderr) {
return Ok(true);
}

Expand All @@ -386,4 +379,79 @@ fn parse_git_remote_prune_output(output: Output) -> Result<(), GitError> {
Err(external_git_error(&output.stderr))
}

// TODO(bsdinis): add unit tests of error parsing here
#[cfg(test)]
mod test {
use super::*;
const SAMPLE_NO_SUCH_REMOTE_ERROR: &[u8] =
r###"fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists. "###
.as_bytes();
const SAMPLE_NO_REMOTE_REF_ERROR: &[u8] =
"fatal: couldn't find remote ref refs/heads/noexist".as_bytes();
const SAMPLE_NO_REMOTE_TRACKING_BRANCH_ERROR: &[u8] =
"error: remote-tracking branch 'bookmark' not found".as_bytes();
const SAMPLE_FORCE_WITH_LEASE_ERROR: &[u8] = r###"To origin
! [rejected] cb17dcdc74d5c974e836ad8ee9e9beace9864e28 -> bookmark1 (stale info)
error: failed to push some refs to 'origin'"###
.as_bytes();
const SAMPLE_OK_STDERR: &[u8] = "".as_bytes();

#[test]
fn test_parse_no_such_remote() {
assert_eq!(
parse_no_such_remote(SAMPLE_NO_SUCH_REMOTE_ERROR),
Err("origin".to_string())
);
assert_eq!(parse_no_such_remote(SAMPLE_NO_REMOTE_REF_ERROR), Ok(()));
assert_eq!(
parse_no_such_remote(SAMPLE_NO_REMOTE_TRACKING_BRANCH_ERROR),
Ok(())
);
assert_eq!(parse_no_such_remote(SAMPLE_FORCE_WITH_LEASE_ERROR), Ok(()));
assert_eq!(parse_no_such_remote(SAMPLE_OK_STDERR), Ok(()));
}

#[test]
fn test_parse_no_remote_ref() {
assert_eq!(parse_no_remote_ref(SAMPLE_NO_SUCH_REMOTE_ERROR), None);
assert_eq!(
parse_no_remote_ref(SAMPLE_NO_REMOTE_REF_ERROR),
Some("noexist".to_string())
);
assert_eq!(
parse_no_remote_ref(SAMPLE_NO_REMOTE_TRACKING_BRANCH_ERROR),
None
);
assert_eq!(parse_no_remote_ref(SAMPLE_FORCE_WITH_LEASE_ERROR), None);
assert_eq!(parse_no_remote_ref(SAMPLE_OK_STDERR), None);
}

#[test]
fn test_parse_no_remote_tracking_branch() {
assert!(!parse_no_remote_tracking_branch(
SAMPLE_NO_SUCH_REMOTE_ERROR
));
assert!(!parse_no_remote_tracking_branch(SAMPLE_NO_REMOTE_REF_ERROR));
assert!(parse_no_remote_tracking_branch(
SAMPLE_NO_REMOTE_TRACKING_BRANCH_ERROR
));
assert!(!parse_no_remote_tracking_branch(
SAMPLE_FORCE_WITH_LEASE_ERROR
));
assert!(!parse_no_remote_tracking_branch(SAMPLE_OK_STDERR));
}

#[test]
fn test_parse_force_with_lease() {
assert!(!parse_force_with_lease(SAMPLE_NO_SUCH_REMOTE_ERROR));
assert!(!parse_force_with_lease(SAMPLE_NO_REMOTE_REF_ERROR));
assert!(!parse_force_with_lease(
SAMPLE_NO_REMOTE_TRACKING_BRANCH_ERROR
));
assert!(parse_force_with_lease(SAMPLE_FORCE_WITH_LEASE_ERROR));
assert!(!parse_force_with_lease(SAMPLE_OK_STDERR));
}
}

0 comments on commit fd77709

Please sign in to comment.