Skip to content

Commit 4b27874

Browse files
committed
add tests and fix win32 dev_ipc_path
1 parent f8623e7 commit 4b27874

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

tests/core/providers/test_ipc_provider.py

Lines changed: 83 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,
@@ -16,6 +20,8 @@
1620
)
1721
from web3.providers.ipc import (
1822
IPCProvider,
23+
get_default_ipc_path,
24+
get_dev_ipc_path,
1925
)
2026
from web3.types import (
2127
RPCEndpoint,
@@ -49,6 +55,83 @@ 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+
"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+
@pytest.mark.parametrize(
88+
"provider_env_uri",
89+
[
90+
"/sample/path/to/ipc/geth.ipc",
91+
"",
92+
],
93+
)
94+
@pytest.mark.parametrize(
95+
"platform, expected_result, expected_error",
96+
[
97+
("darwin", "/var/path/to/tmp/T/geth.ipc", None),
98+
("linux", "/tmp/geth.ipc", None),
99+
("freebsd", "/tmp/geth.ipc", None),
100+
("win32", r"\\.\pipe\geth.ipc", None),
101+
(
102+
"unknown",
103+
None,
104+
{
105+
"error": ValueError,
106+
"match": (
107+
"Unsupported platform 'unknown'. Only darwin/linux/win32/"
108+
"freebsd are supported. You must specify the ipc_path"
109+
),
110+
},
111+
),
112+
],
113+
)
114+
def test_get_dev_ipc_path_(provider_env_uri, platform, expected_result, expected_error):
115+
with patch.object(sys, "platform", platform):
116+
with patch.dict(
117+
os.environ,
118+
{
119+
"TMPDIR": "/var/path/to/tmp/T/",
120+
"WEB3_PROVIDER_URI": provider_env_uri,
121+
},
122+
):
123+
124+
if provider_env_uri:
125+
assert get_dev_ipc_path() == provider_env_uri
126+
elif expected_error:
127+
with pytest.raises(
128+
expected_error["error"], match=expected_error["match"]
129+
):
130+
get_dev_ipc_path()
131+
else:
132+
assert get_dev_ipc_path().endswith(expected_result)
133+
134+
52135
@pytest.fixture
53136
def simple_ipc_server(jsonrpc_ipc_pipe_path):
54137
serv = socket.socket(socket.AF_UNIX)

web3/providers/ipc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def get_default_ipc_path() -> str:
9797

9898
else:
9999
raise ValueError(
100-
f"Unsupported platform '{sys.platform}'. Only darwin/linux/win32/"
100+
f"Unsupported platform '{sys.platform}'. Only darwin/linux/win32/"
101101
"freebsd are supported. You must specify the ipc_path"
102102
)
103103

@@ -114,11 +114,11 @@ 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(
121-
f"Unsupported platform '{sys.platform}'. Only darwin/linux/win32/"
121+
f"Unsupported platform '{sys.platform}'. Only darwin/linux/win32/"
122122
"freebsd are supported. You must specify the ipc_path"
123123
)
124124

0 commit comments

Comments
 (0)