Merge pull request #1340 from gitbutlerapp/Add-empty-bare-repository-function

use a real (though local) remote repository for tests
This commit is contained in:
Nikita Galaiko 2023-10-11 14:14:32 +02:00 committed by GitHub
commit e6c5ddfbb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 26 deletions

View File

@ -17,6 +17,12 @@ impl<'a> From<&'a Repository> for &'a git2::Repository {
}
impl Repository {
#[cfg(test)]
pub fn init_bare<P: AsRef<path::Path>>(path: P) -> Result<Self> {
let inner = git2::Repository::init_bare(path)?;
Ok(Repository(inner))
}
pub fn init<P: AsRef<path::Path>>(path: P) -> Result<Self> {
let inner = git2::Repository::init(path)?;
Ok(Repository(inner))

View File

@ -133,6 +133,12 @@ pub fn temp_dir() -> path::PathBuf {
path
}
pub fn empty_bare_repository() -> git::Repository {
let path = temp_dir();
let repository = git::Repository::init_bare(path).expect("failed to init repository");
repository
}
pub fn test_repository() -> git::Repository {
let path = temp_dir();
let repository = git::Repository::init(path).expect("failed to init repository");

View File

@ -12,8 +12,8 @@ use git2::TreeWalkResult;
use pretty_assertions::{assert_eq, assert_ne};
use crate::{
gb_repository, git, project_repository, reader, sessions,
test_utils::{self, Case, Suite},
gb_repository, git, keys, project_repository, reader, sessions,
test_utils::{self, empty_bare_repository, Case, Suite},
};
use super::*;
@ -23,31 +23,24 @@ fn set_test_target(
gb_repo: &gb_repository::Repository,
project_repository: &project_repository::Repository,
) -> Result<()> {
target::Writer::new(gb_repo).write_default(&target::Target {
branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: "origin".to_string(),
sha: project_repository
.git_repository
.head()
.unwrap()
.target()
.unwrap(),
})?;
project_repository.git_repository.reference(
"refs/remotes/origin/master",
project_repository
.git_repository
.head()
.unwrap()
.target()
.unwrap(),
true,
"update target",
)?;
project_repository
let remote_repo = empty_bare_repository();
let mut remote = project_repository
.git_repository
.remote("origin", "http://origin.com/project")?;
super::integration::update_gitbutler_integration(gb_repo, project_repository)?;
.remote("origin", remote_repo.path().to_str().unwrap())
.expect("failed to add remote");
remote.push(&["refs/heads/master:refs/heads/master"], None)?;
target::Writer::new(gb_repo)
.write_default(&target::Target {
branch: "refs/remotes/origin/master".parse().unwrap(),
remote_url: remote_repo.path().to_str().unwrap().parse().unwrap(),
sha: remote_repo.head().unwrap().target().unwrap(),
})
.expect("failed to write target");
super::integration::update_gitbutler_integration(gb_repo, project_repository)
.expect("failed to update integration");
Ok(())
}