Replace std::env::temp_dir with tempdir in tests (#8103)

* std::env::temp_dir -> tempdir in test context

* fix lifetime issue so tempdir removes tmpfiles
This commit is contained in:
Niklas Adolfsson
2018-03-14 12:26:20 +01:00
committed by Marek Kotewicz
parent 7e948a088f
commit 23fc5517b5
15 changed files with 65 additions and 21 deletions

View File

@@ -204,11 +204,13 @@ mod tests {
use header::Header;
use spec::Spec;
use engines::Seal;
use tempdir::TempDir;
/// Create a new test chain spec with `BasicAuthority` consensus engine.
fn new_test_authority() -> Spec {
let bytes: &[u8] = include_bytes!("../../res/basic_authority.json");
Spec::load(&::std::env::temp_dir(), bytes).expect("invalid chain spec")
let tempdir = TempDir::new("").unwrap();
Spec::load(&tempdir.path(), bytes).expect("invalid chain spec")
}
#[test]

View File

@@ -495,9 +495,11 @@ mod tests {
use super::super::{new_morden, new_mcip3_test, new_homestead_test_machine};
use super::{Ethash, EthashParams, ecip1017_eras_block_reward};
use rlp;
use tempdir::TempDir;
fn test_spec() -> Spec {
new_morden(&::std::env::temp_dir())
let tempdir = TempDir::new("").unwrap();
new_morden(&tempdir.path())
}
fn get_default_ethash_params() -> EthashParams {
@@ -776,7 +778,8 @@ mod tests {
fn difficulty_frontier() {
let machine = new_homestead_test_machine();
let ethparams = get_default_ethash_params();
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
let tempdir = TempDir::new("").unwrap();
let ethash = Ethash::new(tempdir.path(), ethparams, machine, None);
let mut parent_header = Header::default();
parent_header.set_number(1000000);
@@ -794,7 +797,8 @@ mod tests {
fn difficulty_homestead() {
let machine = new_homestead_test_machine();
let ethparams = get_default_ethash_params();
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
let tempdir = TempDir::new("").unwrap();
let ethash = Ethash::new(tempdir.path(), ethparams, machine, None);
let mut parent_header = Header::default();
parent_header.set_number(1500000);
@@ -815,7 +819,8 @@ mod tests {
ecip1010_pause_transition: 3000000,
..get_default_ethash_params()
};
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
let tempdir = TempDir::new("").unwrap();
let ethash = Ethash::new(tempdir.path(), ethparams, machine, None);
let mut parent_header = Header::default();
parent_header.set_number(3500000);
@@ -849,7 +854,8 @@ mod tests {
ecip1010_continue_transition: 5000000,
..get_default_ethash_params()
};
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
let tempdir = TempDir::new("").unwrap();
let ethash = Ethash::new(tempdir.path(), ethparams, machine, None);
let mut parent_header = Header::default();
parent_header.set_number(5000102);
@@ -895,7 +901,8 @@ mod tests {
fn difficulty_max_timestamp() {
let machine = new_homestead_test_machine();
let ethparams = get_default_ethash_params();
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
let tempdir = TempDir::new("").unwrap();
let ethash = Ethash::new(tempdir.path(), ethparams, machine, None);
let mut parent_header = Header::default();
parent_header.set_number(1000000);
@@ -913,7 +920,8 @@ mod tests {
fn test_extra_info() {
let machine = new_homestead_test_machine();
let ethparams = get_default_ethash_params();
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
let tempdir = TempDir::new("").unwrap();
let ethash = Ethash::new(tempdir.path(), ethparams, machine, None);
let mut header = Header::default();
header.set_seal(vec![rlp::encode(&H256::from("b251bd2e0283d0658f2cadfdc8ca619b5de94eca5742725e2e757dd13ed7503d")).into_vec(), rlp::encode(&H64::zero()).into_vec()]);
let info = ethash.extra_info(&header);

View File

@@ -59,9 +59,11 @@ mod difficulty_test_byzantium {
mod difficulty_test_foundation {
use super::json_difficulty_test;
use tempdir::TempDir;
fn do_json_test(json_data: &[u8]) -> Vec<String> {
json_difficulty_test(json_data, ::ethereum::new_foundation(&::std::env::temp_dir()))
let tempdir = TempDir::new("").unwrap();
json_difficulty_test(json_data, ::ethereum::new_foundation(&tempdir.path()))
}
declare_test!{DifficultyTests_difficultyMainNetwork, "BasicTests/difficultyMainNetwork.json"}

View File

@@ -27,6 +27,7 @@ use snapshot::tests::helpers as snapshot_helpers;
use spec::Spec;
use tests::helpers;
use transaction::{Transaction, Action, SignedTransaction};
use tempdir::TempDir;
use ethereum_types::Address;
use kvdb_memorydb;
@@ -59,7 +60,8 @@ lazy_static! {
/// `test_validator_set::ValidatorSet` provides a native wrapper for the ABi.
fn spec_fixed_to_contract() -> Spec {
let data = include_bytes!("test_validator_contract.json");
Spec::load(&::std::env::temp_dir(), &data[..]).unwrap()
let tempdir = TempDir::new("").unwrap();
Spec::load(&tempdir.path(), &data[..]).unwrap()
}
// creates an account provider, filling it with accounts from all the given

View File

@@ -832,11 +832,13 @@ mod tests {
use state::State;
use tests::helpers::get_temp_state_db;
use views::BlockView;
use tempdir::TempDir;
// https://github.com/paritytech/parity/issues/1840
#[test]
fn test_load_empty() {
assert!(Spec::load(&::std::env::temp_dir(), &[] as &[u8]).is_err());
let tempdir = TempDir::new("").unwrap();
assert!(Spec::load(&tempdir.path(), &[] as &[u8]).is_err());
}
#[test]

View File

@@ -118,6 +118,7 @@ mod test {
use ethkey::{Secret, KeyPair};
use super::TransactionFilter;
use transaction::{Transaction, Action};
use tempdir::TempDir;
/// Contract code: https://gist.github.com/arkpar/38a87cb50165b7e683585eec71acb05a
#[test]
@@ -168,7 +169,8 @@ mod test {
}
"#;
let spec = Spec::load(&::std::env::temp_dir(), spec_data.as_bytes()).unwrap();
let tempdir = TempDir::new("").unwrap();
let spec = Spec::load(&tempdir.path(), spec_data.as_bytes()).unwrap();
let client_db = Arc::new(::kvdb_memorydb::create(::db::NUM_COLUMNS.unwrap_or(0)));
let client = Client::new(