Skip to content

Commit

Permalink
bugfix: Fix gethdev ipc path for auto.gethdev
Browse files Browse the repository at this point in the history
  • Loading branch information
fselmo committed Jan 13, 2025
1 parent a35efbc commit 12c9d83
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions newsfragments/3576.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug related to building the ipc path for connecting to a geth ``--dev`` instance via ``web3.auto.gethdev``.
14 changes: 13 additions & 1 deletion tests/core/providers/test_auto_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,19 @@ def test_load_provider_from_env(monkeypatch, uri, expected_type, expected_attrs)


def test_get_dev_ipc_path(monkeypatch, tmp_path):
uri = str(tmp_path)
# test default path
path = get_dev_ipc_path()
assert path == "/tmp/geth.ipc"

uri = str(tmp_path) + "/geth.ipc"

# test setting the "TMPDIR" environment variable
monkeypatch.setenv("TMPDIR", str(tmp_path))
path = get_dev_ipc_path()
assert path == uri
monkeypatch.delenv("TMPDIR") # reset

# test with WEB3_PROVIDER_URI set
monkeypatch.setenv("WEB3_PROVIDER_URI", uri)
path = get_dev_ipc_path()
assert path == uri
6 changes: 3 additions & 3 deletions tests/core/providers/test_ipc_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_get_default_ipc_path(platform, expected_result, expected_error):
"platform, expected_result, expected_error",
[
("darwin", "/var/path/to/tmp/T/geth.ipc", None),
("linux", "/tmp/geth.ipc", None),
("linux", "/var/path/to/tmp/T/geth.ipc", None),
("freebsd", "/tmp/geth.ipc", None),
("win32", r"\\.\pipe\geth.ipc", None),
(
Expand All @@ -119,7 +119,7 @@ def test_get_dev_ipc_path_(provider_env_uri, platform, expected_result, expected
os.environ,
{
"TMPDIR": "/var/path/to/tmp/T/",
"WEB3_PROVIDER_URI": provider_env_uri,
"WEB3_PROVIDER_URI": provider_env_uri or "",
},
):
if provider_env_uri:
Expand All @@ -130,7 +130,7 @@ def test_get_dev_ipc_path_(provider_env_uri, platform, expected_result, expected
):
get_dev_ipc_path()
else:
assert get_dev_ipc_path().endswith(expected_result)
assert get_dev_ipc_path() == expected_result


@pytest.fixture
Expand Down
11 changes: 6 additions & 5 deletions web3/providers/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ def get_default_ipc_path() -> str:


def get_dev_ipc_path() -> str:
if os.environ.get("WEB3_PROVIDER_URI", ""):
return os.environ.get("WEB3_PROVIDER_URI")
web3_provider_uri = os.environ.get("WEB3_PROVIDER_URI", "")
if web3_provider_uri and "geth.ipc" in web3_provider_uri:
return web3_provider_uri

elif sys.platform == "darwin":
tmpdir = os.environ.get("TMPDIR", "")
elif sys.platform == "darwin" or sys.platform.startswith("linux"):
tmpdir = os.environ.get("TMPDIR", "/tmp")
return os.path.expanduser(os.path.join(tmpdir, "geth.ipc"))

elif sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
elif sys.platform.endswith("freebsd"):
return os.path.expanduser(os.path.join("/tmp", "geth.ipc"))

elif sys.platform == "win32":
Expand Down

0 comments on commit 12c9d83

Please sign in to comment.