Skip to main content

ci/ts

1#!/usr/bin/env bash
3set -o errexit
4set -o nounset
6function run_python_tests() {
7 GIT_ROOT=$(git rev-parse --show-toplevel)
8
9 pushd "$GIT_ROOT" >/dev/null
10 # If there's a `scripts` folder with a test script, run that
11 if find "scripts" -name 'run_*_tests.sh' >/dev/null 2>&1; then
12 print_info "-> bash scripts/run_*_tests.sh"
13 bash scripts/run_*_tests.sh
14 return 0
15 fi
17 # If there's a `go.mod` in the root, this is a Go project.
18 # Look for changed Go packages, and pass the list to `go test`.
19 if test -e go.mod; then
20 if test -e "./tool/go"; then
21 GO="./tool/go"
22 else
23 GO=$(which go)
24 fi
26 CHANGED_PACKAGES=$(
27 git ls-files --modified \
28 | xargs dirname \
29 | uniq \
30 | xargs -I '{}' echo "./{}" \
31 | tr '\n' ' '
32 )
34 print_info "$GO test $CHANGED_PACKAGES"
35 "$GO" test $CHANGED_PACKAGES
36 fi
37 popd >/dev/null
39 REPO_NAME="$(basename $(pwd))"
41 # Run ruff to do Python formatting
42 print_info "-> ruff format"
43 ruff format .
45 echo ""
47 print_info "-> ruff check --fix"
48 ruff check --fix .
50 echo ""
52 # This is one repo which is a bit special -- I'm gradually trying to chase
53 # the code into `src`, but for now I have to remember to look at *.py
54 if [[ "$REPO_NAME" = "library-lookup" ]]
55 then
56 print_info '-> mypy *.py src tests'
57 mypy *.py src tests
58 else
59 print_info '-> mypy src tests'
61 if mypy src tests >/dev/null
62 then
63 mypy src tests --no-color-output
64 else
65 mypy src tests
66 fi
67 fi
69 echo ""
71 # Run the tests.
72 print_info "-> coverage run -m pytest tests"
73 coverage run -m pytest tests --quiet
75 echo ""
77 print_info "-> coverage report"
79 if [[ $(coverage report --format=total) = "100" ]]
80 then
81 echo "100% coverage!"
82 else
83 coverage report
84 fi
87function run_rust_tests() {
88 print_info "-> cargo fmt"
89 cargo fmt
91 echo ""
93 print_info "-> cargo build"
94 cargo build
96 echo ""
98 print_info -n "-> cargo test"
99 cargo test --quiet
102if test -f Cargo.toml
103then
104 run_rust_tests
105else
106 run_python_tests "$@"
107fi