blob: 1acd6c032c916c47a4499088e7916b725598c9ba [file] [log] [blame]
#!/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/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() {
local subcommand="mycommand1"
local summary="Simple mock script used to test help extraction"
_create_subcommand "${subcommand}" "tools/devshell" "howdy!" "${summary}" "line1" "line2"
# check the command name and summary
BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} help | grep "${subcommand}")" "${subcommand}"
BT_EXPECT_STRING_CONTAINS_SUBSTRING "$(${fx} help | 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 | 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-build-dir-write "${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}"
}
BT_RUN_TESTS "$@"