From bfa0bd632a7ce5d04005e20cba79abb32aec8da8 Mon Sep 17 00:00:00 2001 From: ns-xvrn Date: Sat, 2 Sep 2023 01:09:43 -0400 Subject: test: Use pathlib over os.path #28362 revert netutil chgs py3.8 compliant fixes based on PR review --- test/functional/wallet_backup.py | 63 ++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 38 deletions(-) (limited to 'test/functional/wallet_backup.py') diff --git a/test/functional/wallet_backup.py b/test/functional/wallet_backup.py index 9f6f54c7a6..eb3e0ae728 100755 --- a/test/functional/wallet_backup.py +++ b/test/functional/wallet_backup.py @@ -109,36 +109,35 @@ class WalletBackupTest(BitcoinTestFramework): self.stop_node(2) def erase_three(self): - os.remove(os.path.join(self.nodes[0].wallets_path, self.default_wallet_name, self.wallet_data_filename)) - os.remove(os.path.join(self.nodes[1].wallets_path, self.default_wallet_name, self.wallet_data_filename)) - os.remove(os.path.join(self.nodes[2].wallets_path, self.default_wallet_name, self.wallet_data_filename)) + for node_num in range(3): + (self.nodes[node_num].wallets_path / self.default_wallet_name / self.wallet_data_filename).unlink() def restore_invalid_wallet(self): node = self.nodes[3] - invalid_wallet_file = os.path.join(self.nodes[0].datadir, 'invalid_wallet_file.bak') + invalid_wallet_file = self.nodes[0].datadir_path / 'invalid_wallet_file.bak' open(invalid_wallet_file, 'a', encoding="utf8").write('invald wallet') wallet_name = "res0" - not_created_wallet_file = os.path.join(node.wallets_path, wallet_name) + not_created_wallet_file = node.wallets_path / wallet_name error_message = "Wallet file verification failed. Failed to load database path '{}'. Data is not in recognized format.".format(not_created_wallet_file) assert_raises_rpc_error(-18, error_message, node.restorewallet, wallet_name, invalid_wallet_file) - assert not os.path.exists(not_created_wallet_file) + assert not not_created_wallet_file.exists() def restore_nonexistent_wallet(self): node = self.nodes[3] - nonexistent_wallet_file = os.path.join(self.nodes[0].datadir, 'nonexistent_wallet.bak') + nonexistent_wallet_file = self.nodes[0].datadir_path / 'nonexistent_wallet.bak' wallet_name = "res0" assert_raises_rpc_error(-8, "Backup file does not exist", node.restorewallet, wallet_name, nonexistent_wallet_file) - not_created_wallet_file = os.path.join(node.wallets_path, wallet_name) - assert not os.path.exists(not_created_wallet_file) + not_created_wallet_file = node.wallets_path / wallet_name + assert not not_created_wallet_file.exists() def restore_wallet_existent_name(self): node = self.nodes[3] - backup_file = os.path.join(self.nodes[0].datadir, 'wallet.bak') + backup_file = self.nodes[0].datadir_path / 'wallet.bak' wallet_name = "res0" - wallet_file = os.path.join(node.wallets_path, wallet_name) + wallet_file = node.wallets_path / wallet_name error_message = "Failed to create database path '{}'. Database already exists.".format(wallet_file) assert_raises_rpc_error(-36, error_message, node.restorewallet, wallet_name, backup_file) - assert os.path.exists(wallet_file) + assert wallet_file.exists() def run_test(self): self.log.info("Generating initial blockchain") @@ -159,14 +158,12 @@ class WalletBackupTest(BitcoinTestFramework): self.log.info("Backing up") - self.nodes[0].backupwallet(os.path.join(self.nodes[0].datadir, 'wallet.bak')) - self.nodes[1].backupwallet(os.path.join(self.nodes[1].datadir, 'wallet.bak')) - self.nodes[2].backupwallet(os.path.join(self.nodes[2].datadir, 'wallet.bak')) + for node_num in range(3): + self.nodes[node_num].backupwallet(self.nodes[node_num].datadir_path / 'wallet.bak') if not self.options.descriptors: - self.nodes[0].dumpwallet(os.path.join(self.nodes[0].datadir, 'wallet.dump')) - self.nodes[1].dumpwallet(os.path.join(self.nodes[1].datadir, 'wallet.dump')) - self.nodes[2].dumpwallet(os.path.join(self.nodes[2].datadir, 'wallet.dump')) + for node_num in range(3): + self.nodes[node_num].dumpwallet(self.nodes[node_num].datadir_path / 'wallet.dump') self.log.info("More transactions") for _ in range(5): @@ -193,17 +190,13 @@ class WalletBackupTest(BitcoinTestFramework): self.restore_invalid_wallet() self.restore_nonexistent_wallet() - backup_file_0 = os.path.join(self.nodes[0].datadir, 'wallet.bak') - backup_file_1 = os.path.join(self.nodes[1].datadir, 'wallet.bak') - backup_file_2 = os.path.join(self.nodes[2].datadir, 'wallet.bak') - - self.nodes[3].restorewallet("res0", backup_file_0) - self.nodes[3].restorewallet("res1", backup_file_1) - self.nodes[3].restorewallet("res2", backup_file_2) + backup_files = [] + for node_num in range(3): + backup_files.append(self.nodes[node_num].datadir_path / 'wallet.bak') - assert os.path.exists(os.path.join(self.nodes[3].wallets_path, "res0")) - assert os.path.exists(os.path.join(self.nodes[3].wallets_path, "res1")) - assert os.path.exists(os.path.join(self.nodes[3].wallets_path, "res2")) + for idx, backup_file in enumerate(backup_files): + self.nodes[3].restorewallet(f'res{idx}', backup_file) + assert (self.nodes[3].wallets_path / f'res{idx}').exists() res0_rpc = self.nodes[3].get_wallet_rpc("res0") res1_rpc = self.nodes[3].get_wallet_rpc("res1") @@ -221,22 +214,16 @@ class WalletBackupTest(BitcoinTestFramework): self.erase_three() #start node2 with no chain - shutil.rmtree(os.path.join(self.nodes[2].blocks_path)) - shutil.rmtree(os.path.join(self.nodes[2].chain_path, 'chainstate')) + shutil.rmtree(self.nodes[2].blocks_path) + shutil.rmtree(self.nodes[2].chain_path / 'chainstate') self.start_three(["-nowallet"]) # Create new wallets for the three nodes. # We will use this empty wallets to test the 'importwallet()' RPC command below. for node_num in range(3): self.nodes[node_num].createwallet(wallet_name=self.default_wallet_name, descriptors=self.options.descriptors, load_on_startup=True) - - assert_equal(self.nodes[0].getbalance(), 0) - assert_equal(self.nodes[1].getbalance(), 0) - assert_equal(self.nodes[2].getbalance(), 0) - - self.nodes[0].importwallet(os.path.join(self.nodes[0].datadir, 'wallet.dump')) - self.nodes[1].importwallet(os.path.join(self.nodes[1].datadir, 'wallet.dump')) - self.nodes[2].importwallet(os.path.join(self.nodes[2].datadir, 'wallet.dump')) + assert_equal(self.nodes[node_num].getbalance(), 0) + self.nodes[node_num].importwallet(self.nodes[node_num].datadir_path / 'wallet.dump') self.sync_blocks() -- cgit v1.2.3