From f4add4643f1d841c2ed59706a6a9659328b18304 Mon Sep 17 00:00:00 2001 From: kclowes Date: Mon, 14 Jun 2021 10:50:39 -0600 Subject: [PATCH] Honor WEB3_PROVIDER_URI for geth auto dev connection (#2023) --- newsfragments/2023.bugfix.rst | 1 + tests/core/providers/test_auto_provider.py | 10 ++++++++++ web3/providers/ipc.py | 6 +++++- 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 newsfragments/2023.bugfix.rst diff --git a/newsfragments/2023.bugfix.rst b/newsfragments/2023.bugfix.rst new file mode 100644 index 0000000000..7f1d261eab --- /dev/null +++ b/newsfragments/2023.bugfix.rst @@ -0,0 +1 @@ +Have the geth dev IPC auto connection check for the ``WEB3_PROVIDER_URI`` environment variable. diff --git a/tests/core/providers/test_auto_provider.py b/tests/core/providers/test_auto_provider.py index 6fd773a90a..c0e30295f6 100644 --- a/tests/core/providers/test_auto_provider.py +++ b/tests/core/providers/test_auto_provider.py @@ -17,6 +17,9 @@ from web3.providers.auto import ( load_provider_from_environment, ) +from web3.providers.ipc import ( + get_dev_ipc_path, +) # Ugly hack to import infura now that API KEY is required os.environ['WEB3_INFURA_API_KEY'] = 'test' @@ -51,6 +54,13 @@ def test_load_provider_from_env(monkeypatch, uri, expected_type, expected_attrs) assert getattr(provider, attr) == val +def test_get_dev_ipc_path(monkeypatch, tmp_path): + uri = str(tmp_path) + monkeypatch.setenv('WEB3_PROVIDER_URI', uri) + path = get_dev_ipc_path() + assert path == uri + + @pytest.mark.parametrize('environ_name', ['WEB3_INFURA_API_KEY', 'WEB3_INFURA_PROJECT_ID']) def test_web3_auto_infura_empty_key(monkeypatch, caplog, environ_name): monkeypatch.setenv('WEB3_INFURA_SCHEME', 'https') diff --git a/web3/providers/ipc.py b/web3/providers/ipc.py index 57a2998a63..45faf63b61 100644 --- a/web3/providers/ipc.py +++ b/web3/providers/ipc.py @@ -157,7 +157,11 @@ def get_default_ipc_path() -> str: # type: ignore # type ignored b/c missing return statement is by design here def get_dev_ipc_path() -> str: # type: ignore - if sys.platform == 'darwin': + if os.environ.get('WEB3_PROVIDER_URI', ''): + ipc_path = os.environ.get('WEB3_PROVIDER_URI') + if os.path.exists(ipc_path): + return ipc_path + elif sys.platform == 'darwin': tmpdir = os.environ.get('TMPDIR', '') ipc_path = os.path.expanduser(os.path.join( tmpdir,