| #!/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 the common fx execution flows, namely fx help and fx <subcommand> |
| BT_FILE_DEPS=( |
| "scripts/fx" |
| "scripts/fx-help.awk" |
| "tools/devshell/vendor" |
| "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/platform.sh" |
| ) |
| |
| BT_SET_UP() { |
| base_dir="${BT_TEMP_DIR}" |
| source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh" |
| fx="$(btf::setup_fx)" |
| } |
| |
| _create_subcommand() { |
| local subcommand="$1" |
| local subcommand_file="${base_dir}/$2/${subcommand}" |
| local subcommand_output="$3" |
| local summary="$4" |
| local long_description_1="$5" |
| local long_description_2="$6" |
| local category="Testing" |
| if [[ $# -gt 6 ]]; then |
| category="$7" |
| fi |
| mkdir -p "$(dirname "${subcommand_file}")" |
| cat >"${subcommand_file}" <<EOF |
| #!/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. |
| |
| ### ${summary} |
| #### CATEGORY=${category} |
| |
| ## ${long_description_1} |
| ## ${long_description_2} |
| |
| echo "${subcommand_output}" |
| EOF |
| chmod u+x "${subcommand_file}" |
| BT_ASSERT_FILE_EXISTS "${subcommand_file}" |
| [[ -x "${subcommand_file}" ]] |
| BT_ASSERT_GOOD_STATUS $? "File ${subcommand_file} must be executable" |
| } |
| |
| # test for `fx help` |
| TEST_fx-help() { |
| # check that the usage and --full lines are present |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} help | grep "Run Fuchsia")" "Run Fuchsia development commands" |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} help | grep "usage:")" "usage: fx" |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} help | grep "\-\-full")" "--full" |
| } |
| |
| # test for `fx help --full` |
| TEST_fx-help-full() { |
| local subcommand="mycommand1" |
| local summary="Simple mock script used to test help extraction" |
| _create_subcommand "${subcommand}" "tools/devshell" "howdy!" "${summary}" "line1" "line2" |
| # check the sections are included, and the command name and summary |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} help --full | grep "Run Fuchsia")" "Run Fuchsia development commands" |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} help --full | grep "${subcommand}")" "${subcommand}" |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} help --full | grep "${subcommand}")" "${summary}" |
| } |
| |
| # ensure that categories are normalized |
| TEST_fx-normalized-category() { |
| local subcommand="mycommand1" |
| local category="Category UpperCASE 1" |
| local norm_category_header="Category uppercase 1:" |
| _create_subcommand "${subcommand}" "tools/devshell" "howdy!" "summary1" "line1" "line2" "${category}" |
| # check that the normalized category shows up in fx help |
| BT_EXPECT_EQ "$(${fx} help --full | grep -B 1 "${subcommand}" | head -n 1)" "${norm_category_header}" |
| } |
| |
| # test for `fx help <subcommand>` |
| TEST_fx-help-subcommand() { |
| local subcommand="mycommand1" |
| local line1="Usage:" |
| local line2=" fx mysubcommand bla bla" |
| _create_subcommand "${subcommand}" "tools/devshell" "howdy!" "summary" "${line1}" "${line2}" |
| # check the long description |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} help "${subcommand}")" "${line1}" |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} help "${subcommand}")" "${line2}" |
| } |
| |
| # executes a simple subcommand and checks its output |
| TEST_fx-subcommand-run() { |
| local subcommand="mycommand1" |
| local output="Howdy!" |
| _create_subcommand "${subcommand}" "tools/devshell" "${output}" "summary" "line1" "line2" |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} "${subcommand}")" "${output}" |
| } |
| |
| # executes a simple subcommand in contrib and checks its output |
| TEST_fx-contrib-subcommand-run() { |
| local subcommand="mycommand-contrib" |
| local output="Hello contrib!" |
| _create_subcommand "${subcommand}" "tools/devshell/contrib" "${output}" "summary" "line1" "line2" |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} "${subcommand}")" "${output}" |
| } |
| |
| # executes a simple subcommand in vendor/mycompany and checks its output |
| TEST_fx-vendor-subcommand-run() { |
| local subcommand="mycommand-vendor" |
| local output="Hello vendor!" |
| local vendor="mycompany" |
| _create_subcommand "${subcommand}" "vendor/${vendor}/scripts/devshell" "${output}" "summary" "line1" "line2" |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} vendor "${vendor}" "${subcommand}")" "${output}" |
| } |
| |
| # executes a host tool subcommand |
| # if this test fails, check if the directory that host tools are installed has |
| # changed. There are at least two places that use this path hard coded: |
| # //build/host.gni (template install_host_tools) |
| # //scripts/fx (method get_host_tools_dir) |
| # This test will fail if fx is changed to look for host tools in a different |
| # directory. It will NOT fail if the install_host_tools template changes. |
| TEST_fx-hosttools-subcommand-run() { |
| # create build directory in a subshell to not pollute the test |
| build_dir="out/default" |
| ( |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/../../lib/vars.sh || exit $? |
| mkdir -p "${BT_TEMP_DIR}/${build_dir}" |
| fx-change-build-dir "${build_dir}" |
| ) |
| local host_tools_dir="${build_dir}/host-tools" |
| local subcommand="myhosttool" |
| local output="Hello host tool!" |
| _create_subcommand "${subcommand}" "${host_tools_dir}" "${output}" "summary" "line1" "line2" |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} "${subcommand}")" "${output}" |
| } |
| |
| # executes a simple fx command from different fuchsia checkout, |
| # while the two fuchsia checkouts have the same version of fx. This is |
| # not allowed, as while the callers API may be the same, the callee's |
| # expectations may not be met. |
| TEST_fx-run_from_same_version() { |
| # creates a temp fuchsia checkout directory. |
| BT_TEMP_DIR_1="$(mktemp -d)" |
| cp -RL "${BT_TEMP_DIR}/." "${BT_TEMP_DIR_1}" |
| cd "${BT_TEMP_DIR_1}" |
| # check whether fx command executed successfully, since the two fuchsia checkouts |
| # have the same versions of fx code. |
| output="$(${fx} help 2>&1)" |
| BT_EXPECT_EQ $? 1 |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "${output}" "ERROR: You are executing fx from outside of the current source tree" |
| } |
| |
| # executes fx command from different fuchsia checkout to detect the |
| # inconsistencies in fx versions. |
| TEST_fx-run_from_different_version() { |
| local output |
| |
| local line1="ERROR: You are executing fx from outside of the current source tree" |
| local line2=" 'fx' was executed from:" |
| |
| # creates a temp fuchsia checkout directory and run fx command. |
| BT_TEMP_DIR_1="$(mktemp -d)" |
| cp -RL "${BT_TEMP_DIR}/." "${BT_TEMP_DIR_1}" |
| cd "${BT_TEMP_DIR_1}" |
| # Modify the fx script of the new fuchsia source code tree, so the content |
| # comparsion of fx script fails between the two versions of source code. |
| echo "# test" >> "${BT_TEMP_DIR}/scripts/fx" |
| |
| output="$(${fx} help 2>&1)" |
| BT_EXPECT_EQ $? 1 |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "${output}" "${line1}" |
| BT_EXPECT_STRING_CONTAINS_SUBSTRING "${output}" "${line2} ${BT_TEMP_DIR}" |
| } |
| |
| BT_RUN_TESTS "$@" |