gitbutler/src-tauri/src/test_utils.rs

58 lines
1.8 KiB
Rust
Raw Normal View History

2023-08-28 10:44:04 +03:00
use std::{fs, path};
2023-08-28 10:34:30 +03:00
use tempfile::tempdir;
2023-08-30 10:46:52 +03:00
use crate::git;
2023-08-28 10:34:30 +03:00
pub fn temp_dir() -> path::PathBuf {
let path = tempdir().unwrap().path().to_path_buf();
fs::create_dir_all(&path).unwrap();
path
}
2023-08-30 10:46:52 +03:00
pub fn test_repository() -> git::Repository {
2023-08-28 10:34:30 +03:00
let path = temp_dir();
2023-08-30 10:46:52 +03:00
let repository = git::Repository::init(path).expect("failed to init repository");
2023-08-28 10:34:30 +03:00
let mut index = repository.index().expect("failed to get index");
let oid = index.write_tree().expect("failed to write tree");
let signature = git2::Signature::now("test", "test@email.com").unwrap();
repository
.commit(
Some("HEAD"),
&signature,
&signature,
"Initial commit",
&repository.find_tree(oid).expect("failed to find tree"),
&[],
)
.expect("failed to commit");
repository
}
2023-08-28 11:28:26 +03:00
2023-08-30 10:46:52 +03:00
pub fn commit_all(repository: &git::Repository) -> git2::Oid {
2023-08-28 11:28:26 +03:00
let mut index = repository.index().expect("failed to get index");
index
.add_all(["."], git2::IndexAddOption::DEFAULT, None)
.expect("failed to add all");
index.write().expect("failed to write index");
let oid = index.write_tree().expect("failed to write tree");
let signature = git2::Signature::now("test", "test@email.com").unwrap();
let commit_oid = repository
.commit(
Some("HEAD"),
&signature,
&signature,
"some commit",
&repository.find_tree(oid).expect("failed to find tree"),
&[&repository
.find_commit(
repository
.refname_to_id("HEAD")
.expect("failed to get head"),
)
.expect("failed to find commit")],
)
.expect("failed to commit");
commit_oid
}