From a9f0d71eb9cd2dc4dc0b23c8c49d52c5497507d9 Mon Sep 17 00:00:00 2001 From: TomorrowX6 <454565615@qq.com> Date: Sun, 1 Nov 2026 21:29:35 +0800 Subject: [PATCH 01/11] fix: persist installer shell PATH configuration --- docs/public/install.sh | 9 +++- tests/test_install_script.py | 88 +++++++++++++++++++++++++++++++++--- 2 files changed, 90 insertions(+), 7 deletions(-) diff --git a/docs/public/install.sh b/docs/public/install.sh index 700fcd7fe9e..2b678e0b42c 100755 --- a/docs/public/install.sh +++ b/docs/public/install.sh @@ -400,6 +400,7 @@ install_uv() { } # Recover an existing installation before downloading uv again. +original_path="$PATH" refresh_uv_path if ! has uv; then install_uv; fi if ! has uv; then err "$(message UvFailed)"; exit 1; fi @@ -439,7 +440,13 @@ if [ "$tool_install_exit_code" -ne 0 ]; then exit "$tool_install_exit_code" fi ok "$(message Installed)" -uv tool update-shell >/dev/null 2>&1 || true +# uv skips shell configuration when its tool directory is already on PATH. +# Use the caller's PATH and resolve uv before removing our temporary additions. +uv_executable="$(type -P uv)" +if ! PATH="$original_path" "$uv_executable" tool update-shell; then + warn 'Could not update shell PATH. Run the following command, then reload your shell:' + printf '%s %q tool update-shell\n' "$indent" "$uv_executable" +fi printf '\n%s%s\n' "$indent" "$(message ReloadShell)" printf '%s Bash: source ~/.bashrc\n' "$indent" printf '%s Zsh: source ~/.zshrc\n' "$indent" diff --git a/tests/test_install_script.py b/tests/test_install_script.py index 34901a829aa..e198bf659a9 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -24,16 +24,33 @@ "https://mirrors.ustc.edu.cn/github-release/astral-sh/python-build-standalone/" ) -FAKE_UV = b"""#!/bin/bash -if [ "$1" = --version ]; then printf 'uv fixture\\n'; exit 0; fi -if [ "$1" = tool ] && [ "$2" = update-shell ]; then touch "$TEST_DIR/uv-update-shell"; exit 0; fi -printf '%s\\n' "$@" > "$TEST_DIR/uv-args" -printf '%s\\n' "${UV_PYTHON_INSTALL_MIRROR:-}" >> "$TEST_DIR/python-mirrors" +FAKE_UV = rb"""#!/bin/bash +if [ "$1" = --version ]; then printf 'uv fixture\n'; exit 0; fi +if [ "$1" = tool ] && [ "$2" = update-shell ]; then + touch "$TEST_DIR/uv-update-shell" + printf '%s\n' "$PATH" > "$TEST_DIR/update-shell-path" + if [ "${TEST_UPDATE_SHELL_FAIL:-0}" = 1 ]; then + printf 'Cannot write shell configuration\n' >&2 + exit 1 + fi + case ":$PATH:" in + *":$UV_UNMANAGED_INSTALL:"*) exit 0 ;; + esac + line="$(printf 'export PATH=%q:\"$PATH\"' "$UV_UNMANAGED_INSTALL")" + if ! grep -Fqx "$line" "$HOME/.bashrc"; then + printf '%s\n' "$line" >> "$HOME/.bashrc" + fi + exit 0 +fi +printf '%s\n' "$@" > "$TEST_DIR/uv-args" +printf '%s\n' "${UV_PYTHON_INSTALL_MIRROR:-}" >> "$TEST_DIR/python-mirrors" if [ "${TEST_FAIL_ONCE:-0}" = 1 ] && [ ! -f "$TEST_DIR/failed-once" ]; then touch "$TEST_DIR/failed-once" exit 1 fi -exit "${TEST_INSTALL_EXIT:-0}" +if [ "${TEST_INSTALL_EXIT:-0}" != 0 ]; then exit "$TEST_INSTALL_EXIT"; fi +printf '#!/bin/bash\nexit 0\n' > "$UV_UNMANAGED_INSTALL/astrbot" +chmod +x "$UV_UNMANAGED_INSTALL/astrbot" """ # BASH_ENV supplies only fixture commands; uv itself is a real fixture executable. @@ -116,6 +133,7 @@ def setUp(self): self.temporary = tempfile.TemporaryDirectory(prefix="astrbot-shell-test-") self.addCleanup(self.temporary.cleanup) self.root = Path(self.temporary.name) + (self.root / ".bashrc").touch() (self.root / "fake-uv").write_bytes(FAKE_UV) (self.root / "fake-uv").chmod(0o755) (self.root / "env.sh").write_text(MOCK_ENV) @@ -138,6 +156,8 @@ def setUp(self): ): self.env.pop(key, None) self.env.update( + HOME=str(self.root), + PATH="/usr/bin:/bin", BASH_ENV=str(self.root / "env.sh"), TEST_DIR=str(self.root), UV_UNMANAGED_INSTALL=str(self.root / "bin"), @@ -250,6 +270,62 @@ def test_defaults_ignore_system_locale(self): output, ) + def test_fresh_install_commands_available_after_bash_reload(self): + status, output = self.run_installer(["--non-interactive"], existing=False) + self.assertEqual(status, 0, output) + shell_env = self.env.copy() + shell_env.pop("BASH_ENV") + result = subprocess.run( + [ + "/bin/bash", + "--noprofile", + "--norc", + "-c", + 'source "$HOME/.bashrc"; command -v uv; command -v astrbot', + ], + env=shell_env, + capture_output=True, + text=True, + timeout=15, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual( + result.stdout.splitlines(), + [str(self.root / "bin/uv"), str(self.root / "bin/astrbot")], + ) + + def test_recovered_uv_updates_shell_with_original_path(self): + # Spaces in the install directory must survive executable lookup. + install_dir = self.root / "custom bin" + install_dir.mkdir() + shutil.copy2(self.root / "fake-uv", install_dir / "uv") + self.env["UV_UNMANAGED_INSTALL"] = str(install_dir) + self.env["PATH"] += ":/custom/bin" + for _ in range(2): + status, output = self.run_installer(["--non-interactive"], existing=False) + self.assertEqual(status, 0, output) + self.assertEqual( + (self.root / "update-shell-path").read_text().strip(), + self.env["PATH"], + ) + self.assertEqual(len((self.root / ".bashrc").read_text().splitlines()), 1) + self.assertFalse((self.root / "requests").exists()) + + def test_existing_path_does_not_modify_shell_configuration(self): + self.env["PATH"] = f"{self.root / 'bin'}:{self.env['PATH']}" + status, output = self.run_installer(["--non-interactive"]) + self.assertEqual(status, 0, output) + self.assertEqual((self.root / ".bashrc").read_text(), "") + + def test_shell_update_failure_shows_diagnostic_and_retry_command(self): + self.env["TEST_UPDATE_SHELL_FAIL"] = "1" + status, output = self.run_installer(["--non-interactive"]) + self.assertEqual(status, 0, output) + self.assertIn("AstrBot has been installed", output) + self.assertIn("Cannot write shell configuration", output) + self.assertIn("[WARN]", output) + self.assertIn(f"{self.root / 'bin/uv'} tool update-shell", output) + def test_chinese_defaults_to_all_china_mirrors(self): status, output = self.run_installer( ["--language", "zh-CN", "--non-interactive"] From 66ae882b23a7fcdb5bb3f54cb8f7831942676c71 Mon Sep 17 00:00:00 2001 From: TomorrowX6 <454565615@qq.com> Date: Sun, 1 Nov 2026 21:51:49 +0800 Subject: [PATCH 02/11] fix: handle custom uv paths during shell setup --- docs/public/install.sh | 21 ++++++- tests/test_install_script.py | 116 +++++++++++++++++++++++++++++++++-- 2 files changed, 132 insertions(+), 5 deletions(-) diff --git a/docs/public/install.sh b/docs/public/install.sh index 2b678e0b42c..0d1a19d4672 100755 --- a/docs/public/install.sh +++ b/docs/public/install.sh @@ -442,11 +442,30 @@ fi ok "$(message Installed)" # uv skips shell configuration when its tool directory is already on PATH. # Use the caller's PATH and resolve uv before removing our temporary additions. -uv_executable="$(type -P uv)" +if uv_executable="$(type -P uv)"; then + case "$uv_executable" in + /*) ;; + *) uv_executable="$PWD/$uv_executable" ;; + esac +else + # An exported shell function can provide uv without a binary on PATH. + uv_executable='uv' +fi if ! PATH="$original_path" "$uv_executable" tool update-shell; then warn 'Could not update shell PATH. Run the following command, then reload your shell:' printf '%s %q tool update-shell\n' "$indent" "$uv_executable" fi +# A custom uv installation can live outside the tool executable directory. +# Let uv configure that directory too, without changing where tools are installed. +if [ "$uv_executable" != 'uv' ] && \ + tool_bin_dir="$("$uv_executable" tool dir --bin)" && \ + [ "${uv_executable%/*}" != "$tool_bin_dir" ]; then + uv_bin_dir="${uv_executable%/*}" + if ! PATH="$original_path" UV_TOOL_BIN_DIR="$uv_bin_dir" "$uv_executable" tool update-shell; then + warn 'Could not add the uv installation directory to shell PATH. Run the following command, then reload your shell:' + printf '%s env UV_TOOL_BIN_DIR=%q %q tool update-shell\n' "$indent" "$uv_bin_dir" "$uv_executable" + fi +fi printf '\n%s%s\n' "$indent" "$(message ReloadShell)" printf '%s Bash: source ~/.bashrc\n' "$indent" printf '%s Zsh: source ~/.zshrc\n' "$indent" diff --git a/tests/test_install_script.py b/tests/test_install_script.py index e198bf659a9..08f6e6f69a0 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -26,6 +26,11 @@ FAKE_UV = rb"""#!/bin/bash if [ "$1" = --version ]; then printf 'uv fixture\n'; exit 0; fi +tool_bin_dir="${UV_TOOL_BIN_DIR:-$HOME/.local/bin}" +if [ "$1" = tool ] && [ "$2" = dir ] && [ "$3" = --bin ]; then + printf '%s\n' "$tool_bin_dir" + exit 0 +fi if [ "$1" = tool ] && [ "$2" = update-shell ]; then touch "$TEST_DIR/uv-update-shell" printf '%s\n' "$PATH" > "$TEST_DIR/update-shell-path" @@ -34,9 +39,9 @@ exit 1 fi case ":$PATH:" in - *":$UV_UNMANAGED_INSTALL:"*) exit 0 ;; + *":$tool_bin_dir:"*) exit 0 ;; esac - line="$(printf 'export PATH=%q:\"$PATH\"' "$UV_UNMANAGED_INSTALL")" + line="$(printf 'export PATH=%q:\"$PATH\"' "$tool_bin_dir")" if ! grep -Fqx "$line" "$HOME/.bashrc"; then printf '%s\n' "$line" >> "$HOME/.bashrc" fi @@ -49,8 +54,9 @@ exit 1 fi if [ "${TEST_INSTALL_EXIT:-0}" != 0 ]; then exit "$TEST_INSTALL_EXIT"; fi -printf '#!/bin/bash\nexit 0\n' > "$UV_UNMANAGED_INSTALL/astrbot" -chmod +x "$UV_UNMANAGED_INSTALL/astrbot" +mkdir -p "$tool_bin_dir" +printf '#!/bin/bash\nexit 0\n' > "$tool_bin_dir/astrbot" +chmod +x "$tool_bin_dir/astrbot" """ # BASH_ENV supplies only fixture commands; uv itself is a real fixture executable. @@ -161,6 +167,7 @@ def setUp(self): BASH_ENV=str(self.root / "env.sh"), TEST_DIR=str(self.root), UV_UNMANAGED_INSTALL=str(self.root / "bin"), + UV_TOOL_BIN_DIR=str(self.root / "bin"), NO_COLOR="1", TMPDIR=str(self.root), ) @@ -300,6 +307,7 @@ def test_recovered_uv_updates_shell_with_original_path(self): install_dir.mkdir() shutil.copy2(self.root / "fake-uv", install_dir / "uv") self.env["UV_UNMANAGED_INSTALL"] = str(install_dir) + self.env["UV_TOOL_BIN_DIR"] = str(install_dir) self.env["PATH"] += ":/custom/bin" for _ in range(2): status, output = self.run_installer(["--non-interactive"], existing=False) @@ -326,6 +334,106 @@ def test_shell_update_failure_shows_diagnostic_and_retry_command(self): self.assertIn("[WARN]", output) self.assertIn(f"{self.root / 'bin/uv'} tool update-shell", output) + def test_exported_uv_function_reaches_shell_setup(self): + with (self.root / "env.sh").open("a") as fixture: + fixture.write( + '\ncommand() { builtin command "$@"; }\n' + 'uv() { "$TEST_DIR/fake-uv" "$@"; }\n' + ) + (self.root / "bin").mkdir() + for fail_update in ("0", "1"): + with self.subTest(fail_update=fail_update): + self.env["TEST_UPDATE_SHELL_FAIL"] = fail_update + status, output = self.run_installer( + ["--non-interactive"], existing=False + ) + self.assertEqual(status, 0, output) + self.assertTrue((self.root / "uv-update-shell").exists()) + self.assertIn("Bash: source ~/.bashrc", output) + if fail_update == "1": + self.assertIn("Cannot write shell configuration", output) + self.assertIn(" uv tool update-shell", output) + else: + self.assertTrue((self.root / ".bashrc").read_text()) + + def test_retry_from_relative_path_works_in_another_directory(self): + with (self.root / "env.sh").open("a") as fixture: + fixture.write('\ncd "$TEST_DIR"\n') + self.env.update(UV_UNMANAGED_INSTALL="bin", TEST_UPDATE_SHELL_FAIL="1") + status, output = self.run_installer(["--non-interactive"]) + self.assertEqual(status, 0, output) + retry = next( + line.strip() + for line in output.splitlines() + if line.endswith(" tool update-shell") + ) + self.assertEqual(retry, f"{self.root / 'bin/uv'} tool update-shell") + retry_env = self.env.copy() + retry_env.pop("BASH_ENV") + retry_env.pop("TEST_UPDATE_SHELL_FAIL") + result = subprocess.run( + ["/bin/bash", "-c", retry], + cwd="/", + env=retry_env, + capture_output=True, + text=True, + timeout=15, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertTrue((self.root / ".bashrc").read_text()) + + def test_separate_uv_and_tool_directories_survive_shell_reload(self): + uv_bin = self.root / "uv bin" + tool_bin = self.root / "tool bin" + self.env["UV_UNMANAGED_INSTALL"] = str(uv_bin) + self.env["UV_TOOL_BIN_DIR"] = str(tool_bin) + for fail_update in ("0", "1"): + with self.subTest(fail_update=fail_update): + (self.root / ".bashrc").write_text("") + self.env["TEST_UPDATE_SHELL_FAIL"] = fail_update + status, output = self.run_installer( + ["--non-interactive"], existing=False + ) + self.assertEqual(status, 0, output) + shell_env = self.env.copy() + shell_env.pop("BASH_ENV") + shell_env.pop("TEST_UPDATE_SHELL_FAIL") + if fail_update == "1": + retries = [ + line.strip() + for line in output.splitlines() + if line.endswith(" tool update-shell") + ] + self.assertTrue(retries, output) + for retry in retries: + result = subprocess.run( + ["/bin/bash", "-c", retry], + cwd="/", + env=shell_env, + capture_output=True, + text=True, + timeout=15, + ) + self.assertEqual(result.returncode, 0, result.stderr) + result = subprocess.run( + [ + "/bin/bash", + "--noprofile", + "--norc", + "-c", + 'source "$HOME/.bashrc"; command -v uv; command -v astrbot', + ], + env=shell_env, + capture_output=True, + text=True, + timeout=15, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual( + result.stdout.splitlines(), + [str(uv_bin / "uv"), str(tool_bin / "astrbot")], + ) + def test_chinese_defaults_to_all_china_mirrors(self): status, output = self.run_installer( ["--language", "zh-CN", "--non-interactive"] From 94e47833f6d711728b160a92d21a7bc8feaf94d2 Mon Sep 17 00:00:00 2001 From: TomorrowX6 <454565615@qq.com> Date: Sun, 1 Nov 2026 22:18:38 +0800 Subject: [PATCH 03/11] fix: preserve uv wrapper settings and shell retry paths --- docs/public/install.sh | 41 +++++++++++++------- tests/test_install_script.py | 73 ++++++++++++++++++++++++++++++++++-- 2 files changed, 98 insertions(+), 16 deletions(-) diff --git a/docs/public/install.sh b/docs/public/install.sh index 0d1a19d4672..fb3732e7ac8 100755 --- a/docs/public/install.sh +++ b/docs/public/install.sh @@ -442,28 +442,43 @@ fi ok "$(message Installed)" # uv skips shell configuration when its tool directory is already on PATH. # Use the caller's PATH and resolve uv before removing our temporary additions. -if uv_executable="$(type -P uv)"; then - case "$uv_executable" in +uv_binary="$(type -P uv)" || uv_binary='' +if [ -n "$uv_binary" ]; then + case "$uv_binary" in /*) ;; - *) uv_executable="$PWD/$uv_executable" ;; + *) uv_binary="$PWD/$uv_binary" ;; esac -else - # An exported shell function can provide uv without a binary on PATH. +fi +uv_executable="${uv_binary:-uv}" +if [ "$(type -t uv)" = 'function' ]; then + # Keep wrapper settings consistent with the preceding tool installation. uv_executable='uv' fi -if ! PATH="$original_path" "$uv_executable" tool update-shell; then +# Preserve invocation-only settings in the retry command, including wrapper overrides. +tool_bin_dir="$("$uv_executable" tool dir --bin)" || tool_bin_dir="${UV_TOOL_BIN_DIR:-}" +if ! ( + export PATH="$original_path" + # Wrappers using `command uv` must still find a recovered binary, while uv + # itself must see the original PATH when deciding whether to update the shell. + if [ -n "$uv_binary" ]; then hash -p "$uv_binary" uv; fi + "$uv_executable" tool update-shell +); then warn 'Could not update shell PATH. Run the following command, then reload your shell:' - printf '%s %q tool update-shell\n' "$indent" "$uv_executable" + if [ -n "$tool_bin_dir" ]; then + # An assignment also works with shell functions, unlike invoking through env. + printf '%s UV_TOOL_BIN_DIR=%q %q tool update-shell\n' "$indent" "$tool_bin_dir" "${uv_binary:-$uv_executable}" + else + printf '%s %q tool update-shell\n' "$indent" "${uv_binary:-$uv_executable}" + fi fi # A custom uv installation can live outside the tool executable directory. # Let uv configure that directory too, without changing where tools are installed. -if [ "$uv_executable" != 'uv' ] && \ - tool_bin_dir="$("$uv_executable" tool dir --bin)" && \ - [ "${uv_executable%/*}" != "$tool_bin_dir" ]; then - uv_bin_dir="${uv_executable%/*}" - if ! PATH="$original_path" UV_TOOL_BIN_DIR="$uv_bin_dir" "$uv_executable" tool update-shell; then +if [ -n "$uv_binary" ] && [ -n "$tool_bin_dir" ] && \ + [ "${uv_binary%/*}" != "$tool_bin_dir" ]; then + uv_bin_dir="${uv_binary%/*}" + if ! PATH="$original_path" UV_TOOL_BIN_DIR="$uv_bin_dir" "$uv_binary" tool update-shell; then warn 'Could not add the uv installation directory to shell PATH. Run the following command, then reload your shell:' - printf '%s env UV_TOOL_BIN_DIR=%q %q tool update-shell\n' "$indent" "$uv_bin_dir" "$uv_executable" + printf '%s env UV_TOOL_BIN_DIR=%q %q tool update-shell\n' "$indent" "$uv_bin_dir" "$uv_binary" fi fi printf '\n%s%s\n' "$indent" "$(message ReloadShell)" diff --git a/tests/test_install_script.py b/tests/test_install_script.py index 08f6e6f69a0..d7b450cd36e 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -70,7 +70,6 @@ fi builtin command "$@" } -uv() { "$UV_UNMANAGED_INSTALL/uv" "$@"; } uname() { case "$1" in -s) printf '%s\n' "${TEST_PLATFORM:-Darwin}" ;; @@ -352,7 +351,7 @@ def test_exported_uv_function_reaches_shell_setup(self): self.assertIn("Bash: source ~/.bashrc", output) if fail_update == "1": self.assertIn("Cannot write shell configuration", output) - self.assertIn(" uv tool update-shell", output) + self.assertIn(" uv tool update-shell", output) else: self.assertTrue((self.root / ".bashrc").read_text()) @@ -367,10 +366,11 @@ def test_retry_from_relative_path_works_in_another_directory(self): for line in output.splitlines() if line.endswith(" tool update-shell") ) - self.assertEqual(retry, f"{self.root / 'bin/uv'} tool update-shell") + self.assertIn(f"{self.root / 'bin/uv'} tool update-shell", retry) retry_env = self.env.copy() retry_env.pop("BASH_ENV") retry_env.pop("TEST_UPDATE_SHELL_FAIL") + retry_env.pop("UV_TOOL_BIN_DIR") result = subprocess.run( ["/bin/bash", "-c", retry], cwd="/", @@ -398,6 +398,7 @@ def test_separate_uv_and_tool_directories_survive_shell_reload(self): shell_env = self.env.copy() shell_env.pop("BASH_ENV") shell_env.pop("TEST_UPDATE_SHELL_FAIL") + shell_env.pop("UV_TOOL_BIN_DIR") if fail_update == "1": retries = [ line.strip() @@ -434,6 +435,72 @@ def test_separate_uv_and_tool_directories_survive_shell_reload(self): [str(uv_bin / "uv"), str(tool_bin / "astrbot")], ) + def test_shadowing_uv_function_keeps_its_tool_configuration(self): + wrapper_bin = self.root / "wrapper bin" + original_path = self.env["PATH"] + with (self.root / "env.sh").open("a") as fixture: + # Exercise Bash's real command builtin inside the wrapper. + fixture.write( + '\nunset -f command\nuv() { UV_TOOL_BIN_DIR="$TEST_DIR/wrapper bin" ' + 'command uv "$@"; }\n' + ) + for binary_on_path, fail_update in ( + (True, "0"), + (True, "1"), + (False, "0"), + (False, "1"), + ): + with self.subTest(binary_on_path=binary_on_path, fail_update=fail_update): + self.env["PATH"] = ( + f"{self.root / 'bin'}:{original_path}" + if binary_on_path + else original_path + ) + (self.root / ".bashrc").write_text("") + self.env["TEST_UPDATE_SHELL_FAIL"] = fail_update + status, output = self.run_installer(["--non-interactive"]) + self.assertEqual(status, 0, output) + self.assertTrue((wrapper_bin / "astrbot").exists()) + if fail_update == "1": + retries = [ + line.strip() + for line in output.splitlines() + if line.endswith(" tool update-shell") + ] + self.assertTrue(retries, output) + retry_env = self.env.copy() + retry_env.pop("TEST_UPDATE_SHELL_FAIL") + retry_env.pop("UV_TOOL_BIN_DIR") + for retry in retries: + result = subprocess.run( + ["/bin/bash", "-c", retry], + env=retry_env, + capture_output=True, + text=True, + timeout=15, + ) + self.assertEqual(result.returncode, 0, result.stderr) + shell_env = self.env.copy() + shell_env.pop("BASH_ENV") + result = subprocess.run( + [ + "/bin/bash", + "--noprofile", + "--norc", + "-c", + 'source "$HOME/.bashrc"; command -v uv; command -v astrbot', + ], + env=shell_env, + capture_output=True, + text=True, + timeout=15, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual( + result.stdout.splitlines(), + [str(self.root / "bin/uv"), str(wrapper_bin / "astrbot")], + ) + def test_chinese_defaults_to_all_china_mirrors(self): status, output = self.run_installer( ["--language", "zh-CN", "--non-interactive"] From a5d0b22c417d2aedbe709d5964f449212201717e Mon Sep 17 00:00:00 2001 From: TomorrowX6 <454565615@qq.com> Date: Sun, 1 Nov 2026 23:30:29 +0800 Subject: [PATCH 04/11] fix: recognize existing uv shell configuration --- docs/public/install.sh | 22 ++++++++++--- tests/test_install_script.py | 63 ++++++++++++++++++++++++------------ 2 files changed, 60 insertions(+), 25 deletions(-) diff --git a/docs/public/install.sh b/docs/public/install.sh index fb3732e7ac8..bd2b3fd3dbb 100755 --- a/docs/public/install.sh +++ b/docs/public/install.sh @@ -456,13 +456,21 @@ if [ "$(type -t uv)" = 'function' ]; then fi # Preserve invocation-only settings in the retry command, including wrapper overrides. tool_bin_dir="$("$uv_executable" tool dir --bin)" || tool_bin_dir="${UV_TOOL_BIN_DIR:-}" -if ! ( +# uv uses an error exit even when its shell configuration is already correct. +# Match only that diagnostic; preserve all other errors and their retry commands. +if shell_update_output="$( export PATH="$original_path" # Wrappers using `command uv` must still find a recovered binary, while uv # itself must see the original PATH when deciding whether to update the shell. if [ -n "$uv_binary" ]; then hash -p "$uv_binary" uv; fi - "$uv_executable" tool update-shell -); then + "$uv_executable" tool update-shell --color never 2>&1 +)"; then + [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 +elif [[ "$shell_update_output" != *$'\n'* && + "$shell_update_output" == 'error: The executable directory `'*'` is not in PATH, but the '*' configuration files are already up-to-date' ]]; then + info 'Shell PATH is already configured. Reload your shell to apply it.' +else + [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 warn 'Could not update shell PATH. Run the following command, then reload your shell:' if [ -n "$tool_bin_dir" ]; then # An assignment also works with shell functions, unlike invoking through env. @@ -476,7 +484,13 @@ fi if [ -n "$uv_binary" ] && [ -n "$tool_bin_dir" ] && \ [ "${uv_binary%/*}" != "$tool_bin_dir" ]; then uv_bin_dir="${uv_binary%/*}" - if ! PATH="$original_path" UV_TOOL_BIN_DIR="$uv_bin_dir" "$uv_binary" tool update-shell; then + if shell_update_output="$(PATH="$original_path" UV_TOOL_BIN_DIR="$uv_bin_dir" "$uv_binary" tool update-shell --color never 2>&1)"; then + [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 + elif [[ "$shell_update_output" != *$'\n'* && + "$shell_update_output" == 'error: The executable directory `'*'` is not in PATH, but the '*' configuration files are already up-to-date' ]]; then + info 'Shell PATH is already configured. Reload your shell to apply it.' + else + [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 warn 'Could not add the uv installation directory to shell PATH. Run the following command, then reload your shell:' printf '%s env UV_TOOL_BIN_DIR=%q %q tool update-shell\n' "$indent" "$uv_bin_dir" "$uv_binary" fi diff --git a/tests/test_install_script.py b/tests/test_install_script.py index d7b450cd36e..3dfd3a06840 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -34,17 +34,24 @@ if [ "$1" = tool ] && [ "$2" = update-shell ]; then touch "$TEST_DIR/uv-update-shell" printf '%s\n' "$PATH" > "$TEST_DIR/update-shell-path" + if [ "${TEST_UPDATE_SHELL_FAIL:-0}" = mixed ]; then + printf 'error: The executable directory `%s` is not in PATH, but the current shell could not be determined\n' "$tool_bin_dir" >&2 + printf 'error: The executable directory `%s` is not in PATH, but the Bash configuration files are already up-to-date\n' "$tool_bin_dir" >&2 + exit 2 + fi if [ "${TEST_UPDATE_SHELL_FAIL:-0}" = 1 ]; then printf 'Cannot write shell configuration\n' >&2 - exit 1 + exit 2 fi case ":$PATH:" in *":$tool_bin_dir:"*) exit 0 ;; esac line="$(printf 'export PATH=%q:\"$PATH\"' "$tool_bin_dir")" - if ! grep -Fqx "$line" "$HOME/.bashrc"; then - printf '%s\n' "$line" >> "$HOME/.bashrc" + if grep -Fqx "$line" "$HOME/.bashrc"; then + printf 'error: The executable directory `%s` is not in PATH, but the Bash configuration files are already up-to-date\n' "$tool_bin_dir" >&2 + exit 2 fi + printf '%s\n' "$line" >> "$HOME/.bashrc" exit 0 fi printf '%s\n' "$@" > "$TEST_DIR/uv-args" @@ -306,16 +313,25 @@ def test_recovered_uv_updates_shell_with_original_path(self): install_dir.mkdir() shutil.copy2(self.root / "fake-uv", install_dir / "uv") self.env["UV_UNMANAGED_INSTALL"] = str(install_dir) - self.env["UV_TOOL_BIN_DIR"] = str(install_dir) self.env["PATH"] += ":/custom/bin" - for _ in range(2): - status, output = self.run_installer(["--non-interactive"], existing=False) - self.assertEqual(status, 0, output) - self.assertEqual( - (self.root / "update-shell-path").read_text().strip(), - self.env["PATH"], - ) - self.assertEqual(len((self.root / ".bashrc").read_text().splitlines()), 1) + for tool_bin, entries in ((install_dir, 1), (self.root / "tool bin", 2)): + with self.subTest(tool_bin=tool_bin): + (self.root / ".bashrc").write_text("") + self.env["UV_TOOL_BIN_DIR"] = str(tool_bin) + for _ in range(2): + status, output = self.run_installer( + ["--non-interactive"], existing=False + ) + self.assertEqual(status, 0, output) + self.assertNotIn("[WARN]", output) + self.assertNotIn("tool update-shell", output) + self.assertEqual( + (self.root / "update-shell-path").read_text().strip(), + self.env["PATH"], + ) + self.assertEqual( + len((self.root / ".bashrc").read_text().splitlines()), entries + ) self.assertFalse((self.root / "requests").exists()) def test_existing_path_does_not_modify_shell_configuration(self): @@ -325,13 +341,18 @@ def test_existing_path_does_not_modify_shell_configuration(self): self.assertEqual((self.root / ".bashrc").read_text(), "") def test_shell_update_failure_shows_diagnostic_and_retry_command(self): - self.env["TEST_UPDATE_SHELL_FAIL"] = "1" - status, output = self.run_installer(["--non-interactive"]) - self.assertEqual(status, 0, output) - self.assertIn("AstrBot has been installed", output) - self.assertIn("Cannot write shell configuration", output) - self.assertIn("[WARN]", output) - self.assertIn(f"{self.root / 'bin/uv'} tool update-shell", output) + for failure, diagnostic in ( + ("1", "Cannot write shell configuration"), + ("mixed", "current shell could not be determined"), + ): + with self.subTest(failure=failure): + self.env["TEST_UPDATE_SHELL_FAIL"] = failure + status, output = self.run_installer(["--non-interactive"]) + self.assertEqual(status, 0, output) + self.assertIn("AstrBot has been installed", output) + self.assertIn(diagnostic, output) + self.assertIn("[WARN]", output) + self.assertIn(f"{self.root / 'bin/uv'} tool update-shell", output) def test_exported_uv_function_reaches_shell_setup(self): with (self.root / "env.sh").open("a") as fixture: @@ -387,7 +408,7 @@ def test_separate_uv_and_tool_directories_survive_shell_reload(self): tool_bin = self.root / "tool bin" self.env["UV_UNMANAGED_INSTALL"] = str(uv_bin) self.env["UV_TOOL_BIN_DIR"] = str(tool_bin) - for fail_update in ("0", "1"): + for fail_update in ("0", "1", "mixed"): with self.subTest(fail_update=fail_update): (self.root / ".bashrc").write_text("") self.env["TEST_UPDATE_SHELL_FAIL"] = fail_update @@ -399,7 +420,7 @@ def test_separate_uv_and_tool_directories_survive_shell_reload(self): shell_env.pop("BASH_ENV") shell_env.pop("TEST_UPDATE_SHELL_FAIL") shell_env.pop("UV_TOOL_BIN_DIR") - if fail_update == "1": + if fail_update != "0": retries = [ line.strip() for line in output.splitlines() From 4bb72c6bc605886e293b26e9c25df04b200c7dfc Mon Sep 17 00:00:00 2001 From: AzusaMoe Date: Mon, 2 Nov 2026 00:11:16 +0800 Subject: [PATCH 05/11] fix: harden installer PATH validation and legacy uv support Handle uv diagnostics with and without directory backticks, reject relative tool executable directories before package installation, and localize the shell PATH diagnostics for all supported languages. Add regression coverage for legacy uv, relative paths, wrapper overrides, localized diagnostics, and repeated shell setup using a real uv executable. --- docs/public/install.sh | 40 ++++++--- tests/test_install_script.py | 153 +++++++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+), 9 deletions(-) diff --git a/docs/public/install.sh b/docs/public/install.sh index bd2b3fd3dbb..b7edc5cf8c5 100755 --- a/docs/public/install.sh +++ b/docs/public/install.sh @@ -36,6 +36,9 @@ Usage: bash install.sh [options] --pypi-index-url HTTPS-URL PyPI simple index for custom sources --python-install-mirror HTTPS-URL Optional Python interpreter download mirror --non-interactive Skip all menus (defaults: English; source follows language) + +Environment: + UV_TOOL_BIN_DIR Absolute directory for installed tool executables HELP exit 0 ;; @@ -130,6 +133,10 @@ message() { zh-CN:ReloadShell) format='请根据你使用的终端重新加载环境:' ;; zh-CN:RunCommands) format='重新加载后,请手动运行:' ;; zh-CN:Installed) format='AstrBot 安装完成。' ;; + zh-CN:ShellPathConfigured) format='Shell PATH 已配置。请重新加载终端以使其生效。' ;; + zh-CN:ShellPathUpdateFailed) format='无法更新 Shell PATH。请运行以下命令,然后重新加载终端:' ;; + zh-CN:UvPathUpdateFailed) format='无法将 uv 安装目录添加到 Shell PATH。请运行以下命令,然后重新加载终端:' ;; + zh-CN:ToolBinMustBeAbsolute) format='工具可执行文件目录必须是绝对路径:%s。请将 UV_TOOL_BIN_DIR 设为绝对路径后重试。' ;; en-US:ChooseSource) format='Select a download source' ;; en-US:China) format='Mainland China accelerated download mirrors' ;; en-US:Official) format='Official sources' ;; @@ -160,6 +167,10 @@ message() { en-US:ReloadShell) format='Reload the environment for your shell:' ;; en-US:RunCommands) format='Then run these commands manually:' ;; en-US:Installed) format='AstrBot has been installed.' ;; + en-US:ShellPathConfigured) format='Shell PATH is already configured. Reload your shell to apply it.' ;; + en-US:ShellPathUpdateFailed) format='Could not update shell PATH. Run the following command, then reload your shell:' ;; + en-US:UvPathUpdateFailed) format='Could not add the uv installation directory to shell PATH. Run the following command, then reload your shell:' ;; + en-US:ToolBinMustBeAbsolute) format='The tool executable directory must be an absolute path: %s. Set UV_TOOL_BIN_DIR to an absolute path and retry.' ;; ja-JP:ChooseSource) format='ダウンロード元を選択してください' ;; ja-JP:China) format='中国本土向け高速ダウンロードミラー' ;; ja-JP:Official) format='公式ソース' ;; @@ -190,6 +201,10 @@ message() { ja-JP:ReloadShell) format='使用するシェルに合わせて環境を再読み込みしてください:' ;; ja-JP:RunCommands) format='その後、以下のコマンドを手動で実行してください:' ;; ja-JP:Installed) format='AstrBot のインストールが完了しました。' ;; + ja-JP:ShellPathConfigured) format='シェルの PATH は設定済みです。シェルを再読み込みして反映してください。' ;; + ja-JP:ShellPathUpdateFailed) format='シェルの PATH を更新できませんでした。次のコマンドを実行してからシェルを再読み込みしてください:' ;; + ja-JP:UvPathUpdateFailed) format='uv のインストール先をシェルの PATH に追加できませんでした。次のコマンドを実行してからシェルを再読み込みしてください:' ;; + ja-JP:ToolBinMustBeAbsolute) format='ツールの実行ファイルディレクトリは絶対パスで指定してください:%s。UV_TOOL_BIN_DIR を絶対パスに設定して再試行してください。' ;; *) printf 'Unknown message: %s\n' "$key" >&2; return 1 ;; esac printf "$format\n" "$@" @@ -417,6 +432,14 @@ if [ -n "$python_install_mirror" ]; then else info "$(message PythonDefault)" fi +# Resolve through the same provider as installation, including shell wrappers. +# Relative tool directories change meaning when installation switches to TMPDIR +# and cannot be persisted safely for shells opened in another directory. +tool_bin_dir="$(uv tool dir --bin)" || tool_bin_dir="${UV_TOOL_BIN_DIR:-}" +case "$tool_bin_dir" in + ''|/*) ;; + *) err "$(message ToolBinMustBeAbsolute "$tool_bin_dir")"; exit 2 ;; +esac info "$(message InstallingAstrBot)" # Use a valid working directory even if the caller's directory has been removed. # The subshell scopes Python mirror settings to this installation only. @@ -454,10 +477,9 @@ if [ "$(type -t uv)" = 'function' ]; then # Keep wrapper settings consistent with the preceding tool installation. uv_executable='uv' fi -# Preserve invocation-only settings in the retry command, including wrapper overrides. -tool_bin_dir="$("$uv_executable" tool dir --bin)" || tool_bin_dir="${UV_TOOL_BIN_DIR:-}" # uv uses an error exit even when its shell configuration is already correct. -# Match only that diagnostic; preserve all other errors and their retry commands. +# Older uv releases omit backticks around the directory in this diagnostic. +# Match only that single-line diagnostic; preserve other errors and retry commands. if shell_update_output="$( export PATH="$original_path" # Wrappers using `command uv` must still find a recovered binary, while uv @@ -467,11 +489,11 @@ if shell_update_output="$( )"; then [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 elif [[ "$shell_update_output" != *$'\n'* && - "$shell_update_output" == 'error: The executable directory `'*'` is not in PATH, but the '*' configuration files are already up-to-date' ]]; then - info 'Shell PATH is already configured. Reload your shell to apply it.' + "$shell_update_output" == 'error: The executable directory '*' is not in PATH, but the '*' configuration files are already up-to-date' ]]; then + info "$(message ShellPathConfigured)" else [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 - warn 'Could not update shell PATH. Run the following command, then reload your shell:' + warn "$(message ShellPathUpdateFailed)" if [ -n "$tool_bin_dir" ]; then # An assignment also works with shell functions, unlike invoking through env. printf '%s UV_TOOL_BIN_DIR=%q %q tool update-shell\n' "$indent" "$tool_bin_dir" "${uv_binary:-$uv_executable}" @@ -487,11 +509,11 @@ if [ -n "$uv_binary" ] && [ -n "$tool_bin_dir" ] && \ if shell_update_output="$(PATH="$original_path" UV_TOOL_BIN_DIR="$uv_bin_dir" "$uv_binary" tool update-shell --color never 2>&1)"; then [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 elif [[ "$shell_update_output" != *$'\n'* && - "$shell_update_output" == 'error: The executable directory `'*'` is not in PATH, but the '*' configuration files are already up-to-date' ]]; then - info 'Shell PATH is already configured. Reload your shell to apply it.' + "$shell_update_output" == 'error: The executable directory '*' is not in PATH, but the '*' configuration files are already up-to-date' ]]; then + info "$(message ShellPathConfigured)" else [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 - warn 'Could not add the uv installation directory to shell PATH. Run the following command, then reload your shell:' + warn "$(message UvPathUpdateFailed)" printf '%s env UV_TOOL_BIN_DIR=%q %q tool update-shell\n' "$indent" "$uv_bin_dir" "$uv_binary" fi fi diff --git a/tests/test_install_script.py b/tests/test_install_script.py index 3dfd3a06840..f57b55d64c8 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -522,6 +522,159 @@ def test_shadowing_uv_function_keeps_its_tool_configuration(self): [str(self.root / "bin/uv"), str(wrapper_bin / "astrbot")], ) + def test_legacy_uv_repeated_install_is_already_configured(self): + # uv 0.10.0 omits backticks around directories in these diagnostics. + (self.root / "fake-uv").write_bytes(FAKE_UV.replace(b"`%s`", b"%s")) + for tool_bin, entries in ((self.root / "bin", 1), (self.root / "tool bin", 2)): + with self.subTest(tool_bin=tool_bin): + (self.root / ".bashrc").write_text("") + self.env["UV_TOOL_BIN_DIR"] = str(tool_bin) + for _ in range(2): + status, output = self.run_installer(["--non-interactive"]) + self.assertEqual(status, 0, output) + self.assertNotIn("[WARN]", output) + self.assertNotIn("tool update-shell", output) + self.assertEqual( + len((self.root / ".bashrc").read_text().splitlines()), entries + ) + + def test_legacy_uv_genuine_errors_are_not_suppressed(self): + (self.root / "fake-uv").write_bytes(FAKE_UV.replace(b"`%s`", b"%s")) + for failure, diagnostic in ( + ("1", "Cannot write shell configuration"), + ("mixed", "current shell could not be determined"), + ): + with self.subTest(failure=failure): + self.env["TEST_UPDATE_SHELL_FAIL"] = failure + status, output = self.run_installer(["--non-interactive"]) + self.assertEqual(status, 0, output) + self.assertIn(diagnostic, output) + self.assertIn("[WARN]", output) + self.assertIn(" tool update-shell", output) + + def test_relative_tool_bin_is_rejected_before_astrbot_install(self): + install_tmp = self.root / "install-temp" + install_tmp.mkdir() + self.env["TMPDIR"] = str(install_tmp) + for language, diagnostic in ( + ("en-US", "The tool executable directory must be an absolute path"), + ("zh-CN", "工具可执行文件目录必须是绝对路径"), + ("ja-JP", "ツールの実行ファイルディレクトリは絶対パスで指定してください"), + ): + for directory in ("relative bin", "./tools", "../tools", "."): + with self.subTest(language=language, directory=directory): + self.env["UV_TOOL_BIN_DIR"] = directory + status, output = self.run_installer( + ["--language", language, "--non-interactive"] + ) + self.assertEqual(status, 2, output) + self.assertIn(diagnostic, output) + self.assertIn(directory, output) + self.assertIn("UV_TOOL_BIN_DIR", output) + self.assertFalse((self.root / "uv-args").exists()) + self.assertFalse((self.root / "uv-update-shell").exists()) + self.assertEqual((self.root / ".bashrc").read_text(), "") + + def test_wrapper_relative_tool_bin_is_rejected_before_astrbot_install(self): + with (self.root / "env.sh").open("a") as fixture: + fixture.write( + '\nunset -f command\n' + 'uv() { UV_TOOL_BIN_DIR="relative tools" command uv "$@"; }\n' + ) + status, output = self.run_installer(["--non-interactive"]) + self.assertEqual(status, 2, output) + self.assertIn("relative tools", output) + self.assertFalse((self.root / "uv-args").exists()) + self.assertEqual((self.root / ".bashrc").read_text(), "") + + def test_shell_path_messages_follow_selected_language(self): + self.env["UV_TOOL_BIN_DIR"] = str(self.root / "tool bin") + for language, configured, tool_failure, uv_failure in ( + ( + "en-US", + "Shell PATH is already configured.", + "Could not update shell PATH.", + "Could not add the uv installation directory to shell PATH.", + ), + ( + "zh-CN", + "Shell PATH 已配置。", + "无法更新 Shell PATH。", + "无法将 uv 安装目录添加到 Shell PATH。", + ), + ( + "ja-JP", + "シェルの PATH は設定済みです。", + "シェルの PATH を更新できませんでした。", + "uv のインストール先をシェルの PATH に追加できませんでした。", + ), + ): + with self.subTest(language=language): + args = ["--language", language, "--non-interactive"] + self.env.pop("TEST_UPDATE_SHELL_FAIL", None) + (self.root / ".bashrc").write_text("") + for _ in range(2): + status, output = self.run_installer(args) + self.assertEqual(status, 0, output) + self.assertNotIn("[WARN]", output) + self.assertEqual(output.count(configured), 2, output) + self.env["TEST_UPDATE_SHELL_FAIL"] = "1" + status, output = self.run_installer(args) + self.assertEqual(status, 0, output) + self.assertIn(tool_failure, output) + self.assertIn(uv_failure, output) + self.assertIn("Cannot write shell configuration", output) + self.assertEqual(output.count(" tool update-shell"), 2, output) + + @unittest.skipUnless(shutil.which("uv"), "Real uv is not installed") + def test_real_uv_shell_setup_survives_repeated_install(self): + # Stub only package installation; use real uv for directory and shell setup. + uv_bin = self.root / "bin" + uv_bin.mkdir() + shutil.copy2(shutil.which("uv"), uv_bin / "uv") + self.env.update( + SHELL="/bin/bash", + XDG_CONFIG_HOME=str(self.root / ".config"), + UV_TOOL_BIN_DIR=str(self.root / "tool bin"), + ) + with (self.root / "env.sh").open("a") as fixture: + fixture.write( + '\nunset -f command\n' + 'uv() {\n' + ' if [ "$1" = tool ] && [ "$2" = install ]; then\n' + ' "$TEST_DIR/fake-uv" "$@"\n' + ' else\n' + ' command uv "$@"\n' + ' fi\n' + '}\n' + ) + for _ in range(2): + status, output = self.run_installer(["--non-interactive"], existing=False) + self.assertEqual(status, 0, output) + self.assertNotIn("[WARN]", output) + shell_env = self.env.copy() + shell_env.pop("BASH_ENV") + shell_env.pop("UV_TOOL_BIN_DIR") + result = subprocess.run( + [ + "/bin/bash", + "--noprofile", + "--norc", + "-c", + 'source "$HOME/.bashrc"; command -v uv; command -v astrbot', + ], + cwd="/", + env=shell_env, + capture_output=True, + text=True, + timeout=15, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual( + result.stdout.splitlines(), + [str(uv_bin / "uv"), str(self.root / "tool bin/astrbot")], + ) + def test_chinese_defaults_to_all_china_mirrors(self): status, output = self.run_installer( ["--language", "zh-CN", "--non-interactive"] From 3fd4d382419cc7aef277afa66df3bd749480b910 Mon Sep 17 00:00:00 2001 From: AzusaMoe Date: Mon, 2 Nov 2026 00:26:20 +0800 Subject: [PATCH 06/11] fix: keep installer PATH changes narrowly scoped Remove the additional absolute-only tool directory restriction and its help text and translations. Leave directory validation and semantics to uv, without introducing path normalization or changing installation. Restore the tool-bin query to shell setup and replace the two restriction tests with one pass-through regression. Preserve the existing PATH, wrapper, retry, legacy uv, and localization coverage. --- docs/public/install.sh | 16 ++------------- tests/test_install_script.py | 38 +++++------------------------------- 2 files changed, 7 insertions(+), 47 deletions(-) diff --git a/docs/public/install.sh b/docs/public/install.sh index b7edc5cf8c5..6ef4b9f46ce 100755 --- a/docs/public/install.sh +++ b/docs/public/install.sh @@ -36,9 +36,6 @@ Usage: bash install.sh [options] --pypi-index-url HTTPS-URL PyPI simple index for custom sources --python-install-mirror HTTPS-URL Optional Python interpreter download mirror --non-interactive Skip all menus (defaults: English; source follows language) - -Environment: - UV_TOOL_BIN_DIR Absolute directory for installed tool executables HELP exit 0 ;; @@ -136,7 +133,6 @@ message() { zh-CN:ShellPathConfigured) format='Shell PATH 已配置。请重新加载终端以使其生效。' ;; zh-CN:ShellPathUpdateFailed) format='无法更新 Shell PATH。请运行以下命令,然后重新加载终端:' ;; zh-CN:UvPathUpdateFailed) format='无法将 uv 安装目录添加到 Shell PATH。请运行以下命令,然后重新加载终端:' ;; - zh-CN:ToolBinMustBeAbsolute) format='工具可执行文件目录必须是绝对路径:%s。请将 UV_TOOL_BIN_DIR 设为绝对路径后重试。' ;; en-US:ChooseSource) format='Select a download source' ;; en-US:China) format='Mainland China accelerated download mirrors' ;; en-US:Official) format='Official sources' ;; @@ -170,7 +166,6 @@ message() { en-US:ShellPathConfigured) format='Shell PATH is already configured. Reload your shell to apply it.' ;; en-US:ShellPathUpdateFailed) format='Could not update shell PATH. Run the following command, then reload your shell:' ;; en-US:UvPathUpdateFailed) format='Could not add the uv installation directory to shell PATH. Run the following command, then reload your shell:' ;; - en-US:ToolBinMustBeAbsolute) format='The tool executable directory must be an absolute path: %s. Set UV_TOOL_BIN_DIR to an absolute path and retry.' ;; ja-JP:ChooseSource) format='ダウンロード元を選択してください' ;; ja-JP:China) format='中国本土向け高速ダウンロードミラー' ;; ja-JP:Official) format='公式ソース' ;; @@ -204,7 +199,6 @@ message() { ja-JP:ShellPathConfigured) format='シェルの PATH は設定済みです。シェルを再読み込みして反映してください。' ;; ja-JP:ShellPathUpdateFailed) format='シェルの PATH を更新できませんでした。次のコマンドを実行してからシェルを再読み込みしてください:' ;; ja-JP:UvPathUpdateFailed) format='uv のインストール先をシェルの PATH に追加できませんでした。次のコマンドを実行してからシェルを再読み込みしてください:' ;; - ja-JP:ToolBinMustBeAbsolute) format='ツールの実行ファイルディレクトリは絶対パスで指定してください:%s。UV_TOOL_BIN_DIR を絶対パスに設定して再試行してください。' ;; *) printf 'Unknown message: %s\n' "$key" >&2; return 1 ;; esac printf "$format\n" "$@" @@ -432,14 +426,6 @@ if [ -n "$python_install_mirror" ]; then else info "$(message PythonDefault)" fi -# Resolve through the same provider as installation, including shell wrappers. -# Relative tool directories change meaning when installation switches to TMPDIR -# and cannot be persisted safely for shells opened in another directory. -tool_bin_dir="$(uv tool dir --bin)" || tool_bin_dir="${UV_TOOL_BIN_DIR:-}" -case "$tool_bin_dir" in - ''|/*) ;; - *) err "$(message ToolBinMustBeAbsolute "$tool_bin_dir")"; exit 2 ;; -esac info "$(message InstallingAstrBot)" # Use a valid working directory even if the caller's directory has been removed. # The subshell scopes Python mirror settings to this installation only. @@ -477,6 +463,8 @@ if [ "$(type -t uv)" = 'function' ]; then # Keep wrapper settings consistent with the preceding tool installation. uv_executable='uv' fi +# Preserve invocation-only settings in the retry command, including wrapper overrides. +tool_bin_dir="$("$uv_executable" tool dir --bin)" || tool_bin_dir="${UV_TOOL_BIN_DIR:-}" # uv uses an error exit even when its shell configuration is already correct. # Older uv releases omit backticks around the directory in this diagnostic. # Match only that single-line diagnostic; preserve other errors and retry commands. diff --git a/tests/test_install_script.py b/tests/test_install_script.py index f57b55d64c8..5a524a21c9e 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -552,40 +552,12 @@ def test_legacy_uv_genuine_errors_are_not_suppressed(self): self.assertIn("[WARN]", output) self.assertIn(" tool update-shell", output) - def test_relative_tool_bin_is_rejected_before_astrbot_install(self): - install_tmp = self.root / "install-temp" - install_tmp.mkdir() - self.env["TMPDIR"] = str(install_tmp) - for language, diagnostic in ( - ("en-US", "The tool executable directory must be an absolute path"), - ("zh-CN", "工具可执行文件目录必须是绝对路径"), - ("ja-JP", "ツールの実行ファイルディレクトリは絶対パスで指定してください"), - ): - for directory in ("relative bin", "./tools", "../tools", "."): - with self.subTest(language=language, directory=directory): - self.env["UV_TOOL_BIN_DIR"] = directory - status, output = self.run_installer( - ["--language", language, "--non-interactive"] - ) - self.assertEqual(status, 2, output) - self.assertIn(diagnostic, output) - self.assertIn(directory, output) - self.assertIn("UV_TOOL_BIN_DIR", output) - self.assertFalse((self.root / "uv-args").exists()) - self.assertFalse((self.root / "uv-update-shell").exists()) - self.assertEqual((self.root / ".bashrc").read_text(), "") - - def test_wrapper_relative_tool_bin_is_rejected_before_astrbot_install(self): - with (self.root / "env.sh").open("a") as fixture: - fixture.write( - '\nunset -f command\n' - 'uv() { UV_TOOL_BIN_DIR="relative tools" command uv "$@"; }\n' - ) + def test_relative_tool_bin_is_passed_through_to_uv(self): + self.env["UV_TOOL_BIN_DIR"] = "relative tools" status, output = self.run_installer(["--non-interactive"]) - self.assertEqual(status, 2, output) - self.assertIn("relative tools", output) - self.assertFalse((self.root / "uv-args").exists()) - self.assertEqual((self.root / ".bashrc").read_text(), "") + self.assertEqual(status, 0, output) + self.assert_install(OFFICIAL_INDEX, [""]) + self.assertTrue((self.root / "relative tools/astrbot").is_file()) def test_shell_path_messages_follow_selected_language(self): self.env["UV_TOOL_BIN_DIR"] = str(self.root / "tool bin") From 5d9b7158ad8826771a037385fc9a6beffcb515e5 Mon Sep 17 00:00:00 2001 From: AzusaMoe Date: Mon, 2 Nov 2026 00:41:05 +0800 Subject: [PATCH 07/11] refactor: simplify installer PATH persistence fix Use one small loop for the tool and uv executable directories, avoiding duplicated update-shell and error handling code. Keep original PATH and actionable errors; remove nonessential wrapper and localization branches. Reduce installer-specific regression tests to core behaviors. --- docs/public/install.sh | 82 +++-------- tests/test_install_script.py | 270 +---------------------------------- 2 files changed, 25 insertions(+), 327 deletions(-) diff --git a/docs/public/install.sh b/docs/public/install.sh index 6ef4b9f46ce..736e36bd20e 100755 --- a/docs/public/install.sh +++ b/docs/public/install.sh @@ -130,9 +130,7 @@ message() { zh-CN:ReloadShell) format='请根据你使用的终端重新加载环境:' ;; zh-CN:RunCommands) format='重新加载后,请手动运行:' ;; zh-CN:Installed) format='AstrBot 安装完成。' ;; - zh-CN:ShellPathConfigured) format='Shell PATH 已配置。请重新加载终端以使其生效。' ;; zh-CN:ShellPathUpdateFailed) format='无法更新 Shell PATH。请运行以下命令,然后重新加载终端:' ;; - zh-CN:UvPathUpdateFailed) format='无法将 uv 安装目录添加到 Shell PATH。请运行以下命令,然后重新加载终端:' ;; en-US:ChooseSource) format='Select a download source' ;; en-US:China) format='Mainland China accelerated download mirrors' ;; en-US:Official) format='Official sources' ;; @@ -163,9 +161,7 @@ message() { en-US:ReloadShell) format='Reload the environment for your shell:' ;; en-US:RunCommands) format='Then run these commands manually:' ;; en-US:Installed) format='AstrBot has been installed.' ;; - en-US:ShellPathConfigured) format='Shell PATH is already configured. Reload your shell to apply it.' ;; en-US:ShellPathUpdateFailed) format='Could not update shell PATH. Run the following command, then reload your shell:' ;; - en-US:UvPathUpdateFailed) format='Could not add the uv installation directory to shell PATH. Run the following command, then reload your shell:' ;; ja-JP:ChooseSource) format='ダウンロード元を選択してください' ;; ja-JP:China) format='中国本土向け高速ダウンロードミラー' ;; ja-JP:Official) format='公式ソース' ;; @@ -196,9 +192,7 @@ message() { ja-JP:ReloadShell) format='使用するシェルに合わせて環境を再読み込みしてください:' ;; ja-JP:RunCommands) format='その後、以下のコマンドを手動で実行してください:' ;; ja-JP:Installed) format='AstrBot のインストールが完了しました。' ;; - ja-JP:ShellPathConfigured) format='シェルの PATH は設定済みです。シェルを再読み込みして反映してください。' ;; ja-JP:ShellPathUpdateFailed) format='シェルの PATH を更新できませんでした。次のコマンドを実行してからシェルを再読み込みしてください:' ;; - ja-JP:UvPathUpdateFailed) format='uv のインストール先をシェルの PATH に追加できませんでした。次のコマンドを実行してからシェルを再読み込みしてください:' ;; *) printf 'Unknown message: %s\n' "$key" >&2; return 1 ;; esac printf "$format\n" "$@" @@ -449,62 +443,30 @@ if [ "$tool_install_exit_code" -ne 0 ]; then exit "$tool_install_exit_code" fi ok "$(message Installed)" -# uv skips shell configuration when its tool directory is already on PATH. -# Use the caller's PATH and resolve uv before removing our temporary additions. -uv_binary="$(type -P uv)" || uv_binary='' -if [ -n "$uv_binary" ]; then - case "$uv_binary" in - /*) ;; - *) uv_binary="$PWD/$uv_binary" ;; - esac -fi -uv_executable="${uv_binary:-uv}" -if [ "$(type -t uv)" = 'function' ]; then - # Keep wrapper settings consistent with the preceding tool installation. - uv_executable='uv' -fi -# Preserve invocation-only settings in the retry command, including wrapper overrides. -tool_bin_dir="$("$uv_executable" tool dir --bin)" || tool_bin_dir="${UV_TOOL_BIN_DIR:-}" -# uv uses an error exit even when its shell configuration is already correct. -# Older uv releases omit backticks around the directory in this diagnostic. -# Match only that single-line diagnostic; preserve other errors and retry commands. -if shell_update_output="$( - export PATH="$original_path" - # Wrappers using `command uv` must still find a recovered binary, while uv - # itself must see the original PATH when deciding whether to update the shell. - if [ -n "$uv_binary" ]; then hash -p "$uv_binary" uv; fi - "$uv_executable" tool update-shell --color never 2>&1 -)"; then - [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 -elif [[ "$shell_update_output" != *$'\n'* && - "$shell_update_output" == 'error: The executable directory '*' is not in PATH, but the '*' configuration files are already up-to-date' ]]; then - info "$(message ShellPathConfigured)" -else - [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 - warn "$(message ShellPathUpdateFailed)" - if [ -n "$tool_bin_dir" ]; then - # An assignment also works with shell functions, unlike invoking through env. - printf '%s UV_TOOL_BIN_DIR=%q %q tool update-shell\n' "$indent" "$tool_bin_dir" "${uv_binary:-$uv_executable}" - else - printf '%s %q tool update-shell\n' "$indent" "${uv_binary:-$uv_executable}" - fi -fi -# A custom uv installation can live outside the tool executable directory. -# Let uv configure that directory too, without changing where tools are installed. -if [ -n "$uv_binary" ] && [ -n "$tool_bin_dir" ] && \ - [ "${uv_binary%/*}" != "$tool_bin_dir" ]; then - uv_bin_dir="${uv_binary%/*}" - if shell_update_output="$(PATH="$original_path" UV_TOOL_BIN_DIR="$uv_bin_dir" "$uv_binary" tool update-shell --color never 2>&1)"; then - [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 - elif [[ "$shell_update_output" != *$'\n'* && - "$shell_update_output" == 'error: The executable directory '*' is not in PATH, but the '*' configuration files are already up-to-date' ]]; then - info "$(message ShellPathConfigured)" +# Configure both tool and uv directories against the caller's original PATH. +uv_binary="$(type -P uv)" || uv_binary='uv' +case "$uv_binary" in + /*) ;; + */*) uv_binary="$PWD/$uv_binary" ;; +esac +uv_bin_dir='' +case "$uv_binary" in */*) uv_bin_dir="${uv_binary%/*}" ;; esac +tool_bin_dir="$(uv tool dir --bin)" || tool_bin_dir='' +previous_dir='' +for bin_dir in "$tool_bin_dir" "$uv_bin_dir"; do + [ -n "$bin_dir" ] && [ "$bin_dir" != "$previous_dir" ] || continue + previous_dir="$bin_dir" + if output="$(PATH="$original_path" UV_TOOL_BIN_DIR="$bin_dir" "$uv_binary" tool update-shell --color never 2>&1)"; then + [ -z "$output" ] || printf '%s\n' "$output" >&2 + elif [[ "$output" != *$'\n'* && + "$output" == 'error: The executable directory '*' is not in PATH, but the '*' configuration files are already up-to-date' ]]; then + : # Already configured; the caller has not reloaded the shell yet. else - [ -z "$shell_update_output" ] || printf '%s\n' "$shell_update_output" >&2 - warn "$(message UvPathUpdateFailed)" - printf '%s env UV_TOOL_BIN_DIR=%q %q tool update-shell\n' "$indent" "$uv_bin_dir" "$uv_binary" + [ -z "$output" ] || printf '%s\n' "$output" >&2 + warn "$(message ShellPathUpdateFailed)" + printf '%s UV_TOOL_BIN_DIR=%q %q tool update-shell\n' "$indent" "$bin_dir" "$uv_binary" fi -fi +done printf '\n%s%s\n' "$indent" "$(message ReloadShell)" printf '%s Bash: source ~/.bashrc\n' "$indent" printf '%s Zsh: source ~/.zshrc\n' "$indent" diff --git a/tests/test_install_script.py b/tests/test_install_script.py index 5a524a21c9e..6b8b4736396 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -354,279 +354,16 @@ def test_shell_update_failure_shows_diagnostic_and_retry_command(self): self.assertIn("[WARN]", output) self.assertIn(f"{self.root / 'bin/uv'} tool update-shell", output) - def test_exported_uv_function_reaches_shell_setup(self): - with (self.root / "env.sh").open("a") as fixture: - fixture.write( - '\ncommand() { builtin command "$@"; }\n' - 'uv() { "$TEST_DIR/fake-uv" "$@"; }\n' - ) - (self.root / "bin").mkdir() - for fail_update in ("0", "1"): - with self.subTest(fail_update=fail_update): - self.env["TEST_UPDATE_SHELL_FAIL"] = fail_update - status, output = self.run_installer( - ["--non-interactive"], existing=False - ) - self.assertEqual(status, 0, output) - self.assertTrue((self.root / "uv-update-shell").exists()) - self.assertIn("Bash: source ~/.bashrc", output) - if fail_update == "1": - self.assertIn("Cannot write shell configuration", output) - self.assertIn(" uv tool update-shell", output) - else: - self.assertTrue((self.root / ".bashrc").read_text()) - - def test_retry_from_relative_path_works_in_another_directory(self): - with (self.root / "env.sh").open("a") as fixture: - fixture.write('\ncd "$TEST_DIR"\n') - self.env.update(UV_UNMANAGED_INSTALL="bin", TEST_UPDATE_SHELL_FAIL="1") - status, output = self.run_installer(["--non-interactive"]) - self.assertEqual(status, 0, output) - retry = next( - line.strip() - for line in output.splitlines() - if line.endswith(" tool update-shell") - ) - self.assertIn(f"{self.root / 'bin/uv'} tool update-shell", retry) - retry_env = self.env.copy() - retry_env.pop("BASH_ENV") - retry_env.pop("TEST_UPDATE_SHELL_FAIL") - retry_env.pop("UV_TOOL_BIN_DIR") - result = subprocess.run( - ["/bin/bash", "-c", retry], - cwd="/", - env=retry_env, - capture_output=True, - text=True, - timeout=15, - ) - self.assertEqual(result.returncode, 0, result.stderr) - self.assertTrue((self.root / ".bashrc").read_text()) def test_separate_uv_and_tool_directories_survive_shell_reload(self): uv_bin = self.root / "uv bin" tool_bin = self.root / "tool bin" self.env["UV_UNMANAGED_INSTALL"] = str(uv_bin) self.env["UV_TOOL_BIN_DIR"] = str(tool_bin) - for fail_update in ("0", "1", "mixed"): - with self.subTest(fail_update=fail_update): - (self.root / ".bashrc").write_text("") - self.env["TEST_UPDATE_SHELL_FAIL"] = fail_update - status, output = self.run_installer( - ["--non-interactive"], existing=False - ) - self.assertEqual(status, 0, output) - shell_env = self.env.copy() - shell_env.pop("BASH_ENV") - shell_env.pop("TEST_UPDATE_SHELL_FAIL") - shell_env.pop("UV_TOOL_BIN_DIR") - if fail_update != "0": - retries = [ - line.strip() - for line in output.splitlines() - if line.endswith(" tool update-shell") - ] - self.assertTrue(retries, output) - for retry in retries: - result = subprocess.run( - ["/bin/bash", "-c", retry], - cwd="/", - env=shell_env, - capture_output=True, - text=True, - timeout=15, - ) - self.assertEqual(result.returncode, 0, result.stderr) - result = subprocess.run( - [ - "/bin/bash", - "--noprofile", - "--norc", - "-c", - 'source "$HOME/.bashrc"; command -v uv; command -v astrbot', - ], - env=shell_env, - capture_output=True, - text=True, - timeout=15, - ) - self.assertEqual(result.returncode, 0, result.stderr) - self.assertEqual( - result.stdout.splitlines(), - [str(uv_bin / "uv"), str(tool_bin / "astrbot")], - ) - - def test_shadowing_uv_function_keeps_its_tool_configuration(self): - wrapper_bin = self.root / "wrapper bin" - original_path = self.env["PATH"] - with (self.root / "env.sh").open("a") as fixture: - # Exercise Bash's real command builtin inside the wrapper. - fixture.write( - '\nunset -f command\nuv() { UV_TOOL_BIN_DIR="$TEST_DIR/wrapper bin" ' - 'command uv "$@"; }\n' - ) - for binary_on_path, fail_update in ( - (True, "0"), - (True, "1"), - (False, "0"), - (False, "1"), - ): - with self.subTest(binary_on_path=binary_on_path, fail_update=fail_update): - self.env["PATH"] = ( - f"{self.root / 'bin'}:{original_path}" - if binary_on_path - else original_path - ) - (self.root / ".bashrc").write_text("") - self.env["TEST_UPDATE_SHELL_FAIL"] = fail_update - status, output = self.run_installer(["--non-interactive"]) - self.assertEqual(status, 0, output) - self.assertTrue((wrapper_bin / "astrbot").exists()) - if fail_update == "1": - retries = [ - line.strip() - for line in output.splitlines() - if line.endswith(" tool update-shell") - ] - self.assertTrue(retries, output) - retry_env = self.env.copy() - retry_env.pop("TEST_UPDATE_SHELL_FAIL") - retry_env.pop("UV_TOOL_BIN_DIR") - for retry in retries: - result = subprocess.run( - ["/bin/bash", "-c", retry], - env=retry_env, - capture_output=True, - text=True, - timeout=15, - ) - self.assertEqual(result.returncode, 0, result.stderr) - shell_env = self.env.copy() - shell_env.pop("BASH_ENV") - result = subprocess.run( - [ - "/bin/bash", - "--noprofile", - "--norc", - "-c", - 'source "$HOME/.bashrc"; command -v uv; command -v astrbot', - ], - env=shell_env, - capture_output=True, - text=True, - timeout=15, - ) - self.assertEqual(result.returncode, 0, result.stderr) - self.assertEqual( - result.stdout.splitlines(), - [str(self.root / "bin/uv"), str(wrapper_bin / "astrbot")], - ) - - def test_legacy_uv_repeated_install_is_already_configured(self): - # uv 0.10.0 omits backticks around directories in these diagnostics. - (self.root / "fake-uv").write_bytes(FAKE_UV.replace(b"`%s`", b"%s")) - for tool_bin, entries in ((self.root / "bin", 1), (self.root / "tool bin", 2)): - with self.subTest(tool_bin=tool_bin): - (self.root / ".bashrc").write_text("") - self.env["UV_TOOL_BIN_DIR"] = str(tool_bin) - for _ in range(2): - status, output = self.run_installer(["--non-interactive"]) - self.assertEqual(status, 0, output) - self.assertNotIn("[WARN]", output) - self.assertNotIn("tool update-shell", output) - self.assertEqual( - len((self.root / ".bashrc").read_text().splitlines()), entries - ) - - def test_legacy_uv_genuine_errors_are_not_suppressed(self): - (self.root / "fake-uv").write_bytes(FAKE_UV.replace(b"`%s`", b"%s")) - for failure, diagnostic in ( - ("1", "Cannot write shell configuration"), - ("mixed", "current shell could not be determined"), - ): - with self.subTest(failure=failure): - self.env["TEST_UPDATE_SHELL_FAIL"] = failure - status, output = self.run_installer(["--non-interactive"]) - self.assertEqual(status, 0, output) - self.assertIn(diagnostic, output) - self.assertIn("[WARN]", output) - self.assertIn(" tool update-shell", output) - - def test_relative_tool_bin_is_passed_through_to_uv(self): - self.env["UV_TOOL_BIN_DIR"] = "relative tools" - status, output = self.run_installer(["--non-interactive"]) + status, output = self.run_installer(["--non-interactive"], existing=False) self.assertEqual(status, 0, output) - self.assert_install(OFFICIAL_INDEX, [""]) - self.assertTrue((self.root / "relative tools/astrbot").is_file()) - - def test_shell_path_messages_follow_selected_language(self): - self.env["UV_TOOL_BIN_DIR"] = str(self.root / "tool bin") - for language, configured, tool_failure, uv_failure in ( - ( - "en-US", - "Shell PATH is already configured.", - "Could not update shell PATH.", - "Could not add the uv installation directory to shell PATH.", - ), - ( - "zh-CN", - "Shell PATH 已配置。", - "无法更新 Shell PATH。", - "无法将 uv 安装目录添加到 Shell PATH。", - ), - ( - "ja-JP", - "シェルの PATH は設定済みです。", - "シェルの PATH を更新できませんでした。", - "uv のインストール先をシェルの PATH に追加できませんでした。", - ), - ): - with self.subTest(language=language): - args = ["--language", language, "--non-interactive"] - self.env.pop("TEST_UPDATE_SHELL_FAIL", None) - (self.root / ".bashrc").write_text("") - for _ in range(2): - status, output = self.run_installer(args) - self.assertEqual(status, 0, output) - self.assertNotIn("[WARN]", output) - self.assertEqual(output.count(configured), 2, output) - self.env["TEST_UPDATE_SHELL_FAIL"] = "1" - status, output = self.run_installer(args) - self.assertEqual(status, 0, output) - self.assertIn(tool_failure, output) - self.assertIn(uv_failure, output) - self.assertIn("Cannot write shell configuration", output) - self.assertEqual(output.count(" tool update-shell"), 2, output) - - @unittest.skipUnless(shutil.which("uv"), "Real uv is not installed") - def test_real_uv_shell_setup_survives_repeated_install(self): - # Stub only package installation; use real uv for directory and shell setup. - uv_bin = self.root / "bin" - uv_bin.mkdir() - shutil.copy2(shutil.which("uv"), uv_bin / "uv") - self.env.update( - SHELL="/bin/bash", - XDG_CONFIG_HOME=str(self.root / ".config"), - UV_TOOL_BIN_DIR=str(self.root / "tool bin"), - ) - with (self.root / "env.sh").open("a") as fixture: - fixture.write( - '\nunset -f command\n' - 'uv() {\n' - ' if [ "$1" = tool ] && [ "$2" = install ]; then\n' - ' "$TEST_DIR/fake-uv" "$@"\n' - ' else\n' - ' command uv "$@"\n' - ' fi\n' - '}\n' - ) - for _ in range(2): - status, output = self.run_installer(["--non-interactive"], existing=False) - self.assertEqual(status, 0, output) - self.assertNotIn("[WARN]", output) shell_env = self.env.copy() shell_env.pop("BASH_ENV") - shell_env.pop("UV_TOOL_BIN_DIR") result = subprocess.run( [ "/bin/bash", @@ -635,7 +372,6 @@ def test_real_uv_shell_setup_survives_repeated_install(self): "-c", 'source "$HOME/.bashrc"; command -v uv; command -v astrbot', ], - cwd="/", env=shell_env, capture_output=True, text=True, @@ -643,10 +379,10 @@ def test_real_uv_shell_setup_survives_repeated_install(self): ) self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual( - result.stdout.splitlines(), - [str(uv_bin / "uv"), str(self.root / "tool bin/astrbot")], + result.stdout.splitlines(), [str(uv_bin / "uv"), str(tool_bin / "astrbot")] ) + def test_chinese_defaults_to_all_china_mirrors(self): status, output = self.run_installer( ["--language", "zh-CN", "--non-interactive"] From ad0ba55cb1034087037da84712929c13e89b24ae Mon Sep 17 00:00:00 2001 From: AzusaMoe Date: Mon, 2 Nov 2026 00:54:42 +0800 Subject: [PATCH 08/11] fix: keep PATH persistence change minimal Reuse uv's own update-shell command with the original PATH and resolved executable for both tool and uv directories. Normalize an invocation-only relative UV_TOOL_BIN_DIR before the installer changes its working directory. Remove diagnostics parsing, redundant handling, and nonessential tests; retain three targeted PATH reload regressions. --- docs/public/install.sh | 36 ++++---------- tests/test_install_script.py | 91 ++++++++++++------------------------ 2 files changed, 38 insertions(+), 89 deletions(-) diff --git a/docs/public/install.sh b/docs/public/install.sh index 736e36bd20e..5a01e63ce9f 100755 --- a/docs/public/install.sh +++ b/docs/public/install.sh @@ -130,7 +130,6 @@ message() { zh-CN:ReloadShell) format='请根据你使用的终端重新加载环境:' ;; zh-CN:RunCommands) format='重新加载后,请手动运行:' ;; zh-CN:Installed) format='AstrBot 安装完成。' ;; - zh-CN:ShellPathUpdateFailed) format='无法更新 Shell PATH。请运行以下命令,然后重新加载终端:' ;; en-US:ChooseSource) format='Select a download source' ;; en-US:China) format='Mainland China accelerated download mirrors' ;; en-US:Official) format='Official sources' ;; @@ -161,7 +160,6 @@ message() { en-US:ReloadShell) format='Reload the environment for your shell:' ;; en-US:RunCommands) format='Then run these commands manually:' ;; en-US:Installed) format='AstrBot has been installed.' ;; - en-US:ShellPathUpdateFailed) format='Could not update shell PATH. Run the following command, then reload your shell:' ;; ja-JP:ChooseSource) format='ダウンロード元を選択してください' ;; ja-JP:China) format='中国本土向け高速ダウンロードミラー' ;; ja-JP:Official) format='公式ソース' ;; @@ -192,7 +190,6 @@ message() { ja-JP:ReloadShell) format='使用するシェルに合わせて環境を再読み込みしてください:' ;; ja-JP:RunCommands) format='その後、以下のコマンドを手動で実行してください:' ;; ja-JP:Installed) format='AstrBot のインストールが完了しました。' ;; - ja-JP:ShellPathUpdateFailed) format='シェルの PATH を更新できませんでした。次のコマンドを実行してからシェルを再読み込みしてください:' ;; *) printf 'Unknown message: %s\n' "$key" >&2; return 1 ;; esac printf "$format\n" "$@" @@ -404,6 +401,11 @@ install_uv() { # Recover an existing installation before downloading uv again. original_path="$PATH" +# uv tool install changes directories, so keep custom bin paths absolute. +case "${UV_TOOL_BIN_DIR:-}" in + ''|/*) ;; + *) export UV_TOOL_BIN_DIR="$PWD/$UV_TOOL_BIN_DIR" ;; +esac refresh_uv_path if ! has uv; then install_uv; fi if ! has uv; then err "$(message UvFailed)"; exit 1; fi @@ -443,30 +445,10 @@ if [ "$tool_install_exit_code" -ne 0 ]; then exit "$tool_install_exit_code" fi ok "$(message Installed)" -# Configure both tool and uv directories against the caller's original PATH. -uv_binary="$(type -P uv)" || uv_binary='uv' -case "$uv_binary" in - /*) ;; - */*) uv_binary="$PWD/$uv_binary" ;; -esac -uv_bin_dir='' -case "$uv_binary" in */*) uv_bin_dir="${uv_binary%/*}" ;; esac -tool_bin_dir="$(uv tool dir --bin)" || tool_bin_dir='' -previous_dir='' -for bin_dir in "$tool_bin_dir" "$uv_bin_dir"; do - [ -n "$bin_dir" ] && [ "$bin_dir" != "$previous_dir" ] || continue - previous_dir="$bin_dir" - if output="$(PATH="$original_path" UV_TOOL_BIN_DIR="$bin_dir" "$uv_binary" tool update-shell --color never 2>&1)"; then - [ -z "$output" ] || printf '%s\n' "$output" >&2 - elif [[ "$output" != *$'\n'* && - "$output" == 'error: The executable directory '*' is not in PATH, but the '*' configuration files are already up-to-date' ]]; then - : # Already configured; the caller has not reloaded the shell yet. - else - [ -z "$output" ] || printf '%s\n' "$output" >&2 - warn "$(message ShellPathUpdateFailed)" - printf '%s UV_TOOL_BIN_DIR=%q %q tool update-shell\n' "$indent" "$bin_dir" "$uv_binary" - fi -done +# uv must remain executable while uv checks the unmodified caller PATH. +uv_binary="$(type -P uv)" +PATH="$original_path" "$uv_binary" tool update-shell >/dev/null 2>&1 || true +PATH="$original_path" UV_TOOL_BIN_DIR="${uv_binary%/*}" "$uv_binary" tool update-shell >/dev/null 2>&1 || true printf '\n%s%s\n' "$indent" "$(message ReloadShell)" printf '%s Bash: source ~/.bashrc\n' "$indent" printf '%s Zsh: source ~/.zshrc\n' "$indent" diff --git a/tests/test_install_script.py b/tests/test_install_script.py index 6b8b4736396..be99bf05fcc 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -33,24 +33,10 @@ fi if [ "$1" = tool ] && [ "$2" = update-shell ]; then touch "$TEST_DIR/uv-update-shell" - printf '%s\n' "$PATH" > "$TEST_DIR/update-shell-path" - if [ "${TEST_UPDATE_SHELL_FAIL:-0}" = mixed ]; then - printf 'error: The executable directory `%s` is not in PATH, but the current shell could not be determined\n' "$tool_bin_dir" >&2 - printf 'error: The executable directory `%s` is not in PATH, but the Bash configuration files are already up-to-date\n' "$tool_bin_dir" >&2 - exit 2 - fi - if [ "${TEST_UPDATE_SHELL_FAIL:-0}" = 1 ]; then - printf 'Cannot write shell configuration\n' >&2 - exit 2 - fi case ":$PATH:" in *":$tool_bin_dir:"*) exit 0 ;; esac line="$(printf 'export PATH=%q:\"$PATH\"' "$tool_bin_dir")" - if grep -Fqx "$line" "$HOME/.bashrc"; then - printf 'error: The executable directory `%s` is not in PATH, but the Bash configuration files are already up-to-date\n' "$tool_bin_dir" >&2 - exit 2 - fi printf '%s\n' "$line" >> "$HOME/.bashrc" exit 0 fi @@ -178,7 +164,9 @@ def setUp(self): TMPDIR=str(self.root), ) - def run_installer(self, args, *, existing=True, answers=None, piped=False): + def run_installer( + self, args, *, existing=True, answers=None, piped=False, cwd=None + ): if existing: (self.root / "bin").mkdir(exist_ok=True) shutil.copy2(self.root / "fake-uv", self.root / "bin/uv") @@ -190,6 +178,7 @@ def run_installer(self, args, *, existing=True, answers=None, piped=False): stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=self.env, + cwd=cwd, timeout=15, start_new_session=True, ) @@ -307,53 +296,32 @@ def test_fresh_install_commands_available_after_bash_reload(self): [str(self.root / "bin/uv"), str(self.root / "bin/astrbot")], ) - def test_recovered_uv_updates_shell_with_original_path(self): - # Spaces in the install directory must survive executable lookup. - install_dir = self.root / "custom bin" - install_dir.mkdir() - shutil.copy2(self.root / "fake-uv", install_dir / "uv") - self.env["UV_UNMANAGED_INSTALL"] = str(install_dir) - self.env["PATH"] += ":/custom/bin" - for tool_bin, entries in ((install_dir, 1), (self.root / "tool bin", 2)): - with self.subTest(tool_bin=tool_bin): - (self.root / ".bashrc").write_text("") - self.env["UV_TOOL_BIN_DIR"] = str(tool_bin) - for _ in range(2): - status, output = self.run_installer( - ["--non-interactive"], existing=False - ) - self.assertEqual(status, 0, output) - self.assertNotIn("[WARN]", output) - self.assertNotIn("tool update-shell", output) - self.assertEqual( - (self.root / "update-shell-path").read_text().strip(), - self.env["PATH"], - ) - self.assertEqual( - len((self.root / ".bashrc").read_text().splitlines()), entries - ) - self.assertFalse((self.root / "requests").exists()) - - def test_existing_path_does_not_modify_shell_configuration(self): - self.env["PATH"] = f"{self.root / 'bin'}:{self.env['PATH']}" - status, output = self.run_installer(["--non-interactive"]) + def test_relative_tool_bin_survives_reload_from_another_directory(self): + self.env["UV_TOOL_BIN_DIR"] = "relative tools" + status, output = self.run_installer( + ["--non-interactive"], existing=False, cwd=self.root + ) self.assertEqual(status, 0, output) - self.assertEqual((self.root / ".bashrc").read_text(), "") - - def test_shell_update_failure_shows_diagnostic_and_retry_command(self): - for failure, diagnostic in ( - ("1", "Cannot write shell configuration"), - ("mixed", "current shell could not be determined"), - ): - with self.subTest(failure=failure): - self.env["TEST_UPDATE_SHELL_FAIL"] = failure - status, output = self.run_installer(["--non-interactive"]) - self.assertEqual(status, 0, output) - self.assertIn("AstrBot has been installed", output) - self.assertIn(diagnostic, output) - self.assertIn("[WARN]", output) - self.assertIn(f"{self.root / 'bin/uv'} tool update-shell", output) - + tool = self.root / "relative tools/astrbot" + self.assertTrue(tool.is_file()) + shell_env = self.env.copy() + shell_env.pop("BASH_ENV") + result = subprocess.run( + [ + "/bin/bash", + "--noprofile", + "--norc", + "-c", + 'source "$HOME/.bashrc"; command -v astrbot', + ], + cwd="/", + env=shell_env, + capture_output=True, + text=True, + timeout=15, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout.strip(), str(tool)) def test_separate_uv_and_tool_directories_survive_shell_reload(self): uv_bin = self.root / "uv bin" @@ -382,7 +350,6 @@ def test_separate_uv_and_tool_directories_survive_shell_reload(self): result.stdout.splitlines(), [str(uv_bin / "uv"), str(tool_bin / "astrbot")] ) - def test_chinese_defaults_to_all_china_mirrors(self): status, output = self.run_installer( ["--language", "zh-CN", "--non-interactive"] From d307e6567d715297b5b426398b50245398b0eb85 Mon Sep 17 00:00:00 2001 From: AzusaMoe Date: Mon, 2 Nov 2026 01:04:09 +0800 Subject: [PATCH 09/11] refactor: avoid redundant uv shell updates Query the actual uv tool executable directory before updating uv's own directory, so shared directories are not configured twice. Strengthen the existing reload regression test to reject duplicate PATH entries; retain the real uv directory query fixture because it is now used. --- docs/public/install.sh | 4 +++- tests/test_install_script.py | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/public/install.sh b/docs/public/install.sh index 5a01e63ce9f..f0d533c3d8a 100755 --- a/docs/public/install.sh +++ b/docs/public/install.sh @@ -448,7 +448,9 @@ ok "$(message Installed)" # uv must remain executable while uv checks the unmodified caller PATH. uv_binary="$(type -P uv)" PATH="$original_path" "$uv_binary" tool update-shell >/dev/null 2>&1 || true -PATH="$original_path" UV_TOOL_BIN_DIR="${uv_binary%/*}" "$uv_binary" tool update-shell >/dev/null 2>&1 || true +if [ "$("$uv_binary" tool dir --bin)" != "${uv_binary%/*}" ]; then + PATH="$original_path" UV_TOOL_BIN_DIR="${uv_binary%/*}" "$uv_binary" tool update-shell >/dev/null 2>&1 || true +fi printf '\n%s%s\n' "$indent" "$(message ReloadShell)" printf '%s Bash: source ~/.bashrc\n' "$indent" printf '%s Zsh: source ~/.zshrc\n' "$indent" diff --git a/tests/test_install_script.py b/tests/test_install_script.py index be99bf05fcc..20a422d87d3 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -295,6 +295,7 @@ def test_fresh_install_commands_available_after_bash_reload(self): result.stdout.splitlines(), [str(self.root / "bin/uv"), str(self.root / "bin/astrbot")], ) + self.assertEqual(len((self.root / ".bashrc").read_text().splitlines()), 1) def test_relative_tool_bin_survives_reload_from_another_directory(self): self.env["UV_TOOL_BIN_DIR"] = "relative tools" From 59812859c018758bf0b71522af13b2da46938396 Mon Sep 17 00:00:00 2001 From: AzusaMoe Date: Mon, 2 Nov 2026 01:11:57 +0800 Subject: [PATCH 10/11] refactor: remove unnecessary uv shell precheck Rely on uv's built-in idempotent shell configuration instead of querying tool dir before updating uv's executable directory. Delete the unused fake tool-dir command and redundant test assertions; retain three behavioral PATH reload regressions. --- docs/public/install.sh | 4 +--- tests/test_install_script.py | 6 ------ 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/docs/public/install.sh b/docs/public/install.sh index f0d533c3d8a..5a01e63ce9f 100755 --- a/docs/public/install.sh +++ b/docs/public/install.sh @@ -448,9 +448,7 @@ ok "$(message Installed)" # uv must remain executable while uv checks the unmodified caller PATH. uv_binary="$(type -P uv)" PATH="$original_path" "$uv_binary" tool update-shell >/dev/null 2>&1 || true -if [ "$("$uv_binary" tool dir --bin)" != "${uv_binary%/*}" ]; then - PATH="$original_path" UV_TOOL_BIN_DIR="${uv_binary%/*}" "$uv_binary" tool update-shell >/dev/null 2>&1 || true -fi +PATH="$original_path" UV_TOOL_BIN_DIR="${uv_binary%/*}" "$uv_binary" tool update-shell >/dev/null 2>&1 || true printf '\n%s%s\n' "$indent" "$(message ReloadShell)" printf '%s Bash: source ~/.bashrc\n' "$indent" printf '%s Zsh: source ~/.zshrc\n' "$indent" diff --git a/tests/test_install_script.py b/tests/test_install_script.py index 20a422d87d3..1dc36b29def 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -27,10 +27,6 @@ FAKE_UV = rb"""#!/bin/bash if [ "$1" = --version ]; then printf 'uv fixture\n'; exit 0; fi tool_bin_dir="${UV_TOOL_BIN_DIR:-$HOME/.local/bin}" -if [ "$1" = tool ] && [ "$2" = dir ] && [ "$3" = --bin ]; then - printf '%s\n' "$tool_bin_dir" - exit 0 -fi if [ "$1" = tool ] && [ "$2" = update-shell ]; then touch "$TEST_DIR/uv-update-shell" case ":$PATH:" in @@ -295,7 +291,6 @@ def test_fresh_install_commands_available_after_bash_reload(self): result.stdout.splitlines(), [str(self.root / "bin/uv"), str(self.root / "bin/astrbot")], ) - self.assertEqual(len((self.root / ".bashrc").read_text().splitlines()), 1) def test_relative_tool_bin_survives_reload_from_another_directory(self): self.env["UV_TOOL_BIN_DIR"] = "relative tools" @@ -304,7 +299,6 @@ def test_relative_tool_bin_survives_reload_from_another_directory(self): ) self.assertEqual(status, 0, output) tool = self.root / "relative tools/astrbot" - self.assertTrue(tool.is_file()) shell_env = self.env.copy() shell_env.pop("BASH_ENV") result = subprocess.run( From 60c6b40cac08f26d41b58d345af8ba6ff8c1f178 Mon Sep 17 00:00:00 2001 From: AzusaMoe Date: Mon, 2 Nov 2026 01:29:16 +0800 Subject: [PATCH 11/11] fix: preserve uv PATH setup across shell wrappers and relative directories Resolve existing installer directory overrides before changing directories, and invoke the same uv provider when configuring tool PATH. Preserve the uv binary lookup in the original PATH and report genuine shell configuration errors while ignoring uv's already-configured diagnostic. Add focused regression tests for wrapper, function-only, relative uv path, repeated install, and configuration failures. --- docs/public/install.sh | 42 ++++++++++--- tests/test_install_script.py | 115 ++++++++++++++++++++--------------- 2 files changed, 100 insertions(+), 57 deletions(-) diff --git a/docs/public/install.sh b/docs/public/install.sh index 5a01e63ce9f..27132f2ed23 100755 --- a/docs/public/install.sh +++ b/docs/public/install.sh @@ -130,6 +130,7 @@ message() { zh-CN:ReloadShell) format='请根据你使用的终端重新加载环境:' ;; zh-CN:RunCommands) format='重新加载后,请手动运行:' ;; zh-CN:Installed) format='AstrBot 安装完成。' ;; + zh-CN:ShellPathUpdateFailed) format='无法更新 Shell PATH,请检查以下错误信息。' ;; en-US:ChooseSource) format='Select a download source' ;; en-US:China) format='Mainland China accelerated download mirrors' ;; en-US:Official) format='Official sources' ;; @@ -160,6 +161,7 @@ message() { en-US:ReloadShell) format='Reload the environment for your shell:' ;; en-US:RunCommands) format='Then run these commands manually:' ;; en-US:Installed) format='AstrBot has been installed.' ;; + en-US:ShellPathUpdateFailed) format='Could not update shell PATH; see the error below.' ;; ja-JP:ChooseSource) format='ダウンロード元を選択してください' ;; ja-JP:China) format='中国本土向け高速ダウンロードミラー' ;; ja-JP:Official) format='公式ソース' ;; @@ -190,6 +192,7 @@ message() { ja-JP:ReloadShell) format='使用するシェルに合わせて環境を再読み込みしてください:' ;; ja-JP:RunCommands) format='その後、以下のコマンドを手動で実行してください:' ;; ja-JP:Installed) format='AstrBot のインストールが完了しました。' ;; + ja-JP:ShellPathUpdateFailed) format='シェルの PATH を更新できませんでした。以下のエラーを確認してください。' ;; *) printf 'Unknown message: %s\n' "$key" >&2; return 1 ;; esac printf "$format\n" "$@" @@ -401,11 +404,12 @@ install_uv() { # Recover an existing installation before downloading uv again. original_path="$PATH" -# uv tool install changes directories, so keep custom bin paths absolute. -case "${UV_TOOL_BIN_DIR:-}" in - ''|/*) ;; - *) export UV_TOOL_BIN_DIR="$PWD/$UV_TOOL_BIN_DIR" ;; -esac +# uv tool install runs from a temporary directory; resolve bin paths first. +for var in UV_INSTALL_DIR UV_UNMANAGED_INSTALL UV_TOOL_BIN_DIR XDG_BIN_HOME XDG_DATA_HOME; do + if [ -n "${!var:-}" ] && [[ "${!var}" != /* ]]; then + export "$var=$PWD/${!var}" + fi +done refresh_uv_path if ! has uv; then install_uv; fi if ! has uv; then err "$(message UvFailed)"; exit 1; fi @@ -445,10 +449,30 @@ if [ "$tool_install_exit_code" -ne 0 ]; then exit "$tool_install_exit_code" fi ok "$(message Installed)" -# uv must remain executable while uv checks the unmodified caller PATH. -uv_binary="$(type -P uv)" -PATH="$original_path" "$uv_binary" tool update-shell >/dev/null 2>&1 || true -PATH="$original_path" UV_TOOL_BIN_DIR="${uv_binary%/*}" "$uv_binary" tool update-shell >/dev/null 2>&1 || true +# uv reports a nonzero exit if the configuration already exists but PATH is stale. +# Let uv inspect the caller's PATH, while keeping the recovered executable usable. +uv_binary="$(type -P uv)" || uv_binary='' +case "$uv_binary" in /*|'') ;; *) uv_binary="$PWD/$uv_binary" ;; esac +shell_output="$( + export PATH="$original_path" + if [ -n "$uv_binary" ]; then hash -p "$uv_binary" uv; fi + uv tool update-shell 2>&1 +)" || { + if [[ "$shell_output" == *$'\n'* || + "$shell_output" != *' configuration files are already up-to-date' ]]; then + warn "$(message ShellPathUpdateFailed)" + [ -z "$shell_output" ] || printf '%s\n' "$shell_output" >&2 + fi +} +if [ -n "$uv_binary" ]; then + shell_output="$(PATH="$original_path" UV_TOOL_BIN_DIR="${uv_binary%/*}" "$uv_binary" tool update-shell 2>&1)" || { + if [[ "$shell_output" == *$'\n'* || + "$shell_output" != *' configuration files are already up-to-date' ]]; then + warn "$(message ShellPathUpdateFailed)" + [ -z "$shell_output" ] || printf '%s\n' "$shell_output" >&2 + fi + } +fi printf '\n%s%s\n' "$indent" "$(message ReloadShell)" printf '%s Bash: source ~/.bashrc\n' "$indent" printf '%s Zsh: source ~/.zshrc\n' "$indent" diff --git a/tests/test_install_script.py b/tests/test_install_script.py index 1dc36b29def..f79d7dd4592 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -32,7 +32,15 @@ case ":$PATH:" in *":$tool_bin_dir:"*) exit 0 ;; esac + if [ "${TEST_UPDATE_SHELL_FAIL:-0}" = 1 ]; then + printf 'Cannot write shell configuration\n' >&2 + exit 2 + fi line="$(printf 'export PATH=%q:\"$PATH\"' "$tool_bin_dir")" + if grep -Fqx "$line" "$HOME/.bashrc"; then + printf 'error: The executable directory %s is not in PATH, but the Bash configuration files are already up-to-date\n' "$tool_bin_dir" >&2 + exit 2 + fi printf '%s\n' "$line" >> "$HOME/.bashrc" exit 0 fi @@ -268,28 +276,26 @@ def test_defaults_ignore_system_locale(self): output, ) - def test_fresh_install_commands_available_after_bash_reload(self): - status, output = self.run_installer(["--non-interactive"], existing=False) - self.assertEqual(status, 0, output) + def assert_reloaded_commands(self, *paths): shell_env = self.env.copy() shell_env.pop("BASH_ENV") result = subprocess.run( - [ - "/bin/bash", - "--noprofile", - "--norc", - "-c", - 'source "$HOME/.bashrc"; command -v uv; command -v astrbot', - ], + ["/bin/bash", "--noprofile", "--norc", "-c", + 'source "$HOME/.bashrc"; command -v uv; command -v astrbot'], + cwd="/", env=shell_env, capture_output=True, text=True, timeout=15, ) self.assertEqual(result.returncode, 0, result.stderr) - self.assertEqual( - result.stdout.splitlines(), - [str(self.root / "bin/uv"), str(self.root / "bin/astrbot")], + self.assertEqual(result.stdout.splitlines(), list(paths)) + + def test_fresh_install_commands_available_after_bash_reload(self): + status, output = self.run_installer(["--non-interactive"], existing=False) + self.assertEqual(status, 0, output) + self.assert_reloaded_commands( + str(self.root / "bin/uv"), str(self.root / "bin/astrbot") ) def test_relative_tool_bin_survives_reload_from_another_directory(self): @@ -298,25 +304,9 @@ def test_relative_tool_bin_survives_reload_from_another_directory(self): ["--non-interactive"], existing=False, cwd=self.root ) self.assertEqual(status, 0, output) - tool = self.root / "relative tools/astrbot" - shell_env = self.env.copy() - shell_env.pop("BASH_ENV") - result = subprocess.run( - [ - "/bin/bash", - "--noprofile", - "--norc", - "-c", - 'source "$HOME/.bashrc"; command -v astrbot', - ], - cwd="/", - env=shell_env, - capture_output=True, - text=True, - timeout=15, + self.assert_reloaded_commands( + str(self.root / "bin/uv"), str(self.root / "relative tools/astrbot") ) - self.assertEqual(result.returncode, 0, result.stderr) - self.assertEqual(result.stdout.strip(), str(tool)) def test_separate_uv_and_tool_directories_survive_shell_reload(self): uv_bin = self.root / "uv bin" @@ -325,25 +315,54 @@ def test_separate_uv_and_tool_directories_survive_shell_reload(self): self.env["UV_TOOL_BIN_DIR"] = str(tool_bin) status, output = self.run_installer(["--non-interactive"], existing=False) self.assertEqual(status, 0, output) - shell_env = self.env.copy() - shell_env.pop("BASH_ENV") - result = subprocess.run( - [ - "/bin/bash", - "--noprofile", - "--norc", - "-c", - 'source "$HOME/.bashrc"; command -v uv; command -v astrbot', - ], - env=shell_env, - capture_output=True, - text=True, - timeout=15, + self.assert_reloaded_commands(str(uv_bin / "uv"), str(tool_bin / "astrbot")) + + def test_uv_function_wrapper_preserves_tool_directory(self): + (self.root / "env.sh").write_text( + (self.root / "env.sh").read_text() + + '\nuv() { UV_TOOL_BIN_DIR="$TEST_DIR/wrapped bin" command uv "$@"; }\n' ) - self.assertEqual(result.returncode, 0, result.stderr) - self.assertEqual( - result.stdout.splitlines(), [str(uv_bin / "uv"), str(tool_bin / "astrbot")] + status, output = self.run_installer(["--non-interactive"]) + self.assertEqual(status, 0, output) + self.assert_reloaded_commands( + str(self.root / "bin/uv"), str(self.root / "wrapped bin/astrbot") + ) + + def test_uv_shell_function_without_path_binary_completes(self): + (self.root / "env.sh").write_text( + (self.root / "env.sh").read_text() + + '\nunset -f command\nuv() { "$TEST_DIR/fake-uv" "$@"; }\n' + ) + self.env["UV_UNMANAGED_INSTALL"] = str(self.root / "missing-bin") + status, output = self.run_installer(["--non-interactive"], existing=False) + self.assertEqual(status, 0, output) + self.assertIn("Reload the environment for your shell:", output) + self.assert_reloaded_commands(str(self.root / "bin/astrbot")) + + def test_relative_uv_install_directory_survives_shell_reload(self): + self.env["UV_UNMANAGED_INSTALL"] = "relative uv" + status, output = self.run_installer( + ["--non-interactive"], existing=False, cwd=self.root ) + self.assertEqual(status, 0, output) + self.assert_reloaded_commands( + str(self.root / "relative uv/uv"), str(self.root / "bin/astrbot") + ) + + def test_shell_configuration_write_failure_is_reported(self): + self.env["TEST_UPDATE_SHELL_FAIL"] = "1" + status, output = self.run_installer(["--non-interactive"]) + self.assertEqual(status, 0, output) + self.assertIn("Cannot write shell configuration", output) + self.assertIn("[WARN]", output) + self.assertEqual((self.root / ".bashrc").read_text(), "") + + def test_repeated_install_does_not_warn_for_existing_shell_config(self): + for _ in range(2): + status, output = self.run_installer(["--non-interactive"]) + self.assertEqual(status, 0, output) + self.assertNotIn("[WARN]", output) + self.assertEqual(len((self.root / ".bashrc").read_text().splitlines()), 1) def test_chinese_defaults_to_all_china_mirrors(self): status, output = self.run_installer(