Skip to content

Commit 075429a

Browse files
committed
Use common SetDataDir method to create temp directory in tests.
1 parent 43ae5ee commit 075429a

File tree

5 files changed

+42
-29
lines changed

5 files changed

+42
-29
lines changed

src/test/dbwrapper_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
2727
{
2828
// Perform tests both obfuscated and non-obfuscated.
2929
for (bool obfuscate : {false, true}) {
30-
fs::path ph = fs::temp_directory_path() / fs::unique_path();
30+
fs::path ph = SetDataDir(std::string("dbwrapper").append(obfuscate ? "_true" : "_false"));
3131
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
3232
char key = 'k';
3333
uint256 in = InsecureRand256();
@@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
4747
{
4848
// Perform tests both obfuscated and non-obfuscated.
4949
for (bool obfuscate : {false, true}) {
50-
fs::path ph = fs::temp_directory_path() / fs::unique_path();
50+
fs::path ph = SetDataDir(std::string("dbwrapper_batch").append(obfuscate ? "_true" : "_false"));
5151
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
5252

5353
char key = 'i';
@@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
8383
{
8484
// Perform tests both obfuscated and non-obfuscated.
8585
for (bool obfuscate : {false, true}) {
86-
fs::path ph = fs::temp_directory_path() / fs::unique_path();
86+
fs::path ph = SetDataDir(std::string("dbwrapper_iterator").append(obfuscate ? "_true" : "_false"));
8787
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
8888

8989
// The two keys are intentionally chosen for ordering
@@ -123,7 +123,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
123123
BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
124124
{
125125
// We're going to share this fs::path between two wrappers
126-
fs::path ph = fs::temp_directory_path() / fs::unique_path();
126+
fs::path ph = SetDataDir("existing_data_no_obfuscate");
127127
create_directories(ph);
128128

129129
// Set up a non-obfuscated wrapper to write some initial data.
@@ -164,7 +164,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
164164
BOOST_AUTO_TEST_CASE(existing_data_reindex)
165165
{
166166
// We're going to share this fs::path between two wrappers
167-
fs::path ph = fs::temp_directory_path() / fs::unique_path();
167+
fs::path ph = SetDataDir("existing_data_reindex");
168168
create_directories(ph);
169169

170170
// Set up a non-obfuscated wrapper to write some initial data.
@@ -199,7 +199,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
199199

200200
BOOST_AUTO_TEST_CASE(iterator_ordering)
201201
{
202-
fs::path ph = fs::temp_directory_path() / fs::unique_path();
202+
fs::path ph = SetDataDir("iterator_ordering");
203203
CDBWrapper dbw(ph, (1 << 20), true, false, false);
204204
for (int x=0x00; x<256; ++x) {
205205
uint8_t key = x;
@@ -277,7 +277,7 @@ BOOST_AUTO_TEST_CASE(iterator_string_ordering)
277277
{
278278
char buf[10];
279279

280-
fs::path ph = fs::temp_directory_path() / fs::unique_path();
280+
fs::path ph = SetDataDir("iterator_string_ordering");
281281
CDBWrapper dbw(ph, (1 << 20), true, false, false);
282282
for (int x=0x00; x<10; ++x) {
283283
for (int y = 0; y < 10; y++) {

src/test/test_bitcoin.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,43 @@ std::ostream& operator<<(std::ostream& os, const uint256& num)
4646
}
4747

4848
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
49+
: m_path_root(fs::temp_directory_path() / "test_bitcoin" / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
4950
{
50-
SHA256AutoDetect();
51-
RandomInit();
52-
ECC_Start();
53-
SetupEnvironment();
54-
SetupNetworking();
55-
InitSignatureCache();
56-
InitScriptExecutionCache();
57-
fCheckBlockIndex = true;
58-
SelectParams(chainName);
59-
noui_connect();
51+
SHA256AutoDetect();
52+
RandomInit();
53+
ECC_Start();
54+
SetupEnvironment();
55+
SetupNetworking();
56+
InitSignatureCache();
57+
InitScriptExecutionCache();
58+
fCheckBlockIndex = true;
59+
SelectParams(chainName);
60+
noui_connect();
6061
}
6162

6263
BasicTestingSetup::~BasicTestingSetup()
6364
{
64-
ECC_Stop();
65+
fs::remove_all(m_path_root);
66+
ECC_Stop();
67+
}
68+
69+
fs::path BasicTestingSetup::SetDataDir(const std::string& name)
70+
{
71+
fs::path ret = m_path_root / name;
72+
fs::create_directories(ret);
73+
gArgs.ForceSetArg("-datadir", ret.string());
74+
return ret;
6575
}
6676

6777
TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
6878
{
79+
SetDataDir("tempdir");
6980
const CChainParams& chainparams = Params();
7081
// Ideally we'd move all the RPC tests to the functional testing framework
7182
// instead of unit tests, but for now we need these here.
7283

7384
RegisterAllCoreRPCCommands(tableRPC);
7485
ClearDatadirCache();
75-
pathTemp = fs::temp_directory_path() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30)));
76-
fs::create_directories(pathTemp);
77-
gArgs.ForceSetArg("-datadir", pathTemp.string());
7886

7987
// We have to run a scheduler thread to prevent ActivateBestChain
8088
// from blocking due to queue overrun.
@@ -114,7 +122,6 @@ TestingSetup::~TestingSetup()
114122
pcoinsTip.reset();
115123
pcoinsdbview.reset();
116124
pblocktree.reset();
117-
fs::remove_all(pathTemp);
118125
}
119126

120127
TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)

src/test/test_bitcoin.h

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

4646
explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
4747
~BasicTestingSetup();
48+
49+
fs::path SetDataDir(const std::string& name);
50+
51+
private:
52+
const fs::path m_path_root;
4853
};
4954

5055
/** Testing setup that configures a complete environment.
@@ -59,7 +64,6 @@ struct CConnmanTest {
5964

6065
class PeerLogicValidation;
6166
struct TestingSetup: public BasicTestingSetup {
62-
fs::path pathTemp;
6367
boost::thread_group threadGroup;
6468
CConnman* connman;
6569
CScheduler scheduler;

src/test/util_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ static void TestOtherProcess(fs::path dirname, std::string lockname, int fd)
11001100

11011101
BOOST_AUTO_TEST_CASE(test_LockDirectory)
11021102
{
1103-
fs::path dirname = fs::temp_directory_path() / fs::unique_path();
1103+
fs::path dirname = SetDataDir("test_LockDirectory") / fs::unique_path();
11041104
const std::string lockname = ".lock";
11051105
#ifndef WIN32
11061106
// Revert SIGCHLD to default, otherwise boost.test will catch and fail on
@@ -1188,12 +1188,12 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)
11881188

11891189
BOOST_AUTO_TEST_CASE(test_DirIsWritable)
11901190
{
1191-
// Should be able to write to the system tmp dir.
1192-
fs::path tmpdirname = fs::temp_directory_path();
1191+
// Should be able to write to the data dir.
1192+
fs::path tmpdirname = SetDataDir("test_DirIsWritable");
11931193
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true);
11941194

11951195
// Should not be able to write to a non-existent dir.
1196-
tmpdirname = fs::temp_directory_path() / fs::unique_path();
1196+
tmpdirname = tmpdirname / fs::unique_path();
11971197
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), false);
11981198

11991199
fs::create_directory(tmpdirname);

src/wallet/test/wallet_tests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
130130

131131
LOCK(cs_main);
132132

133+
std::string backup_file = (SetDataDir("importwallet_rescan") / "wallet.backup").string();
134+
133135
// Import key into wallet and call dumpwallet to create backup file.
134136
{
135137
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>("dummy", WalletDatabase::CreateDummy());
@@ -139,7 +141,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
139141

140142
JSONRPCRequest request;
141143
request.params.setArray();
142-
request.params.push_back((pathTemp / "wallet.backup").string());
144+
request.params.push_back(backup_file);
143145
AddWallet(wallet);
144146
::dumpwallet(request);
145147
RemoveWallet(wallet);
@@ -152,7 +154,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
152154

153155
JSONRPCRequest request;
154156
request.params.setArray();
155-
request.params.push_back((pathTemp / "wallet.backup").string());
157+
request.params.push_back(backup_file);
156158
AddWallet(wallet);
157159
::importwallet(request);
158160
RemoveWallet(wallet);

0 commit comments

Comments
 (0)