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

Code before:
        assert_equal(info["descriptors"], True)
        assert_equal(info["format"], "sqlite")
        assert_equal(info["private_keys_enabled"], False)
        assert_equal(info["walletname"], "default_wallet_watchonly")
        # Check the default wallet is not available anymore
        assert not (self.master_node.wallets_path / "wallet.dat").exists()

        wallet.unloadwallet()
        self.clear_default_wallet(backup_file=Path(res["backup_path"]))

    def test_migration_failure(self, wallet_name):
        wallet_pretty_name = wallet_name or "unnamed (default)"
        self.log.info(f"Test failure during migration of wallet named: \"{wallet_pretty_name}\"")
        # Preface, set up legacy wallet and unload it
        master_wallet = self.master_node.get_wallet_rpc(self.default_wallet_name)
        wallet = self.create_legacy_wallet(wallet_name, blank=True)
        wallet.importaddress(master_wallet.getnewaddress(address_type="legacy"))
        wallet.unloadwallet()

        is_absolute = os.path.isabs(wallet_name)
        if is_absolute:
            old_path_prefix  = Path('/')
            master_path_prefix = Path('/')
        else:
            old_path_prefix = self.old_node.wallets_path
            master_path_prefix = self.master_node.wallets_path

        is_default = wallet_name == ""
        wo_prefix = "default_wallet" if is_default else os.path.basename(wallet_name)
        wo_dirname = f"{wo_prefix}_watchonly"

        if is_absolute:
            watch_only_dir = Path(wallet_name) / ".." / wo_dirname
        else:
            watch_only_dir = self.master_node.wallets_path / wallet_name / wo_dirname
        os.mkdir(watch_only_dir)
        shutil.copyfile(old_path_prefix / wallet_name / "wallet.dat", watch_only_dir / "wallet.dat")

        # Copying wallet.dat is not necessary for the absolute wallet
        if not is_absolute:
            os.makedirs(master_path_prefix / wallet_name, exist_ok=True)
            shutil.copyfile(old_path_prefix / wallet_name / "wallet.dat", master_path_prefix / wallet_name / "wallet.dat")

        mocked_time = int(time.time())
        self.master_node.setmocktime(mocked_time)
        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()


Review comment (suggestion):
```python3
old_path_prefix = Path() if is_absolute else self.old_node.wallets_path
master_path_prefix = Path() if is_absolute else self.master_node.wallets_path
```

Code after:
        assert_equal(info["descriptors"], True)
        assert_equal(info["format"], "sqlite")
        assert_equal(info["private_keys_enabled"], False)
        assert_equal(info["walletname"], "default_wallet_watchonly")
        # Check the default wallet is not available anymore
        assert not (self.master_node.wallets_path / "wallet.dat").exists()

        wallet.unloadwallet()
        self.clear_default_wallet(backup_file=Path(res["backup_path"]))

    def test_migration_failure(self, wallet_name):
        is_default = wallet_name == ""
        wallet_pretty_name = "unnamed (default)" if is_default else f'"{wallet_name}"'
        self.log.info(f"Test failure during migration of wallet named: {wallet_pretty_name}")
        # Preface, set up legacy wallet and unload it
        master_wallet = self.master_node.get_wallet_rpc(self.default_wallet_name)
        wallet = self.create_legacy_wallet(wallet_name, blank=True)
        wallet.importaddress(master_wallet.getnewaddress(address_type="legacy"))
        wallet.unloadwallet()

        if os.path.isabs(wallet_name):
            old_path = master_path = Path(wallet_name)
        else:
            old_path = self.old_node.wallets_path / wallet_name
            master_path = self.master_node.wallets_path / wallet_name
            os.makedirs(master_path, exist_ok=True)
            shutil.copyfile(old_path / "wallet.dat", master_path / "wallet.dat")

        # This will be the watch-only directory the migration tries to create,
        # we make migration fail by placing a wallet.dat file there.
        wo_prefix = wallet_name or "default_wallet"
        # wo_prefix might have path characters in it, this corresponds with
        # DoMigration().
        wo_dirname = f"{wo_prefix}_watchonly"
        watch_only_dir = self.master_node.wallets_path / wo_dirname
        os.mkdir(watch_only_dir)
        shutil.copyfile(old_path / "wallet.dat", watch_only_dir / "wallet.dat")

        mocked_time = int(time.time())
        self.master_node.setmocktime(mocked_time)
        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 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))