Skip to content

Commit 316c40b

Browse files
committed
add tests and fix win32 dev_ipc_path
1 parent f8623e7 commit 316c40b

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

tests/core/providers/test_ipc_provider.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
import pathlib
33
import pytest
44
import socket
5+
import sys
56
import tempfile
67
from threading import (
78
Thread,
89
)
910
import time
11+
from unittest.mock import (
12+
patch,
13+
)
1014

1115
from web3.auto.gethdev import (
1216
w3,
@@ -15,6 +19,8 @@
1519
ProviderConnectionError,
1620
)
1721
from web3.providers.ipc import (
22+
get_default_ipc_path,
23+
get_dev_ipc_path,
1824
IPCProvider,
1925
)
2026
from web3.types import (
@@ -49,6 +55,91 @@ def test_ipc_tilda_in_path():
4955
assert IPCProvider(pathlib.Path("~/foo")).ipc_path == expectedPath
5056

5157

58+
@pytest.mark.parametrize(
59+
"platform, expected_result, expected_error",
60+
[
61+
("darwin", "/Library/Ethereum/geth.ipc", None),
62+
("linux", "/.ethereum/geth.ipc", None),
63+
("freebsd", "/.ethereum/geth.ipc", None),
64+
("win32", r"\\.\pipe\geth.ipc", None),
65+
(
66+
"unknown",
67+
None,
68+
{
69+
"error": ValueError,
70+
"match": (
71+
f"Unsupported platform 'unknown'. Only darwin/linux/win32/"
72+
"freebsd are supported. You must specify the ipc_path"
73+
),
74+
},
75+
),
76+
],
77+
)
78+
def test_get_default_ipc_path(platform, expected_result, expected_error):
79+
with patch.object(sys, "platform", platform):
80+
if expected_error:
81+
with pytest.raises(expected_error["error"], match=expected_error["match"]):
82+
get_default_ipc_path()
83+
else:
84+
assert get_default_ipc_path().endswith(expected_result)
85+
86+
87+
class MockEnviron:
88+
def __init__(self, env_vars):
89+
self.env_vars = env_vars
90+
91+
def get(self, key, default=None):
92+
return self.env_vars.get(key, default)
93+
94+
95+
@pytest.mark.parametrize(
96+
"provider_env_uri",
97+
[
98+
"/sample/path/to/ipc/geth.ipc",
99+
"",
100+
],
101+
)
102+
@pytest.mark.parametrize(
103+
"platform, expected_result, expected_error",
104+
[
105+
("darwin", "/var/path/to/tmp/T/geth.ipc", None),
106+
("linux", "/tmp/geth.ipc", None),
107+
("freebsd", "/tmp/geth.ipc", None),
108+
("win32", r"\\.\pipe\geth.ipc", None),
109+
(
110+
"unknown",
111+
None,
112+
{
113+
"error": ValueError,
114+
"match": (
115+
f"Unsupported platform 'unknown'. Only darwin/linux/win32/"
116+
"freebsd are supported. You must specify the ipc_path"
117+
),
118+
},
119+
),
120+
],
121+
)
122+
def test_get_dev_ipc_path_(provider_env_uri, platform, expected_result, expected_error):
123+
with patch.object(sys, "platform", platform):
124+
with patch.dict(
125+
os.environ,
126+
{
127+
"TMPDIR": "/var/path/to/tmp/T/",
128+
"WEB3_PROVIDER_URI": provider_env_uri,
129+
},
130+
):
131+
132+
if provider_env_uri:
133+
assert get_dev_ipc_path() == provider_env_uri
134+
elif expected_error:
135+
with pytest.raises(
136+
expected_error["error"], match=expected_error["match"]
137+
):
138+
get_dev_ipc_path()
139+
else:
140+
assert get_dev_ipc_path().endswith(expected_result)
141+
142+
52143
@pytest.fixture
53144
def simple_ipc_server(jsonrpc_ipc_pipe_path):
54145
serv = socket.socket(socket.AF_UNIX)

web3/providers/ipc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def get_dev_ipc_path() -> str:
114114
return os.path.expanduser(os.path.join("/tmp", "geth.ipc"))
115115

116116
elif sys.platform == "win32":
117-
return os.path.join("\\\\", ".", "pipe", "geth.ipc")
117+
return r"\\.\pipe\geth.ipc"
118118

119119
else:
120120
raise ValueError(

0 commit comments

Comments
 (0)