Repository: bitcoin/bitcoin
File: test/functional/wallet_migration.py (Python)

Code before:
        assert_raises_rpc_error(-4, "Failed to create database", self.master_node.migratewallet, wallet_name)
        self.master_node.setmocktime(0)

        # Verify the /wallets/ path exists
        assert self.master_node.wallets_path.exists()

        # Verify the /wallets/ path exists
        assert Path(old_path_prefix / wallet_name / "wallet.dat").exists()
        assert Path(master_path_prefix / wallet_name / "wallet.dat").exists()

        backup_prefix = "default_wallet" if is_default else os.path.basename(os.path.abspath(master_path_prefix / wallet_name))
        backup_path = self.master_node.wallets_path / f"{backup_prefix}_{mocked_time}.legacy.bak"
        assert backup_path.exists()

        self.assert_is_bdb(wallet_name)

        # Cleanup
        if is_default:
            self.clear_default_wallet(backup_path)
        else:
            backup_path.unlink()
            Path(watch_only_dir / "wallet.dat").unlink()
            Path(watch_only_dir).rmdir()
            if is_absolute:
                os.unlink(os.path.join(wallet_name, "wallet.dat"))
            else:
                Path(master_path_prefix / wallet_name / "wallet.dat").unlink()
                Path(old_path_prefix / wallet_name / "wallet.dat").unlink()

    def test_direct_file(self):
        self.log.info("Test migration of a wallet that is not in a wallet directory")
        wallet = self.create_legacy_wallet("plainfile")
        wallet.unloadwallet()

        shutil.copyfile(
            self.old_node.wallets_path / "plainfile" / "wallet.dat" ,
            self.master_node.wallets_path / "plainfile"
        )
        assert (self.master_node.wallets_path / "plainfile").is_file()

        mocked_time = int(time.time())
        self.master_node.setmocktime(mocked_time)
        migrate_res = self.master_node.migratewallet("plainfile")
        assert_equal(f"plainfile_{mocked_time}.legacy.bak", os.path.basename(migrate_res["backup_path"]))
        wallet = self.master_node.get_wallet_rpc("plainfile")
        info = wallet.getwalletinfo()
        assert_equal(info["descriptors"], True)
        assert_equal(info["format"], "sqlite")

        assert (self.master_node.wallets_path / "plainfile").is_dir()
        assert (self.master_node.wallets_path / "plainfile" / "wallet.dat").is_file()

Review comment (suggestion):
I think this special case is unnecessary. You can do `unlink(missing_ok=True)` to avoid an error on the second one.

Code after:

        # Verify both wallet paths exist.
        assert Path(old_path / "wallet.dat").exists()
        assert Path(master_path / "wallet.dat").exists()

        backup_prefix = "default_wallet" if is_default else os.path.basename(os.path.abspath(master_path))
        backup_path = self.master_node.wallets_path / f"{backup_prefix}_{mocked_time}.legacy.bak"
        assert backup_path.exists()

        self.assert_is_bdb(wallet_name)

        # Cleanup
        if is_default:
            self.clear_default_wallet(backup_path)
        else:
            backup_path.unlink()
            Path(watch_only_dir / "wallet.dat").unlink()
            Path(watch_only_dir).rmdir()
            Path(master_path / "wallet.dat").unlink()
            Path(old_path / "wallet.dat").unlink(missing_ok=True)

    def test_direct_file(self):
        self.log.info("Test migration of a wallet that is not in a wallet directory")
        wallet = self.create_legacy_wallet("plainfile")
        wallet.unloadwallet()

        shutil.copyfile(
            self.old_node.wallets_path / "plainfile" / "wallet.dat" ,
            self.master_node.wallets_path / "plainfile"
        )
        assert (self.master_node.wallets_path / "plainfile").is_file()

        mocked_time = int(time.time())
        self.master_node.setmocktime(mocked_time)
        migrate_res = self.master_node.migratewallet("plainfile")
        assert_equal(f"plainfile_{mocked_time}.legacy.bak", os.path.basename(migrate_res["backup_path"]))
        wallet = self.master_node.get_wallet_rpc("plainfile")
        info = wallet.getwalletinfo()
        assert_equal(info["descriptors"], True)
        assert_equal(info["format"], "sqlite")

        assert (self.master_node.wallets_path / "plainfile").is_dir()
        assert (self.master_node.wallets_path / "plainfile" / "wallet.dat").is_file()

    def test_addressbook(self):
        df_wallet = self.master_node.get_wallet_rpc(self.default_wallet_name)

        self.log.info("Test migration of address book data")
        wallet = self.create_legacy_wallet("legacy_addrbook")
        df_wallet.sendtoaddress(wallet.getnewaddress(), 3)