55"""Test various command line arguments and configuration file parameters."""
66
77import os
8- import pathlib
8+ from pathlib import Path
99import re
1010import sys
1111import tempfile
@@ -39,8 +39,8 @@ def test_config_file_parser(self):
3939 extra_args = [f'-conf={ bad_conf_file_path } ' ],
4040 expected_msg = conf_in_config_file_err ,
4141 )
42- inc_conf_file_path = os . path . join ( self .nodes [0 ].datadir , 'include.conf' )
43- with open (os . path . join ( self .nodes [0 ].datadir , 'bitcoin.conf' ) , 'a' , encoding = 'utf-8' ) as conf :
42+ inc_conf_file_path = self .nodes [0 ].datadir_path / 'include.conf'
43+ with open (self .nodes [0 ].datadir_path / 'bitcoin.conf' , 'a' , encoding = 'utf-8' ) as conf :
4444 conf .write (f'includeconf={ inc_conf_file_path } \n ' )
4545 with open (inc_conf_file_path , 'w' , encoding = 'utf-8' ) as conf :
4646 conf .write ('conf=some.conf\n ' )
@@ -97,8 +97,8 @@ def test_config_file_parser(self):
9797 conf .write ('server=1\n rpcuser=someuser\n [main]\n rpcpassword=some#pass' )
9898 self .nodes [0 ].assert_start_raises_init_error (expected_msg = 'Error: Error reading configuration file: parse error on line 4, using # in rpcpassword can be ambiguous and should be avoided' )
9999
100- inc_conf_file2_path = os . path . join ( self .nodes [0 ].datadir , 'include2.conf' )
101- with open (os . path . join ( self .nodes [0 ].datadir , 'bitcoin.conf' ) , 'a' , encoding = 'utf-8' ) as conf :
100+ inc_conf_file2_path = self .nodes [0 ].datadir_path / 'include2.conf'
101+ with open (self .nodes [0 ].datadir_path / 'bitcoin.conf' , 'a' , encoding = 'utf-8' ) as conf :
102102 conf .write (f'includeconf={ inc_conf_file2_path } \n ' )
103103
104104 with open (inc_conf_file_path , 'w' , encoding = 'utf-8' ) as conf :
@@ -123,15 +123,15 @@ def test_config_file_log(self):
123123
124124 # Create a temporary directory that will be treated as the default data
125125 # directory by bitcoind.
126- env , default_datadir = util .get_temp_default_datadir (pathlib . Path (self .options .tmpdir , "test_config_file_log" ))
126+ env , default_datadir = util .get_temp_default_datadir (Path (self .options .tmpdir , "test_config_file_log" ))
127127 default_datadir .mkdir (parents = True )
128128
129129 # Write a bitcoin.conf file in the default data directory containing a
130130 # datadir= line pointing at the node datadir.
131131 node = self .nodes [0 ]
132- conf_text = pathlib . Path ( node .bitcoinconf ) .read_text ()
132+ conf_text = node .bitcoinconf .read_text ()
133133 conf_path = default_datadir / "bitcoin.conf"
134- conf_path .write_text (f"datadir={ node .datadir } \n { conf_text } " )
134+ conf_path .write_text (f"datadir={ node .datadir_path } \n { conf_text } " )
135135
136136 # Drop the node -datadir= argument during this test, because if it is
137137 # specified it would take precedence over the datadir setting in the
@@ -218,7 +218,8 @@ def test_networkactive(self):
218218
219219 def test_seed_peers (self ):
220220 self .log .info ('Test seed peers' )
221- default_data_dir = self .nodes [0 ].datadir
221+ default_data_dir = self .nodes [0 ].datadir_path
222+ peer_dat = default_data_dir / 'peers.dat'
222223 # Only regtest has no fixed seeds. To avoid connections to random
223224 # nodes, regtest is the only network where it is safe to enable
224225 # -fixedseeds in tests
@@ -229,7 +230,7 @@ def test_seed_peers(self):
229230 # We expect the node will use DNS Seeds, but Regtest mode does not have
230231 # any valid DNS seeds. So after 60 seconds, the node should fallback to
231232 # fixed seeds
232- assert not os . path . exists (os . path . join ( default_data_dir , "peers.dat" ) )
233+ assert not peer_dat . exists ()
233234 start = int (time .time ())
234235 with self .nodes [0 ].assert_debug_log (
235236 expected_msgs = [
@@ -248,7 +249,7 @@ def test_seed_peers(self):
248249
249250 # No peers.dat exists and -dnsseed=0
250251 # We expect the node will fallback immediately to fixed seeds
251- assert not os . path . exists (os . path . join ( default_data_dir , "peers.dat" ) )
252+ assert not peer_dat . exists ()
252253 with self .nodes [0 ].assert_debug_log (expected_msgs = [
253254 "Loaded 0 addresses from peers.dat" ,
254255 "DNS seeding disabled" ,
@@ -260,7 +261,7 @@ def test_seed_peers(self):
260261
261262 # No peers.dat exists and dns seeds are disabled.
262263 # We expect the node will not add fixed seeds when explicitly disabled.
263- assert not os . path . exists (os . path . join ( default_data_dir , "peers.dat" ) )
264+ assert not peer_dat . exists ()
264265 with self .nodes [0 ].assert_debug_log (expected_msgs = [
265266 "Loaded 0 addresses from peers.dat" ,
266267 "DNS seeding disabled" ,
@@ -271,7 +272,7 @@ def test_seed_peers(self):
271272
272273 # No peers.dat exists and -dnsseed=0, but a -addnode is provided
273274 # We expect the node will allow 60 seconds prior to using fixed seeds
274- assert not os . path . exists (os . path . join ( default_data_dir , "peers.dat" ) )
275+ assert not peer_dat . exists ()
275276 start = int (time .time ())
276277 with self .nodes [0 ].assert_debug_log (
277278 expected_msgs = [
@@ -323,16 +324,16 @@ def test_ignored_conf(self):
323324 'because a conflicting -conf file argument is passed.' )
324325 node = self .nodes [0 ]
325326 with tempfile .NamedTemporaryFile (dir = self .options .tmpdir , mode = "wt" , delete = False ) as temp_conf :
326- temp_conf .write (f"datadir={ node .datadir } \n " )
327+ temp_conf .write (f"datadir={ node .datadir_path } \n " )
327328 node .assert_start_raises_init_error ([f"-conf={ temp_conf .name } " ], re .escape (
328- f'Error: Data directory "{ node .datadir } " contains a "bitcoin.conf" file which is ignored, because a '
329+ f'Error: Data directory "{ node .datadir_path } " contains a "bitcoin.conf" file which is ignored, because a '
329330 f'different configuration file "{ temp_conf .name } " from command line argument "-conf={ temp_conf .name } " '
330331 f'is being used instead.' ) + r"[\s\S]*" , match = ErrorMatch .FULL_REGEX )
331332
332333 # Test that passing a redundant -conf command line argument pointing to
333334 # the same bitcoin.conf that would be loaded anyway does not trigger an
334335 # error.
335- self .start_node (0 , [f'-conf={ node .datadir } /bitcoin.conf' ])
336+ self .start_node (0 , [f'-conf={ node .datadir_path } /bitcoin.conf' ])
336337 self .stop_node (0 )
337338
338339 def test_ignored_default_conf (self ):
@@ -346,23 +347,23 @@ def test_ignored_default_conf(self):
346347
347348 # Create a temporary directory that will be treated as the default data
348349 # directory by bitcoind.
349- env , default_datadir = util .get_temp_default_datadir (pathlib . Path (self .options .tmpdir , "home" ))
350+ env , default_datadir = util .get_temp_default_datadir (Path (self .options .tmpdir , "home" ))
350351 default_datadir .mkdir (parents = True )
351352
352353 # Write a bitcoin.conf file in the default data directory containing a
353354 # datadir= line pointing at the node datadir. This will trigger a
354355 # startup error because the node datadir contains a different
355356 # bitcoin.conf that would be ignored.
356357 node = self .nodes [0 ]
357- (default_datadir / "bitcoin.conf" ).write_text (f"datadir={ node .datadir } \n " )
358+ (default_datadir / "bitcoin.conf" ).write_text (f"datadir={ node .datadir_path } \n " )
358359
359360 # Drop the node -datadir= argument during this test, because if it is
360361 # specified it would take precedence over the datadir setting in the
361362 # config file.
362363 node_args = node .args
363364 node .args = [arg for arg in node .args if not arg .startswith ("-datadir=" )]
364365 node .assert_start_raises_init_error ([], re .escape (
365- f'Error: Data directory "{ node .datadir } " contains a "bitcoin.conf" file which is ignored, because a '
366+ f'Error: Data directory "{ node .datadir_path } " contains a "bitcoin.conf" file which is ignored, because a '
366367 f'different configuration file "{ default_datadir } /bitcoin.conf" from data directory "{ default_datadir } " '
367368 f'is being used instead.' ) + r"[\s\S]*" , env = env , match = ErrorMatch .FULL_REGEX )
368369 node .args = node_args
@@ -392,16 +393,16 @@ def run_test(self):
392393 # Remove the -datadir argument so it doesn't override the config file
393394 self .nodes [0 ].args = [arg for arg in self .nodes [0 ].args if not arg .startswith ("-datadir" )]
394395
395- default_data_dir = self .nodes [0 ].datadir
396- new_data_dir = os . path . join ( default_data_dir , 'newdatadir' )
397- new_data_dir_2 = os . path . join ( default_data_dir , 'newdatadir2' )
396+ default_data_dir = self .nodes [0 ].datadir_path
397+ new_data_dir = default_data_dir / 'newdatadir'
398+ new_data_dir_2 = default_data_dir / 'newdatadir2'
398399
399400 # Check that using -datadir argument on non-existent directory fails
400- self .nodes [0 ].datadir = new_data_dir
401+ self .nodes [0 ].datadir_path = new_data_dir
401402 self .nodes [0 ].assert_start_raises_init_error ([f'-datadir={ new_data_dir } ' ], f'Error: Specified data directory "{ new_data_dir } " does not exist.' )
402403
403404 # Check that using non-existent datadir in conf file fails
404- conf_file = os . path . join ( default_data_dir , "bitcoin.conf" )
405+ conf_file = default_data_dir / "bitcoin.conf"
405406
406407 # datadir needs to be set before [chain] section
407408 with open (conf_file , encoding = 'utf8' ) as f :
@@ -413,20 +414,20 @@ def run_test(self):
413414 self .nodes [0 ].assert_start_raises_init_error ([f'-conf={ conf_file } ' ], f'Error: Error reading configuration file: specified data directory "{ new_data_dir } " does not exist.' )
414415
415416 # Check that an explicitly specified config file that cannot be opened fails
416- none_existent_conf_file = os . path . join ( default_data_dir , "none_existent_bitcoin.conf" )
417- self .nodes [0 ].assert_start_raises_init_error (['-conf=' + none_existent_conf_file ], 'Error: Error reading configuration file: specified config file "' + none_existent_conf_file + '" could not be opened.' )
417+ none_existent_conf_file = default_data_dir / "none_existent_bitcoin.conf"
418+ self .nodes [0 ].assert_start_raises_init_error (['-conf=' + f' { none_existent_conf_file } ' ], 'Error: Error reading configuration file: specified config file "' + f' { none_existent_conf_file } ' + '" could not be opened.' )
418419
419420 # Create the directory and ensure the config file now works
420- os .mkdir (new_data_dir )
421+ new_data_dir .mkdir ()
421422 self .start_node (0 , [f'-conf={ conf_file } ' ])
422423 self .stop_node (0 )
423- assert os . path . exists ( os . path . join ( new_data_dir , self .chain , 'blocks' ))
424+ assert ( new_data_dir / self .chain / 'blocks' ). exists ( )
424425
425426 # Ensure command line argument overrides datadir in conf
426- os .mkdir (new_data_dir_2 )
427- self .nodes [0 ].datadir = new_data_dir_2
427+ new_data_dir_2 .mkdir ()
428+ self .nodes [0 ].datadir_path = new_data_dir_2
428429 self .start_node (0 , [f'-datadir={ new_data_dir_2 } ' , f'-conf={ conf_file } ' ])
429- assert os . path . exists ( os . path . join ( new_data_dir_2 , self .chain , 'blocks' ))
430+ assert ( new_data_dir_2 / self .chain / 'blocks' ). exists ( )
430431
431432
432433if __name__ == '__main__' :
0 commit comments