Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

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

Merged
merged 2 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ethash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ crunchy = "0.1.0"
memmap = "0.6"
either = "1.0.0"

[dev-dependencies]
tempdir = "0.3"

[features]
benches = []
13 changes: 7 additions & 6 deletions ethash/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ fn calculate_dag_item(node_index: u32, cache: &[Node]) -> Node {
mod test {
use super::*;
use std::fs;
use tempdir::TempDir;

#[test]
fn test_get_cache_size() {
Expand Down Expand Up @@ -387,26 +388,26 @@ mod test {
];
let nonce = 0xd7b3ac70a301a249;
// difficulty = 0x085657254bd9u64;
let light = NodeCacheBuilder::new(None).light(&::std::env::temp_dir(), 486382);
let light = NodeCacheBuilder::new(None).light(TempDir::new("").unwrap().path(), 486382);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here ;)

let result = light_compute(&light, &hash, nonce);
assert_eq!(result.mix_hash[..], mix_hash[..]);
assert_eq!(result.value[..], boundary[..]);
}

#[test]
fn test_drop_old_data() {
let path = ::std::env::temp_dir();
let tempdir = TempDir::new("").unwrap();
let builder = NodeCacheBuilder::new(None);
let first = builder.light(&path, 0).to_file().unwrap().to_owned();
let first = builder.light(tempdir.path(), 0).to_file().unwrap().to_owned();

let second = builder.light(&path, ETHASH_EPOCH_LENGTH).to_file().unwrap().to_owned();
let second = builder.light(tempdir.path(), ETHASH_EPOCH_LENGTH).to_file().unwrap().to_owned();
assert!(fs::metadata(&first).is_ok());

let _ = builder.light(&path, ETHASH_EPOCH_LENGTH * 2).to_file();
let _ = builder.light(tempdir.path(), ETHASH_EPOCH_LENGTH * 2).to_file();
assert!(fs::metadata(&first).is_err());
assert!(fs::metadata(&second).is_ok());

let _ = builder.light(&path, ETHASH_EPOCH_LENGTH * 3).to_file();
let _ = builder.light(tempdir.path(), ETHASH_EPOCH_LENGTH * 3).to_file();
assert!(fs::metadata(&second).is_err());
}
}
7 changes: 6 additions & 1 deletion ethash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ extern crate crunchy;
#[macro_use]
extern crate log;

#[cfg(test)]
extern crate tempdir;

