|
2 | 2 | import pathlib
|
3 | 3 | import pytest
|
4 | 4 | import socket
|
| 5 | +import sys |
5 | 6 | import tempfile
|
6 | 7 | from threading import (
|
7 | 8 | Thread,
|
8 | 9 | )
|
9 | 10 | import time
|
| 11 | +from unittest.mock import ( |
| 12 | + patch, |
| 13 | +) |
10 | 14 |
|
11 | 15 | from web3.auto.gethdev import (
|
12 | 16 | w3,
|
|
16 | 20 | )
|
17 | 21 | from web3.providers.ipc import (
|
18 | 22 | IPCProvider,
|
| 23 | + get_default_ipc_path, |
| 24 | + get_dev_ipc_path, |
19 | 25 | )
|
20 | 26 | from web3.types import (
|
21 | 27 | RPCEndpoint,
|
@@ -49,6 +55,83 @@ def test_ipc_tilda_in_path():
|
49 | 55 | assert IPCProvider(pathlib.Path("~/foo")).ipc_path == expectedPath
|
50 | 56 |
|
51 | 57 |
|
| 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 | + |
52 | 135 | @pytest.fixture
|
53 | 136 | def simple_ipc_server(jsonrpc_ipc_pipe_path):
|
54 | 137 | serv = socket.socket(socket.AF_UNIX)
|
|
0 commit comments