| #!/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 basic fx execution flows | |
| export BT_FILE_DEPS=( | |
| "scripts/fx" | |
| "tools/devshell/set" | |
| "tools/devshell/lib/get_fuchsia_subdir_git_revision.sh" | |
| "tools/devshell/lib/fx-cmd-locator.sh" | |
| "tools/devshell/lib/fx-optional-features.sh" | |
| "tools/devshell/lib/generate-ssh-config.sh" | |
| "tools/devshell/lib/vars.sh" | |
| "tools/devshell/lib/is_invoked_by_agent.sh" | |
| "tools/devshell/lib/agents.txt" | |
| ) | |
| export BT_MOCKED_TOOLS=( | |
| "tools/devshell/lib/metrics.sh" | |
| "tools/devshell/lib/platform.sh" | |
| "tools/devshell/rbe" | |
| "_isolated_path_for/git" | |
| ) | |
| export BT_MKDIR_DEPS=( | |
| ".fx" | |
| ) | |
| CURRENT_REVISION="abc123" | |
| BT_SET_UP() { | |
| PATH_DIR_FOR_TEST="${BT_TEMP_DIR}/_isolated_path_for" | |
| export PATH="${PATH_DIR_FOR_TEST}:${PATH}" | |
| _FUCHSIA_DIR="${BT_TEMP_DIR}" | |
| mkdir -p ${_FUCHSIA_DIR}/.git | |
| touch "${_FUCHSIA_DIR}/.fx-root" | |
| fx="${_FUCHSIA_DIR}/scripts/fx" | |
| BT_ASSERT_FILE_EXISTS "${fx}" | |
| cat > "${_FUCHSIA_DIR}/tools/devshell/lib/metrics.sh.mock_side_effects" <<EOF | |
| function metrics-init { | |
| echo "" | |
| } | |
| function track-command-execution { | |
| echo "" | |
| } | |
| function track-feature-status { | |
| echo "" | |
| } | |
| function track-command-finished { | |
| echo "" | |
| } | |
| EOF | |
| echo "echo $CURRENT_REVISION" > "${PATH_DIR_FOR_TEST}/git.mock_side_effects" | |
| } | |
| # Neither .fx/fx-set nor .fx/fx-set-revision exists. | |
| TEST_fx-set-clean-build() { | |
| fx_set_output="fx-set running" | |
| # Mock an `fx go` command that produces an `fx-set` executable. | |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/go" | |
| fx_set=${_FUCHSIA_DIR}/.fx/fx-set | |
| echo "echo 'echo $fx_set_output' > '$fx_set' && chmod +x '$fx_set'" > "${BT_TEMP_DIR}/tools/devshell/go.mock_side_effects" | |
| output="$(BT_ASSERT "$fx" set myproduct.myboard)" | |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$output" "$fx_set_output" | |
| } | |
| # .fx/fx-set and .fx/fx-set-revision both exist, and are up-to-date, so no | |
| # rebuild is necessary. | |
| TEST_fx-set-no-rebuild() { | |
| # If the fx-set-revision and fx-set files are already present, no rebuild | |
| # should happen. | |
| echo "$CURRENT_REVISION" > "${_FUCHSIA_DIR}/.fx/fx-set-revision" | |
| fx_set_output="fx-set running" | |
| echo "echo '$fx_set_output'" > "${_FUCHSIA_DIR}/.fx/fx-set" | |
| chmod +x "${_FUCHSIA_DIR}/.fx/fx-set" | |
| # Make sure `fx go` fails if called. | |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/go" | |
| cat > "${BT_TEMP_DIR}/tools/devshell/go.mock_side_effects" << "EOF" | |
| exit 1 | |
| EOF | |
| output="$(BT_ASSERT "$fx" set myproduct.myboard)" | |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$output" "$fx_set_output" | |
| } | |
| # .fx/fx-set exists but an old revision is stored in .fx/fx-set-revision, so we | |
| # need to rebuild. | |
| TEST_fx-set-out-of-date-rebuild() { | |
| echo "old_revision" > "${_FUCHSIA_DIR}/.fx/fx-set-revision" | |
| fx_set_output="fx-set running" | |
| echo "echo '$fx_set_output'" > "${_FUCHSIA_DIR}/.fx/fx-set" | |
| chmod +x "${_FUCHSIA_DIR}/.fx/fx-set" | |
| # Make sure `fx go` prints a distinctive substring if called. | |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/go" | |
| cat > "${BT_TEMP_DIR}/tools/devshell/go.mock_side_effects" << "EOF" | |
| echo "rebuilding" | |
| EOF | |
| output="$(BT_ASSERT "$fx" set myproduct.myboard)" | |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$output" "$fx_set_output" | |
| # `fx go build` should have been called. | |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$output" "rebuilding" | |
| } | |
| TEST_fx-set-with-dir() { | |
| echo "$CURRENT_REVISION" > "${_FUCHSIA_DIR}/.fx/fx-set-revision" | |
| fx_set_output="fx-set running" | |
| cat > "${_FUCHSIA_DIR}/.fx/fx-set" << EOF | |
| if [[ -n "\$_FX_BUILD_DIR" ]]; then | |
| echo _FX_BUILD_DIR=[\$_FX_BUILD_DIR] | |
| else | |
| echo ARGS=["\$@"] | |
| fi | |
| EOF | |
| chmod +x "${_FUCHSIA_DIR}/.fx/fx-set" | |
| # Make sure `fx go` fails if called. | |
| btf::make_mock "${BT_TEMP_DIR}/tools/devshell/go" | |
| cat > "${BT_TEMP_DIR}/tools/devshell/go.mock_side_effects" << "EOF" | |
| exit 1 | |
| EOF | |
| # Verify that using `--dir <name>` sets _FX_BUILD_DIR to <name> | |
| # before calling the fx-set binary. | |
| output="$(BT_ASSERT "$fx" --dir out/foo set myproduct.myboard)" | |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$output" "_FX_BUILD_DIR=\[out/foo\]" | |
| # Verify that not using `--dir <name>` does not set _FX_BUILD_DIR | |
| # before calling the fx-set binary. The real binary's behavior | |
| # will be to use the args to select the build directory. | |
| output="$(BT_ASSERT "$fx" set myproduct.myboard)" | |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$output" "ARGS=\[myproduct.myboard\]" | |
| } | |
| BT_RUN_TESTS "$@" |