Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 75 additions & 20 deletions ci/run_task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ usage() {
cat <<EOF
Usage:

./run_task.sh TASK
./run_task.sh TASK [CRATE1] [CRATE2] ...

TASK
- stable Run tests with stable toolchain.
Expand All @@ -41,6 +41,11 @@ TASK
- docsrs Build docs with nightly toolchain.
- bench Run the bench tests.

CRATES (optional)
- If provided, run the task only on the specified crate(s).
- Multiple crates can be specified as space-delimited arguments.
- If not provided, run the task on all crates from contrib/crates.sh.

Environment Variables:
MAINTAINER_TOOLS_LOG_LEVEL Control script and cargo output verbosity.
verbose (default) Show all script and cargo messages.
Expand All @@ -52,11 +57,15 @@ main() {
local task="${1:-usage}"
local crates_script="$REPO_DIR/contrib/crates.sh"

# FIXME: This is a hackish way to get the help flag.
if [ "$task" = "usage" ] || [ "$task" = "-h" ] || [ "$task" = "--help" ]; then
usage
exit 0
fi
# Check for help flags in any position.
for arg in "$@"; do
case "$arg" in
-h|--help|help|usage)
usage
exit 0
;;
esac
done

check_required_commands

Expand All @@ -70,9 +79,16 @@ main() {
fi

verbose_say "Repository: $REPO_DIR"
verbose_say "Script invocation: $0 $task"
verbose_say "Script invocation: $0 $*"

if [ -e "$crates_script" ]; then
# Shift to remove the task argument, leaving only crate arguments.
shift

# Override crates.sh if there are crate args.
if [ "$#" -gt 0 ]; then
CRATES="$*"
verbose_say "Using specified crate(s): $CRATES"
elif [ -e "$crates_script" ]; then
verbose_say "Sourcing $crates_script"
# can't find the file because of the ENV var
# shellcheck source=/dev/null
Expand All @@ -88,15 +104,15 @@ main() {
stable)
# Test, run examples, do feature matrix.
# crate/contrib/test_vars.sh is sourced in this function.
build_and_test
build_and_test "stable"
;;

nightly)
build_and_test
build_and_test "nightly"
;;

msrv)
build_and_test
build_and_test "msrv"
;;

lint)
Expand Down Expand Up @@ -124,7 +140,11 @@ main() {
}

# Build and test for each crate, done with each toolchain.
#
# Usage: build_and_test "toolchain"
build_and_test() {
local toolchain="$1"

for crate in $CRATES; do
local test_vars_script="$REPO_DIR/$crate/contrib/test_vars.sh"

Expand Down Expand Up @@ -152,6 +172,7 @@ build_and_test() {
fi
pushd "$REPO_DIR/$crate" > /dev/null

need_toolchain "$toolchain"
do_test
do_feature_matrix

Expand Down Expand Up @@ -243,14 +264,14 @@ loop_features() {

# Lint the workspace.
do_lint_workspace() {
need_nightly
need_toolchain "nightly"
$cargo clippy --workspace --all-targets --all-features --keep-going -- -D warnings
$cargo clippy --workspace --all-targets --keep-going -- -D warnings
}

# Run extra crate specific lints, e.g. clippy with no-default-features.
do_lint_crates() {
need_nightly
need_toolchain "nightly"
for crate in $CRATES; do
pushd "$REPO_DIR/$crate" > /dev/null
if [ -e ./contrib/extra_lints.sh ]; then
Expand Down Expand Up @@ -307,20 +328,22 @@ do_dup_deps() {
# Build the docs with a nightly toolchain, in unison with the function
# below this checks that we feature guarded docs imports correctly.
build_docs_with_nightly_toolchain() {
need_nightly
need_toolchain "nightly"
# -j1 is because docs build fails if multiple versions of `bitcoin_hashes` are present in dep tree.
RUSTDOCFLAGS="--cfg docsrs -D warnings -D rustdoc::broken-intra-doc-links" $cargo doc --all-features -j1
}

# Build the docs with a stable toolchain, in unison with the function
# above this checks that we feature guarded docs imports correctly.
build_docs_with_stable_toolchain() {
need_toolchain "stable"
local cargo="cargo +stable --locked" # Can't use global because of `+stable`.
RUSTDOCFLAGS="-D warnings" $cargo doc --all-features -j1
}

# Bench only works with a non-stable toolchain (nightly, beta).
do_bench() {
need_toolchain "nightly"
verbose_say "Running bench tests for: $CRATES"

for crate in $CRATES; do
Expand Down Expand Up @@ -367,16 +390,48 @@ need_cmd() {
fi
}

need_nightly() {
cargo_ver=$(cargo --version)
if echo "$cargo_ver" | grep -q -v nightly; then
err "Need a nightly compiler; have $(cargo --version)"
fi
# Check that we have the required toolchain.
need_toolchain() {
local required_toolchain="$1"
local current_toolchain
current_toolchain=$(rustc --version)

case "$required_toolchain" in
nightly)
if ! echo "$current_toolchain" | grep -q nightly; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case you like it, claude told me this can be done like this

if [[ "$STRING" == *"world"* ]]; then
    echo "contains world"
fi

Note: Use *pattern* for literal substring matching.

Or

STRING="hello world"
[[ "$STRING" =~ world ]] && echo "contains world"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, no strong feelings either way. I think I'd roll with one of these if the rest of the script was all bash, but it's kinda all jumbled up right now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No sweat.

err "Need a nightly compiler; have $current_toolchain"
fi
;;
stable)
if echo "$current_toolchain" | grep -q nightly || echo "$current_toolchain" | grep -q beta; then
err "Need a stable compiler; have $current_toolchain"
fi
;;
msrv)
# This check must be run in a crate directory
# since it is possible for crates in a workspace to have different MSRVs.
local manifest_path
manifest_path="$(pwd)/Cargo.toml"
local msrv_version
msrv_version=$(cargo metadata --format-version 1 --no-deps | jq -r ".packages[] | select(.manifest_path == \"$manifest_path\") | .rust_version // empty")
if [ -z "$msrv_version" ]; then
err "No MSRV specified in $manifest_path or not in a crate directory"
fi

local current_version
current_version=$(echo "$current_toolchain" | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+' | head -1)
if [ "$current_version" != "$msrv_version" ]; then
err "Need Rust $msrv_version for MSRV testing in $manifest_path; have $current_version"
fi
;;
*)
err "Unknown toolchain requirement: $required_toolchain"
;;
esac
}

#
# Main script
#
main "$@"
exit 0