blob: 41ab171d2f0ca98b99f21ea6af01b50d0a0fcc3c [file]
#!/bin/bash
# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
### Test expected usage of host-tool in fx scripts
# Source platform.sh so that we can point to jq and include it as a runtime dependency.
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)/../../lib/platform.sh" || exit $?
BT_FILE_DEPS=(
"build/bazel/config/bazel_top_dir"
"build/profile/procfs.py"
"build/scripts/lock_utils.py"
"scripts/fx"
"scripts/fx-reentry"
"prebuilt/third_party/jq/${HOST_PLATFORM}/bin/jq"
"tools/devshell/fx_cli/__init__.py"
"tools/devshell/fx_cli/builder.py"
"tools/devshell/fx_cli/fx_builtins.py"
"tools/devshell/fx_cli/locator.py"
"tools/devshell/fx_cli/main.py"
"tools/devshell/fx_cli/metadata.py"
"tools/devshell/fx_cli/metrics.py"
"tools/devshell/fx_cli/registry.py"
"tools/devshell/fx_host_tools.json"
"tools/devshell/lib/fx-cmd-locator.sh"
"tools/devshell/lib/fx-optional-features.sh"
"tools/devshell/lib/host_symbolizer.sh"
"tools/devshell/lib/generate-ssh-config.sh"
"tools/devshell/lib/vars.sh"
"tools/devshell/lib/is_invoked_by_agent.sh"
"tools/devshell/lib/agent_session_ids.txt"
"tools/devshell/lib/agents.txt"
"tools/devshell/lib/platform.sh"
"tools/devshell/host-tool"
"tools/devshell/list-build-artifacts"
"tools/devshell/jq.fx"
)
BT_MOCKED_TOOLS=(
"tools/devshell/build"
)
declare fx ffx _TEST_PY3 _TEST_ALL_PATHS
BT_SET_UP() {
source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh"
fx="$(btf::setup_fx)"
source "${BT_TEMP_DIR}/tools/devshell/lib/vars.sh"
fx-config-read
mkdir -p "${BT_TEMP_DIR}/.git" "${BT_TEMP_DIR}/manifests"
echo "rev-initial-1111" > "${BT_TEMP_DIR}/.git/JIRI_HEAD"
echo '[{"package":"initial-lock-v1"}]' > "${BT_TEMP_DIR}/manifests/jiri.lock"
local _plat_lines
_plat_lines="$(
FUCHSIA_DIR="${BT_TEMP_DIR}" bash -c '
source "$1" || exit $?
py_var="PREBUILT_""PYTHON3"
paths_var="PREBUILT_""ALL_PATHS"
printf "%s\n%s\n" "${!py_var}" "${!paths_var}"
' _ "${BT_TEMP_DIR}/tools/devshell/lib/platform.sh"
)"
local -a _plat_arr
mapfile -t _plat_arr <<< "${_plat_lines}"
_TEST_PY3="${_plat_arr[0]}"
_TEST_ALL_PATHS="${_plat_arr[1]}"
mkdir -p "$(dirname "${_TEST_PY3}")"
ln -sf "$(command -v python3)" "${_TEST_PY3}"
# Start with an empty registry so GN fallback tests stay isolated from real entries.
echo '{}' > "${BT_TEMP_DIR}/tools/devshell/fx_host_tools.json"
ffx="$(btf::make_installed_hosttools_mock ffx)"
ffx_rel="${ffx##${FUCHSIA_BUILD_DIR}/}"
# slow_tool is used by the TEST_kill_bg_process below and must be
# listed in tool_paths.json.
slow_tool="$(btf::make_installed_hosttools_mock slow_tool)"
slow_tool_rel="${slow_tool##${FUCHSIA_BUILD_DIR}/}"
# Set up dummy build API modules.
cat > "${BT_TEMP_DIR}/out/default/tool_paths.json" <<EOF
[
{
"cpu": "${HOST_CPU}",
"label": "//tools/ffx(//build/toolchain:host_${HOST_CPU})",
"name": "ffx",
"os": "${HOST_OS}",
"path": "${ffx_rel}"
},
{
"cpu": "${HOST_CPU}",
"label": "//tools/slow_tool(//build/toolchain:host_${HOST_CPU})",
"name": "slow_tool",
"os": "${HOST_OS}",
"path": "${slow_tool_rel}"
}
]
EOF
}
_install_mock_bazel_fx_build() {
cat > "${BT_TEMP_DIR}/tools/devshell/build.mock_side_effects" <<'EOF'
_mock_build_dir="${FUCHSIA_BUILD_DIR:-}"
if [[ -z "${_mock_build_dir}" ]]; then
echo "ERROR: FUCHSIA_BUILD_DIR not set for fx-reentry build" >&2
return 1
fi
mkdir -p "${_mock_build_dir}"
_mock_state_file="${_mock_build_dir}/bazel_build.mock_state"
printf 'ARGS=%s\n' "$*" >> "${_mock_state_file}"
_mock_bep_file=""
_mock_target_label=""
for _mock_arg in "$@"; do
case "${_mock_arg}" in
--build_event_json_file=*)
_mock_bep_file="${_mock_arg#--build_event_json_file=}"
;;
@//*|//*)
_mock_target_label="${_mock_arg#@}"
;;
esac
done
if [[ -n "${_mock_bep_file}" && -n "${_mock_target_label}" ]]; then
_mock_clean_rel="${_mock_target_label#//}"
if [[ "${_mock_clean_rel}" == *:* ]]; then
_mock_pkg_rel="${_mock_clean_rel%%:*}"
_mock_tname="${_mock_clean_rel#*:}"
else
_mock_pkg_rel="${_mock_clean_rel}"
_mock_tname="${_mock_pkg_rel##*/}"
fi
_mock_exec_bin_dir="${_mock_build_dir}/gen/build/bazel/output_base/execroot/_main/bazel-out/x86_64-opt/bin/${_mock_pkg_rel}"
_mock_out_bin=""
for _mock_cand in "${_mock_exec_bin_dir}/${_mock_tname}" "${_mock_exec_bin_dir}"/*; do
if [[ -f "${_mock_cand}" && "${_mock_cand}" != *.runtime_dir ]]; then
_mock_out_bin="${_mock_cand}"
break
fi
done
if [[ -n "${_mock_out_bin}" ]]; then
_mock_out_name="$(basename "${_mock_out_bin}")"
_mock_encoded_out_bin="${_mock_out_bin// /%20}"
cat > "${_mock_bep_file}" <<JSON
{"id":{"namedSet":{"id":"0"}},"namedSetOfFiles":{"files":[{"name":"${_mock_pkg_rel}/${_mock_out_name}","uri":"file://${_mock_encoded_out_bin}"}]}}
{"id":{"targetCompleted":{"label":"@@//${_mock_pkg_rel}:${_mock_tname}"}},"completed":{"success":true}}
JSON
fi
fi
EOF
}
_stage_fake_bazel_output() {
local pkg_rel="$1"
local out_name="$2"
local message="$3"
local exec_marker="${4:-}"
local bazel_pkg_bin="${BT_TEMP_DIR}/out/default/gen/build/bazel/output_base/execroot/_main/bazel-out/x86_64-opt/bin/${pkg_rel}"
mkdir -p "${bazel_pkg_bin}/${out_name}.runtime_dir"
printf '%s\n' "${message}" > "${bazel_pkg_bin}/${out_name}.runtime_dir/payload.txt"
cat > "${bazel_pkg_bin}/${out_name}" <<EOF
#!/bin/bash
set -euo pipefail
if [[ -n "${exec_marker}" ]]; then
touch "${exec_marker}"
fi
SCRIPT_DIR="\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" && pwd)"
PAYLOAD="\$(cat "\${SCRIPT_DIR}/${out_name}.runtime_dir/payload.txt")"
echo "\${PAYLOAD}|no_build_env=\${FX_NO_BUILD_HOST_TOOL:-<unset>}|inject=\${FX_INJECT_PREBUILT_PATHS:-<unset>}|args=\$*"
EOF
chmod +x "${bazel_pkg_bin}/${out_name}"
}
TEST_tool_executed() {
BT_EXPECT ${fx} host-tool "ffx" "--flag1" "arg1"
btf::expect-mock-args "${ffx}.mock_state" "--flag1" "arg1"
}
TEST_tool_not_built() {
BT_EXPECT ${fx} host-tool "ffx" "--flag1" "arg1"
# tool exists, so build should not be called
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/tools/devshell/build.mock_state"
}
TEST_build_if_not_found() {
# tool does not exist, but is known to the build, so build should be called
mv "${ffx}" "${ffx}_backup"
# recreate the tool when 'fx build' is called
cat >"${BT_TEMP_DIR}/tools/devshell/build.mock_side_effects" <<EOF
mv "${ffx}_backup" "${ffx}"
EOF
# When run under the fx test framework, stdout is not a tty, so
# host-tool will not build, instead it will print an error.
BT_EXPECT_FAIL ${fx} host-tool "ffx" "--flag1" "arg1" 2>/dev/null
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/tools/devshell/build.mock_state"
# HACK: this tool checks to see if stdout is a tty, and builds iff it
# is, so it is mocked.
cat >>"${BT_TEMP_DIR}/tools/devshell/lib/vars.sh" <<EOF
function fx-is-stderr-tty {
return 0
}
EOF
BT_EXPECT ${fx} host-tool "ffx" "--flag2" "arg2"
BT_ASSERT_FILE_EXISTS "${BT_TEMP_DIR}/tools/devshell/build.mock_state"
btf::expect-mock-args "${ffx}.mock_state" "--flag2" "arg2"
}
TEST_build_if_not_found_in_agent() {
# tool does not exist, but is known to the build, so build should be called
mv "${ffx}" "${ffx}_backup"
# recreate the tool when 'fx build' is called
cat >"${BT_TEMP_DIR}/tools/devshell/build.mock_side_effects" <<EOF
mv "${ffx}_backup" "${ffx}"
EOF
# Even when stderr is not a tty, host-tool should build if invoked by an agent.
BT_EXPECT _FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 ${fx} host-tool "ffx" "--flag2" "arg2" 2>/dev/null
BT_ASSERT_FILE_EXISTS "${BT_TEMP_DIR}/tools/devshell/build.mock_state"
btf::expect-mock-args "${ffx}.mock_state" "--flag2" "arg2"
}
TEST_no_build() {
# tool does not exist, is known to the build, but --no-build is specified
mv "${ffx}" "${ffx}_backup"
# should fail because tool cannot be found
BT_EXPECT_FAIL ${fx} host-tool "--no-build" "ffx" "--flag1" "arg1" 2> /dev/null
BT_EXPECT_FAIL _FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 ${fx} host-tool "--no-build" "ffx" "--flag1" "arg1" 2> /dev/null
# ensure that build was not called
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/tools/devshell/build.mock_state"
}
# test if sending the host-tool to the background and killing it effectively
# kills it as expected.
# the logic for this test is the following:
# - execute a custom fx command (mycmd)
# - 'fx mycmd' runs 'host-tool slow_tool' in the background, waits a bit and
# attempts to kill it with SIGTERM, simulating a user Ctrl+C in the foreground
# command. SIGINT can't be used because old versions of Bash ignore it
# when the process is running in background.
# - 'slow_tool' should be stopped and have a chance to clean up resources, in
# this case a lock file in a known location, which is checked as a condition
# for the test to succeeed.
TEST_kill_bg_process() {
lockfile="${BT_TEMP_DIR}/.slow_tool.lock"
local frontend_cmd="${BT_TEMP_DIR}/tools/devshell/mycmd"
cat >"${frontend_cmd}" <<EOF
#!/bin/bash
source ${BT_TEMP_DIR}/tools/devshell/lib/vars.sh
fx-command-exec host-tool slow_tool &
pid=\$!
# give some time for slow_tool to create the lockfile
SECONDS=0
while [[ ! -f "$lockfile" ]] ; do
if [[ $SECONDS -gt 10 ]]; then
echo >&2 "ERROR, timed out waiting for lockfile to be created"
exit 1
fi
done
kill -TERM \$pid
wait \$pid
EOF
chmod +x "${frontend_cmd}"
cat >"${slow_tool}.mock_side_effects" <<EOF
cleanup() {
local st=\$1
echo >&2 "cleanup executed, status \$st."
rm -f "$lockfile"
exit \$st
}
trap 'cleanup 0' SIGTERM
touch "$lockfile"
c=0
while (( c < 10 )); do
sleep 1
echo >&2 "sleeping inside slow_tool"
(( c++ ))
done
echo >&2 "ERROR! timeout passed without slow_tool being killed: $slow_tool"
cleanup 1
EOF
local _out="${BT_TEMP_DIR}/_out.log"
BT_EXPECT ${fx} mycmd >"$_out" 2>&1
BT_EXPECT_FILE_DOES_NOT_EXIST "$lockfile"
}
TEST_registered_target_builds_and_executes_before_tool_paths() {
_install_mock_bazel_fx_build
_stage_fake_bazel_output "tools/ffx" "fx_ffx" "BAZEL_FFX_V1"
# Register "ffx" in fx_host_tools.json even though it also exists in tool_paths.json.
cat > "${BT_TEMP_DIR}/tools/devshell/fx_host_tools.json" <<'EOF'
{
"ffx": {
"category": "Build",
"summary": "Bazel ffx host tool",
"target": "//tools/ffx:fx_ffx"
}
}
EOF
# Non-TTY, non-agent with unbuilt registered tool must fail with exit code 2 without building,
# print the host-tool missing-tool message (not a misleading --no-build error), preserve
# registry action logs when enabled, and must NOT fall back to tool_paths.json.
local nontty_err="${BT_TEMP_DIR}/_nontty_err"
local rc=0
FX_OUTPUT_REGISTRY_ACTIONS=1 "${fx}" host-tool "ffx" "--flag1" "arg1" >/dev/null 2>"${nontty_err}" || rc=$?
BT_EXPECT_EQ "${rc}" 2
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${nontty_err}" "\[fx-registry\] lookup 'ffx': found in core registry (mode=target, target=//tools/ffx:fx_ffx)"
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${nontty_err}" 'Tool "ffx" is required by this command and has not been built.'
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${nontty_err}" "Please build it first: fx build --host @//tools/ffx:fx_ffx"
if grep -q "no-build was specified" "${nontty_err}"; then
BT_FAIL "Expected non-TTY skip not to claim --no-build was specified, got: $(<"${nontty_err}")"
fi
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
BT_ASSERT_FILE_DOES_NOT_EXIST "${ffx}.mock_state"
# When invoked by an agent (even non-TTY), build and run the registered Bazel tool.
local out
out="$(_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 "${fx}" host-tool "ffx" "--flag1" "arg1" 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "BAZEL_FFX_V1|no_build_env=<unset>|inject=0|args=--flag1 arg1"
BT_ASSERT_FILE_EXISTS "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${BT_TEMP_DIR}/out/default/bazel_build.mock_state" "ARGS=--no-checks --no-status --no-resultstore --skip-auth-check --host @//tools/ffx:fx_ffx"
BT_ASSERT_FILE_EXISTS "${BT_TEMP_DIR}/out/_fx_host_tools/ffx"
BT_ASSERT_FILE_DOES_NOT_EXIST "${ffx}.mock_state"
# Warm invocation with valid stamp skips rebuild and logs registry action when enabled.
rm -f "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
local reg_log="${BT_TEMP_DIR}/_reg_actions.log"
out="$(FX_OUTPUT_REGISTRY_ACTIONS=1 _FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 "${fx}" host-tool "ffx" "--flag2" "arg2" 2>"${reg_log}")"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "BAZEL_FFX_V1|no_build_env=<unset>|inject=0|args=--flag2 arg2"
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${reg_log}" "\[fx-registry\] lookup 'ffx': found in core registry (mode=target, target=//tools/ffx:fx_ffx)"
}
TEST_registered_target_no_build_and_non_tty_skip() {
_install_mock_bazel_fx_build
_stage_fake_bazel_output "tools/reg" "reg_tool" "REG_V1"
cat > "${BT_TEMP_DIR}/tools/devshell/fx_host_tools.json" <<'EOF'
{
"reg-tool": {
"category": "Build",
"summary": "Registered target tool",
"target": "//tools/reg:reg_tool"
}
}
EOF
# 1. Missing binary with subcommand --no-build fails with exit code 2 (non-agent and agent).
local nobuild_err="${BT_TEMP_DIR}/_nobuild_err"
local rc=0
"${fx}" host-tool --no-build reg-tool arg1 >/dev/null 2>"${nobuild_err}" || rc=$?
BT_EXPECT_EQ "${rc}" 2
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${nobuild_err}" "no-build was specified"
rc=0
_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" host-tool --no-build reg-tool arg1 >/dev/null 2>&1 || rc=$?
BT_EXPECT_EQ "${rc}" 2
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
# 2. Missing binary with global `fx --no-build` fails with exit code 2 even in agent.
rc=0
_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" --no-build host-tool reg-tool arg1 >/dev/null 2>&1 || rc=$?
BT_EXPECT_EQ "${rc}" 2
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
# 3. Subcommand `--no-build` overrides global `fx --build` without conflict error.
local err_file="${BT_TEMP_DIR}/_override_err"
rc=0
_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" --build host-tool --no-build reg-tool arg1 >/dev/null 2>"${err_file}" || rc=$?
BT_EXPECT_EQ "${rc}" 2
if grep -q "mutually exclusive" "${err_file}"; then
BT_FAIL "Expected subcommand --no-build to override global --build cleanly, got: $(<"${err_file}")"
fi
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
# 4. Build once via mocked TTY so staged V1 exists.
cat >> "${BT_TEMP_DIR}/tools/devshell/lib/vars.sh" <<'EOF'
function fx-is-stderr-tty {
return 0
}
EOF
local out
out="$("${fx}" host-tool reg-tool first 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "REG_V1|no_build_env=<unset>|inject=0|args=first"
BT_ASSERT_FILE_EXISTS "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
# 5. Invalidate stamp and stage V2 in fake Bazel output.
echo '[{"package":"updated-lock-v2"}]' > "${BT_TEMP_DIR}/manifests/jiri.lock"
_stage_fake_bazel_output "tools/reg" "reg_tool" "REG_V2"
rm -f "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
# 5a. Subcommand `--no-build` runs stale V1 without building and does NOT leak FX_NO_BUILD_HOST_TOOL.
out="$("${fx}" host-tool --no-build reg-tool nobuild-arg 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "REG_V1|no_build_env=<unset>|inject=0|args=nobuild-arg"
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
# 5b. Global `fx --build` + subcommand `--no-build` still skips rebuild cleanly.
out="$("${fx}" --build host-tool --no-build reg-tool override-arg 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "REG_V1|no_build_env=<unset>|inject=0|args=override-arg"
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
# 5c. Non-TTY + non-agent skips rebuild and runs existing staged V1.
cat >> "${BT_TEMP_DIR}/tools/devshell/lib/vars.sh" <<'EOF'
function fx-is-stderr-tty {
return 1
}
EOF
out="$("${fx}" host-tool reg-tool nontty-arg 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "REG_V1|no_build_env=<unset>|inject=0|args=nontty-arg"
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
# 5d. Global `fx --build` forces rebuild even in non-TTY / non-agent mode.
out="$("${fx}" --build host-tool reg-tool force-arg 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "REG_V2|no_build_env=<unset>|inject=0|args=force-arg"
BT_ASSERT_FILE_EXISTS "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
}
TEST_registered_target_print() {
_install_mock_bazel_fx_build
local marker="${BT_TEMP_DIR}/_tool_executed"
_stage_fake_bazel_output "tools/reg" "reg_print" "PRINT_V1" "${marker}"
cat > "${BT_TEMP_DIR}/tools/devshell/fx_host_tools.json" <<'EOF'
{
"reg-print": {
"category": "Build",
"summary": "Printable registered target",
"target": "//tools/reg:reg_print"
}
}
EOF
# --no-build --print when missing fails with exit code 2 and prints nothing to stdout.
local out rc=0
out="$("${fx}" host-tool --no-build --print reg-print 2>/dev/null)" || rc=$?
BT_EXPECT_EQ "${rc}" 2
BT_EXPECT_EQ "${out}" ""
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
BT_ASSERT_FILE_DOES_NOT_EXIST "${marker}"
# --print with agent enabled builds, prints staged path, ignores trailing args, and does NOT execute.
out="$(_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" host-tool --print reg-print ignored1 ignored2 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "${BT_TEMP_DIR}/out/_fx_host_tools/reg-print"
BT_ASSERT_FILE_EXISTS "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
BT_ASSERT_FILE_DOES_NOT_EXIST "${marker}"
# --no-build --print succeeds without building even when .fx-build-dir is absent.
rm -f "${BT_TEMP_DIR}/out/default/bazel_build.mock_state" "${BT_TEMP_DIR}/.fx-build-dir"
out="$("${fx}" host-tool --no-build --print reg-print 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "${BT_TEMP_DIR}/out/_fx_host_tools/reg-print"
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/out/default/bazel_build.mock_state"
BT_ASSERT_FILE_DOES_NOT_EXIST "${marker}"
}
TEST_registered_script_sh_and_py_execute_and_print() {
local sh_marker="${BT_TEMP_DIR}/_sh_ran"
local py_marker="${BT_TEMP_DIR}/_py_ran"
mkdir -p "${BT_TEMP_DIR}/tools/devshell/host_tools/reg_sh" \
"${BT_TEMP_DIR}/tools/devshell/host_tools/reg_py"
cat > "${BT_TEMP_DIR}/tools/devshell/host_tools/reg_sh/reg_sh.sh" <<EOF
#!/bin/bash
touch "${sh_marker}"
echo "SH_SCRIPT|args=\$*"
EOF
chmod +x "${BT_TEMP_DIR}/tools/devshell/host_tools/reg_sh/reg_sh.sh"
cat > "${BT_TEMP_DIR}/tools/devshell/host_tools/reg_py/reg_py.py" <<EOF
import pathlib
import sys
pathlib.Path("${py_marker}").touch()
print("PY_SCRIPT|args=" + " ".join(sys.argv[1:]))
EOF
# Intentionally keep reg_py.py non-executable to verify python3 dispatch.
chmod 0644 "${BT_TEMP_DIR}/tools/devshell/host_tools/reg_py/reg_py.py"
cat > "${BT_TEMP_DIR}/tools/devshell/fx_host_tools.json" <<'EOF'
{
"reg-py": {
"category": "Build",
"script": "//tools/devshell/host_tools/reg_py/reg_py.py",
"summary": "Registered python script"
},
"reg-sh": {
"category": "Build",
"script": "//tools/devshell/host_tools/reg_sh/reg_sh.sh",
"summary": "Registered shell script"
}
}
EOF
# Registered scripts should work even without .fx-build-dir.
rm -f "${BT_TEMP_DIR}/.fx-build-dir"
# --print prints single script path without executing.
local out
out="$("${fx}" host-tool --print reg-sh ignored-arg 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "${BT_TEMP_DIR}/tools/devshell/host_tools/reg_sh/reg_sh.sh"
BT_ASSERT_FILE_DOES_NOT_EXIST "${sh_marker}"
out="$("${fx}" host-tool --no-build --print reg-py ignored-arg 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "${BT_TEMP_DIR}/tools/devshell/host_tools/reg_py/reg_py.py"
BT_ASSERT_FILE_DOES_NOT_EXIST "${py_marker}"
# Execution forwards args and runs both .sh and .py scripts cleanly.
out="$("${fx}" host-tool reg-sh foo bar 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "SH_SCRIPT|args=foo bar"
BT_ASSERT_FILE_EXISTS "${sh_marker}"
out="$("${fx}" host-tool --no-build reg-py baz qux 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "PY_SCRIPT|args=baz qux"
BT_ASSERT_FILE_EXISTS "${py_marker}"
}
TEST_registered_tool_path_isolation_and_inject_prebuilt_paths() {
mkdir -p "${BT_TEMP_DIR}/tools/devshell/host_tools/path_probe"
cat > "${BT_TEMP_DIR}/tools/devshell/host_tools/path_probe/probe.sh" <<'EOF'
#!/bin/bash
echo "INJECT=${FX_INJECT_PREBUILT_PATHS:-<unset>}|PATH=${PATH}"
EOF
chmod +x "${BT_TEMP_DIR}/tools/devshell/host_tools/path_probe/probe.sh"
cat > "${BT_TEMP_DIR}/tools/devshell/fx_host_tools.json" <<'EOF'
{
"injected-tool": {
"category": "Build",
"inject_prebuilt_paths": true,
"script": "//tools/devshell/host_tools/path_probe/probe.sh",
"summary": "Tool opting into prebuilt PATH"
},
"isolated-tool": {
"category": "Build",
"script": "//tools/devshell/host_tools/path_probe/probe.sh",
"summary": "Tool with isolated PATH"
}
}
EOF
local first_prebuilt_dir="${_TEST_ALL_PATHS%%:*}"
local out_isolated
out_isolated="$("${fx}" host-tool isolated-tool 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_STRING_CONTAINS_SUBSTRING "${out_isolated}" "INJECT=0|"
if [[ "${out_isolated}" == *"${first_prebuilt_dir}"* ]]; then
BT_FAIL "Expected isolated-tool PATH to omit ${first_prebuilt_dir}, got: ${out_isolated}"
fi
local out_injected
out_injected="$("${fx}" host-tool injected-tool 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_STRING_CONTAINS_SUBSTRING "${out_injected}" "INJECT=1|"
BT_EXPECT_STRING_CONTAINS_SUBSTRING "${out_injected}" "${first_prebuilt_dir}"
}
TEST_registered_vendor_precedence_and_collision() {
_install_mock_bazel_fx_build
_stage_fake_bazel_output "tools/core" "shared_core" "CORE_SHARED"
_stage_fake_bazel_output "tools/core" "core_only" "CORE_ONLY"
_stage_fake_bazel_output "vendor/acme/tools" "shared_acme" "ACME_SHARED"
cat > "${BT_TEMP_DIR}/tools/devshell/fx_host_tools.json" <<'EOF'
{
"core-only": {
"category": "Build",
"summary": "Core registered tool",
"target": "//tools/core:core_only"
},
"shared-tool": {
"category": "Build",
"summary": "Core shared tool",
"target": "//tools/core:shared_core"
}
}
EOF
mkdir -p "${BT_TEMP_DIR}/vendor/acme/scripts/devshell"
cat > "${BT_TEMP_DIR}/vendor/acme/scripts/devshell/fx_host_tools.json" <<'EOF'
{
"shared-tool": {
"category": "Build",
"summary": "Acme shared tool",
"target": "//vendor/acme/tools:shared_acme"
}
}
EOF
# A vendor legacy devshell script must NOT shadow a core registered host tool.
cat > "${BT_TEMP_DIR}/vendor/acme/scripts/devshell/core-only" <<'EOF'
#!/bin/bash
echo "WRONG_VENDOR_LEGACY"
EOF
chmod +x "${BT_TEMP_DIR}/vendor/acme/scripts/devshell/core-only"
local out
out="$(_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" host-tool core-only arg1 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "CORE_ONLY|no_build_env=<unset>|inject=0|args=arg1"
# Vendor registry overrides core registry and stages under vendor/<vendor>/.
out="$(_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" host-tool --print shared-tool 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "${BT_TEMP_DIR}/out/_fx_host_tools/vendor/acme/shared-tool"
out="$(_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" host-tool shared-tool varg 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "ACME_SHARED|no_build_env=<unset>|inject=0|args=varg"
# Multi-vendor collision fails with exit code 2 instead of falling back to tool_paths.json.
mkdir -p "${BT_TEMP_DIR}/vendor/beta/scripts/devshell"
cat > "${BT_TEMP_DIR}/vendor/beta/scripts/devshell/fx_host_tools.json" <<'EOF'
{
"shared-tool": {
"category": "Build",
"summary": "Beta shared tool",
"target": "//vendor/beta/tools:shared_beta"
}
}
EOF
local err_file="${BT_TEMP_DIR}/_coll_err" rc=0
_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" host-tool shared-tool >/dev/null 2>"${err_file}" || rc=$?
BT_EXPECT_EQ "${rc}" 2
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${err_file}" "multiple vendor repositories"
}
TEST_fx_reentry_host_tool_and_python_cli() {
_install_mock_bazel_fx_build
_stage_fake_bazel_output "tools/reg" "reentry_tool" "REENTRY_OK"
cat > "${BT_TEMP_DIR}/tools/devshell/fx_host_tools.json" <<'EOF'
{
"reentry-tool": {
"category": "Build",
"summary": "Registered tool invoked via fx-reentry",
"target": "//tools/reg:reentry_tool"
}
}
EOF
local caller="${BT_TEMP_DIR}/tools/devshell/my-reentry-caller"
cat > "${caller}" <<'EOF'
#!/bin/bash
exec "${FUCHSIA_DIR}/scripts/fx-reentry" host-tool "$@"
EOF
chmod +x "${caller}"
# Via scripts/fx-reentry (legacy fx front door).
local out
out="$(_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" my-reentry-caller --print reentry-tool 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "${BT_TEMP_DIR}/out/_fx_host_tools/reentry-tool"
out="$(_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" my-reentry-caller reentry-tool via-reentry 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "REENTRY_OK|no_build_env=<unset>|inject=0|args=via-reentry"
# Via FX_PYTHON_CLI=1 front door (both direct host-tool and fx-reentry).
out="$(FX_PYTHON_CLI=1 _FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" host-tool --print reentry-tool 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "${BT_TEMP_DIR}/out/_fx_host_tools/reentry-tool"
out="$(FX_PYTHON_CLI=1 _FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" host-tool reentry-tool via-pycli 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "REENTRY_OK|no_build_env=<unset>|inject=0|args=via-pycli"
out="$(FX_PYTHON_CLI=1 _FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" my-reentry-caller reentry-tool via-pycli-reentry 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "REENTRY_OK|no_build_env=<unset>|inject=0|args=via-pycli-reentry"
}
TEST_gn_fallback_print_and_unknown_tool_exit_codes() {
# --print for GN tool prints path without executing.
local out
out="$("${fx}" host-tool --print ffx --ignored 2>/dev/null)"
BT_EXPECT_EQ $? 0
BT_EXPECT_EQ "${out}" "${ffx}"
BT_ASSERT_FILE_DOES_NOT_EXIST "${ffx}.mock_state"
# Missing GN tool with --no-build or global `fx --no-build` exits with code 2 without building.
mv "${ffx}" "${ffx}_backup"
local rc=0
"${fx}" host-tool --no-build ffx >/dev/null 2>&1 || rc=$?
BT_EXPECT_EQ "${rc}" 2
rc=0
_FX_PROC_STATUS="${BT_TEMP_DIR}/nonexistent" ANTIGRAVITY_AGENT=1 \
"${fx}" --no-build host-tool ffx >/dev/null 2>&1 || rc=$?
BT_EXPECT_EQ "${rc}" 2
BT_ASSERT_FILE_DOES_NOT_EXIST "${BT_TEMP_DIR}/tools/devshell/build.mock_state"
# Unknown tool in both registry and tool_paths.json exits with code 1.
local err_file="${BT_TEMP_DIR}/_unknown_err"
rc=0
FX_OUTPUT_REGISTRY_ACTIONS=1 "${fx}" host-tool nonexistent-tool >/dev/null 2>"${err_file}" || rc=$?
BT_EXPECT_EQ "${rc}" 1
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${err_file}" "\[fx-registry\] lookup 'nonexistent-tool': not in registry; falling back to tool_paths.json"
BT_EXPECT_FILE_CONTAINS_SUBSTRING "${err_file}" "found no artifacts"
}
BT_RUN_TESTS "$@"