From 9228ce4bae46dfcd8707459763f866d63c90b195 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 20 Oct 2017 12:11:34 +0200 Subject: [PATCH] util crates use tempdir crate instead of devtools to create temp path (#6807) * use tempdir instead of devtools in kvdb-rocksdb * use tempdir instead of devtools in migration * use tempdir instead of devtools in ethcore-network * fixed wrong merge --- Cargo.lock | 6 ++-- util/Cargo.toml | 1 - util/kvdb-rocksdb/Cargo.toml | 8 +++-- util/kvdb-rocksdb/src/lib.rs | 27 +++++++++-------- util/migration/Cargo.toml | 4 ++- util/migration/src/lib.rs | 5 ---- util/migration/src/tests.rs | 55 +++++++++++++++++----------------- util/network/Cargo.toml | 5 +++- util/network/src/connection.rs | 2 +- util/network/src/host.rs | 9 +++--- util/network/src/lib.rs | 3 ++ util/network/src/node_table.rs | 8 ++--- util/src/lib.rs | 1 - 13 files changed, 70 insertions(+), 64 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc6c35554..b38433244 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -701,6 +701,7 @@ dependencies = [ "serde_json 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "snappy 0.1.0", + "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)", "tiny-keccak 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -769,7 +770,6 @@ dependencies = [ "eth-secp256k1 0.5.6 (git+https://github.com/paritytech/rust-secp256k1)", "ethcore-bigint 0.1.3", "ethcore-bytes 0.1.0", - "ethcore-devtools 1.9.0", "ethcore-logger 1.9.0", "hash 0.1.0", "hashdb 0.1.0", @@ -1402,13 +1402,13 @@ version = "0.1.0" dependencies = [ "elastic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethcore-bigint 0.1.3", - "ethcore-devtools 1.9.0", "kvdb 0.1.0", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.2.0", "rocksdb 0.4.5 (git+https://github.com/paritytech/rust-rocksdb)", + "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1563,11 +1563,11 @@ name = "migration" version = "0.1.0" dependencies = [ "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", - "ethcore-devtools 1.9.0", "kvdb 0.1.0", "kvdb-rocksdb 0.1.0", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "macros 0.1.0", + "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/util/Cargo.toml b/util/Cargo.toml index 6dee72718..9341a1bdd 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -18,7 +18,6 @@ rlp = { path = "rlp" } heapsize = "0.4" hash = { path = "hash" } clippy = { version = "0.0.103", optional = true} -ethcore-devtools = { path = "../devtools" } libc = "0.2.7" target_info = "0.1" ethcore-bigint = { path = "bigint", features = ["heapsizeof"] } diff --git a/util/kvdb-rocksdb/Cargo.toml b/util/kvdb-rocksdb/Cargo.toml index 0f1809d14..e055c853e 100644 --- a/util/kvdb-rocksdb/Cargo.toml +++ b/util/kvdb-rocksdb/Cargo.toml @@ -4,12 +4,14 @@ version = "0.1.0" authors = ["Parity Technologies "] [dependencies] -log = "0.3" elastic-array = "0.9" ethcore-bigint = { path = "../bigint" } -ethcore-devtools = { path = "../../devtools" } +kvdb = { path = "../kvdb" } +log = "0.3" parking_lot = "0.4" regex = "0.2" rlp = { path = "../rlp" } rocksdb = { git = "https://github.com/paritytech/rust-rocksdb" } -kvdb = { path = "../kvdb" } + +[dev-dependencies] +tempdir = "0.3" diff --git a/util/kvdb-rocksdb/src/lib.rs b/util/kvdb-rocksdb/src/lib.rs index 45461a60f..d08c8ec85 100644 --- a/util/kvdb-rocksdb/src/lib.rs +++ b/util/kvdb-rocksdb/src/lib.rs @@ -23,7 +23,6 @@ extern crate regex; extern crate rocksdb; extern crate ethcore_bigint as bigint; -extern crate ethcore_devtools as devtools; extern crate kvdb; extern crate rlp; @@ -679,14 +678,16 @@ impl Drop for Database { #[cfg(test)] mod tests { + extern crate tempdir; + + use std::str::FromStr; + use self::tempdir::TempDir; use bigint::hash::H256; use super::*; - use devtools::*; - use std::str::FromStr; fn test_db(config: &DatabaseConfig) { - let path = RandomTempPath::create_dir(); - let db = Database::open(config, path.as_path().to_str().unwrap()).unwrap(); + let tempdir = TempDir::new("").unwrap(); + let db = Database::open(config, tempdir.path().to_str().unwrap()).unwrap(); let key1 = H256::from_str("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc").unwrap(); let key2 = H256::from_str("03c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc").unwrap(); let key3 = H256::from_str("01c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc").unwrap(); @@ -739,8 +740,8 @@ mod tests { #[test] fn kvdb() { - let path = RandomTempPath::create_dir(); - let _ = Database::open_default(path.as_path().to_str().unwrap()).unwrap(); + let tempdir = TempDir::new("").unwrap(); + let _ = Database::open_default(tempdir.path().to_str().unwrap()).unwrap(); test_db(&DatabaseConfig::default()); } @@ -759,11 +760,11 @@ mod tests { let config = DatabaseConfig::default(); let config_5 = DatabaseConfig::with_columns(Some(5)); - let path = RandomTempPath::create_dir(); + let tempdir = TempDir::new("").unwrap(); // open empty, add 5. { - let db = Database::open(&config, path.as_path().to_str().unwrap()).unwrap(); + let db = Database::open(&config, tempdir.path().to_str().unwrap()).unwrap(); assert_eq!(db.num_columns(), 0); for i in 0..5 { @@ -774,7 +775,7 @@ mod tests { // reopen as 5. { - let db = Database::open(&config_5, path.as_path().to_str().unwrap()).unwrap(); + let db = Database::open(&config_5, tempdir.path().to_str().unwrap()).unwrap(); assert_eq!(db.num_columns(), 5); } } @@ -784,11 +785,11 @@ mod tests { let config = DatabaseConfig::default(); let config_5 = DatabaseConfig::with_columns(Some(5)); - let path = RandomTempPath::create_dir(); + let tempdir = TempDir::new("").unwrap(); // open 5, remove all. { - let db = Database::open(&config_5, path.as_path().to_str().unwrap()).unwrap(); + let db = Database::open(&config_5, tempdir.path().to_str().unwrap()).unwrap(); assert_eq!(db.num_columns(), 5); for i in (0..5).rev() { @@ -799,7 +800,7 @@ mod tests { // reopen as 0. { - let db = Database::open(&config, path.as_path().to_str().unwrap()).unwrap(); + let db = Database::open(&config, tempdir.path().to_str().unwrap()).unwrap(); assert_eq!(db.num_columns(), 0); } } diff --git a/util/migration/Cargo.toml b/util/migration/Cargo.toml index 55b63f133..66d78b0a7 100644 --- a/util/migration/Cargo.toml +++ b/util/migration/Cargo.toml @@ -8,5 +8,7 @@ log = "0.3" macros = { path = "../macros" } kvdb = { path = "../kvdb" } kvdb-rocksdb = { path = "../kvdb-rocksdb" } -ethcore-devtools = { path = "../../devtools" } error-chain = "0.11.0" + +[dev-dependencies] +tempdir = "0.3" diff --git a/util/migration/src/lib.rs b/util/migration/src/lib.rs index f9d3f7cac..7763b7d9a 100644 --- a/util/migration/src/lib.rs +++ b/util/migration/src/lib.rs @@ -25,7 +25,6 @@ extern crate macros; #[macro_use] extern crate error_chain; -extern crate ethcore_devtools as devtools; extern crate kvdb; extern crate kvdb_rocksdb; @@ -38,10 +37,6 @@ use kvdb::DBTransaction; use kvdb_rocksdb::{CompactionProfile, Database, DatabaseConfig}; error_chain! { - types { - Error, ErrorKind, ResultExt, Result; - } - links { Db(kvdb::Error, kvdb::ErrorKind); } diff --git a/util/migration/src/tests.rs b/util/migration/src/tests.rs index 1f712262f..5d6e941da 100644 --- a/util/migration/src/tests.rs +++ b/util/migration/src/tests.rs @@ -18,17 +18,18 @@ //! A random temp directory is created. A database is created within it, and migrations //! are performed in temp sub-directories. -use std::collections::BTreeMap; -use std::sync::Arc; -use std::path::{Path, PathBuf}; -use {Batch, Config, Error, SimpleMigration, Migration, Manager, ChangeColumns}; -use kvdb_rocksdb::Database; -use devtools::RandomTempPath; +extern crate tempdir; +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use self::tempdir::TempDir; +use kvdb_rocksdb::Database; +use {Batch, Config, Error, SimpleMigration, Migration, Manager, ChangeColumns}; + +#[inline] fn db_path(path: &Path) -> PathBuf { - let mut p = path.to_owned(); - p.push("db"); - p + path.join("db") } // initialize a database at the given directory with the given values. @@ -109,8 +110,8 @@ impl Migration for AddsColumn { #[test] fn one_simple_migration() { - let dir = RandomTempPath::create_dir(); - let db_path = db_path(dir.as_path()); + let tempdir = TempDir::new("").unwrap(); + let db_path = db_path(tempdir.path()); let mut manager = Manager::new(Config::default()); make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]); let expected = map![vec![0x11] => vec![0x22], vec![1, 0x11] => vec![1, 0x22]]; @@ -124,8 +125,8 @@ fn one_simple_migration() { #[test] #[should_panic] fn no_migration_needed() { - let dir = RandomTempPath::create_dir(); - let db_path = db_path(dir.as_path()); + let tempdir = TempDir::new("").unwrap(); + let db_path = db_path(tempdir.path()); let mut manager = Manager::new(Config::default()); make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]); @@ -136,8 +137,8 @@ fn no_migration_needed() { #[test] #[should_panic] fn wrong_adding_order() { - let dir = RandomTempPath::create_dir(); - let db_path = db_path(dir.as_path()); + let tempdir = TempDir::new("").unwrap(); + let db_path = db_path(tempdir.path()); let mut manager = Manager::new(Config::default()); make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]); @@ -147,8 +148,8 @@ fn wrong_adding_order() { #[test] fn multiple_migrations() { - let dir = RandomTempPath::create_dir(); - let db_path = db_path(dir.as_path()); + let tempdir = TempDir::new("").unwrap(); + let db_path = db_path(tempdir.path()); let mut manager = Manager::new(Config::default()); make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]); let expected = map![vec![0x11] => vec![], vec![1, 0x11] => vec![]]; @@ -162,8 +163,8 @@ fn multiple_migrations() { #[test] fn second_migration() { - let dir = RandomTempPath::create_dir(); - let db_path = db_path(dir.as_path()); + let tempdir = TempDir::new("").unwrap(); + let db_path = db_path(tempdir.path()); let mut manager = Manager::new(Config::default()); make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]); let expected = map![vec![] => vec![], vec![1] => vec![]]; @@ -177,8 +178,8 @@ fn second_migration() { #[test] fn first_and_noop_migration() { - let dir = RandomTempPath::create_dir(); - let db_path = db_path(dir.as_path()); + let tempdir = TempDir::new("").unwrap(); + let db_path = db_path(tempdir.path()); let mut manager = Manager::new(Config::default()); make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]); let expected = map![vec![0x11] => vec![0x22], vec![1, 0x11] => vec![1, 0x22]]; @@ -191,8 +192,8 @@ fn first_and_noop_migration() { #[test] fn noop_and_second_migration() { - let dir = RandomTempPath::create_dir(); - let db_path = db_path(dir.as_path()); + let tempdir = TempDir::new("").unwrap(); + let db_path = db_path(tempdir.path()); let mut manager = Manager::new(Config::default()); make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]); let expected = map![vec![] => vec![], vec![1] => vec![]]; @@ -219,8 +220,8 @@ fn pre_columns() { let mut manager = Manager::new(Config::default()); manager.add_migration(AddsColumn).unwrap(); - let dir = RandomTempPath::create_dir(); - let db_path = db_path(dir.as_path()); + let tempdir = TempDir::new("").unwrap(); + let db_path = db_path(tempdir.path()); // this shouldn't fail to open the database even though it's one column // short of the one before it. @@ -238,8 +239,8 @@ fn change_columns() { version: 1, }).unwrap(); - let dir = RandomTempPath::create_dir(); - let db_path = db_path(dir.as_path()); + let tempdir = TempDir::new("").unwrap(); + let db_path = db_path(tempdir.path()); let new_path = manager.execute(&db_path, 0).unwrap(); diff --git a/util/network/Cargo.toml b/util/network/Cargo.toml index e989fb599..d7928b492 100644 --- a/util/network/Cargo.toml +++ b/util/network/Cargo.toml @@ -25,7 +25,6 @@ ethcore-io = { path = "../io" } ethcore-util = { path = ".." } ethcore-bigint = { path = "../bigint" } ethcore-bytes = { path = "../bytes" } -ethcore-devtools = { path = "../../devtools" } ethkey = { path = "../../ethkey" } ethcrypto = { path = "../../ethcrypto" } rlp = { path = "../rlp" } @@ -36,6 +35,10 @@ hash = { path = "../hash" } snappy = { path = "../snappy" } serde_json = "1.0" +[dev-dependencies] +ethcore-devtools = { path = "../../devtools" } +tempdir = "0.3" + [features] default = [] dev = ["clippy"] diff --git a/util/network/src/connection.rs b/util/network/src/connection.rs index 726952648..91e5e9889 100644 --- a/util/network/src/connection.rs +++ b/util/network/src/connection.rs @@ -512,7 +512,7 @@ mod tests { use mio::{Ready}; use std::collections::VecDeque; use ethcore_bytes::Bytes; - use devtools::*; + use devtools::TestSocket; use io::*; impl GenericSocket for TestSocket {} diff --git a/util/network/src/host.rs b/util/network/src/host.rs index 3d21bf5fe..cfecb5ed4 100644 --- a/util/network/src/host.rs +++ b/util/network/src/host.rs @@ -1267,11 +1267,12 @@ fn load_key(path: &Path) -> Option { #[test] fn key_save_load() { - use ::devtools::RandomTempPath; - let temp_path = RandomTempPath::create_dir(); + use tempdir::TempDir; + + let tempdir = TempDir::new("").unwrap(); let key = H256::random().into(); - save_key(temp_path.as_path(), &key); - let r = load_key(temp_path.as_path()); + save_key(tempdir.path(), &key); + let r = load_key(tempdir.path()); assert_eq!(key, r.unwrap()); } diff --git a/util/network/src/lib.rs b/util/network/src/lib.rs index 8f2ccb8d4..bc5459d5a 100644 --- a/util/network/src/lib.rs +++ b/util/network/src/lib.rs @@ -89,6 +89,9 @@ extern crate log; #[cfg(test)] extern crate ethcore_devtools as devtools; +#[cfg(test)] +extern crate tempdir; + mod host; mod connection; mod handshake; diff --git a/util/network/src/node_table.rs b/util/network/src/node_table.rs index 0b7725adc..94ad4d30a 100644 --- a/util/network/src/node_table.rs +++ b/util/network/src/node_table.rs @@ -374,7 +374,7 @@ mod tests { use std::net::{SocketAddr, SocketAddrV4, Ipv4Addr}; use bigint::hash::H512; use std::str::FromStr; - use devtools::*; + use tempdir::TempDir; use ipnetwork::IpNetwork; #[test] @@ -429,20 +429,20 @@ mod tests { #[test] fn table_save_load() { - let temp_path = RandomTempPath::create_dir(); + let tempdir = TempDir::new("").unwrap(); let node1 = Node::from_str("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").unwrap(); let node2 = Node::from_str("enode://b979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").unwrap(); let id1 = H512::from_str("a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap(); let id2 = H512::from_str("b979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap(); { - let mut table = NodeTable::new(Some(temp_path.as_path().to_str().unwrap().to_owned())); + let mut table = NodeTable::new(Some(tempdir.path().to_str().unwrap().to_owned())); table.add_node(node1); table.add_node(node2); table.note_failure(&id2); } { - let table = NodeTable::new(Some(temp_path.as_path().to_str().unwrap().to_owned())); + let table = NodeTable::new(Some(tempdir.path().to_str().unwrap().to_owned())); let r = table.nodes(IpFilter::default()); assert_eq!(r[0][..], id1[..]); assert_eq!(r[1][..], id2[..]); diff --git a/util/src/lib.rs b/util/src/lib.rs index d63119a5c..6d5db7938 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -92,7 +92,6 @@ extern crate rocksdb; extern crate env_logger; extern crate secp256k1; extern crate elastic_array; -extern crate ethcore_devtools as devtools; extern crate libc; extern crate target_info; extern crate ethcore_bigint as bigint;