mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-18 06:22:28 +03:00
add snapshotting tests
This commit is contained in:
parent
9b0661e7db
commit
34d12fb1e6
@ -94,7 +94,7 @@ pub fn create(project: &Project, details: SnapshotDetails) -> Result<()> {
|
||||
/// An alternative way of retrieving the snapshots would be to manually the oplog head `git log <oplog_head>` available in `.git/gitbutler/oplog.toml`.
|
||||
///
|
||||
/// If there are no snapshots, an empty list is returned.
|
||||
pub fn list(project: Project, limit: usize) -> Result<Vec<Snapshot>> {
|
||||
pub fn list(project: &Project, limit: usize) -> Result<Vec<Snapshot>> {
|
||||
let repo_path = project.path.as_path();
|
||||
let repo = git2::Repository::init(repo_path)?;
|
||||
|
||||
@ -178,3 +178,88 @@ pub fn restore(project: &Project, sha: String) -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::PathBuf;
|
||||
|
||||
use super::*;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn test_create_and_restore() {
|
||||
let dir = tempdir().unwrap();
|
||||
let repo = git2::Repository::init(dir.path()).unwrap();
|
||||
let file_path = dir.path().join("1.txt");
|
||||
std::fs::write(file_path, "test").unwrap();
|
||||
let file_path = dir.path().join("2.txt");
|
||||
std::fs::write(file_path, "test").unwrap();
|
||||
let mut index = repo.index().unwrap();
|
||||
index.add_path(&PathBuf::from("1.txt")).unwrap();
|
||||
index.add_path(&PathBuf::from("2.txt")).unwrap();
|
||||
let oid = index.write_tree().unwrap();
|
||||
let name = "Your Name";
|
||||
let email = "your.email@example.com";
|
||||
let signature = git2::Signature::now(name, email).unwrap();
|
||||
let initial_commit = repo
|
||||
.commit(
|
||||
Some("HEAD"),
|
||||
&signature,
|
||||
&signature,
|
||||
"initial commit",
|
||||
&repo.find_tree(oid).unwrap(),
|
||||
&[],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// create a new branch called "gitbutler/integraion" from initial commit
|
||||
repo.branch(
|
||||
"gitbutler/integration",
|
||||
&repo.find_commit(initial_commit).unwrap(),
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let project = Project {
|
||||
path: dir.path().to_path_buf(),
|
||||
enable_snapshots: Some(true),
|
||||
..Default::default()
|
||||
};
|
||||
// create gb_dir folder
|
||||
std::fs::create_dir_all(project.gb_dir()).unwrap();
|
||||
|
||||
let vb_state = VirtualBranchesHandle::new(&project.gb_dir());
|
||||
|
||||
let target_sha = initial_commit.to_string();
|
||||
let default_target = crate::virtual_branches::target::Target {
|
||||
branch: crate::git::RemoteRefname::new("origin", "main"),
|
||||
remote_url: Default::default(),
|
||||
sha: crate::git::Oid::from_str(&target_sha).unwrap(),
|
||||
};
|
||||
vb_state.set_default_target(default_target).unwrap();
|
||||
|
||||
// create a snapshot
|
||||
create(&project, SnapshotDetails::new(OperationType::CreateCommit)).unwrap();
|
||||
let snapshots = list(&project, 100).unwrap();
|
||||
|
||||
let file_path = dir.path().join("1.txt");
|
||||
std::fs::write(file_path, "TEST").unwrap();
|
||||
let file_path = dir.path().join("2.txt");
|
||||
std::fs::remove_file(file_path).unwrap();
|
||||
let file_path = dir.path().join("3.txt");
|
||||
std::fs::write(file_path, "something_new").unwrap();
|
||||
// File 1 was modified, file 2 was deleted, file 3 was added
|
||||
|
||||
restore(&project, snapshots.first().unwrap().id.clone()).unwrap();
|
||||
|
||||
let file_path = dir.path().join("1.txt");
|
||||
let file_lines = std::fs::read_to_string(file_path).unwrap();
|
||||
assert_eq!(file_lines, "test");
|
||||
let file_path = dir.path().join("2.txt");
|
||||
assert!(file_path.exists());
|
||||
let file_lines = std::fs::read_to_string(file_path).unwrap();
|
||||
assert_eq!(file_lines, "test");
|
||||
// let file_path = dir.path().join("3.txt");
|
||||
// assert!(!file_path.exists());
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ pub async fn list_snapshots(
|
||||
.state::<projects::Controller>()
|
||||
.get(&project_id)
|
||||
.context("failed to get project")?;
|
||||
let snapshots = snapshot::list(project, limit)?;
|
||||
let snapshots = snapshot::list(&project, limit)?;
|
||||
Ok(snapshots)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user