| #!/bin/bash |
| # Copyright 2025 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 behavior of 'fx set-device' |
| |
| BT_FILE_DEPS=( |
| "scripts/fx" |
| "tools/devshell/lib/vars.sh" |
| "tools/devshell/set-device" |
| "tools/devshell/lib/fx-cmd-locator.sh" |
| "tools/devshell/lib/fx-optional-features.sh" |
| "tools/devshell/lib/platform.sh" |
| "tools/devshell/lib/generate-ssh-config.sh" |
| ) |
| |
| declare fx |
| |
| BT_MKDIR_DEPS=( |
| "out/default" |
| ) |
| |
| BT_SET_UP() { |
| source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh" |
| fx="$(btf::setup_fx)" |
| FUCHSIA_DIR="${BT_TEMP_DIR}" |
| } |
| |
| # Check that the device file is written when setting the device |
| TEST_fx-set-device-writes-file() { |
| local device_name="foo" |
| BT_EXPECT "${fx}" set-device ${device_name} 2>/dev/null |
| |
| local device_file="${FUCHSIA_DIR}/out/default.device" |
| BT_EXPECT_FILE_EXISTS "${device_file}" |
| BT_EXPECT_FILE_CONTAINS "${device_file}" "${device_name}" |
| } |
| |
| # Check that the device file is written when setting the device with a non-default build-dir |
| TEST_fx-set-device-writes-file-non-standard-build-dir() { |
| local device_name="bar" |
| local rel_build_dir="out/foo" |
| local build_dir="${FUCHSIA_DIR}/${rel_build_dir}" |
| echo "${rel_build_dir}" > "${FUCHSIA_DIR}/.fx-build-dir" |
| mkdir -p "${build_dir}" |
| cat > "${build_dir}/fx.config" << EOF |
| FUCHSIA_BUILD_DIR="${FUCHSIA_DIR}/out/foo" |
| EOF |
| |
| BT_EXPECT "${fx}" set-device ${device_name} 2>/dev/null |
| |
| local device_file="${build_dir}.device" |
| BT_EXPECT_FILE_EXISTS "${device_file}" |
| BT_EXPECT_FILE_CONTAINS "${device_file}" "${device_name}" |
| } |
| |
| # Check that the command fails if used with -t |
| TEST_fx-set-device-fails-with-target-flag() { |
| local out="${BT_TEMP_DIR}/_fx_set_device_output" |
| |
| BT_EXPECT_FAIL "${fx}" -t foo set-device bar >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "ERROR: The -t flag is not supported when calling this function" |
| BT_EXPECT_FILE_DOES_NOT_EXIST "${device_file}" |
| } |
| |
| # fx set-device should print an error if FUCHSIA_NODENAME is set outside of fx. |
| TEST_fx-set-device-errors-if-nodename-env-var-set-already() { |
| local out="${BT_TEMP_DIR}/_fx_set_device_output" |
| export FUCHSIA_NODENAME="bar" |
| local device="foo" |
| |
| BT_EXPECT_FAIL "${fx}" set-device "${device}" >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "Default device for '${FUCHSIA_DIR}/out/default' is now foo |
| ERROR: However, you've overriden this value by setting \$FUCHSIA_NODENAME=\"bar\". |
| ERROR: If you want to use \"foo\", please unset the \$FUCHSIA_NODENAME environment variable." |
| |
| local device_file="${FUCHSIA_DIR}/out/default.device" |
| BT_EXPECT_FILE_EXISTS "${device_file}" |
| BT_EXPECT_FILE_CONTAINS "${device_file}" "${device}" |
| } |
| |
| # fx set-device should print an error if FUCHSIA_DEVICE_ADDR is set outside of fx. |
| TEST_fx-set-device-errors-if-device-addr-env-var-set() { |
| local out="${BT_TEMP_DIR}/_fx_set_device_output" |
| export FUCHSIA_DEVICE_ADDR="baz" |
| local device="foo" |
| |
| BT_EXPECT_FAIL "${fx}" set-device "${device}" >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "Default device for '${FUCHSIA_DIR}/out/default' is now foo |
| ERROR: However, you've overriden this value by setting \$FUCHSIA_DEVICE_ADDR=\"baz\". |
| ERROR: If you want to use \"foo\", please unset the \$FUCHSIA_DEVICE_ADDR environment variable." |
| |
| local device_file="${FUCHSIA_DIR}/out/default.device" |
| BT_EXPECT_FILE_EXISTS "${device_file}" |
| BT_EXPECT_FILE_CONTAINS "${device_file}" "${device}" |
| } |
| |
| # fx set-device should print an error if FUCHSIA_NODENAME and |
| # FUCHSIA_DEVICE_ADDR are both set outside of fx. |
| TEST_fx-set-device-errors-if-both-env-vars-set() { |
| local out="${BT_TEMP_DIR}/_fx_set_device_output" |
| export FUCHSIA_NODENAME="bar" |
| export FUCHSIA_DEVICE_ADDR="baz" |
| local device="foo" |
| |
| BT_EXPECT_FAIL "${fx}" set-device "${device}" >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "Default device for '${FUCHSIA_DIR}/out/default' is now foo |
| ERROR: However, you've overriden this value by setting \$FUCHSIA_NODENAME=\"bar\" and \$FUCHSIA_DEVICE_ADDR=\"baz\". |
| ERROR: If you want to use \"foo\", please unset the \$FUCHSIA_NODENAME and \$FUCHSIA_DEVICE_ADDR environment variable." |
| |
| local device_file="${FUCHSIA_DIR}/out/default.device" |
| BT_EXPECT_FILE_EXISTS "${device_file}" |
| BT_EXPECT_FILE_CONTAINS "${device_file}" "${device}" |
| } |
| |
| BT_RUN_TESTS "$@" |