mod compute;
mod seed_compute;
mod cache;
Expand Down Expand Up @@ -135,7 +138,9 @@ impl EthashManager {

#[test]
fn test_lru() {
let ethash = EthashManager::new(&::std::env::temp_dir(), None);
use tempdir::TempDir;

let ethash = EthashManager::new(TempDir::new("").unwrap().path(), None);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't work as expected, cause TempDir is dropped before EthashManager, so the directory won't be cleaned up

let hash = [0u8; 32];
ethash.compute_light(1, &hash, 1);
ethash.compute_light(50000, &hash, 1);
Expand Down
1 change: 1 addition & 0 deletions ethcore/node_filter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ lru-cache = "0.1"
[dev-dependencies]
kvdb-memorydb = { path = "../../util/kvdb-memorydb" }
ethcore-io = { path = "../../util/io" }
tempdir = "0.3"
5 changes: 4 additions & 1 deletion ethcore/node_filter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ extern crate ethabi_contract;
extern crate ethcore_io as io;
#[cfg(test)]
extern crate kvdb_memorydb;
#[cfg(test)]
extern crate tempdir;
#[macro_use]
extern crate log;

Expand Down Expand Up @@ -126,13 +128,14 @@ mod test {
use network::{ConnectionDirection, ConnectionFilter, NodeId};
use io::IoChannel;
use super::NodeFilter;
use tempdir::TempDir;

/// Contract code: https://gist.github.com/arkpar/467dbcc73cbb85b0997a7a10ffa0695f
#[test]
fn node_filter() {
let contract_addr = Address::from_str("0000000000000000000000000000000000000005").unwrap();
let data = include_bytes!("../res/node_filter.json");
let spec = Spec::load(&::std::env::temp_dir(), &data[..]).unwrap();
let spec = Spec::load(&TempDir::new("").unwrap().path(), &data[..]).unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, TempDir will be dropped to early

let client_db = Arc::new(::kvdb_memorydb::create(::ethcore::db::NUM_COLUMNS.unwrap_or(0)));

let client = Client::new(
Expand Down
3 changes: 2 additions & 1 deletion ethcore/src/engines/basic_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,12 @@ 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")
Spec::load(&TempDir::new("").unwrap().path(), bytes).expect("invalid chain spec")
}

#[test]
Expand Down
15 changes: 8 additions & 7 deletions ethcore/src/ethereum/ethash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,10 @@ 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())
new_morden(&TempDir::new("").unwrap().path())
}

fn get_default_ethash_params() -> EthashParams {
Expand Down Expand Up @@ -776,7 +777,7 @@ 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 ethash = Ethash::new(&TempDir::new("").unwrap().path(), ethparams, machine, None);

let mut parent_header = Header::default();
parent_header.set_number(1000000);
Expand All @@ -794,7 +795,7 @@ 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 ethash = Ethash::new(&TempDir::new("").unwrap().path(), ethparams, machine, None);

let mut parent_header = Header::default();
parent_header.set_number(1500000);
Expand All @@ -815,7 +816,7 @@ mod tests {
ecip1010_pause_transition: 3000000,
..get_default_ethash_params()
};
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
let ethash = Ethash::new(&TempDir::new("").unwrap().path(), ethparams, machine, None);

let mut parent_header = Header::default();
parent_header.set_number(3500000);
Expand Down Expand Up @@ -849,7 +850,7 @@ mod tests {
ecip1010_continue_transition: 5000000,
..get_default_ethash_params()
};
let ethash = Ethash::new(&::std::env::temp_dir(), ethparams, machine, None);
let ethash = Ethash::new(&TempDir::new("").unwrap().path(), ethparams, machine, None);

let mut parent_header = Header::default();
parent_header.set_number(5000102);
Expand Down Expand Up @@ -895,7 +896,7 @@ 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 ethash = Ethash::new(&TempDir::new("").unwrap().path(), ethparams, machine, None);

let mut parent_header = Header::default();
parent_header.set_number(1000000);
Expand All @@ -913,7 +914,7 @@ 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 ethash = Ethash::new(&TempDir::new("").unwrap().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);
Expand Down
3 changes: 2 additions & 1 deletion ethcore/src/json_tests/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ 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()))
json_difficulty_test(json_data, ::ethereum::new_foundation(&TempDir::new("").unwrap().path()))
}

declare_test!{DifficultyTests_difficultyMainNetwork, "BasicTests/difficultyMainNetwork.json"}
Expand Down
3 changes: 2 additions & 1 deletion ethcore/src/snapshot/tests/proof_of_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,7 +60,7 @@ 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()
Spec::load(&TempDir::new("").unwrap().path(), &data[..]).unwrap()
}

// creates an account provider, filling it with accounts from all the given
Expand Down
3 changes: 2 additions & 1 deletion ethcore/src/spec/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,11 +832,12 @@ 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());
assert!(Spec::load(&TempDir::new("").unwrap().path(), &[] as &[u8]).is_err());
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion ethcore/src/tx_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -168,7 +169,7 @@ mod test {
}
"#;

let spec = Spec::load(&::std::env::temp_dir(), spec_data.as_bytes()).unwrap();
let spec = Spec::load(&TempDir::new("").unwrap().path(), spec_data.as_bytes()).unwrap();
let client_db = Arc::new(::kvdb_memorydb::create(::db::NUM_COLUMNS.unwrap_or(0)));

let client = Client::new(
Expand Down
1 change: 1 addition & 0 deletions evmbin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ vm = { path = "../ethcore/vm" }

[dev-dependencies]
pretty_assertions = "0.1"
tempdir = "0.3"

[features]
evm-debug = ["ethcore/evm-debug-tests"]
3 changes: 2 additions & 1 deletion evmbin/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ pub mod tests {
use std::sync::Arc;
use rustc_hex::FromHex;
use super::*;
use tempdir::TempDir;

pub fn run_test<T, I, F>(
informant: I,
Expand All @@ -191,7 +192,7 @@ pub mod tests {
params.code = Some(Arc::new(code.from_hex().unwrap()));
params.gas = gas.into();

let spec = ::ethcore::ethereum::new_foundation(&::std::env::temp_dir());
let spec = ::ethcore::ethereum::new_foundation(&TempDir::new("").unwrap().path());
let result = run_action(&spec, params, informant);
match result {
Ok(Success { traces, .. }) => {
Expand Down
3 changes: 3 additions & 0 deletions evmbin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ extern crate panic_hook;
#[macro_use]
extern crate pretty_assertions;

#[cfg(test)]
extern crate tempdir;

use std::sync::Arc;
use std::{fmt, fs};
use std::path::PathBuf;
Expand Down