| #!/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 "format-code" script. | |
| readonly TESTDATA="data/fx_format_code_test" | |
| BT_FILE_DEPS=( | |
| "scripts/fx" | |
| "tools/devshell/format-code" | |
| "tools/devshell/format_code.py" | |
| "tools/devshell/lib/scm.py" | |
| "tools/devshell/lib/shac_runner.py" | |
| "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/is_invoked_by_agent.sh" | |
| "tools/devshell/lib/agents.txt" | |
| "tools/devshell/tests/subcommands/${TESTDATA}/cml.cml" | |
| "tools/devshell/tests/subcommands/${TESTDATA}/cplusplus.cc" | |
| "tools/devshell/tests/subcommands/${TESTDATA}/rust.rs" | |
| "tools/devshell/tests/subcommands/${TESTDATA}/fidl.test.fidl" | |
| "tools/devshell/tests/subcommands/${TESTDATA}/gidl.gidl" | |
| "tools/devshell/tests/subcommands/${TESTDATA}/gn.gn" | |
| "tools/devshell/tests/subcommands/${TESTDATA}/gn.gni" | |
| "tools/devshell/tests/subcommands/${TESTDATA}/json5.json5" | |
| "tools/devshell/tests/subcommands/${TESTDATA}/python3.py" | |
| ) | |
| MOCK_SHAC="prebuilt/tools/shac/shac" | |
| MOCK_PYTHON3_DIR="prebuilt/third_party/python3/test" | |
| BT_MOCKED_TOOLS=( | |
| "tools/devshell/list-build-artifacts" | |
| "tools/devshell/lib/platform.sh" | |
| "${MOCK_PYTHON3_DIR}/bin/python3" | |
| "${MOCK_SHAC}" | |
| ) | |
| BT_MKDIR_DEPS=( | |
| ".jiri_root" | |
| ".git" | |
| ) | |
| BT_SET_UP() { | |
| # Set up the testing framework. | |
| source "${BT_TEMP_DIR}/tools/devshell/tests/lib/fuchsia-mock.sh" | |
| fx="$(btf::setup_fx)" | |
| touch "${BT_TEMP_DIR}/shac.textproto" | |
| export PATH="${BT_TEMP_DIR}/bin:${PATH}" | |
| cat > "${BT_TEMP_DIR}/${MOCK_PYTHON3_DIR}/bin/python3.mock_side_effects" <<EOF | |
| exec "\$(command -v python3)" "\$@" | |
| EOF | |
| cat > "${BT_TEMP_DIR}/tools/devshell/lib/platform.sh.mock_side_effects" <<EOF | |
| readonly PREBUILT_SHAC=${BT_TEMP_DIR}/${MOCK_SHAC} | |
| readonly PREBUILT_PYTHON3_DIR=${BT_TEMP_DIR}/${MOCK_PYTHON3_DIR} | |
| readonly PREBUILT_PYTHON3=${BT_TEMP_DIR}/${MOCK_PYTHON3_DIR}/bin/python3 | |
| EOF | |
| } | |
| # Ensure that formatting an unchanged tree works and passes --quiet by default. | |
| TEST_fx-format-code-no-args() { | |
| local out="${BT_TEMP_DIR}/_fx_format_code_output" | |
| BT_EXPECT "${fx}" format-code --dry-run > "${out}" 2> stderr | |
| BT_EXPECT grep -q -E '"shac fmt .*--quiet"' "${out}" | |
| } | |
| # Ensure that the commands for common file types are sensible. | |
| TEST_fx-format-code-commands() { | |
| local out="${BT_TEMP_DIR}/_fx_format_code_output" | |
| local files=( | |
| "${TESTDATA}/cplusplus.cc" | |
| "${TESTDATA}/rust.rs" | |
| "${TESTDATA}/gn.gn" | |
| "${TESTDATA}/gn.gni" | |
| "${TESTDATA}/python3.py" | |
| ) | |
| local csfiles | |
| printf -v csfiles ",%s" "${files[@]}" | |
| BT_EXPECT "${fx}" format-code --verbose --files="${csfiles:1}" > "${out}" | |
| BT_EXPECT grep -q -E '"shac fmt .*cplusplus.cc.*rust.rs"' "${out}" | |
| } | |
| # Ensure that the --emit flag is passed through to shac fmt. | |
| TEST_fx-format-code-emit-flag() { | |
| local out="${BT_TEMP_DIR}/_fx_format_code_output" | |
| local files=( | |
| "${TESTDATA}/rust.rs" | |
| ) | |
| local csfiles | |
| printf -v csfiles ",%s" "${files[@]}" | |
| BT_EXPECT "${fx}" format-code --emit --verbose --files="${csfiles:1}" > "${out}" | |
| BT_EXPECT grep -q -E '"shac fmt .*--emit.*rust.rs"' "${out}" | |
| } | |
| # Ensure that formatting a nonexistent file prints an error. | |
| TEST_fx-format-code-nonexistent-file() { | |
| local err="${BT_TEMP_DIR}/_fx_format_code_stderr" | |
| local path="${TESTDATA}/doesnotexist.py" | |
| BT_EXPECT "${fx}" format-code --verbose --files="${path}" > /dev/null 2> "${err}" | |
| BT_EXPECT grep -q -E '"ERROR: no such file: .*/doesnotexist.py"' "${err}" | |
| } | |
| BT_RUN_TESTS "$@" |