Skip to content

Commit 1b910f4

Browse files
committed
fix: resolve anvil executable on Windows
1 parent 7880737 commit 1b910f4

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

brownie/network/rpc/anvil.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/python3
22

3+
import shutil
34
import sys
45
import warnings
56
from subprocess import DEVNULL, PIPE
@@ -34,12 +35,15 @@ def launch(cmd: str, **kwargs: Any) -> None:
3435
3536
Args:
3637
cmd: command string to execute as subprocess"""
37-
if sys.platform == "win32" and not cmd.split(" ")[0].endswith(".cmd"):
38-
if " " in cmd:
39-
cmd = cmd.replace(" ", ".cmd ", 1)
40-
else:
41-
cmd += ".cmd"
4238
cmd_list = cmd.split(" ")
39+
if sys.platform == "win32":
40+
executable = cmd_list[0]
41+
resolved = shutil.which(executable)
42+
if resolved is not None:
43+
cmd_list[0] = resolved
44+
elif not executable.endswith(".cmd"):
45+
cmd_list[0] = f"{executable}.cmd"
46+
4347
cmd_list.append("--quiet")
4448
for key, value in kwargs.items():
4549
if value is None or value is False:

tests/network/rpc/test_anvil.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_anvil_launcher_keeps_falsy_command_values(popen_calls):
3030
steps_tracing=True,
3131
)
3232

33-
cmd_list, _ = calls.pop()
33+
cmd_list, _ = popen_calls.pop()
3434

3535
assert cmd_list == [
3636
"anvil",
@@ -45,6 +45,32 @@ def test_anvil_launcher_keeps_falsy_command_values(popen_calls):
4545
assert "--block-time" not in cmd_list
4646

4747

48+
def test_anvil_launcher_uses_resolved_windows_executable(monkeypatch, popen_calls):
49+
monkeypatch.setattr(anvil.sys, "platform", "win32")
50+
monkeypatch.setattr(
51+
anvil.shutil,
52+
"which",
53+
lambda executable: r"C:\Users\runner\.foundry\bin\anvil.exe"
54+
if executable == "anvil"
55+
else None,
56+
)
57+
58+
anvil.launch("anvil")
59+
60+
cmd_list, _ = popen_calls.pop()
61+
assert cmd_list[0] == r"C:\Users\runner\.foundry\bin\anvil.exe"
62+
63+
64+
def test_anvil_launcher_falls_back_to_cmd_on_windows(monkeypatch, popen_calls):
65+
monkeypatch.setattr(anvil.sys, "platform", "win32")
66+
monkeypatch.setattr(anvil.shutil, "which", lambda _: None)
67+
68+
anvil.launch("anvil")
69+
70+
cmd_list, _ = popen_calls.pop()
71+
assert cmd_list[0] == "anvil.cmd"
72+
73+
4874
def test_anvil_launcher_does_not_set_default_balance(popen_calls):
4975
anvil.launch("anvil")
5076

0 commit comments

Comments
 (0)