From c89270969ce1b130c2f56f9b6c72ddac0751618b Mon Sep 17 00:00:00 2001 From: Felix Krull Date: Tue, 21 May 2019 22:58:40 +0200 Subject: [PATCH] Refactor tests and add test for traverse_commit --- rust-bindings/rust/Cargo.toml | 1 + rust-bindings/rust/tests/data/test.tar | Bin 0 -> 10240 bytes rust-bindings/rust/tests/repo.rs | 63 ++++++++++++++++++++ rust-bindings/rust/tests/roundtrip.rs | 79 ------------------------- rust-bindings/rust/tests/util/mod.rs | 67 +++++++++++++++++++++ 5 files changed, 131 insertions(+), 79 deletions(-) create mode 100644 rust-bindings/rust/tests/data/test.tar create mode 100644 rust-bindings/rust/tests/repo.rs delete mode 100644 rust-bindings/rust/tests/roundtrip.rs create mode 100644 rust-bindings/rust/tests/util/mod.rs diff --git a/rust-bindings/rust/Cargo.toml b/rust-bindings/rust/Cargo.toml index d8fbc959..33f8848a 100644 --- a/rust-bindings/rust/Cargo.toml +++ b/rust-bindings/rust/Cargo.toml @@ -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] diff --git a/rust-bindings/rust/tests/data/test.tar b/rust-bindings/rust/tests/data/test.tar new file mode 100644 index 0000000000000000000000000000000000000000..fd39eda107121340ec04f790c9680533323308d7 GIT binary patch literal 10240 zcmeIxK@Ng25QX7cdy1X_rr6T+mGTq&yL z#V=NCS&2V} Result { - 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 { - 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 { - repo.prepare_transaction(NONE_CANCELLABLE)?; - let repo_file = repo - .write_mtree(mtree, NONE_CANCELLABLE)? - .downcast::() - .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 { - 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); -} diff --git a/rust-bindings/rust/tests/util/mod.rs b/rust-bindings/rust/tests/util/mod.rs new file mode 100644 index 00000000..923a2f9b --- /dev/null +++ b/rust-bindings/rust/tests/util/mod.rs @@ -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::() + .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 +}