From 7a08fe0940af560188babff5d8fd2eb9e5049fe5 Mon Sep 17 00:00:00 2001 From: Felix Krull Date: Tue, 9 Oct 2018 22:02:48 +0200 Subject: [PATCH] Add simple repo roundtrip test --- rust-bindings/rust/libostree/Cargo.toml | 19 +++--- .../rust/libostree/tests/roundtrip.rs | 63 +++++++++++++++++++ 2 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 rust-bindings/rust/libostree/tests/roundtrip.rs diff --git a/rust-bindings/rust/libostree/Cargo.toml b/rust-bindings/rust/libostree/Cargo.toml index af71b777..3c1a628c 100644 --- a/rust-bindings/rust/libostree/Cargo.toml +++ b/rust-bindings/rust/libostree/Cargo.toml @@ -6,12 +6,15 @@ version = "0.2.0" name = "libostree" [dependencies] -libc = "^0.2.0" -bitflags = "^1.0.0" -lazy_static = "1.1.0" -glib = "^0.6.0" -gio = "^0.5.0" -glib-sys = "^0.7" -gobject-sys = "^0.7" -gio-sys = "^0.7" +libc = "0.2" +bitflags = "1" +lazy_static = "1.1" +glib = "0.6" +gio = "0.5" +glib-sys = "0.7" +gobject-sys = "0.7" +gio-sys = "0.7" libostree-sys = { path = "../libostree-sys" } + +[dev-dependencies] +tempfile = "3" diff --git a/rust-bindings/rust/libostree/tests/roundtrip.rs b/rust-bindings/rust/libostree/tests/roundtrip.rs new file mode 100644 index 00000000..35dbfa3a --- /dev/null +++ b/rust-bindings/rust/libostree/tests/roundtrip.rs @@ -0,0 +1,63 @@ +extern crate gio; +extern crate glib; +extern crate libostree; +extern crate tempfile; + +use std::fs; +use std::io; +use std::io::Write; +use glib::prelude::*; +use libostree::prelude::*; + + + +fn create_repo(repodir: &tempfile::TempDir) -> Result { + let repo = libostree::Repo::new_for_path(repodir.path()); + repo.create(libostree::RepoMode::Archive, None)?; + 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: &libostree::Repo) -> Result { + let gfile = gio::File::new_for_path(treedir.path()); + let mtree = libostree::MutableTree::new(); + repo.write_directory_to_mtree(&gfile, &mtree, None, None)?; + Ok(mtree) +} + +fn commit_mtree(repo: &libostree::Repo, mtree: &libostree::MutableTree) -> Result { + repo.prepare_transaction(None)?; + let repo_file = repo.write_mtree(mtree, None)?.downcast().unwrap(); + let checksum = repo.write_commit(None, "Test Commit", None, None, &repo_file, None)?; + repo.transaction_set_ref(None, "test", checksum.as_str()); + repo.commit_transaction(None)?; + Ok(checksum) +} + +fn open_repo(repodir: &tempfile::TempDir) -> Result { + let repo = libostree::Repo::new_for_path(repodir.path()); + repo.open(None)?; + 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) + .expect("failed to list refs"); + assert_eq!(refs.len(), 1); + assert_eq!(refs["test"], checksum); +}