test-attribute.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env bash
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # test-attribute.sh - checks usage of test/tokio::test attributes
  12. #
  13. # We should favor mz_ore::test instead since it automatically initializes
  14. # logging, see also
  15. # https://github.com/MaterializeInc/materialize/blob/main/doc/developer/style.md
  16. # Checking this in clippy is unfortunately not possible since the mz_ore::test
  17. # macro's internal usage of test/tokio::test would cause warnings even on
  18. # proper use.
  19. set -euo pipefail
  20. # shellcheck source=misc/shlib/shlib.bash
  21. . "$(dirname "$0")/../shlib/shlib.bash"
  22. if [[ $# -lt 1 ]]; then
  23. echo "usage: $0 <file>" >&2
  24. exit 1
  25. fi
  26. errors=0
  27. for file in "$@"; do
  28. if [[ ! -f "$file" ]]; then
  29. echo "lint: $(red error:) test-attribute: internal error: $file is not a file" >&2
  30. exit 1
  31. fi
  32. if grep "#\[test\]" "$file" | grep -qvE "//\s*allow\(test-attribute\)"; then
  33. echo "lint: $(red error:) test-attribute: $file: use of disallowed \`#[test]\` attribute. Use the \`#[mz_ore::test]\` attribute instead or add a \`// allow(test-attribute)\` comment" >&2
  34. ((errors++))
  35. fi
  36. if grep "#\[tokio::test" "$file" | grep -qvE "//\s*allow\(test-attribute\)"; then
  37. echo "lint: $(red error:) test-attribute: $file: use of disallowed \`#[tokio::test] attribute. Use the \`#[mz_ore::test(tokio::test)]\` attribute instead or add a \`// allow(test-attribute)\` comment" >&2
  38. ((errors++))
  39. fi
  40. done
  41. if [[ $errors -gt 0 ]]; then
  42. exit 1
  43. fi