From 78a957268d6551bb60be3b0e9196ed0e3809e36e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Sun, 21 Nov 2021 22:00:18 +0100 Subject: [PATCH] Add a few simple integration tests --- Cargo.lock | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +++ src/lib.rs | 2 +- tests/helpers.rs | 11 +++++++++ tests/repo.rs | 43 +++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/helpers.rs create mode 100644 tests/repo.rs diff --git a/Cargo.lock b/Cargo.lock index 438dec3..a56c80f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -169,6 +169,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "getrandom" version = "0.2.3" @@ -191,6 +197,7 @@ dependencies = [ "regex", "serde", "shellexpand", + "tempdir", "toml", ] @@ -498,6 +505,43 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "redox_syscall" version = "0.2.10" @@ -534,6 +578,15 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + [[package]] name = "scopeguard" version = "1.1.0" @@ -640,6 +693,16 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" +dependencies = [ + "rand", + "remove_dir_all", +] + [[package]] name = "termcolor" version = "1.1.2" diff --git a/Cargo.toml b/Cargo.toml index 827c1e4..c984b3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,3 +60,6 @@ version = "1.5" [dependencies.comfy-table] version = "5.0" + +[dev-dependencies.tempdir] +version = "0.3" diff --git a/src/lib.rs b/src/lib.rs index dc847a0..adace4e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ use std::process; mod cmd; mod config; mod output; -mod repo; +pub mod repo; use config::{Config, Tree}; use output::*; diff --git a/tests/helpers.rs b/tests/helpers.rs new file mode 100644 index 0000000..a679dc1 --- /dev/null +++ b/tests/helpers.rs @@ -0,0 +1,11 @@ +use tempdir::TempDir; + +pub fn init_tmpdir() -> TempDir { + let tmp_dir = TempDir::new("grm-test").unwrap(); + println!("Temporary directory: {}", tmp_dir.path().display()); + tmp_dir +} + +pub fn cleanup_tmpdir(tempdir: TempDir) { + tempdir.close().unwrap(); +} diff --git a/tests/repo.rs b/tests/repo.rs new file mode 100644 index 0000000..261fd18 --- /dev/null +++ b/tests/repo.rs @@ -0,0 +1,43 @@ +use grm::repo::*; + +mod helpers; + +use helpers::*; + +#[test] +fn open_empty_repo() { + let tmpdir = init_tmpdir(); + assert!(matches!( + open_repo(tmpdir.path(), true), + Err(RepoError { + kind: RepoErrorKind::NotFound + }) + )); + assert!(matches!( + open_repo(tmpdir.path(), false), + Err(RepoError { + kind: RepoErrorKind::NotFound + }) + )); + cleanup_tmpdir(tmpdir); +} + +#[test] +fn create_repo() -> Result<(), Box> { + let tmpdir = init_tmpdir(); + let repo = init_repo(tmpdir.path(), false)?; + assert!(!repo.is_bare()); + assert!(repo.is_empty()?); + cleanup_tmpdir(tmpdir); + Ok(()) +} + +#[test] +fn create_repo_with_worktree() -> Result<(), Box> { + let tmpdir = init_tmpdir(); + let repo = init_repo(tmpdir.path(), true)?; + assert!(repo.is_bare()); + assert!(repo.is_empty()?); + cleanup_tmpdir(tmpdir); + Ok(()) +}