Skip to content

Commit

Permalink
Replace the Makefile with scripts
Browse files Browse the repository at this point in the history
This adds two new scripts: test.sh, which is much more amenable and
flexible to use than the previous testing logic in the Makefile, and
a lint.sh script.  Both are used from the merge checks, but they can
also be used interactively.
  • Loading branch information
jmmv committed Dec 3, 2023
1 parent 7bdeee9 commit dfb0d4b
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 90 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- uses: mozilla-actions/[email protected]
- run: sudo apt update
- run: sudo apt install pre-commit
- run: make lint
- run: ./lint.sh

test-individually:
runs-on: ubuntu-latest
Expand All @@ -42,7 +42,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: mozilla-actions/[email protected]
- run: make test-individually
- run: ./test.sh all

test-workspace:
runs-on: ubuntu-latest
Expand All @@ -59,4 +59,4 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: mozilla-actions/[email protected]
- run: make test-workspace
- run: ./test.sh
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Cargo.lock
config.mk
config.env
target
vscode-target
76 changes: 0 additions & 76 deletions Makefile

This file was deleted.

19 changes: 11 additions & 8 deletions config.mk.tmpl → config.env.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@

# User-configurable settings to run tests.
#
# To modify this file, first copy it to config.mk and then edit that local copy. This file
# (config.mk.tmpl) must not contain secrets and the config.mk file must never be checked in.
# To modify this file, first copy it to config.env and then edit that local copy. This file
# (config.env.tmpl) must not contain secrets and the config.env file must never be checked in.

# Azure Maps key to use to geolocate IPs.
AZURE_MAPS_KEY=
export AZURE_MAPS_KEY=

# Settings to connect to the test database.
PGSQL_TEST_HOST=
PGSQL_TEST_PORT=5432
PGSQL_TEST_DATABASE=iii-iv-test
PGSQL_TEST_USERNAME=
PGSQL_TEST_PASSWORD=
export PGSQL_TEST_HOST=
export PGSQL_TEST_PORT=5432
export PGSQL_TEST_DATABASE=iii-iv-test
export PGSQL_TEST_USERNAME=
export PGSQL_TEST_PASSWORD=

# Debugging.
export RUST_LOG=debug
3 changes: 1 addition & 2 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Cargo.lock
build.stamp
config.env
perfectschedule
config.mk
target/
vscode-target/
23 changes: 23 additions & 0 deletions lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#! /bin/sh
# III-IV
# Copyright 2023 Julio Merino
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

set -eu

pre-commit run -a
cargo clippy -- -D warnings
cargo clippy --features=testutils -- -D warnings
cargo clippy --all-features --all-targets -- -D warnings
cargo fmt -- --check
137 changes: 137 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#! /bin/sh
# III-IV
# Copyright 2023 Julio Merino
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

set -eu

readonly PROGNAME="${0##*/}"

err() {
echo "${PROGNAME}: E: ${*}" 1>&2
exit 1
}

info() {
echo "${PROGNAME}: I: ${*}" 1>&2
}

run_tests() {
local dir="${1}"; shift
local cargo_args="${1}"; shift
local test_args="${1}"; shift
local features="${1}"; shift

(
if [ -e ./config.env ]; then
info "Loading ./config.env"
. ./config.env
fi

cd "${dir}"

for feature in ${features}; do
if [ "${feature}" = default ]; then
info "Testing ${dir} with default features"
cargo test ${cargo_args} -- --include-ignored ${test_args}
else
if grep -q "^{feature} = \[" Cargo.toml; then
info "Testing ${dir} with feature=${feature}"
cargo test --features="${feature}" ${cargo_args} -- --include-ignored ${test_args}
else
info "Skipping ${dir} with feature=${feature}"
fi
fi
done
)
}

usage() {
cat <<EOF
Usage: ${PROGNAME} [-A cargo_arg] [-a test_arg] [-f feature] [crate1 .. crateN]
If no crates are specified, runs all tests from the root of the workspace.
If one or more crates are specified, runs all tests from the individual crate
directories. The special name "all" can be used to run tests for all crates
individually.
The -A flag specifies extra arguments to "cargo test". This flag can be used
multiple times and the values all accumulate.
The -a flag specifies extra arguments to the test programs. This flag can be
used multiple times and the values all accumulate.
The -f flag specifies the individual features to test. This flag can be used
multiple times and the valus all accumulate. If not specified, all known
features are tested individually.
EOF
}

main() {
local cargo_args=
local test_args=
local features=
while getopts ':A:a:f:h' arg "${@}"; do
case "${arg}" in
A)
cargo_args="${cargo_args} ${OPTARG}"
;;

a)
test_args="${test_args} ${OPTARG}"
;;

f)
features="${features} ${OPTARG}"
;;

h)
usage
exit 0
;;

:)
err "Missing argument to option -${OPTARG} in build"
;;

\?)
err "Unknown option -${OPTARG} in build"
;;
esac
done
shift $((OPTIND - 1))

[ -n "${features}" ] || features="default postgres sqlite testutils"

[ -e ./.github ] || err "Must be run from the root of the workspace"

if [ ${#} -eq 0 ]; then
run_tests . "${cargo_args}" "${test_args}" "${features}"
else
local crates=
if [ ${#} -eq 1 -a "${1}" = all ]; then
crates="$(grep '^ *"' Cargo.toml | cut -d '"' -f 2)"
info "Expanded crates to test to $(echo ${crates})"
else
crates="${*}"
fi

for crate in ${crates}; do
run_tests "${crate}" "${cargo_args}" "${test_args}" "${features}"
done
fi
}

main "${@}"

0 comments on commit dfb0d4b

Please sign in to comment.