-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathtest_custom_nodes_python_resolution.py
More file actions
156 lines (129 loc) · 6.55 KB
/
test_custom_nodes_python_resolution.py
File metadata and controls
156 lines (129 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
from unittest.mock import patch
from comfy_cli.command.custom_nodes import command
class TestGetInstalledPackages:
def test_uses_resolved_python(self):
command.pip_map = None
with (
patch(
"comfy_cli.command.custom_nodes.command.resolve_workspace_python",
return_value="/resolved/python",
),
patch.object(command.workspace_manager, "workspace_path", "/fake/workspace"),
patch(
"comfy_cli.command.custom_nodes.command.subprocess.check_output",
return_value="Package Version\n------ -------\npip 24.0\n",
) as mock_check_output,
):
command.get_installed_packages()
cmd = mock_check_output.call_args[0][0]
assert cmd[0] == "/resolved/python"
assert cmd == ["/resolved/python", "-m", "pip", "list"]
command.pip_map = None
class TestExecuteInstallScript:
def test_pip_uses_resolved_python(self, tmp_path):
(tmp_path / "requirements.txt").write_text("somepackage\n")
with (
patch(
"comfy_cli.command.custom_nodes.command.resolve_workspace_python",
return_value="/resolved/python",
),
patch.object(command.workspace_manager, "workspace_path", str(tmp_path)),
patch("comfy_cli.command.custom_nodes.command.subprocess.check_call") as mock_check_call,
):
command.execute_install_script(str(tmp_path))
mock_check_call.assert_called()
cmd = mock_check_call.call_args[0][0]
assert cmd[0] == "/resolved/python"
assert "-m" in cmd and "pip" in cmd
def test_install_py_uses_resolved_python(self, tmp_path):
(tmp_path / "install.py").write_text("print('install')\n")
with (
patch(
"comfy_cli.command.custom_nodes.command.resolve_workspace_python",
return_value="/resolved/python",
),
patch.object(command.workspace_manager, "workspace_path", str(tmp_path)),
patch("comfy_cli.command.custom_nodes.command.subprocess.check_call") as mock_check_call,
):
command.execute_install_script(str(tmp_path))
mock_check_call.assert_called()
cmd = mock_check_call.call_args[0][0]
assert cmd == ["/resolved/python", "install.py"]
def test_inline_comment_not_passed_to_pip(self, tmp_path):
# Issue #431 regression: inline comments in requirements.txt must not
# survive into the argv handed to pip. Pre-fix, the raw line was passed
# verbatim (e.g. "matplotlib>=3.3.0 # note") and pip rejected it.
bad_spec = "matplotlib>=3.3.0 # For visualization"
(tmp_path / "requirements.txt").write_text(f"{bad_spec}\n")
with (
patch(
"comfy_cli.command.custom_nodes.command.resolve_workspace_python",
return_value="/resolved/python",
),
patch.object(command.workspace_manager, "workspace_path", str(tmp_path)),
patch("comfy_cli.command.custom_nodes.command.subprocess.check_call") as mock_check_call,
):
command.execute_install_script(str(tmp_path))
for call in mock_check_call.call_args_list:
argv = call[0][0]
assert bad_spec not in argv, f"raw comment-laden spec leaked into pip argv: {argv!r}"
def test_uses_pip_install_r(self, tmp_path):
# Option C: delegate requirements-file parsing to pip via `-r <path>`.
# This lets pip handle inline comments, line continuations, VCS URL
# fragments, env markers, -e, -r, --index-url, etc.
requirements_path = tmp_path / "requirements.txt"
requirements_path.write_text("numpy>=1.0\n")
with (
patch(
"comfy_cli.command.custom_nodes.command.resolve_workspace_python",
return_value="/resolved/python",
),
patch.object(command.workspace_manager, "workspace_path", str(tmp_path)),
patch("comfy_cli.command.custom_nodes.command.subprocess.check_call") as mock_check_call,
):
command.execute_install_script(str(tmp_path))
mock_check_call.assert_called_once()
argv = mock_check_call.call_args[0][0]
assert argv == ["/resolved/python", "-m", "pip", "install", "-r", str(requirements_path)]
def test_requirements_path_is_absolute_when_repo_path_is_relative(self, tmp_path, monkeypatch):
# try_install_script runs pip with cwd=repo_path. If requirements_path
# were relative, pip would resolve `-r <rel>/requirements.txt` against
# that cwd, producing a doubled path like <rel>/<rel>/requirements.txt.
# Guard: the `-r` target must be absolute regardless of the input.
(tmp_path / "requirements.txt").write_text("numpy>=1.0\n")
monkeypatch.chdir(tmp_path.parent)
relative_repo = tmp_path.name # e.g. "test_requirements_path..." relative to cwd
with (
patch(
"comfy_cli.command.custom_nodes.command.resolve_workspace_python",
return_value="/resolved/python",
),
patch.object(command.workspace_manager, "workspace_path", str(tmp_path)),
patch("comfy_cli.command.custom_nodes.command.subprocess.check_call") as mock_check_call,
):
command.execute_install_script(relative_repo)
argv = mock_check_call.call_args[0][0]
target = argv[-1]
assert os.path.isabs(target), f"-r target is not absolute: {target!r}"
assert target == str(tmp_path / "requirements.txt")
class TestUpdateNodeIdCache:
def test_uses_resolved_python(self, tmp_path):
cm_cli_path = tmp_path / "custom_nodes" / "ComfyUI-Manager" / "cm-cli.py"
cm_cli_path.parent.mkdir(parents=True)
cm_cli_path.touch()
config_path = tmp_path / "config"
config_path.mkdir()
with (
patch(
"comfy_cli.command.custom_nodes.command.resolve_workspace_python",
return_value="/resolved/python",
),
patch.object(command.workspace_manager, "workspace_path", str(tmp_path)),
patch("comfy_cli.command.custom_nodes.command.ConfigManager") as MockConfig,
patch("comfy_cli.command.custom_nodes.command.subprocess.run") as mock_run,
):
MockConfig.return_value.get_config_path.return_value = str(config_path)
command.update_node_id_cache()
cmd = mock_run.call_args[0][0]
assert cmd[0] == "/resolved/python"