| #!/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 use' |
| |
| BT_FILE_DEPS=( |
| "scripts/fx" |
| "tools/devshell/lib/vars.sh" |
| "tools/devshell/use" |
| "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" |
| "out/secondary" |
| ) |
| |
| BT_SET_UP() { |
| source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh" |
| fx="$(btf::setup_fx)" |
| btf::make_out_dir "out/secondary" |
| FUCHSIA_DIR="${BT_TEMP_DIR}" |
| mkdir -p "$FUCHSIA_DIR/build" |
| touch "$FUCHSIA_DIR/build/regenerator" |
| chmod +x "$FUCHSIA_DIR/build/regenerator" |
| } |
| |
| # Check that the current build dir is switched with fx use. |
| TEST_fx-use-switches-build-dir() { |
| local out="${BT_TEMP_DIR}/_fx_use_stdout" |
| BT_EXPECT "${fx}" use out/secondary >"$out" 2>&1 |
| BT_EXPECT_FILE_CONTAINS "$FUCHSIA_DIR/.fx-build-dir" "out/secondary" |
| BT_EXPECT_FILE_CONTAINS "${out}" "" |
| } |
| |
| # Check that fx use errors if we try to switch to a nonexistent build dir. |
| TEST_fx-use-nonexistent-build-dir() { |
| local out="${BT_TEMP_DIR}/_fx_use_stdout" |
| BT_EXPECT_FAIL "${fx}" use out/nonexistent >"$out" 2>&1 |
| BT_EXPECT_FILE_CONTAINS "$FUCHSIA_DIR/.fx-build-dir" "out/default" |
| BT_EXPECT_FILE_CONTAINS_SUBSTRING "$out" '"out/nonexistent" is not a valid build dir' |
| } |
| |
| # Check that we log the new default target. |
| TEST_fx-use-logs-default-target() { |
| local device_file="${FUCHSIA_DIR}/out/secondary.device" |
| echo "foo" >"${device_file}" |
| local out="${BT_TEMP_DIR}/_fx_use_stdout" |
| |
| BT_EXPECT "${fx}" use out/secondary >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "INFO: The default device for out/secondary is \"foo\"." |
| BT_EXPECT_FILE_CONTAINS "${device_file}" "foo" |
| } |
| |
| # Check that the default target log prints out an out dir short path. |
| TEST_fx-use-logs-default-target-absolute-path() { |
| local device_file="${FUCHSIA_DIR}/out/secondary.device" |
| echo "foo" >"${device_file}" |
| local out="${BT_TEMP_DIR}/_fx_use_stdout" |
| |
| BT_EXPECT "${fx}" use "${FUCHSIA_DIR}/out/secondary/" >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "INFO: The default device for out/secondary is \"foo\"." |
| BT_EXPECT_FILE_CONTAINS "${device_file}" "foo" |
| } |
| |
| # Check that we warn the user if $FUCHSIA_NODENAME has been set by the user. |
| TEST_fx-use-warns-if-nodename-env-var-set() { |
| local device_file="${FUCHSIA_DIR}/out/secondary.device" |
| echo "foo" >"${device_file}" |
| local out="${BT_TEMP_DIR}/_fx_use_stdout" |
| export FUCHSIA_NODENAME="bar" |
| |
| BT_EXPECT "${fx}" use out/secondary >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "INFO: The default device for out/secondary is \"foo\". |
| WARNING: However, you've overridden this by setting \$FUCHSIA_NODENAME=\"bar\". |
| WARNING: If you want to use \"foo\", please unset the \$FUCHSIA_NODENAME environment variable." |
| BT_EXPECT_FILE_CONTAINS "${device_file}" "foo" |
| } |
| |
| # Check that we warn the user if $FUCHSIA_DEVICE_ADDR has been set by the user. |
| TEST_fx-use-warns-if-device_addr-env-var-set() { |
| local device_file="${FUCHSIA_DIR}/out/secondary.device" |
| echo "foo" >"${device_file}" |
| local out="${BT_TEMP_DIR}/_fx_use_stdout" |
| export FUCHSIA_DEVICE_ADDR="bar" |
| |
| BT_EXPECT "${fx}" use out/secondary >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "INFO: The default device for out/secondary is \"foo\". |
| WARNING: However, you've overridden this by setting \$FUCHSIA_DEVICE_ADDR=\"bar\". |
| WARNING: If you want to use \"foo\", please unset the \$FUCHSIA_DEVICE_ADDR environment variable." |
| BT_EXPECT_FILE_CONTAINS "${device_file}" "foo" |
| } |
| |
| # Check that we warn the user if both $FUCHSIA_NODENAME and $FUCHSIA_DEVICE_ADDR |
| # have been set by the user. |
| TEST_fx-use-warns-if-both-env-vars-set() { |
| local device_file="${FUCHSIA_DIR}/out/secondary.device" |
| echo "foo" >"${device_file}" |
| local out="${BT_TEMP_DIR}/_fx_use_stdout" |
| export FUCHSIA_NODENAME="bar" |
| export FUCHSIA_DEVICE_ADDR="baz" |
| |
| BT_EXPECT "${fx}" use out/secondary >"$out" 2>&1 |
| |
| # Check that the output has the right message |
| BT_EXPECT_FILE_CONTAINS "${out}" "INFO: The default device for out/secondary is \"foo\". |
| WARNING: However, you've overridden this by setting \$FUCHSIA_NODENAME=\"bar\" and \$FUCHSIA_DEVICE_ADDR=\"baz\". |
| WARNING: If you want to use \"foo\", please unset the \$FUCHSIA_NODENAME and \$FUCHSIA_DEVICE_ADDR environment variable." |
| BT_EXPECT_FILE_CONTAINS "${device_file}" "foo" |
| } |
| |
| BT_RUN_TESTS "$@" |