blob: 4408550ffba915458da030e79f2061f8d1afdcb6 [file] [log] [blame]
#!/bin/bash
# Copyright 2020 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 optional features library
BT_FILE_DEPS=(
"scripts/fx"
"tools/devshell/lib/fx-cmd-locator.sh"
"tools/devshell/lib/fx-optional-features.sh"
"tools/devshell/lib/vars.sh"
"tools/devshell/lib/platform.sh"
)
declare fx
BT_SET_UP() {
source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh"
fx="$(btf::setup_fx)"
}
TEST_disable_feature() {
local valid_feature="myfeature"
cat >> "${BT_TEMP_DIR}/tools/devshell/lib/fx-optional-features.sh" <<EOF
list_optional_features() {
echo "${valid_feature}"
}
is_feature_enabled_by_default() {
return 0 # enabled by default
}
EOF
local cmd="mycmd"
local cmd_path="${BT_TEMP_DIR}/tools/devshell/${cmd}"
mkdir -p "$(dirname "${cmd_path}")"
# create a command that runs is_feature_enabled and returns its exit code
cat >"${cmd_path}" <<EOF
#!/bin/bash
source ${BT_TEMP_DIR}/tools/devshell/lib/fx-optional-features.sh || exit \$\?
is_feature_enabled "${valid_feature}"
EOF
chmod u+x "${cmd_path}"
# check that the command, when executed through fx, works as intended for
# an optional feature. It should return 0 (true), since the feature
# is not disabled explicitly, hence enabled.
BT_EXPECT ${fx} ${cmd}
# Same, --enable has no effect
BT_EXPECT ${fx} --enable=${valid_feature} ${cmd}
# Now it should return 1 (false), since the feature is explicitly disabled
# in the fx invocation.
BT_EXPECT_FAIL ${fx} --disable=${valid_feature} ${cmd}
}
TEST_enable_feature() {
local valid_feature="myfeature"
cat >> "${BT_TEMP_DIR}/tools/devshell/lib/fx-optional-features.sh" <<EOF
list_optional_features() {
echo "${valid_feature}"
}
is_feature_enabled_by_default() {
return 1 # disabled by default
}
EOF
local cmd="mycmd"
local cmd_path="${BT_TEMP_DIR}/tools/devshell/${cmd}"
mkdir -p "$(dirname "${cmd_path}")"
# create a command that runs is_feature_enabled and returns its exit code
cat >"${cmd_path}" <<EOF
#!/bin/bash
source ${BT_TEMP_DIR}/tools/devshell/lib/fx-optional-features.sh || exit \$\?
is_feature_enabled "${valid_feature}"
EOF
chmod u+x "${cmd_path}"
# check that the command, when executed through fx, works as intended for
# an optional feature.
# It should return 1 (false), since the feature is not enabled explicitly, hence disabled.
BT_EXPECT_FAIL ${fx} ${cmd}
# Same, --disable has no effect
BT_EXPECT_FAIL ${fx} --disable=${valid_feature} ${cmd}
# Should return 0 (true), since the feature is explicitly enabled
# in the fx invocation.
BT_EXPECT ${fx} --enable=${valid_feature} ${cmd}
}
TEST_features_are_valid() {
source "${BT_TEMP_DIR}/tools/devshell/lib/fx-optional-features.sh"
for f in $(list_optional_features); do
# no spaces
if [[ $f =~ " " ]]; then
BT_FAIL "Optional feature '${f}' should not contain spaces"
fi
# reverse checking
BT_EXPECT is_valid_feature "$f"
done
}
BT_RUN_TESTS "$@"