Skip to content

Commit 48801a9

Browse files
winderfurszy
authored andcommitted
Use common SetDataDir method to create temp directory in tests.
1 parent 06464d3 commit 48801a9

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

src/test/dbwrapper_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup)
2424
BOOST_AUTO_TEST_CASE(dbwrapper)
2525
{
2626
{
27-
fs::path ph = fs::temp_directory_path() / fs::unique_path();
27+
fs::path ph = SetDataDir(std::string("dbwrapper"));
2828
CDBWrapper dbw(ph, (1 << 20), true, false);
2929
char key = 'k';
3030
uint256 in = GetRandHash();
@@ -114,7 +114,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
114114
BOOST_AUTO_TEST_CASE(dbwrapper_batch)
115115
{
116116
{
117-
fs::path ph = fs::temp_directory_path() / fs::unique_path();
117+
fs::path ph = SetDataDir(std::string("dbwrapper_batch"));
118118
CDBWrapper dbw(ph, (1 << 20), true, false);
119119

120120
char key = 'i';
@@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
149149
BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
150150
{
151151
{
152-
fs::path ph = fs::temp_directory_path() / fs::unique_path();
152+
fs::path ph = SetDataDir(std::string("dbwrapper_iterator"));
153153
CDBWrapper dbw(ph, (1 << 20), true, false);
154154

155155
// The two keys are intentionally chosen for ordering

src/test/test_pivx.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,38 @@ std::ostream& operator<<(std::ostream& os, const uint256& num)
3939
}
4040

4141
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
42+
: m_path_root(fs::temp_directory_path() / "test_pivx" / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
4243
{
43-
ECC_Start();
44-
SetupEnvironment();
45-
InitSignatureCache();
46-
fCheckBlockIndex = true;
47-
SelectParams(chainName);
48-
evoDb.reset(new CEvoDB(1 << 20, true, true));
49-
deterministicMNManager.reset(new CDeterministicMNManager(*evoDb));
44+
ECC_Start();
45+
SetupEnvironment();
46+
InitSignatureCache();
47+
fCheckBlockIndex = true;
48+
SelectParams(chainName);
49+
evoDb.reset(new CEvoDB(1 << 20, true, true));
50+
deterministicMNManager.reset(new CDeterministicMNManager(*evoDb));
5051
}
52+
5153
BasicTestingSetup::~BasicTestingSetup()
5254
{
53-
ECC_Stop();
54-
g_connman.reset();
55-
deterministicMNManager.reset();
56-
evoDb.reset();
55+
fs::remove_all(m_path_root);
56+
ECC_Stop();
57+
g_connman.reset();
58+
deterministicMNManager.reset();
59+
evoDb.reset();
60+
}
61+
62+
fs::path BasicTestingSetup::SetDataDir(const std::string& name)
63+
{
64+
fs::path ret = m_path_root / name;
65+
fs::create_directories(ret);
66+
gArgs.ForceSetArg("-datadir", ret.string());
67+
return ret;
5768
}
5869

5970
TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
6071
{
72+
SetDataDir("tempdir");
6173
ClearDatadirCache();
62-
pathTemp = fs::temp_directory_path() / strprintf("test_pivx_%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(100000)));
63-
fs::create_directories(pathTemp);
64-
gArgs.ForceSetArg("-datadir", pathTemp.string());
6574

6675
// Start the lightweight task scheduler thread
6776
CScheduler::Function serviceLoop = std::bind(&CScheduler::serviceQueue, &scheduler);
@@ -110,7 +119,6 @@ TestingSetup::~TestingSetup()
110119
delete pblocktree;
111120
delete zerocoinDB;
112121
delete pSporkDB;
113-
fs::remove_all(pathTemp);
114122
}
115123

116124
// Test chain only available on regtest

src/test/test_pivx.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ struct BasicTestingSetup {
3838

3939
BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
4040
~BasicTestingSetup();
41+
42+
fs::path SetDataDir(const std::string& name);
43+
44+
private:
45+
const fs::path m_path_root;
4146
};
4247

4348
/** Testing setup that configures a complete environment.
@@ -47,7 +52,6 @@ struct BasicTestingSetup {
4752
class CConnman;
4853
struct TestingSetup: public BasicTestingSetup {
4954
CCoinsViewDB *pcoinsdbview;
50-
fs::path pathTemp;
5155
boost::thread_group threadGroup;
5256
CConnman* connman;
5357
CScheduler scheduler;

src/wallet/test/wallet_tests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
412412
SetMockTime(KEY_TIME);
413413
coinbaseTxns.emplace_back(*CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
414414

415+
std::string backup_file = (SetDataDir("importwallet_rescan") / "wallet.backup").string();
416+
415417
// Import key into wallet and call dumpwallet to create backup file.
416418
{
417419
CWallet wallet;
@@ -423,7 +425,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
423425

424426
JSONRPCRequest request;
425427
request.params.setArray();
426-
request.params.push_back((pathTemp / "wallet.backup").string());
428+
request.params.push_back(backup_file);
427429
::pwalletMain = &wallet;
428430
::dumpwallet(request);
429431
}
@@ -435,7 +437,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
435437

436438
JSONRPCRequest request;
437439
request.params.setArray();
438-
request.params.push_back((pathTemp / "wallet.backup").string());
440+
request.params.push_back(backup_file);
439441
::pwalletMain = &wallet;
440442
::importwallet(request);
441443

0 commit comments

Comments
 (0)