diff --git a/docs/public/install.sh b/docs/public/install.sh index 700fcd7fe9e..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" "$@" @@ -400,6 +403,13 @@ install_uv() { } # Recover an existing installation before downloading uv again. +original_path="$PATH" +# 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 @@ -439,7 +449,30 @@ 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 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 34901a829aa..f79d7dd4592 100644 --- a/tests/test_install_script.py +++ b/tests/test_install_script.py @@ -24,16 +24,36 @@ "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 +tool_bin_dir="${UV_TOOL_BIN_DIR:-$HOME/.local/bin}" +if [ "$1" = tool ] && [ "$2" = update-shell ]; then + touch "$TEST_DIR/uv-update-shell" + 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 +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 +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. @@ -47,7 +67,6 @@ fi builtin command "$@" } -uv() { "$UV_UNMANAGED_INSTALL/uv" "$@"; } uname() { case "$1" in -s) printf '%s\n' "${TEST_PLATFORM:-Darwin}" ;; @@ -116,6 +135,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,14 +158,19 @@ 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"), + UV_TOOL_BIN_DIR=str(self.root / "bin"), NO_COLOR="1", 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") @@ -157,6 +182,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, ) @@ -250,6 +276,94 @@ def test_defaults_ignore_system_locale(self): 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'], + cwd="/", + env=shell_env, + capture_output=True, + text=True, + timeout=15, + ) + self.assertEqual(result.returncode, 0, result.stderr) + 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): + 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.assert_reloaded_commands( + str(self.root / "bin/uv"), str(self.root / "relative tools/astrbot") + ) + + 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) + status, output = self.run_installer(["--non-interactive"], existing=False) + self.assertEqual(status, 0, output) + 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' + ) + 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( ["--language", "zh-CN", "--non-interactive"]