Refactor tests and add test for traverse_commit

This commit is contained in:
Felix Krull 2019-05-21 22:58:40 +02:00 committed by Colin Walters
parent ff3e268a3b
commit c89270969c
5 changed files with 131 additions and 79 deletions

View File

@ -39,6 +39,7 @@ gio-sys = "0.8.0"
ostree-sys = { version = "0.3.0", path = "sys" }
[dev-dependencies]
maplit = "1.0.1"
tempfile = "3"
[features]

Binary file not shown.

View File

@ -0,0 +1,63 @@
extern crate gio;
extern crate glib;
extern crate ostree;
extern crate tempfile;
#[macro_use]
extern crate maplit;
mod util;
use util::*;
use gio::NONE_CANCELLABLE;
use ostree::prelude::*;
use ostree::{ObjectName, ObjectType};
#[test]
fn should_commit_content_to_repo_and_list_refs_again() {
let test_repo = TestRepo::new();
let mtree = create_mtree(&test_repo.repo);
let checksum = commit(&test_repo.repo, &mtree, "test");
let repo = ostree::Repo::new_for_path(test_repo.dir.path());
repo.open(NONE_CANCELLABLE).expect("OSTree test_repo");
let refs = repo
.list_refs(None, NONE_CANCELLABLE)
.expect("failed to list refs");
assert_eq!(1, refs.len());
assert_eq!(checksum, refs["test"]);
}
#[test]
fn should_traverse_commit() {
let test_repo = TestRepo::new();
let checksum = test_repo.test_commit();
let objects = test_repo
.repo
.traverse_commit(&checksum, -1, NONE_CANCELLABLE)
.expect("traverse commit");
assert_eq!(
hashset!(
ObjectName::new(
"89f84ca9854a80e85b583e46a115ba4985254437027bad34f0b113219323d3f8",
ObjectType::File
),
ObjectName::new(
"5280a884f930cae329e2e39d52f2c8e910c2ef4733216b67679db32a2b56c4db",
ObjectType::DirTree
),
ObjectName::new(
"c81acde323d73f8639fc84f1ded17bbafc415e645f845e9f3b16a4906857c2d4",
ObjectType::DirTree
),
ObjectName::new(
"ad49a0f4e3bc165361b6d17e8a865d479b373ee67d89ac6f0ce871f27da1be6d",
ObjectType::DirMeta
),
ObjectName::new(checksum, ObjectType::Commit)
),
objects
);
}

View File

@ -1,79 +0,0 @@
extern crate gio;
extern crate glib;
extern crate ostree;
extern crate tempfile;
use gio::NONE_CANCELLABLE;
use glib::prelude::*;
use ostree::prelude::*;
use ostree::RepoFile;
use std::fs;
use std::io;
use std::io::Write;
fn create_repo(repodir: &tempfile::TempDir) -> Result<ostree::Repo, glib::Error> {
let repo = ostree::Repo::new_for_path(repodir.path());
repo.create(ostree::RepoMode::Archive, NONE_CANCELLABLE)?;
Ok(repo)
}
fn create_test_file(treedir: &tempfile::TempDir) -> Result<(), io::Error> {
let mut testfile = fs::File::create(treedir.path().join("test.txt"))?;
write!(testfile, "test")?;
Ok(())
}
fn create_mtree(
treedir: &tempfile::TempDir,
repo: &ostree::Repo,
) -> Result<ostree::MutableTree, glib::Error> {
let gfile = gio::File::new_for_path(treedir.path());
let mtree = ostree::MutableTree::new();
repo.write_directory_to_mtree(&gfile, &mtree, None, NONE_CANCELLABLE)?;
Ok(mtree)
}
fn commit_mtree(repo: &ostree::Repo, mtree: &ostree::MutableTree) -> Result<String, glib::Error> {
repo.prepare_transaction(NONE_CANCELLABLE)?;
let repo_file = repo
.write_mtree(mtree, NONE_CANCELLABLE)?
.downcast::<RepoFile>()
.unwrap();
let checksum = repo
.write_commit(
None,
"Test Commit".into(),
None,
None,
&repo_file,
NONE_CANCELLABLE,
)?
.to_string();
repo.transaction_set_ref(None, "test", checksum.as_str().into());
repo.commit_transaction(NONE_CANCELLABLE)?;
Ok(checksum)
}
fn open_repo(repodir: &tempfile::TempDir) -> Result<ostree::Repo, glib::Error> {
let repo = ostree::Repo::new_for_path(repodir.path());
repo.open(NONE_CANCELLABLE)?;
Ok(repo)
}
#[test]
fn should_commit_content_to_repo_and_list_refs_again() {
let repodir = tempfile::tempdir().unwrap();
let treedir = tempfile::tempdir().unwrap();
let repo = create_repo(&repodir).expect("failed to create repo");
create_test_file(&treedir).expect("failed to create test file");
let mtree = create_mtree(&treedir, &repo).expect("failed to build mtree");
let checksum = commit_mtree(&repo, &mtree).expect("failed to commit mtree");
let repo = open_repo(&repodir).expect("failed to open repo");
let refs = repo
.list_refs(None, NONE_CANCELLABLE)
.expect("failed to list refs");
assert_eq!(refs.len(), 1);
assert_eq!(refs["test"], checksum);
}

View File

@ -0,0 +1,67 @@
use gio::NONE_CANCELLABLE;
use glib::prelude::*;
use glib::GString;
use ostree::prelude::*;
use std::path::Path;
#[derive(Debug)]
pub struct TestRepo {
pub dir: tempfile::TempDir,
pub repo: ostree::Repo,
}
impl TestRepo {
pub fn new() -> TestRepo {
TestRepo::new_with_mode(ostree::RepoMode::BareUser)
}
pub fn new_with_mode(repo_mode: ostree::RepoMode) -> TestRepo {
let dir = tempfile::tempdir().expect("temp repo dir");
let repo = ostree::Repo::new_for_path(dir.path());
repo.create(repo_mode, NONE_CANCELLABLE)
.expect("OSTree repo");
TestRepo { dir, repo }
}
pub fn test_commit(&self) -> GString {
let mtree = create_mtree(&self.repo);
commit(&self.repo, &mtree, "test")
}
}
pub fn create_mtree(repo: &ostree::Repo) -> ostree::MutableTree {
let mtree = ostree::MutableTree::new();
let file = gio::File::new_for_path(
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("data")
.join("test.tar"),
);
repo.write_archive_to_mtree(&file, &mtree, None, true, NONE_CANCELLABLE)
.expect("test mtree");
mtree
}
pub fn commit(repo: &ostree::Repo, mtree: &ostree::MutableTree, ref_: &str) -> GString {
repo.prepare_transaction(NONE_CANCELLABLE)
.expect("prepare transaction");
let repo_file = repo
.write_mtree(mtree, NONE_CANCELLABLE)
.expect("write mtree")
.downcast::<ostree::RepoFile>()
.unwrap();
let checksum = repo
.write_commit(
None,
"Test Commit".into(),
None,
None,
&repo_file,
NONE_CANCELLABLE,
)
.expect("write commit");
repo.transaction_set_ref(None, ref_, checksum.as_str().into());
repo.commit_transaction(NONE_CANCELLABLE)
.expect("commit transaction");
checksum
}