From 1f6a59fc627c51b93d141c4234032a444dd40a74 Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Tue, 17 Nov 2026 09:31:20 +0900 Subject: [PATCH] Fix git_exe and snapshot restore Three fixes. The CHANGELOG has an entry for each. - Give the scheduled snapshot restore the directories of the server - Use the git executable set as git_exe - Report the failed steps of a snapshot restore, restore the pinned commit and run the install step of the node packs it installs --- CHANGELOG.md | 55 +++ cm-cli.py | 39 +- git_helper.py | 10 +- glob/git_utils.py | 27 +- glob/manager_core.py | 194 +++++++--- glob/manager_server.py | 11 +- glob/manager_util.py | 21 + prestartup_script.py | 16 +- tests/test_git_exe_setting.py | 324 ++++++++++++++++ tests/test_install_flags_config.py | 7 + tests/test_restore_summary.py | 489 ++++++++++++++++++++++++ tests/test_scheduled_restore_command.py | 68 ++++ 12 files changed, 1176 insertions(+), 85 deletions(-) create mode 100644 tests/test_git_exe_setting.py create mode 100644 tests/test_restore_summary.py create mode 100644 tests/test_scheduled_restore_command.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 42a21d7287..08257b8a98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,12 +16,67 @@ whatever value a request carried and wrote it to `config.ini`. They answer 400 for a value that is not one of those the Manager's menu offers, and for a JSON body without a `value`, which ended with status 500. +### A snapshot restore reports what failed, and restores the pinned commit + +The summary of a restore listed a node pack as `INSTALLED`, `DISABLED` or +`ENABLED` without looking at the result of the step: a clone that failed, for +example because the directory already existed or the url could not be reached, +was `INSTALLED`. A step that fails is now listed as `FAILED`, and the Manager +itself is no longer listed as `DISABLED`. A registry that cannot be reached +ended the restore at the first node pack to install from it, and so did a +node pack whose directory could not be moved to disable or enable it; such a +node pack is listed as `FAILED`, and the restore goes on. + +A git node pack that the restore had to clone stayed at the newest commit. It +is checked out at the commit of the snapshot, and its install script runs +after that. An installed git node pack that the registry does not know was +checked out only when cm-cli was run from `custom_nodes`, as a scheduled +restore does; it is checked out from any directory. The submodules of a git +node pack are brought to the commits that the commit of the snapshot names; a +submodule that was never fetched is fetched only when the node pack itself is +checked out. + +A registry node pack that the restore had to install was unpacked, but its +requirements were not installed and its install script did not run. The +restore now runs them, and lists the node pack as `FAILED` when they fail. + +A requirement or an install script that failed was taken as done until the +next start: the next node pack that names the requirement skipped it, and +installing or fixing the node pack again did not run the script. Both are +tried again. + +A snapshot recorded no commit for a git node pack whose refs were packed, as +after `git gc`. The commit is read from `packed-refs` too. A `HEAD` that is +not UTF-8 raised an error where the commit of a node pack is read; the commit +of such a pack is now unknown. + +### `git_exe` chooses the git executable + +The path set as `git_exe` in `config.ini` was read but not used: git commands +kept running the `git` found on `PATH`. The Manager, cm-cli and the git helper +now run the configured executable, and it no longer has to be on `PATH`: the +Manager also starts when the only git is the one named by `git_exe`. + +A value that is not a working git is reported in the log, and the default git +is used. The value is read as it is written: with a `%` in it, as in a path +of Windows, the Manager read every setting of `config.ini` as its default. +`git_exe` is used only when the configuration file is in a protected +directory, that is with a ComfyUI that has the System User API. + ### Install requests with an unusable body are answered with 400 `/customnode/install/git_url` and `/customnode/install/pip` answered 500 when the JSON body was not an object, or when `url` or `packages` was not a string. They answer 400, as they do for a missing field. +### A scheduled snapshot restore works with `--base-directory` + +With `--base-directory`, where `custom_nodes` is not inside the ComfyUI +directory, a snapshot restore scheduled for the next start failed with +`ModuleNotFoundError: No module named 'utils'`, and the request was dropped. +The restore now gets the path of ComfyUI, and the user directory and +`custom_nodes` directory the server uses. + ### Console messages show paths, URLs and package names as they are Manager printed many of its messages through rich, which reads square diff --git a/cm-cli.py b/cm-cli.py index 98b3048b06..b7b2d374c5 100644 --- a/cm-cli.py +++ b/cm-cli.py @@ -136,25 +136,26 @@ def set_no_deps(self, no_deps): self.no_deps = no_deps def set_user_directory(self, user_directory): - if user_directory is None: - return - - extra_model_paths_yaml = os.path.join(user_directory, 'extra_model_paths.yaml') - if os.path.exists(extra_model_paths_yaml): - utils.extra_config.load_extra_path_config(extra_model_paths_yaml) - - core.update_user_directory(user_directory) - - if os.path.exists(core.manager_pip_overrides_path): - with open(core.manager_pip_overrides_path, 'r', encoding="UTF-8", errors="ignore") as json_file: - cm_global.pip_overrides = json.load(json_file) - - if os.path.exists(core.manager_pip_blacklist_path): - with open(core.manager_pip_blacklist_path, 'r', encoding="UTF-8", errors="ignore") as f: - for x in f.readlines(): - y = x.strip() - if y != '': - cm_global.pip_blacklist.add(y) + if user_directory is not None: + extra_model_paths_yaml = os.path.join(user_directory, 'extra_model_paths.yaml') + if os.path.exists(extra_model_paths_yaml): + utils.extra_config.load_extra_path_config(extra_model_paths_yaml) + + core.update_user_directory(user_directory) + + if os.path.exists(core.manager_pip_overrides_path): + with open(core.manager_pip_overrides_path, 'r', encoding="UTF-8", errors="ignore") as json_file: + cm_global.pip_overrides = json.load(json_file) + + if os.path.exists(core.manager_pip_blacklist_path): + with open(core.manager_pip_blacklist_path, 'r', encoding="UTF-8", errors="ignore") as f: + for x in f.readlines(): + y = x.strip() + if y != '': + cm_global.pip_blacklist.add(y) + + # the configuration of the user directory is known from here on + core.setup_git_executable() def update_custom_nodes_dir(self, target_dir): import folder_paths diff --git a/git_helper.py b/git_helper.py index 4e184a3b39..869abaf988 100644 --- a/git_helper.py +++ b/git_helper.py @@ -493,8 +493,14 @@ def restore_pip_snapshot(pips, options): def setup_environment(): - if git_exe_path is not None: - git.Git().update_environment(GIT_PYTHON_GIT_EXECUTABLE=git_exe_path) + if git_exe_path: + default_git = git.Git.GIT_PYTHON_GIT_EXECUTABLE + try: + git.refresh(git_exe_path) + except Exception: + # a program that is not git fails in more ways than a path that cannot be run, and stays selected + git.Git.GIT_PYTHON_GIT_EXECUTABLE = default_git + print(f"[ComfyUI-Manager] `git_exe = {git_exe_path}` is not a working git executable. The default git is used.", file=sys.stderr) setup_environment() diff --git a/glob/git_utils.py b/glob/git_utils.py index 26f8444a46..d578390406 100644 --- a/glob/git_utils.py +++ b/glob/git_utils.py @@ -15,21 +15,32 @@ def is_git_repo(path: str) -> bool: def get_commit_hash(fullpath): git_head = os.path.join(fullpath, '.git', 'HEAD') if os.path.exists(git_head): - with open(git_head) as f: + # a ref can be named in any bytes, so the files are read as bytes and the names compared as they are + with open(git_head, 'rb') as f: line = f.readline() - if line.startswith("ref: "): - ref = os.path.join(fullpath, '.git', line[5:].strip()) + if line.startswith(b"ref: "): + ref_name = line[5:].strip() + ref = os.path.join(os.fsencode(fullpath), b'.git', ref_name) if os.path.exists(ref): - with open(ref) as f2: - return f2.readline().strip() - else: - return "unknown" + with open(ref, 'rb') as f2: + return f2.readline().strip().decode('ascii', errors='replace') + + # after `git pack-refs` or `git gc` the ref is a line of packed-refs + packed_refs = os.path.join(fullpath, '.git', 'packed-refs') + if os.path.exists(packed_refs): + with open(packed_refs, 'rb') as f2: + for packed in f2: + fields = packed.split() + if len(fields) == 2 and fields[1] == ref_name: + return fields[0].decode('ascii', errors='replace') + + return "unknown" else: # detached HEAD: the file holds the sha followed by a newline; # without the strip the pinned hash carries that newline into # snapshots, and the next restore's checkout of that hash fails - return line.strip() + return line.strip().decode('ascii', errors='replace') return "unknown" diff --git a/glob/manager_core.py b/glob/manager_core.py index 2b0693486b..c2a5b787ec 100644 --- a/glob/manager_core.py +++ b/glob/manager_core.py @@ -114,11 +114,21 @@ def get_current_comfyui_ver(): return None +working_git_exe = {} # `git_exe` as configured -> its full path, once it has answered as git + + def get_script_env(): new_env = os.environ.copy() git_exe = get_config().get('git_exe') - if git_exe is not None: - new_env['GIT_EXE_PATH'] = git_exe + if git_exe and manager_migration.has_system_user_api(): + # the check starts a process, and this is called for every command of an install + if git_exe not in working_git_exe: + path = manager_util.find_git_executable(git_exe) + if path is not None: + working_git_exe[git_exe] = path + + if git_exe in working_git_exe: + new_env['GIT_EXE_PATH'] = working_git_exe[git_exe] if 'COMFYUI_PATH' not in new_env: new_env['COMFYUI_PATH'] = comfy_path @@ -129,6 +139,26 @@ def get_script_env(): return new_env +def setup_git_executable(): + """ + run the git set as `git_exe` for the git commands of this process + """ + git_exe = get_config()['git_exe'] + + if git_exe == '': + return + + if not manager_migration.has_system_user_api(): + logging.warning("[ComfyUI-Manager] `git_exe` is not used, because this ComfyUI does not keep the configuration file in a protected directory.") + return + + path = manager_util.find_git_executable(git_exe) + if path is None: + logging.error(f"[ComfyUI-Manager] `git_exe = {git_exe}` is not a working git executable. The default git is used.") + else: + git.refresh(path) + + invalid_nodes = {} @@ -872,19 +902,23 @@ def execute_install_script(self, url, repo_path, instant_execution=False, lazy_m for line in lines: package_name = remap_pip_package(line.strip()) if package_name and not package_name.startswith('#') and package_name not in self.processed_install: - self.processed_install.add(package_name) clean_package_name = package_name.split('#')[0].strip() install_cmd = manager_util.make_pip_cmd(["install", clean_package_name]) if clean_package_name != "" and not clean_package_name.startswith('#'): res = res and try_install_script(url, repo_path, install_cmd, instant_execution=instant_execution) + # one that was not installed is tried again for the next node pack that names it + if res: + self.processed_install.add(package_name) pip_fixer.fix_broken() if os.path.exists(install_script_path) and install_script_path not in self.processed_install: - self.processed_install.add(install_script_path) print("Install: install script") install_cmd = [sys.executable, "install.py"] - return res and try_install_script(url, repo_path, install_cmd, instant_execution=instant_execution) + res = res and try_install_script(url, repo_path, install_cmd, instant_execution=instant_execution) + # one that did not run, or failed, is run when the node pack is installed or fixed again + if res: + self.processed_install.add(install_script_path) return res @@ -1886,7 +1920,8 @@ def get_bool(key, default_value): result = { 'http_channel_enabled': get_bool('http_channel_enabled', False), 'preview_method': default_conf.get('preview_method', manager_funcs.get_current_preview_method()).lower(), - 'git_exe': default_conf.get('git_exe', ''), + # read as written: a path of Windows may hold a `%`, which the parser takes as the start of a reference + 'git_exe': default_conf.get('git_exe', '', raw=True), 'use_uv': get_bool('use_uv', False), 'channel_url': default_conf.get('channel_url', DEFAULT_CHANNEL), 'default_cache_as_channel_url': get_bool('default_cache_as_channel_url', False), @@ -3282,6 +3317,22 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): installed_node_packs = [] failed = [] + def run_postinstall(res): + # an install script that fails can raise; the restore goes on with the other packs + try: + return res.postinstall() + except Exception: + traceback.print_exc() + return False + + def move(step, *args): + # disabling and enabling move the directory of the node pack; one that cannot be moved fails alone + try: + return step(*args) + except Exception as e: + logging.error(f"[ComfyUI-Manager] {e}") + return ManagedResult('fail').fail(str(e)) + await unified_manager.reload('cache') await unified_manager.get_custom_nodes('default', 'cache') @@ -3291,8 +3342,6 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): print("Restore snapshot.") - postinstalls = [] - with open(snapshot_path, 'r', encoding="UTF-8") as snapshot_file: if snapshot_path.endswith('.json'): info = json.load(snapshot_file) @@ -3327,11 +3376,17 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): skip_node_packs.append(k) for x in todo_disable: - unified_manager.unified_disable(x, False) - disabled_repos.append(x) + if move(unified_manager.unified_disable, x, False).result: + disabled_repos.append(x) + else: + failed.append(x) for x in todo_checkout: - ps = unified_manager.cnr_switch_version(x[0], x[1], instant_execution=True, no_deps=True, return_postinstall=False) + try: + ps = unified_manager.cnr_switch_version(x[0], x[1], instant_execution=True, no_deps=True, return_postinstall=False) + except Exception as e: + # the request to the registry raises when it cannot be reached; the restore goes on with the other packs + ps = ManagedResult('switch-cnr').fail(str(e)) if ps.action == 'switch-cnr' and ps.result: switched_node_packs.append(f"{x[0]}@{x[1]}") elif ps.action == 'skip': @@ -3346,18 +3401,17 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): if 'comfyui-manager' in k or k in checked_cnr: continue - ps = await unified_manager.install_by_id(k, version_spec=v, instant_execution=True, return_postinstall=True) - if ps.action == 'install-cnr' and ps.result: - installed_node_packs.append(f"{k}@{v}") - elif not ps.result: + try: + ps = await unified_manager.install_by_id(k, version_spec=v, instant_execution=True, return_postinstall=True) + except Exception as e: + ps = ManagedResult('install-cnr').fail(str(e)) + if not ps.result: failed.append(f"{k}@{v}") logging.error(ps.msg) - - if ps is not None and ps.result: - if hasattr(ps, 'postinstall'): - postinstalls.append(ps.postinstall) - else: - print("cm-cli: unexpected [0001]") + elif not run_postinstall(ps): + failed.append(f"{k}@{v}") + elif ps.action == 'install-cnr': + installed_node_packs.append(f"{k}@{v}") # for nightly restore _git_info = info.get('git_custom_nodes') @@ -3410,17 +3464,23 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): processed_urls.append(normalized_url) for x in todo_disable: - unified_manager.unified_disable(x, False) - disabled_repos.append(x) + if move(unified_manager.unified_disable, x, False).result: + disabled_repos.append(x) + else: + failed.append(x) for x in todo_enable: - res = unified_manager.unified_enable(x[0], 'nightly') + res = move(unified_manager.unified_enable, x[0], 'nightly') is_switched = False - if res and res.target: + if res.result and res.target: is_switched = repo_switch_commit(res.target, x[1]) - if is_switched: + if not res.result: + failed.append(x[0]) + elif is_switched is None: + failed.append(f"{x[0]}@{x[1]}") + elif is_switched: checkout_repos.append(f"{x[0]}@{x[1]}") else: enabled_repos.append(x[0]) @@ -3430,15 +3490,30 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): if is_switched: checkout_repos.append(f"{x[0]}@{x[1]}") + elif is_switched is None: + failed.append(f"{x[0]}@{x[1]}") for x in git_info.keys(): normalized_url = git_utils.normalize_url(x) cnr = unified_manager.repo_cnr_map.get(normalized_url) if cnr is not None: pack_id = cnr['id'] - res = await unified_manager.install_by_id(pack_id, 'nightly', instant_execution=True, no_deps=False, return_postinstall=False) + # the install script runs after the checkout, for the files of the pinned commit; + # a clone that cannot be checked out stays, so it runs for that one as well + try: + res = await unified_manager.install_by_id(pack_id, 'nightly', instant_execution=True, no_deps=False, return_postinstall=True) + except Exception as e: + logging.error(f"[ComfyUI-Manager] {e}") + res = ManagedResult('fail').fail(str(e)) if res.action == 'install-git' and res.result: - cloned_repos.append(pack_id) + is_switched = repo_switch_commit(unified_manager.active_nodes[pack_id][1], git_info[x]['hash']) + is_installed = run_postinstall(res) + if is_switched is None: + failed.append(f"{pack_id}@{git_info[x]['hash']}") + elif not is_installed: + failed.append(pack_id) + else: + cloned_repos.append(pack_id) elif res.action == 'skip': skip_node_packs.append(pack_id) elif not res.result: @@ -3456,6 +3531,9 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): processed_urls = [] for k2, v2 in unified_manager.unknown_active_nodes.items(): + if 'comfyui-manager' in k2.lower(): + continue + repo_url = resolve_giturl_from_path(v2[1]) if repo_url is None: @@ -3467,7 +3545,7 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): todo_disable.append(k2) else: commit_hash = git_info[normalized_url]['hash'] - todo_checkout.append((k2, commit_hash)) + todo_checkout.append((k2, v2[1], commit_hash)) processed_urls.append(normalized_url) for k2, v2 in unified_manager.unknown_inactive_nodes.items(): @@ -3484,28 +3562,36 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): processed_urls.append(normalized_url) for x in todo_disable: - unified_manager.unified_disable(x, True) - disabled_repos.append(x) + if move(unified_manager.unified_disable, x, True).result: + disabled_repos.append(x) + else: + failed.append(x) for x in todo_enable: - res = unified_manager.unified_enable(x[0], 'unknown') + res = move(unified_manager.unified_enable, x[0], 'unknown') is_switched = False - if res and res.target: + if res.result and res.target: is_switched = repo_switch_commit(res.target, x[1]) - if is_switched: + if not res.result: + failed.append(x[0]) + elif is_switched is None: + failed.append(f"{x[0]}@{x[1]}") + elif is_switched: checkout_repos.append(f"{x[0]}@{x[1]}") else: enabled_repos.append(x[0]) - for x in todo_checkout: - is_switched = repo_switch_commit(x[0], x[1]) + for name, path, commit_hash in todo_checkout: + is_switched = repo_switch_commit(path, commit_hash) if is_switched: - checkout_repos.append(f"{x[0]}@{x[1]}") + checkout_repos.append(f"{name}@{commit_hash}") + elif is_switched is None: + failed.append(f"{name}@{commit_hash}") else: - skip_node_packs.append(x[0]) + skip_node_packs.append(name) for x in processed_urls: if x in git_info: @@ -3517,8 +3603,19 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): repo_name = repo_name[:-4] to_path = os.path.join(get_default_custom_nodes_path(), repo_name) - unified_manager.repo_install(repo_url, to_path, instant_execution=True, no_deps=False, return_postinstall=False) - cloned_repos.append(repo_name) + res = unified_manager.repo_install(repo_url, to_path, instant_execution=True, no_deps=False, return_postinstall=True) + if not res.result: + failed.append(repo_name) + continue + + is_switched = repo_switch_commit(to_path, git_info[repo_url]['hash']) + is_installed = run_postinstall(res) + if is_switched is None: + failed.append(f"{repo_name}@{git_info[repo_url]['hash']}") + elif not is_installed: + failed.append(repo_name) + else: + cloned_repos.append(repo_name) manager_util.restore_pip_snapshot(pips, git_helper_extras) @@ -3537,7 +3634,7 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): print(f"[ DISABLED ] {x}") for x in skip_node_packs: print(f"[ SKIPPED ] {x}") - for x in failed: + for x in dict.fromkeys(failed): # a pack that failed at two steps is listed once print(f"[ FAILED ] {x}") # if is_failed: @@ -3657,12 +3754,19 @@ def resolve_giturl_from_path(fullpath): def repo_switch_commit(repo_path, commit_hash): + if commit_hash == 'unknown': + return False + try: repo = git.Repo(repo_path) - if repo.head.commit.hexsha == commit_hash: - return False + if repo.head.commit.hexsha != commit_hash: + repo.git.checkout(commit_hash) + repo.git.submodule('update', '--init', '--recursive') + return True - repo.git.checkout(commit_hash) - return True + # an update of the submodules that failed is done again; one that was never fetched is left alone + if os.path.exists(os.path.join(repo_path, '.gitmodules')): + repo.git.submodule('update', '--recursive') + return False except: return None diff --git a/glob/manager_server.py b/glob/manager_server.py index 8bae983ae6..0095a6f347 100644 --- a/glob/manager_server.py +++ b/glob/manager_server.py @@ -314,19 +314,10 @@ def print_comfyui_version(): logging.info("### ComfyUI Revision: UNKNOWN (The currently installed ComfyUI is not a Git repository)") +core.setup_git_executable() print_comfyui_version() core.check_invalid_nodes() - -def setup_environment(): - git_exe = core.get_config()['git_exe'] - - if git_exe != '': - git.Git().update_environment(GIT_PYTHON_GIT_EXECUTABLE=git_exe) - - -setup_environment() - # Expand Server api from aiohttp import web diff --git a/glob/manager_util.py b/glob/manager_util.py index 0e7a658102..95c2a52d30 100644 --- a/glob/manager_util.py +++ b/glob/manager_util.py @@ -10,6 +10,7 @@ import json import threading import os +import shutil from datetime import datetime import subprocess import sys @@ -1109,3 +1110,23 @@ def restore_pip_snapshot(pips, options): failed.append(x) print(f"Installation failed for pip packages: {failed}") + + +def find_git_executable(git_exe, timeout=10): + """ + the full path of the program `git_exe` if it answers `version` as git does; None otherwise + """ + # a name without a directory is looked up on PATH; GitPython would take it as a file of the current directory + path = shutil.which(git_exe) + if path is None: + return None + + try: + res = subprocess.run([path, 'version'], capture_output=True, stdin=subprocess.DEVNULL, timeout=timeout) + except (OSError, subprocess.SubprocessError): + return None + + if res.returncode != 0 or not res.stdout.startswith(b'git version'): + return None + + return os.path.abspath(path) diff --git a/prestartup_script.py b/prestartup_script.py index 11e67b094e..90491e5c90 100644 --- a/prestartup_script.py +++ b/prestartup_script.py @@ -123,8 +123,18 @@ def check_file_logging(): if 'file_logging' in default_conf and default_conf['file_logging'].lower() == 'false': enable_file_logging = False +def set_git_executable(): + # GitPython reads this when it is imported, so that a git that is not on PATH can be named + # read as written: a path of Windows may hold a `%`, which the parser takes as the start of a reference + git_exe = default_conf.get('git_exe', '', raw=True) if 'git_exe' in default_conf else '' + if git_exe != '' and _has_system_user_api: + git_exe = manager_util.find_git_executable(git_exe) + if git_exe is not None: + os.environ['GIT_PYTHON_GIT_EXECUTABLE'] = git_exe + read_config() +set_git_executable() read_uv_mode() security_check.security_check() check_file_logging() @@ -635,6 +645,8 @@ def msg_capture(stream, prefix): print("[ComfyUI-Manager] Restore snapshot.") new_env = os.environ.copy() + if 'COMFYUI_PATH' not in new_env: + new_env["COMFYUI_PATH"] = comfy_path if 'COMFYUI_FOLDERS_BASE_PATH' not in new_env: new_env["COMFYUI_FOLDERS_BASE_PATH"] = comfy_path @@ -644,7 +656,9 @@ def msg_capture(stream, prefix): new_env['_COMFYUI_MANAGER_CNR_ALLOW_FLAGGED'] = str( allow_flagged or manager_util.is_loopback_listener(args.listen) ).lower() - cmd_str = [sys.executable, cm_cli_path, 'restore-snapshot', restore_snapshot_path] + # cm-cli does not see the server's arguments, such as --base-directory + cmd_str = [sys.executable, cm_cli_path, 'restore-snapshot', restore_snapshot_path, + '--user-directory', folder_paths.get_user_directory(), '--restore-to', custom_nodes_base_path] exit_code = process_wrap(cmd_str, custom_nodes_base_path, handler=msg_capture, env=new_env) if exit_code != 0: diff --git a/tests/test_git_exe_setting.py b/tests/test_git_exe_setting.py new file mode 100644 index 0000000000..d6dc15be5e --- /dev/null +++ b/tests/test_git_exe_setting.py @@ -0,0 +1,324 @@ +"""`git_exe` chooses the git executable that GitPython runs. + +Each case runs a real function of the Manager with the real GitPython. The +configured executable is a script that records its arguments and then runs the +git of the test environment. `git_exe` is used only when the configuration file +is in a protected directory and the value is a working git. +""" +import ast +import configparser +import logging +import os +import shutil +import sys +from pathlib import Path +from types import SimpleNamespace + +import git +import pytest + +from manager_test_utils import GLOB_DIR, load_manager_util +from test_install_flags_config import GIT_LOCATION, _run_child + +pytestmark = pytest.mark.skipif(os.name == "nt", reason="the executables of the cases are shell scripts") + +ROOT = GLOB_DIR.parent +NOT_A_GIT = { + "a path that does not exist": None, + "a program that fails": "#!/bin/sh\nexit 3\n", + "a program that is not git": "#!/bin/sh\necho hello\n", + "a program that waits": "#!/bin/sh\nexec sleep 30\n", + "a program that prints bytes that are not text": "#!/bin/sh\nprintf '\\377\\376'\n", + "a file that cannot be run": "not a program\n", +} + + +def take(path, name, namespace): + """Define the function of the file in the namespace and return it.""" + function = [node for node in ast.parse(path.read_text(encoding="utf-8")).body + if isinstance(node, ast.FunctionDef) and node.name == name] + assert len(function) == 1 + exec(compile(ast.Module(body=function, type_ignores=[]), str(path), "exec"), namespace) + return namespace[name] + + +@pytest.fixture +def manager_util(monkeypatch): + module = load_manager_util() + find = module.find_git_executable + monkeypatch.setattr(module, "find_git_executable", lambda git_exe: find(git_exe, timeout=2)) + return module + + +@pytest.fixture(autouse=True) +def default_git(monkeypatch): + # a git that a case runs is to find its repository by the directory it is run in + for name in GIT_LOCATION: + monkeypatch.delenv(name, raising=False) + yield + # the variables a case set are removed before GitPython looks for its git again + monkeypatch.undo() + git.refresh() + + +@pytest.fixture +def recording_git(tmp_path): + """A git executable that appends each command line to `calls`.""" + calls = tmp_path / "calls" + executable = tmp_path / "recording-git" + executable.write_text(f'#!/bin/sh\necho "$@" >> "{calls}"\nexec "{shutil.which("git")}" "$@"\n') + executable.chmod(0o755) + return SimpleNamespace(path=str(executable), + calls=lambda: calls.read_text().splitlines() if calls.exists() else [], + forget=lambda: calls.unlink(missing_ok=True)) + + +@pytest.fixture(params=NOT_A_GIT) +def not_a_git(request, tmp_path): + path = tmp_path / "not-a-git" + if NOT_A_GIT[request.param] is not None: + path.write_text(NOT_A_GIT[request.param]) + path.chmod(0o755) + return str(path) + + +def setup_git_executable(manager_util, git_exe, protected=True): + """Run `setup_git_executable` of `manager_core.py` with `git_exe` as the configured value.""" + take(GLOB_DIR / "manager_core.py", "setup_git_executable", { + "git": git, "logging": logging, "manager_util": manager_util, + "manager_migration": SimpleNamespace(has_system_user_api=lambda: protected), + "get_config": lambda: {"git_exe": git_exe}, + })() + + +def test_configured_git_is_run(manager_util, recording_git, tmp_path): + setup_git_executable(manager_util, recording_git.path) + recording_git.forget() + + git.Repo.init(tmp_path / "repo") + + assert any(line.startswith("init") for line in recording_git.calls()) + assert (tmp_path / "repo" / ".git").is_dir() + + +def test_empty_setting_keeps_the_default_git(manager_util, recording_git, tmp_path, caplog): + default = git.Git.GIT_PYTHON_GIT_EXECUTABLE + + with caplog.at_level(logging.WARNING): + setup_git_executable(manager_util, "") + + assert caplog.text == "" + assert git.Git.GIT_PYTHON_GIT_EXECUTABLE == default + + +def test_setting_that_is_no_git_is_reported_and_the_default_kept(manager_util, not_a_git, tmp_path, caplog): + default = git.Git.GIT_PYTHON_GIT_EXECUTABLE + flags = git.remote.FetchInfo._flag_map.copy() + + with caplog.at_level(logging.WARNING): + setup_git_executable(manager_util, not_a_git) + + assert not_a_git in caplog.text + assert git.Git.GIT_PYTHON_GIT_EXECUTABLE == default and git.remote.FetchInfo._flag_map == flags + git.Repo.init(tmp_path / "repo") + assert (tmp_path / "repo" / ".git").is_dir() + + +def test_setting_of_an_unprotected_configuration_is_not_used(manager_util, recording_git, tmp_path, caplog): + with caplog.at_level(logging.WARNING): + setup_git_executable(manager_util, recording_git.path, protected=False) + + git.Repo.init(tmp_path / "repo") + + assert recording_git.calls() == [] + assert "git_exe" in caplog.text + + +def test_git_is_chosen_before_the_first_git_command_of_the_server(): + """The module runs its start-up statements in the order of the file; the ComfyUI version is read with git.""" + body = ast.parse((GLOB_DIR / "manager_server.py").read_text(encoding="utf-8")).body + calls = [ast.unparse(node.value.func) for node in body if isinstance(node, ast.Expr) and isinstance(node.value, ast.Call)] + steps = [call for call in calls if call in ("core.setup_git_executable", "print_comfyui_version")] + + assert steps == ["core.setup_git_executable", "print_comfyui_version"] + + +def script_env_of(manager_util, git_exe): + return take(GLOB_DIR / "manager_core.py", "get_script_env", { + "os": os, "comfy_path": "/comfy", "manager_util": manager_util, + "manager_migration": SimpleNamespace(has_system_user_api=lambda: True), + "get_config": lambda: {"git_exe": git_exe}, "working_git_exe": {}, + }) + + +@pytest.mark.parametrize("protected", [True, False]) +def test_scripts_are_given_the_git_of_a_protected_configuration(manager_util, recording_git, protected): + get_script_env = take(GLOB_DIR / "manager_core.py", "get_script_env", { + "os": os, "comfy_path": "/comfy", "manager_util": manager_util, + "manager_migration": SimpleNamespace(has_system_user_api=lambda: protected), + "get_config": lambda: {"git_exe": recording_git.path}, "working_git_exe": {}, + }) + + assert get_script_env().get("GIT_EXE_PATH") == (recording_git.path if protected else None) + + +@pytest.mark.parametrize("git_exe", ["", None]) +def test_scripts_are_given_no_git_without_the_setting(manager_util, git_exe): + get_script_env = script_env_of(manager_util, git_exe) + + assert get_script_env().get("GIT_EXE_PATH") is None + + +def test_scripts_are_given_no_git_that_is_not_one(manager_util, not_a_git): + get_script_env = script_env_of(manager_util, not_a_git) + + assert get_script_env().get("GIT_EXE_PATH") is None + + +def test_git_for_scripts_is_checked_once(manager_util, recording_git): + """Every command of an install asks for the environment; the check starts a process.""" + get_script_env = script_env_of(manager_util, recording_git.path) + + assert [get_script_env().get("GIT_EXE_PATH") for _ in range(3)] == [recording_git.path] * 3 + assert recording_git.calls() == ["version"] + + +def test_git_for_scripts_that_was_no_git_is_checked_again(manager_util, recording_git, tmp_path): + later = tmp_path / "installed-later" + get_script_env = script_env_of(manager_util, str(later)) + + assert get_script_env().get("GIT_EXE_PATH") is None + shutil.copy(recording_git.path, later) + assert get_script_env().get("GIT_EXE_PATH") == str(later) + + +def set_git_executable(manager_util, monkeypatch, git_exe, protected=True): + """Run `set_git_executable` of `prestartup_script.py`; the variable GitPython reads when it is imported.""" + # set and removed, so that the variable the function sets is removed after the case, also when there was none + monkeypatch.setenv("GIT_PYTHON_GIT_EXECUTABLE", "") + monkeypatch.delenv("GIT_PYTHON_GIT_EXECUTABLE") + config = configparser.ConfigParser(strict=False) + config.read_string("[default]\n" + ("" if git_exe is None else f"git_exe = {git_exe}\n")) + take(ROOT / "prestartup_script.py", "set_git_executable", { + "os": os, "manager_util": manager_util, "_has_system_user_api": protected, "default_conf": config["default"], + })() + return os.environ.get("GIT_PYTHON_GIT_EXECUTABLE") + + +def test_git_is_known_before_gitpython_is_imported(manager_util, monkeypatch, recording_git): + assert set_git_executable(manager_util, monkeypatch, recording_git.path) == recording_git.path + + +def test_case_that_set_the_variable_left_the_default_git(): + """Runs after the case above, which makes the function set the variable that GitPython reads.""" + assert os.environ.get("GIT_PYTHON_GIT_EXECUTABLE") is None + assert git.Git.GIT_PYTHON_GIT_EXECUTABLE in ("git", shutil.which("git")) + + +def test_git_in_a_directory_with_a_percent_sign_is_known(manager_util, monkeypatch, recording_git, tmp_path): + """A path of Windows may hold a `%`, which the parser takes as the start of a reference.""" + (tmp_path / "100%").mkdir() + path = shutil.copy(recording_git.path, tmp_path / "100%" / "git") + + assert set_git_executable(manager_util, monkeypatch, str(path)) == str(path) + + +def test_start_up_without_a_configuration_file_sets_nothing(manager_util, monkeypatch): + monkeypatch.setenv("GIT_PYTHON_GIT_EXECUTABLE", "") + monkeypatch.delenv("GIT_PYTHON_GIT_EXECUTABLE") + take(ROOT / "prestartup_script.py", "set_git_executable", { + "os": os, "manager_util": manager_util, "_has_system_user_api": True, "default_conf": {}, + })() + + assert os.environ.get("GIT_PYTHON_GIT_EXECUTABLE") is None + + +def test_setting_with_a_percent_sign_is_read_as_written_and_the_others_kept(): + config = _run_child(r''' + write_ini("[default]\ngit_exe = %LOCALAPPDATA%\\Git\\bin\\git.exe\ndb_mode = local\nnetwork_mode = offline\n") + cfg = fresh_read() + print(json.dumps([cfg["git_exe"], cfg["db_mode"], cfg["network_mode"]])) + ''') + + assert config == ["%LOCALAPPDATA%\\Git\\bin\\git.exe", "local", "offline"] + + +@pytest.mark.parametrize("git_exe", ["", None]) +def test_no_setting_leaves_the_environment_alone(manager_util, monkeypatch, git_exe): + assert set_git_executable(manager_util, monkeypatch, git_exe) is None + + +def test_setting_of_an_unprotected_configuration_is_not_put_in_the_environment(manager_util, monkeypatch, recording_git): + assert set_git_executable(manager_util, monkeypatch, recording_git.path, protected=False) is None + + +def test_setting_that_is_no_git_is_not_put_in_the_environment(manager_util, monkeypatch, not_a_git): + assert set_git_executable(manager_util, monkeypatch, not_a_git) is None + + +def helper_setup_environment(git_exe): + take(ROOT / "git_helper.py", "setup_environment", {"git": git, "sys": sys, "git_exe_path": git_exe})() + + +def test_helper_runs_the_git_it_is_given(recording_git, tmp_path): + helper_setup_environment(recording_git.path) + recording_git.forget() + + git.Repo.init(tmp_path / "repo") + + assert any(line.startswith("init") for line in recording_git.calls()) + + +@pytest.mark.parametrize("git_exe", ["", None]) +def test_helper_without_a_git_keeps_the_default(git_exe, capsys): + default = git.Git.GIT_PYTHON_GIT_EXECUTABLE + + helper_setup_environment(git_exe) + + assert capsys.readouterr().err == "" and git.Git.GIT_PYTHON_GIT_EXECUTABLE == default + + +@pytest.mark.parametrize("kind", [kind for kind in NOT_A_GIT if kind != "a program that waits"]) +def test_helper_reports_what_is_no_git_and_keeps_the_default(kind, tmp_path, capsys): + default = git.Git.GIT_PYTHON_GIT_EXECUTABLE + path = tmp_path / "not-a-git" + if NOT_A_GIT[kind] is not None: + path.write_text(NOT_A_GIT[kind]) + path.chmod(0o755) + + helper_setup_environment(str(path)) + + assert str(path) in capsys.readouterr().err + assert git.Git.GIT_PYTHON_GIT_EXECUTABLE == default + git.Repo.init(tmp_path / "repo") + assert Path(tmp_path / "repo" / ".git").is_dir() + + +def test_git_named_by_a_command_on_path_is_run_by_its_full_path(manager_util, tmp_path, monkeypatch): + """GitPython takes a name without a directory as a file of the current directory.""" + monkeypatch.chdir(tmp_path) + + setup_git_executable(manager_util, "git") + + assert git.Git.GIT_PYTHON_GIT_EXECUTABLE == shutil.which("git") + git.Repo.init(tmp_path / "repo") + assert (tmp_path / "repo" / ".git").is_dir() + + +def test_scripts_and_gitpython_are_given_the_full_path_of_a_command_on_path(manager_util, monkeypatch): + get_script_env = script_env_of(manager_util, "git") + + assert get_script_env().get("GIT_EXE_PATH") == shutil.which("git") + assert set_git_executable(manager_util, monkeypatch, "git") == shutil.which("git") + + +def test_setting_saved_beside_a_git_exe_with_a_percent_sign_keeps_it(): + saved = _run_child(r''' + write_ini("[default]\ngit_exe = %LOCALAPPDATA%\\Git\\bin\\git.exe\ndb_mode = local\n") + cfg = fresh_read() + cfg["db_mode"] = "remote" + manager_core.write_config() + print(json.dumps(sorted(line for line in open(CONFIG_PATH).read().splitlines() if line.startswith(("git_exe", "db_mode"))))) + ''') + + assert saved == ["db_mode = remote", "git_exe = %LOCALAPPDATA%\\Git\\bin\\git.exe"] diff --git a/tests/test_install_flags_config.py b/tests/test_install_flags_config.py index ac20bce3ef..e98bb04ee1 100644 --- a/tests/test_install_flags_config.py +++ b/tests/test_install_flags_config.py @@ -17,6 +17,7 @@ parent to assert. """ import json +import os import subprocess import sys import textwrap @@ -24,6 +25,10 @@ from pathlib import Path REPO_ROOT = Path(__file__).resolve().parent.parent +# what `git rev-parse --local-env-vars` lists: with one of them, git does not work on the repository of its directory +GIT_LOCATION = ("GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_CONFIG", "GIT_CONFIG_PARAMETERS", "GIT_CONFIG_COUNT", "GIT_OBJECT_DIRECTORY", + "GIT_DIR", "GIT_WORK_TREE", "GIT_IMPLICIT_WORK_TREE", "GIT_GRAFT_FILE", "GIT_INDEX_FILE", "GIT_NO_REPLACE_OBJECTS", + "GIT_REPLACE_REF_BASE", "GIT_PREFIX", "GIT_SHALLOW_FILE", "GIT_COMMON_DIR") _CHILD_PREAMBLE = textwrap.dedent( """ @@ -65,6 +70,8 @@ def _run_child(body): text=True, timeout=180, cwd=str(REPO_ROOT), + # a git that a scenario runs is to find its repository by the directory it is run in + env={name: value for name, value in os.environ.items() if name not in GIT_LOCATION}, ) if proc.returncode != 0: raise AssertionError( diff --git a/tests/test_restore_summary.py b/tests/test_restore_summary.py new file mode 100644 index 0000000000..bee68064d8 --- /dev/null +++ b/tests/test_restore_summary.py @@ -0,0 +1,489 @@ +"""The summary of a snapshot restore lists a step that failed as failed. + +The restore runs in the isolated child of tests/test_install_flags_config.py, against the +real glob/manager_core. What the restore asks the manager to do is replaced, and answers +with a success or a failure. +""" + +import subprocess + +import pytest + +from test_snapshot_restore_pins import run + +URL = 'https://github.com/owner/fixture' +HASH = '0123456789abcdef0123456789abcdef01234567' +SUMMARY = ''' +import contextlib, io + +HASH = '0123456789abcdef0123456789abcdef01234567' + +def summary(**snapshot): + out = io.StringIO() + with contextlib.redirect_stdout(out): + restore(**snapshot) + return [line.strip() for line in out.getvalue().splitlines() if line.startswith('[ ')] + +def pinned(url): + return {'git_custom_nodes': {url: {'hash': HASH, 'disabled': False}}} + +def git_pack(name, url): + """A directory whose .git/config names `url`, as a git node pack the registry does not know.""" + path = nodes / name + (path / '.git').mkdir(parents=True) + (path / '.git/config').write_text('[remote "origin"]\\n\\turl = %s\\n' % url) + return url, str(path) + +def answer(action, ok): + result = core.ManagedResult(action) + return Mock(return_value=result if ok else result.fail('failed')) + +def repository(path): + """A git repository with two commits at `path`; their hashes.""" + path.mkdir(parents=True) + commits = [] + for version in (1, 2): + (path / '__init__.py').write_text('VERSION = %d' % version) + for args in (['init', '-q'], ['add', '.'], + ['-c', 'user.name=test', '-c', 'user.email=test@example.invalid', 'commit', '-q', '-m', 'version %d' % version]): + subprocess.run(['git', *args], cwd=path, check=True, capture_output=True) + commits.append(head(path)) + return commits + +def head(path): + return subprocess.run(['git', 'rev-parse', 'HEAD'], cwd=path, check=True, capture_output=True, text=True).stdout.strip() +''' + + +def lines(body): + return run(SUMMARY + body + '\nprint(json.dumps(result))\n') + + +@pytest.mark.parametrize('cloned, line', [(True, '[ INSTALLED ] fixture'), (False, '[ FAILED ] fixture')]) +def test_clone_follows_its_result(cloned, line): + assert lines(f''' +manager.repo_install = answer('install-git', {cloned}) +core.repo_switch_commit = Mock(return_value=True) +result = summary(**pinned('{URL}.git')) + [manager.repo_install.call_args.args[1] == str(nodes / 'fixture')] +''') == [line, True] + + +def test_url_that_is_not_cloned_is_reported_as_failed(): + assert lines(''' +result = summary(**pinned('/home/someone/repos/fixture')) + [(nodes / 'fixture').exists()] +''') == ['[ FAILED ] fixture', False] + + +def test_cloned_pack_is_checked_out_at_the_pinned_commit(): + assert lines(f''' +commits = repository(pathlib.Path(tmp) / 'origin') +def clone(url, path, **options): + subprocess.run(['git', 'clone', '-q', str(pathlib.Path(tmp) / 'origin'), path], check=True, capture_output=True) + return core.ManagedResult('install-git') +manager.repo_install = clone +result = summary(git_custom_nodes={{'{URL}': {{'hash': commits[0], 'disabled': False}}}}) +result += [head(nodes / 'fixture') == commits[0], (nodes / 'fixture/__init__.py').read_text()] +''') == ['[ INSTALLED ] fixture', True, 'VERSION = 1'] + + +def test_cloned_registry_pack_is_checked_out_at_the_pinned_commit(): + assert lines(f''' +commits = repository(pathlib.Path(tmp) / 'origin') +async def install(pack_id, version_spec, **options): + subprocess.run(['git', 'clone', '-q', str(pathlib.Path(tmp) / 'origin'), nodes / pack_id], check=True, capture_output=True) + manager.active_nodes[pack_id] = (version_spec, str(nodes / pack_id)) + return core.ManagedResult('install-git') +manager.repo_cnr_map['{URL}'] = {{'id': 'fixture'}} +manager.install_by_id = install +result = summary(git_custom_nodes={{'{URL}': {{'hash': commits[0], 'disabled': False}}}}) +result += [head(nodes / 'fixture') == commits[0]] +''') == ['[ INSTALLED ] fixture', True] + + +def test_cloned_pack_that_cannot_be_checked_out_is_reported_as_failed(): + assert lines(f''' +manager.repo_install = answer('install-git', True) +result = summary(**pinned('{URL}')) +''') == [f'[ FAILED ] fixture@{HASH}'] + + +@pytest.mark.parametrize('disabled, line', [(True, '[ DISABLED ] fixture'), (False, '[ FAILED ] fixture')]) +def test_disabling_a_git_pack_that_is_not_in_the_snapshot_follows_its_result(disabled, line): + assert lines(f''' +manager.unknown_active_nodes['fixture'] = git_pack('fixture', '{URL}') +manager.unified_disable = answer('disable', {disabled}) +result = summary() +''') == [line] + + +@pytest.mark.parametrize('disabled, line', [(True, '[ DISABLED ] fixture'), (False, '[ FAILED ] fixture')]) +def test_disabling_a_registry_pack_that_is_not_in_the_snapshot_follows_its_result(disabled, line): + assert lines(f''' +manager.active_nodes['fixture'] = ('1.0.0', str(nodes / 'fixture')) +manager.unified_disable = answer('disable', {disabled}) +result = summary() +''') == [line] + + +@pytest.mark.parametrize('name', ['ComfyUI-Manager', 'comfyui-manager']) +def test_the_manager_itself_is_not_listed(name): + assert lines(f''' +manager.unknown_active_nodes['{name}'] = git_pack('{name}', 'https://github.com/ltdrdata/ComfyUI-Manager') +manager.unified_disable = answer('disable', True) +result = summary() + [manager.unified_disable.call_count] +''') == [0] + + +@pytest.mark.parametrize('enabled, line', [(True, '[ ENABLED ] fixture'), (False, '[ FAILED ] fixture')]) +def test_enabling_a_git_pack_of_the_snapshot_follows_its_result(enabled, line): + assert lines(f''' +manager.unknown_inactive_nodes['fixture'] = git_pack('.disabled/fixture', '{URL}') +manager.unified_enable = answer('enable', {enabled}) +result = summary(**pinned('{URL}')) +''') == [line] + + +def test_installed_git_pack_the_registry_does_not_know_is_checked_out_at_the_pinned_commit(): + assert lines(f''' +commits = repository(nodes / 'fixture') +subprocess.run(['git', 'remote', 'add', 'origin', '{URL}'], cwd=nodes / 'fixture', check=True) +manager.unknown_active_nodes['fixture'] = ('{URL}', str(nodes / 'fixture')) +result = summary(git_custom_nodes={{'{URL}': {{'hash': commits[0], 'disabled': False}}}}) +result = [line.replace(commits[0], 'PINNED') for line in result] + [head(nodes / 'fixture') == commits[0]] +''') == ['[ CHECKOUT ] fixture@PINNED', True] + + +def test_checkout_that_fails_is_reported_as_failed(): + assert lines(f''' +manager.unknown_active_nodes['fixture'] = git_pack('fixture', '{URL}') +result = summary(**pinned('{URL}')) +''') == [f'[ FAILED ] fixture@{HASH}'] + + +def test_pack_without_a_commit_in_the_snapshot_is_left_where_it_is(): + assert lines(f''' +commits = repository(nodes / 'fixture') +subprocess.run(['git', 'remote', 'add', 'origin', '{URL}'], cwd=nodes / 'fixture', check=True) +manager.unknown_active_nodes['fixture'] = ('{URL}', str(nodes / 'fixture')) +result = summary(git_custom_nodes={{'{URL}': {{'hash': 'unknown', 'disabled': False}}}}) + [head(nodes / 'fixture') == commits[1]] +''') == ['[ SKIPPED ] fixture', True] + + +def test_cloned_pack_without_a_commit_in_the_snapshot_is_installed(): + assert lines(f''' +manager.repo_install = answer('install-git', True) +result = summary(git_custom_nodes={{'{URL}': {{'hash': 'unknown', 'disabled': False}}}}) +''') == ['[ INSTALLED ] fixture'] + + +@pytest.mark.parametrize('installed, line', [(True, '[ INSTALLED ] fixture'), (False, '[ FAILED ] fixture')]) +def test_install_script_of_a_cloned_pack_runs_at_the_pinned_commit(installed, line): + assert lines(f''' +commits = repository(pathlib.Path(tmp) / 'origin') +ran_at = [] +def clone(url, path, return_postinstall=False, **options): + subprocess.run(['git', 'clone', '-q', str(pathlib.Path(tmp) / 'origin'), path], check=True, capture_output=True) + def postinstall(): + ran_at.append(head(path)) + return {installed} + result = core.ManagedResult('install-git') + return result.with_postinstall(postinstall) if return_postinstall else (result if postinstall() else result.fail('install script')) +manager.repo_install = clone +result = summary(git_custom_nodes={{'{URL}': {{'hash': commits[0], 'disabled': False}}}}) + [ran_at == [commits[0]]] +''') == [line, True] + + +def test_commit_of_a_pack_whose_refs_are_packed_is_read(): + assert lines(''' +import git_utils +commits = repository(nodes / 'fixture') +subprocess.run(['git', 'pack-refs', '--all'], cwd=nodes / 'fixture', check=True) +with open(nodes / 'fixture/.git/packed-refs', 'a') as packed: + packed.write('\\n') +branch = (nodes / 'fixture/.git/HEAD').read_text().strip()[5:] +result = [(nodes / 'fixture/.git' / branch).exists(), git_utils.get_commit_hash(str(nodes / 'fixture')) == commits[1]] +''') == [False, True] + + +def test_commit_is_read_beside_refs_that_are_not_utf8(): + assert lines(''' +import git_utils +commits = repository(nodes / 'fixture') +subprocess.run(['git', 'pack-refs', '--all'], cwd=nodes / 'fixture', check=True) +packed = nodes / 'fixture/.git/packed-refs' +packed.write_bytes(b'0123456789abcdef0123456789abcdef01234567 refs/heads/\\xff\\xfe\\n' + packed.read_bytes()) +result = [git_utils.get_commit_hash(str(nodes / 'fixture')) == commits[1]] +''') == [True] + + +def test_head_that_is_not_utf8_gives_no_commit(): + assert lines(''' +import git_utils +(nodes / 'fixture/.git').mkdir(parents=True) +(nodes / 'fixture/.git/HEAD').write_bytes(b'ref: refs/heads/\\xff\\xfe\\n') +result = [git_utils.get_commit_hash(str(nodes / 'fixture'))] +''') == ['unknown'] + + +def test_packed_ref_is_matched_by_its_bytes(): + assert lines(''' +import git_utils +commits = repository(nodes / 'fixture') +subprocess.run(['git', 'pack-refs', '--all'], cwd=nodes / 'fixture', check=True) +git_dir = nodes / 'fixture/.git' +other = b'0123456789abcdef0123456789abcdef01234567 refs/heads/release-\\xff\\n' +(git_dir / 'packed-refs').write_bytes(other + (git_dir / 'packed-refs').read_bytes()) +(git_dir / 'HEAD').write_bytes(b'ref: refs/heads/release-\\xfe\\n') +result = [git_utils.get_commit_hash(str(nodes / 'fixture'))] +(git_dir / 'packed-refs').write_bytes(other + commits[1].encode() + b' refs/heads/release-\\xfe\\n') +result.append(git_utils.get_commit_hash(str(nodes / 'fixture')) == commits[1]) +''') == ['unknown', True] + + +def test_install_script_that_raises_is_reported_as_failed_and_the_restore_goes_on(): + assert lines(f''' +def clone(url, path, return_postinstall=False, **options): + def postinstall(): + raise subprocess.CalledProcessError(1, ['python', 'install.py']) + return core.ManagedResult('install-git').with_postinstall(postinstall) +manager.repo_install = clone +core.repo_switch_commit = Mock(return_value=True) +result = summary(git_custom_nodes={{'{URL}': {{'hash': HASH, 'disabled': False}}, '{URL}-other': {{'hash': HASH, 'disabled': False}}}}) +''') == ['[ FAILED ] fixture', '[ FAILED ] fixture-other'] + + +def test_install_script_of_a_cloned_pack_runs_also_when_the_commit_cannot_be_checked_out(): + """The clone stays where it is, and a later restore only checks it out.""" + assert lines(f''' +ran = [] +manager.repo_install = lambda url, path, **options: core.ManagedResult('install-git').with_postinstall(lambda: ran.append(path) or True) +result = summary(**pinned('{URL}')) + [ran == [str(nodes / 'fixture')]] +''') == [f'[ FAILED ] fixture@{HASH}', True] + + +def test_install_script_of_a_cloned_registry_pack_runs_also_when_the_commit_cannot_be_checked_out(): + assert lines(f''' +ran = [] +async def install(pack_id, version_spec, **options): + manager.active_nodes[pack_id] = (version_spec, str(nodes / pack_id)) + return core.ManagedResult('install-git').with_postinstall(lambda: ran.append(pack_id) or True) +manager.repo_cnr_map['{URL}'] = {{'id': 'fixture'}} +manager.install_by_id = install +result = summary(**pinned('{URL}')) + [ran] +''') == [f'[ FAILED ] fixture@{HASH}', ['fixture']] + + +@pytest.mark.parametrize('outcome, line', [('return True', '[ INSTALLED ] fixture@1.0.0'), ('return False', '[ FAILED ] fixture@1.0.0'), + ("raise subprocess.CalledProcessError(1, ['python', 'install.py'])", '[ FAILED ] fixture@1.0.0')]) +def test_install_script_of_a_registry_pack_is_run(outcome, line): + assert lines(f''' +ran = [] +def postinstall(): + ran.append('fixture') + {outcome} +async def install(pack_id, **options): + ran.append(options['return_postinstall']) + return core.ManagedResult('install-cnr').with_postinstall(postinstall) +manager.install_by_id = install +result = summary(cnr_custom_nodes={{'fixture': '1.0.0'}}) + [ran] +''') == [line, [True, 'fixture']] + + +def test_git_of_a_case_does_not_inherit_a_repository(monkeypatch): + """Run from a git hook, the tests inherit variables such as GIT_DIR, which point git at the repository of the hook.""" + names = subprocess.run(['git', 'rev-parse', '--local-env-vars'], check=True, capture_output=True, text=True).stdout.split() + for name in names: + monkeypatch.setenv(name, 'elsewhere') + + assert run(f'print(json.dumps(sorted(set(os.environ) & set({names}))))') == [] + + +SUBMODULE = ''' +def git(path, *args): + subprocess.run(['git', '-c', 'protocol.file.allow=always', '-c', 'user.name=test', '-c', 'user.email=test@example.invalid', *args], + cwd=path, check=True, capture_output=True) +library = repository(pathlib.Path(tmp) / 'library') +origin = pathlib.Path(tmp) / 'origin' +origin.mkdir() +git(origin, 'init', '-q') +git(origin, 'submodule', 'add', '-q', str(pathlib.Path(tmp) / 'library'), 'library') +pinned_commits = [] +for commit in library: + git(origin / 'library', 'checkout', '-q', commit) + git(origin, 'commit', '-q', '-a', '-m', 'library at ' + commit) + pinned_commits.append(head(origin)) +''' + + +def test_submodules_of_a_cloned_pack_follow_the_pinned_commit(): + assert lines(SUBMODULE + f''' +def clone(url, path, **options): + git(tmp, 'clone', '-q', '--recursive', str(origin), path) + return core.ManagedResult('install-git') +manager.repo_install = clone +result = summary(git_custom_nodes={{'{URL}': {{'hash': pinned_commits[0], 'disabled': False}}}}) +result += [head(nodes / 'fixture') == pinned_commits[0], head(nodes / 'fixture/library') == library[0]] +''') == ['[ INSTALLED ] fixture', True, True] + + +def test_submodules_are_brought_to_the_commit_of_a_pack_that_is_at_it_already(): + """As after a restore whose checkout succeeded and whose update of the submodules failed.""" + assert lines(SUBMODULE + ''' +git(nodes, 'clone', '-q', '--recursive', str(origin), 'fixture') +git(nodes / 'fixture/library', 'checkout', '-q', library[0]) +result = [core.repo_switch_commit(str(nodes / 'fixture'), pinned_commits[1]), head(nodes / 'fixture/library') == library[1]] +''') == [False, True] + + +def test_submodule_that_was_never_fetched_is_left_alone_in_a_pack_that_is_at_the_commit(): + """A snapshot does not hold the state of the submodules; one that cannot be reached is not a failure of the pack.""" + assert lines(SUBMODULE + ''' +import shutil +git(nodes, 'clone', '-q', str(origin), 'fixture') +shutil.rmtree(pathlib.Path(tmp) / 'library') +result = [core.repo_switch_commit(str(nodes / 'fixture'), pinned_commits[1]), list((nodes / 'fixture/library').iterdir())] +''') == [False, []] + + +def test_registry_pack_whose_install_raises_is_reported_as_failed_and_the_restore_goes_on(): + """Without a connection the request to the registry raises.""" + assert lines(''' +async def install(pack_id, **options): + if pack_id == 'fixture': + raise ConnectionError('the registry cannot be reached') + return core.ManagedResult('install-cnr') +manager.install_by_id = install +result = summary(cnr_custom_nodes={'fixture': '1.0.0', 'other': '1.0.0'}) +''') == ['[ INSTALLED ] other@1.0.0', '[ FAILED ] fixture@1.0.0'] + + +def test_registry_pack_whose_switch_raises_is_reported_as_failed_and_the_restore_goes_on(): + assert lines(''' +async def install(pack_id, **options): + return core.ManagedResult('install-cnr') +manager.active_nodes['fixture'] = ('1.0.0', str(nodes / 'fixture')) +manager.cnr_switch_version = Mock(side_effect=ConnectionError('the registry cannot be reached')) +manager.install_by_id = install +result = summary(cnr_custom_nodes={'fixture': '2.0.0', 'other': '1.0.0'}) +''') == ['[ INSTALLED ] other@1.0.0', '[ FAILED ] fixture@2.0.0'] + + +MOVED = { + 'registry_pack': ("manager.active_nodes['fixture'] = ('1.0.0', str(nodes / 'fixture'))", 'unified_disable', False), + 'registry_git_pack': (f"manager.active_nodes['fixture'] = ('nightly', str(nodes / 'fixture'))\nmanager.repo_cnr_map['{URL}'] = {{'id': 'fixture'}}", + 'unified_disable', False), + 'unknown_git_pack': (f"manager.unknown_active_nodes['fixture'] = git_pack('fixture', '{URL}')", 'unified_disable', False), + 'disabled_registry_git_pack': (f"manager.nightly_inactive_nodes['fixture'] = str(nodes / '.disabled/fixture')\nmanager.repo_cnr_map['{URL}'] = {{'id': 'fixture'}}", + 'unified_enable', True), + 'disabled_unknown_git_pack': (f"manager.unknown_inactive_nodes['fixture'] = git_pack('.disabled/fixture', '{URL}')", 'unified_enable', True), +} + + +@pytest.mark.parametrize('installed', MOVED) +def test_pack_whose_directory_cannot_be_moved_is_reported_as_failed_and_the_restore_goes_on(installed): + """Disabling and enabling move the directory of the node pack.""" + arrange, step, in_snapshot = MOVED[installed] + snapshot = f"**pinned('{URL}')" if in_snapshot else '' + assert lines(f''' +{arrange} +manager.{step} = Mock(side_effect=PermissionError(13, 'Permission denied')) +result = summary({snapshot}) + [manager.{step}.call_count] +''') == ['[ FAILED ] fixture', 1] + + +def test_registry_git_pack_that_cannot_be_enabled_is_listed_once(): + """The install that follows for a node pack of the snapshot enables it as well, and raises for the same reason.""" + arrange = MOVED['disabled_registry_git_pack'][0] + assert lines(f''' +{arrange} +async def install(pack_id, version_spec, **options): + raise PermissionError(13, 'Permission denied') +manager.unified_enable = Mock(side_effect=PermissionError(13, 'Permission denied')) +manager.install_by_id = install +result = summary(**pinned('{URL}')) +''') == ['[ FAILED ] fixture'] + + +def test_git_is_not_run_for_the_submodules_of_a_pack_that_has_none(): + """A restore goes through every git node pack; on Windows each git command takes about half a second.""" + assert lines(''' +import git +commits = repository(nodes / 'fixture') +ran = [] +execute = git.Git.execute +def recorded(self, command, *args, **kwargs): + ran.append(list(command)) + return execute(self, command, *args, **kwargs) +git.Git.execute = recorded +result = [core.repo_switch_commit(str(nodes / 'fixture'), commits[1]), [command for command in ran if 'submodule' in command]] +''') == [False, []] + + +INSTALLS = ''' +import os + +def packs(**files): + for name in ('first', 'second'): + (nodes / name).mkdir() + for file, text in files.items(): + (nodes / name / file).write_text(text) + return str(nodes / 'first'), str(nodes / 'second') + +def installs(*answers): + """Replace what runs an install command; the answers are given in turn, and the directories it ran in are returned.""" + answers, ran = list(answers), [] + + def try_install_script(url, repo_path, install_cmd, instant_execution=False): + ran.append(os.path.basename(repo_path)) + return answers.pop(0) + + core.try_install_script = try_install_script + core.remap_pip_package = lambda package: package + manager_util.PIPFixer = Mock() + manager_util.get_installed_packages = Mock(return_value={}) + return ran +''' + + +def install_steps(body): + return lines(INSTALLS + body) + + +def test_requirement_that_was_not_installed_is_tried_for_the_next_pack(): + assert install_steps(''' +first, second = packs(**{'requirements.txt': 'shareddep==1\\n'}) +ran = installs(False, True) +result = [manager.execute_install_script(None, first), manager.execute_install_script(None, second), ran] +''') == [False, True, ['first', 'second']] + + +def test_requirement_after_one_that_failed_is_tried_for_the_next_pack(): + assert install_steps(''' +first, second = packs(**{'requirements.txt': 'fails==1\\nshareddep==1\\n'}) +ran = installs(False, True, True) +result = [manager.execute_install_script(None, first), manager.execute_install_script(None, second), ran] +''') == [False, True, ['first', 'second', 'second']] + + +def test_requirement_that_was_installed_is_not_installed_twice(): + assert install_steps(''' +first, second = packs(**{'requirements.txt': 'shareddep==1\\n'}) +ran = installs(True) +result = [manager.execute_install_script(None, first), manager.execute_install_script(None, second), ran] +''') == [True, True, ['first']] + + +def test_install_script_that_failed_is_run_when_asked_again(): + assert install_steps(''' +first, _ = packs(**{'install.py': ''}) +ran = installs(False, True) +result = [manager.execute_install_script(None, first), manager.execute_install_script(None, first), ran] +''') == [False, True, ['first', 'first']] + + +def test_install_script_not_run_after_a_requirement_failed_is_run_when_asked_again(): + assert install_steps(''' +first, _ = packs(**{'requirements.txt': 'fails==1\\n', 'install.py': ''}) +ran = installs(False, True, True) +result = [manager.execute_install_script(None, first), manager.execute_install_script(None, first), ran] +''') == [False, True, ['first', 'first', 'first']] diff --git a/tests/test_scheduled_restore_command.py b/tests/test_scheduled_restore_command.py new file mode 100644 index 0000000000..3c6c07100a --- /dev/null +++ b/tests/test_scheduled_restore_command.py @@ -0,0 +1,68 @@ +"""The scheduled snapshot restore tells cm-cli where the server's directories are. + +The case runs the real restore block of `prestartup_script.py`; `process_wrap` +is replaced and records the command it is given instead of running it. +""" +import ast +import os +import sys +import types +from pathlib import Path + +import pytest + +PRESTARTUP = Path(__file__).resolve().parent.parent / "prestartup_script.py" +COMFY = os.path.join(os.sep, "opt", "ComfyUI") +BASE = os.path.join(os.sep, "data", "comfy") + + +def restore(tmp_path, monkeypatch, environ): + """The command, working directory and environment the block starts cm-cli with.""" + scheduled = tmp_path / "restore-snapshot.json" + scheduled.write_text("{}") + block = [node for node in ast.parse(PRESTARTUP.read_text(encoding="utf-8")).body + if isinstance(node, ast.If) and ast.unparse(node.test) == "os.path.exists(restore_snapshot_path)"] + assert len(block) == 1 + + cli_args = types.ModuleType("comfy.cli_args") + cli_args.args = types.SimpleNamespace(listen="127.0.0.1") + monkeypatch.setitem(sys.modules, "comfy", types.ModuleType("comfy")) + monkeypatch.setitem(sys.modules, "comfy.cli_args", cli_args) + for name in ("COMFYUI_PATH", "COMFYUI_FOLDERS_BASE_PATH"): + monkeypatch.delenv(name, raising=False) + for name, value in environ.items(): + monkeypatch.setenv(name, value) + + started = [] + + def process_wrap(cmd, cwd=None, handler=None, env=None): + started.append((cmd, cwd, env)) + return 0 + + exec(compile(ast.Module(body=block, type_ignores=[]), str(PRESTARTUP), "exec"), { + "os": os, "sys": sys, "restore_snapshot_path": str(scheduled), "comfy_path": COMFY, + "cm_cli_path": "cm-cli.py", "custom_nodes_base_path": os.path.join(BASE, "custom_nodes"), + "folder_paths": types.SimpleNamespace(get_user_directory=lambda: os.path.join(BASE, "user")), + "default_conf": {}, "manager_util": types.SimpleNamespace(is_loopback_listener=lambda listen: True), + "process_wrap": process_wrap, + }) + assert len(started) == 1 and not scheduled.exists() + return started[0] + + +def test_cm_cli_is_given_the_comfyui_path(tmp_path, monkeypatch): + _, _, env = restore(tmp_path, monkeypatch, {}) + assert env.get("COMFYUI_PATH") == COMFY + + +def test_cm_cli_is_given_the_directories_of_the_server(tmp_path, monkeypatch): + cmd, _, _ = restore(tmp_path, monkeypatch, {}) + assert cmd[1:4] == ["cm-cli.py", "restore-snapshot", str(tmp_path / "restore-snapshot.json")] + assert cmd[4:] == ["--user-directory", os.path.join(BASE, "user"), + "--restore-to", os.path.join(BASE, "custom_nodes")] + + +@pytest.mark.parametrize("name", ["COMFYUI_PATH", "COMFYUI_FOLDERS_BASE_PATH"]) +def test_a_path_set_in_the_environment_is_kept(tmp_path, monkeypatch, name): + _, _, env = restore(tmp_path, monkeypatch, {name: os.path.join(os.sep, "elsewhere")}) + assert env[name] == os.path.join(os.sep, "elsewhere")