diff --git a/Cargo.lock b/Cargo.lock index ec3ebc1..d521002 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,9 +12,15 @@ dependencies = [ "custom_error 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)", + "serial_test 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "simplelog 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cfg-if" version = "0.1.10" @@ -32,6 +38,14 @@ dependencies = [ "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "custom_error" version = "1.8.0" @@ -42,11 +56,24 @@ name = "itoa" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "libc" version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lock_api" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "log" version = "0.4.11" @@ -72,11 +99,59 @@ dependencies = [ "autocfg 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "parking_lot" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "parking_lot_core" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.80 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.57 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ryu" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "serde" version = "1.0.117" @@ -92,6 +167,26 @@ dependencies = [ "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "serial_test" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serial_test_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serial_test_derive" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "simplelog" version = "0.8.0" @@ -102,6 +197,21 @@ dependencies = [ "termcolor 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "smallvec" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "syn" +version = "1.0.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "termcolor" version = "1.1.2" @@ -120,6 +230,11 @@ dependencies = [ "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" @@ -154,20 +269,35 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum autocfg 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum chrono 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)" = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum custom_error 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51ac5e99a7fea3ee8a03fa4721a47e2efd3fbb38358fc61192a54d4c6f866c12" "checksum itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" +"checksum lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" "checksum log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" "checksum num-integer 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" "checksum num-traits 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +"checksum parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e" +"checksum parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3" +"checksum proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +"checksum quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +"checksum redox_syscall 0.1.57 (registry+https://github.com/rust-lang/crates.io-index)" = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" "checksum ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +"checksum scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" "checksum serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)" = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" "checksum serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)" = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" +"checksum serial_test 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fef5f7c7434b2f2c598adc6f9494648a1e41274a75c0ba4056f680ae0c117fd6" +"checksum serial_test_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d08338d8024b227c62bd68a12c7c9883f5c66780abaef15c550dc56f46ee6515" "checksum simplelog 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2b2736f58087298a448859961d3f4a0850b832e72619d75adc69da7993c2cd3c" +"checksum smallvec 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7acad6f34eb9e8a259d3283d1e8c1d34d7415943d4895f65cc73813c7396fc85" +"checksum syn 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)" = "6c1e438504729046a5cfae47f97c30d6d083c7d91d94603efdae3477fc070d4c" "checksum termcolor 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" "checksum time 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +"checksum unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" "checksum wasi 0.10.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" "checksum winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" diff --git a/Cargo.toml b/Cargo.toml index f9c8363..6dfe7a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ simplelog = "0.8" log = "0.4" serde_json = "1.0" custom_error = "1.8.0" +serial_test = "0.4" diff --git a/src/database/io.rs b/src/database/io.rs index 9cbb679..c5a2f6f 100644 --- a/src/database/io.rs +++ b/src/database/io.rs @@ -15,8 +15,6 @@ custom_error! { pub IOError NotDirectory{path: String} = "Path to the database should point at a directory: {path}", } -/// Reads database data from a directory at the specified path -/// as a vector of `Group`s. pub fn read(db_path: &path::Path) -> Result, Box> { if !db_path.is_dir() { error!( @@ -47,9 +45,6 @@ pub fn read(db_path: &path::Path) -> Result, Box> { Ok(groups) } -/// Writes data of the given database into the directory specified by the path. -/// Does not clear it from any previously existing files, -/// you can use `clear_dir()` for that. pub fn write(db_path: &path::Path, db: &Database) -> Result<(), Box> { if db_path.exists() && !db_path.is_dir() { error!( @@ -77,11 +72,6 @@ pub fn write(db_path: &path::Path, db: &Database) -> Result<(), Box> Ok(()) } -/// Given a path to directory that was used for database storage - -/// clears it's data. -/// -/// This means removing any '.json' files from all immediate subdirectories and -/// then removing any subdirectories that were or became empty. pub fn clear_dir(db_path: &path::Path) -> Result<(), Box> { info!( "Clearing directory {} from database files.", @@ -118,8 +108,6 @@ pub fn clear_dir(db_path: &path::Path) -> Result<(), Box> { Ok(()) } -/// helper function to read a group from a given subdirectory: -/// loads data from all containing '.json' files. fn read_group(group_path: &path::Path) -> Result, Box> { let mut files = HashMap::new(); for entry in fs::read_dir(group_path)? { @@ -144,8 +132,6 @@ fn read_group(group_path: &path::Path) -> Result, Box> Ok(None) } -/// Checks if given path points at a directory that can represent -/// a database group (has a valid name). fn check_valid_group_dir(dir_path: &path::Path) -> bool { if !dir_path.is_dir() { warn!( @@ -164,8 +150,6 @@ fn check_valid_group_dir(dir_path: &path::Path) -> bool { true } -/// Checks if given path points at a file that can represent -/// a database group (has a valid name). fn check_valid_data_file(file_path: &path::Path) -> bool { if file_path.is_dir() { warn!( diff --git a/src/database/tests.rs b/src/database/tests.rs index d47f73a..974dc99 100644 --- a/src/database/tests.rs +++ b/src/database/tests.rs @@ -3,32 +3,34 @@ use serde_json::json; use std::fs; use std::path; +use serial_test::serial; + const TEST_DB_PATH: &str = "./fixtures/database"; -const TEST_DB_MOVED_PATH: &str = "./fixtures/moved"; +const TEST_DB_COPY_PATH: &str = "./fixtures/copy"; +const TEST_DB_MOVED_COPY_PATH: &str = "./fixtures/copy_moved"; const NO_DB_MESSAGE: &str = "Can not find/load test database"; -struct TestCleanup { - path: String, - clear_moved: bool, -} +struct TestCleanup; impl Drop for TestCleanup { fn drop(&mut self) { - let _ = fs::remove_dir_all(&self.path); - if self.clear_moved { - let _ = fs::remove_dir_all(TEST_DB_MOVED_PATH); - } + clear_test_db(); } } -fn prepare_db_copy(copy_id: &str, clear_moved: bool) -> (String, TestCleanup) { - let path = format!("{}_{}", TEST_DB_PATH, copy_id); +fn prepare_db_copy() -> TestCleanup { + clear_test_db(); let original_db = Database::load(path::Path::new(TEST_DB_PATH)).expect(NO_DB_MESSAGE); original_db - .write_copy(path::Path::new(&path)) + .write_copy(path::Path::new(TEST_DB_COPY_PATH)) .expect("Should be able to create a new copy of the fixture database."); - (path.clone(), TestCleanup { path: path.to_owned(), clear_moved: clear_moved }) + TestCleanup +} + +fn clear_test_db() { + let _ = fs::remove_dir_all(TEST_DB_COPY_PATH); + let _ = fs::remove_dir_all(TEST_DB_MOVED_COPY_PATH); } #[test] @@ -320,16 +322,17 @@ fn db_remove_value() { } #[test] +#[serial] fn db_save() { - let (path, _cleanup) = prepare_db_copy("db_save", false); + let _cleanup = prepare_db_copy(); // Change something up and save - let mut db = Database::load(path::Path::new(&path)).expect(NO_DB_MESSAGE); + let mut db = Database::load(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); db.remove_group("administration") .expect(r#"Should be able to remove "administration" group"#); db.save() .expect("Should be able to save copy of the database."); // Reload and check changes - let db = Database::load(path::Path::new(&path)).expect(NO_DB_MESSAGE); + let db = Database::load(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); assert_eq!(db.group_names().len(), 1); assert_eq!(db.group_names().get(0), Some(&"game".to_owned())); assert_eq!(db.file_names_in("game").len(), 2); @@ -338,22 +341,23 @@ fn db_save() { } #[test] +#[serial] fn db_change_path() { - let (path, _cleanup) = prepare_db_copy("db_change_path", true); + let _cleanup = prepare_db_copy(); // Change something up and move - let mut db = Database::load(path::Path::new(&path)).expect(NO_DB_MESSAGE); + let mut db = Database::load(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); db.remove_group("administration") .expect(r#"Should be able to remove "administration" group"#); db.file_mut("game", "perks") .unwrap() .insert("", json!({"var":7})) .expect("Should be able to insert into root."); - db.change_path(path::Path::new(TEST_DB_MOVED_PATH)) + db.change_path(path::Path::new(TEST_DB_MOVED_COPY_PATH)) .expect("Should be able to change database's path."); - assert!(!path::Path::new(&path).exists()); - assert!(path::Path::new(TEST_DB_MOVED_PATH).exists()); + assert!(!path::Path::new(TEST_DB_COPY_PATH).exists()); + assert!(path::Path::new(TEST_DB_MOVED_COPY_PATH).exists()); // Reload and check the changes - let db = Database::load(path::Path::new(TEST_DB_MOVED_PATH)).expect(NO_DB_MESSAGE); + let db = Database::load(path::Path::new(TEST_DB_MOVED_COPY_PATH)).expect(NO_DB_MESSAGE); assert_eq!(db.group_names().len(), 1); assert_eq!(db.group_names().get(0), Some(&"game".to_owned())); assert_eq!(db.file_names_in("game").len(), 2); @@ -366,9 +370,10 @@ fn db_change_path() { } #[test] +#[serial] fn db_erase() { - let (path, _cleanup) = prepare_db_copy("db_erase", false); - let db = Database::load(path::Path::new(&path)).expect(NO_DB_MESSAGE); + let _cleanup = prepare_db_copy(); + let db = Database::load(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); db.erase().expect("Should be able to erase data."); - assert!(!path::Path::new(&path).exists()); + assert!(!path::Path::new(TEST_DB_COPY_PATH).exists()); }