| #!/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 unset-device' |
| |
| BT_FILE_DEPS=( |
| "scripts/fx" |
| "tools/devshell/lib/vars.sh" |
| "tools/devshell/unset-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 removed when unsetting the device |
| TEST_fx-unset-device-removes-file() { |
| local device_file="${FUCHSIA_DIR}/out/default.device" |
| echo "foo" >"${device_file}" |
| |
| BT_EXPECT "${fx}" unset-device 2>/dev/null |
| BT_EXPECT_FILE_DOES_NOT_EXIST "${device_file}" |
| } |
| |
| # Check that the device file is removed when unsetting the device with a non-default build-dir |
| TEST_fx-unset-device-writes-file-non-standard-build-dir() { |
| local rel_build_dir="out/foo" |
| local build_dir="${FUCHSIA_DIR}/${rel_build_dir}" |
| local device_file="${build_dir}.device" |
| 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 |
| |
| echo "foo" >"${device_file}" |
| |
| BT_EXPECT "${fx}" unset-device 2>/dev/null |
| BT_EXPECT_FILE_DOES_NOT_EXIST "${device_file}" |
| } |
| |
| # Check that the command fails if used with -t |
| TEST_fx-unset-device-fails-with-target-flag() { |
| BT_EXPECT_FAIL "${fx}" -t foo unset-device foo 2>/dev/null |
| } |
| |
| # Check that the command doesn't fail if a default target hasn't been set yet. |
| TEST_fx-unset-device-if-no-default-set() { |
| local out="${BT_TEMP_DIR}/_fx_set_device_output" |
| |
| BT_EXPECT "${fx}" unset-device >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "The default device has been unset for '${FUCHSIA_DIR}/out/default'" |
| } |
| |
| # Check that we error the user if $FUCHSIA_NODENAME has been set by the user. |
| TEST_fx-unset-device-errors-if-nodename-env-var-set-already() { |
| local device_file="${FUCHSIA_DIR}/out/default.device" |
| echo "foo" >"${device_file}" |
| local out="${BT_TEMP_DIR}/_fx_set_device_output" |
| export FUCHSIA_NODENAME="bar" |
| |
| BT_EXPECT_FAIL "${fx}" unset-device >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "The default device has been unset for '${FUCHSIA_DIR}/out/default' |
| ERROR: However, you've overriden this by setting \$FUCHSIA_NODENAME=\"bar\". |
| ERROR: If you want to clear the default device, please unset the \$FUCHSIA_NODENAME environment variable." |
| BT_EXPECT_FILE_DOES_NOT_EXIST "${device_file}" |
| } |
| |
| # Check that we error the user if $FUCHSIA_DEVICE_ADDR has been set by the user. |
| TEST_fx-unset-device-errors-if-device-addr-env-var-set() { |
| local out="${BT_TEMP_DIR}/_fx_set_device_output" |
| export FUCHSIA_DEVICE_ADDR="bar" |
| |
| BT_EXPECT_FAIL "${fx}" unset-device >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "The default device has been unset for '${FUCHSIA_DIR}/out/default' |
| ERROR: However, you've overriden this by setting \$FUCHSIA_DEVICE_ADDR=\"bar\". |
| ERROR: If you want to clear the default device, please unset the \$FUCHSIA_DEVICE_ADDR environment variable." |
| BT_EXPECT_FILE_DOES_NOT_EXIST "${device_file}" |
| } |
| |
| # Check that we error the user if both $FUCHSIA_NODENAME and |
| # $FUCHSIA_DEVICE_ADDR have been set by the user. |
| TEST_fx-unset-device-errors-if-both-env-vars-set() { |
| local device_file="${FUCHSIA_DIR}/out/default.device" |
| echo "foo" >"${device_file}" |
| local out="${BT_TEMP_DIR}/_fx_set_device_output" |
| export FUCHSIA_NODENAME="bar" |
| export FUCHSIA_DEVICE_ADDR="baz" |
| |
| BT_EXPECT_FAIL "${fx}" unset-device >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "The default device has been unset for '${FUCHSIA_DIR}/out/default' |
| ERROR: However, you've overriden this by setting \$FUCHSIA_NODENAME=\"bar\" and \$FUCHSIA_DEVICE_ADDR=\"baz\". |
| ERROR: If you want to clear the default device, please unset the \$FUCHSIA_NODENAME and \$FUCHSIA_DEVICE_ADDR environment variable." |
| BT_EXPECT_FILE_DOES_NOT_EXIST "${device_file}" |
| } |
| |
| BT_RUN_TESTS "$@" |