From 8a450592b49e92de42cc151f7de86c41dde2e95d Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 27 Jan 2016 19:41:50 +0400 Subject: [PATCH] refactoring of temp path spawning in several places --- src/blockchain.rs | 22 ++++++++-------------- src/tests/chain.rs | 8 +++----- src/tests/mod.rs | 2 +- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/blockchain.rs b/src/blockchain.rs index fb3755ff2..753b83587 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -618,20 +618,18 @@ impl BlockChain { #[cfg(test)] mod tests { - use std::env; use std::str::FromStr; use rustc_serialize::hex::FromHex; use util::hash::*; use blockchain::*; + use tests::helpers::*; #[test] fn valid_tests_extra32() { let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0925002c3260b44e44c3edebad1cc442142b03020209df1ab8bb86752edbd2cd7a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a0363659b251bf8b819179874c8cce7b9b983d7f3704cbb58a3b334431f7032871889032d09c281e1236c0c0".from_hex().unwrap(); - let mut dir = env::temp_dir(); - dir.push(H32::random().hex()); - - let bc = BlockChain::new(&genesis, &dir); + let temp = RandomTempPath::new(); + let bc = BlockChain::new(&genesis, temp.as_path()); let genesis_hash = H256::from_str("3caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942").unwrap(); @@ -674,10 +672,8 @@ mod tests { // b3a is a part of canon chain, whereas b3b is part of sidechain let best_block_hash = H256::from_str("c208f88c9f5bf7e00840439742c12e5226d9752981f3ec0521bdcb6dd08af277").unwrap(); - let mut dir = env::temp_dir(); - dir.push(H32::random().hex()); - - let bc = BlockChain::new(&genesis, &dir); + let temp = RandomTempPath::new(); + let bc = BlockChain::new(&genesis, temp.as_path()); bc.insert_block(&b1); bc.insert_block(&b2); bc.insert_block(&b3a); @@ -754,18 +750,16 @@ mod tests { let genesis_hash = H256::from_str("5716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2").unwrap(); let b1_hash = H256::from_str("437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4f").unwrap(); - let mut dir = env::temp_dir(); - dir.push(H32::random().hex()); - + let temp = RandomTempPath::new(); { - let bc = BlockChain::new(&genesis, &dir); + let bc = BlockChain::new(&genesis, temp.as_path()); assert_eq!(bc.best_block_hash(), genesis_hash); bc.insert_block(&b1); assert_eq!(bc.best_block_hash(), b1_hash); } { - let bc = BlockChain::new(&genesis, &dir); + let bc = BlockChain::new(&genesis, temp.as_path()); assert_eq!(bc.best_block_hash(), b1_hash); } } diff --git a/src/tests/chain.rs b/src/tests/chain.rs index 42b4ee78a..295cd3e91 100644 --- a/src/tests/chain.rs +++ b/src/tests/chain.rs @@ -1,9 +1,9 @@ -use std::env; use super::test_common::*; use client::{BlockChainClient,Client}; use pod_state::*; use block::Block; use ethereum; +use super::helpers::*; fn do_json_test(json_data: &[u8]) -> Vec { let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid"); @@ -28,10 +28,9 @@ fn do_json_test(json_data: &[u8]) -> Vec { spec.overwrite_genesis(test.find("genesisBlockHeader").unwrap()); assert!(spec.is_state_root_valid()); - let mut dir = env::temp_dir(); - dir.push(H32::random().hex()); + let temp = RandomTempPath::new(); { - let client = Client::new(spec, &dir, IoChannel::disconnected()).unwrap(); + let client = Client::new(spec, temp.as_path(), IoChannel::disconnected()).unwrap(); for (b, is_valid) in blocks.into_iter() { if Block::is_good(&b) { let _ = client.import_block(b.clone()); @@ -42,7 +41,6 @@ fn do_json_test(json_data: &[u8]) -> Vec { } fail_unless(client.chain_info().best_block_hash == H256::from_json(&test["lastblockhash"])); } - fs::remove_dir_all(&dir).unwrap(); } if !fail { flush(format!("ok\n")); diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 17da72d77..19e8e9144 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -6,4 +6,4 @@ mod executive; mod state; mod client; mod chain; -mod helpers; \ No newline at end of file +pub mod helpers; \ No newline at end of file