| #!/bin/bash | |
| # Copyright 2026 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 "lint" script. | |
| readonly TESTDATA="data/fx_format_code_test" | |
| BT_FILE_DEPS=( | |
| "scripts/fx" | |
| "tools/devshell/lint" | |
| "tools/devshell/lint.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() { | |
| 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" <<INNER_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 | |
| INNER_EOF | |
| } | |
| TEST_fx-lint-no-args() { | |
| local out="${BT_TEMP_DIR}/_fx_lint_output" | |
| BT_EXPECT "${fx}" lint --dry-run > "${out}" 2> stderr | |
| BT_EXPECT grep -q -E '"shac check .*--quiet"' "${out}" | |
| } | |
| TEST_fx-lint-commands() { | |
| local out="${BT_TEMP_DIR}/_fx_lint_output" | |
| local files=( | |
| "${TESTDATA}/cplusplus.cc" | |
| "${TESTDATA}/rust.rs" | |
| "${TESTDATA}/fidl.test.fidl" | |
| ) | |
| local csfiles | |
| printf -v csfiles ",%s" "${files[@]}" | |
| BT_EXPECT "${fx}" lint --verbose --files="${csfiles:1}" > "${out}" | |
| BT_EXPECT grep -q -E '"shac check .*cplusplus.cc.*fidl.test.fidl.*rust.rs"' "${out}" | |
| } | |
| TEST_fx-lint-fix-flag() { | |
| local out="${BT_TEMP_DIR}/_fx_lint_output" | |
| local files=( | |
| "${TESTDATA}/rust.rs" | |
| ) | |
| local csfiles | |
| printf -v csfiles ",%s" "${files[@]}" | |
| BT_EXPECT "${fx}" lint --fix --verbose --files="${csfiles:1}" > "${out}" | |
| BT_EXPECT grep -q -E '"shac fix .*rust.rs"' "${out}" | |
| } | |
| TEST_fx-lint-only-flag() { | |
| local out="${BT_TEMP_DIR}/_fx_lint_output" | |
| BT_EXPECT "${fx}" lint --only=commit_msg --verbose > "${out}" | |
| BT_EXPECT grep -q -E '"shac check .*--only=commit_msg"' "${out}" | |
| } | |
| TEST_fx-lint-nonexistent-file() { | |
| local err="${BT_TEMP_DIR}/_fx_lint_stderr" | |
| local path="${TESTDATA}/doesnotexist.py" | |
| BT_EXPECT "${fx}" lint --verbose --files="${path}" > /dev/null 2> "${err}" | |
| BT_EXPECT grep -q -E '"ERROR: no such file: .*/doesnotexist.py"' "${err}" | |
| } | |
| BT_RUN_TESTS "$@" |