| #!/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 expected behavior from fx helpdoc |
| |
| BT_FILE_DEPS=( |
| "scripts/fx" |
| "scripts/fx-help.awk" |
| "tools/devshell/helpdoc" |
| "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_MOCKED_TOOLS=( |
| "tools/devshell/lib/metrics.sh" |
| ) |
| |
| BT_MKDIR_DEPS=( |
| ".jiri_root" |
| "helpdoc_out" |
| ) |
| |
| declare fx |
| BT_SET_UP() { |
| source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh" |
| fx="$(btf::setup_fx)" |
| # 'fx helpdoc' executes 'fx help' internally, so mocked fx needs to be in path |
| btf::add_binary_to_path "${fx}" |
| } |
| |
| # ensure that "fx helpdoc" does not generate docs for vendor commands when |
| # executed with the default arguments |
| TEST_check-no-vendor() { |
| mkdir -p "${BT_TEMP_DIR}/vendor/mycompany/scripts/devshell" |
| local vendor_cmd="vendor_command" |
| cp "${BT_TEMP_DIR}/tools/devshell/helpdoc" "${BT_TEMP_DIR}/vendor/mycompany/scripts/devshell/${vendor_cmd}" |
| local out="${BT_TEMP_DIR}/_fx_helpdoc_output" |
| BT_EXPECT "${fx}" helpdoc "${BT_TEMP_DIR}/helpdoc_out" > "${out}" |
| BT_EXPECT_EMPTY "$(find "${BT_TEMP_DIR}/helpdoc_out" -name "${vendor_cmd}*")" |
| } |
| |
| # ensure that "fx helpdoc" does generate docs for vendor commands when |
| # executed with the "--vendor" argument |
| TEST_check-vendor() { |
| mkdir -p "${BT_TEMP_DIR}/vendor/mycompany/scripts/devshell" |
| local vendor_cmd="vendor_command" |
| cp "${BT_TEMP_DIR}/tools/devshell/helpdoc" "${BT_TEMP_DIR}/vendor/mycompany/scripts/devshell/${vendor_cmd}" |
| local out="${BT_TEMP_DIR}/_fx_helpdoc_output" |
| BT_EXPECT "${fx}" helpdoc --vendor "${BT_TEMP_DIR}/helpdoc_out" > "${out}" |
| BT_EXPECT_NOT_EMPTY "$(find "${BT_TEMP_DIR}/helpdoc_out" -name "${vendor_cmd}*")" |
| } |
| |
| _create_subcommand() { |
| local subcommand="$1" |
| local subcommand_file="${BT_TEMP_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" |
| } |
| |
| # ensure that "fx helpdoc" normalizes (camel case) category names |
| TEST_check-category-normalized() { |
| local subcommand="mycommand1" |
| local category="Category UpperCASE 1" |
| local norm_category="Category uppercase 1" |
| _create_subcommand "${subcommand}" "tools/devshell" "howdy!" "summary1" "line1" "line2" "${category}" |
| local _stdout="${BT_TEMP_DIR}/_fx_helpdoc_output" |
| local _dirout="${BT_TEMP_DIR}/helpdoc_out" |
| |
| BT_EXPECT "${fx}" helpdoc "${_dirout}" > "${_stdout}" |
| BT_EXPECT_FILE_CONTAINS_SUBSTRING "${_dirout}/_toc.yaml" "title: \"${norm_category}" |
| BT_EXPECT_FILE_CONTAINS_SUBSTRING "${_dirout}/index.md" "[${norm_category}]" |
| } |
| |
| # ensure that paths in _toc.yaml files created by "fx helpdoc" have the |
| # correct prefix |
| TEST_check-paths() { |
| local prefix="/mycustom_path/should/work" |
| local out="${BT_TEMP_DIR}/_fx_helpdoc_output" |
| BT_EXPECT "${fx}" helpdoc --toc-prefix "${prefix}" helpdoc_out > "${out}" |
| BT_EXPECT_EMPTY "$(find "helpdoc_out" -name "*_toc.yaml" | xargs grep -E "^\s*(path|include):" | grep -v -E "\s${prefix}")" |
| } |
| |
| BT_RUN_TESTS "$@" |