diff --git a/packages/testing/src/execution_testing/cli/pytest_commands/plugins/execute/execute_flags/execute_flags.py b/packages/testing/src/execution_testing/cli/pytest_commands/plugins/execute/execute_flags/execute_flags.py index cb1e2a26fba..a20f22d460d 100644 --- a/packages/testing/src/execution_testing/cli/pytest_commands/plugins/execute/execute_flags/execute_flags.py +++ b/packages/testing/src/execution_testing/cli/pytest_commands/plugins/execute/execute_flags/execute_flags.py @@ -75,5 +75,17 @@ def pytest_configure(config: pytest.Config) -> None: "flags or the CHAIN_ID/RPC_CHAIN_ID environment variables." ) + setattr( # noqa: B010 + config, "_original_chain_id_default", ChainConfigDefaults.chain_id + ) + # write to config ChainConfigDefaults.chain_id = chain_id + + +def pytest_unconfigure(config: pytest.Config) -> None: + """Restore the previous chain-id default after the session ends.""" + if hasattr(config, "_original_chain_id_default"): + ChainConfigDefaults.chain_id = getattr( # noqa: B009 + config, "_original_chain_id_default" + ) diff --git a/packages/testing/src/execution_testing/cli/tests/test_pytest_execute_command.py b/packages/testing/src/execution_testing/cli/tests/test_pytest_execute_command.py index c24b4734469..61036140b5f 100644 --- a/packages/testing/src/execution_testing/cli/tests/test_pytest_execute_command.py +++ b/packages/testing/src/execution_testing/cli/tests/test_pytest_execute_command.py @@ -1,8 +1,16 @@ """Tests for execute command click CLI.""" +from pathlib import Path +from unittest.mock import patch + import pytest from click.testing import CliRunner +from ...test_types.chain_config_types import ( + DEFAULT_CHAIN_ID, + ChainConfigDefaults, +) +from ...test_types.transaction_types import Transaction from ..pytest_commands.execute import execute @@ -108,3 +116,50 @@ def test_all_execute_subcommands_help_no_conflicts(runner: CliRunner) -> None: f"execute {subcommand} --help has conflicting option string\n" f"Output: {result.output}" ) + + +def test_execute_remote_leaks_chain_id_into_later_defaults( + runner: CliRunner, tmp_path: Path +) -> None: + """Demonstrate that an in-process execute session leaks chain ID.""" + inner_test = tmp_path / "test_inner.py" + inner_test.write_text( + "\n".join( + [ + "from execution_testing import (", + " Account,", + " Environment,", + " TestAddress,", + " Transaction,", + ")", + "", + "def test_noop(state_test) -> None:", + " state_test(", + " env=Environment(),", + " pre={TestAddress: Account(balance=1_000_000)},", + " post={},", + " tx=Transaction(),", + " )", + ] + ) + ) + + ChainConfigDefaults.chain_id = DEFAULT_CHAIN_ID + with patch( + "execution_testing.cli.pytest_commands.plugins.execute.rpc.remote.EthRPC" + ) as mock_eth_rpc: + mock_eth_rpc.return_value.chain_id.return_value = 12345 + result = runner.invoke( + execute, + [ + "remote", + "--rpc-endpoint=http://localhost:12345", + "--chain-id=12345", + "--collect-only", + "-q", + str(inner_test), + ], + ) + + assert result.exit_code == 0, result.output + assert Transaction().chain_id == DEFAULT_CHAIN_ID