| #!/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 valid_feature | |
| BT_SET_UP() { | |
| source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh" | |
| fx="$(btf::setup_fx)" | |
| source "${BT_TEMP_DIR}/tools/devshell/lib/fx-optional-features.sh" | |
| # get the first valid feature defined in an array in fx-optional-features.sh | |
| # if there are no optional features, this variable will | |
| # be empty and the tests that depend on it should return SUCCESS. | |
| valid_feature="${_FX_OPTIONAL_FEATURES[0]}" | |
| } | |
| TEST_disable_feature() { | |
| if [[ -z "${valid_feature}" ]]; then | |
| # cannot test without a valid feature to be disabled | |
| return 0 | |
| fi | |
| 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} | |
| # Now it should return false, since the feature is explicitly disabled | |
| # in the fx invocation. | |
| BT_EXPECT_FAIL ${fx} --disable=${valid_feature} ${cmd} | |
| } | |
| BT_RUN_TESTS "$@" |