|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -o errexit # Exit on error |
| 4 | +set -o nounset # Treat unset variables as an error |
| 5 | + |
| 6 | +usage() { |
| 7 | + echo "Usage: $0 [command]" |
| 8 | + echo |
| 9 | + echo "Run tests or a specific command in all examples" |
| 10 | + echo |
| 11 | + echo "Commands:" |
| 12 | + echo " <none> Run all example tests (both 'npm ci' and 'npm test')" |
| 13 | + echo " <cmd> Run a specific npm command (e.g., 'audit' for npm audit)" |
| 14 | + echo |
| 15 | + echo "Examples:" |
| 16 | + echo " $0" |
| 17 | + echo " $0 test" |
| 18 | + echo " $0 audit" |
| 19 | + exit 0 |
| 20 | +} |
| 21 | + |
| 22 | +run_example() { |
| 23 | + local example_dir="$1" |
| 24 | + shift |
| 25 | + if [ $# -gt 0 ]; then |
| 26 | + echo "Ignoring extra arguments: $*" |
| 27 | + fi |
| 28 | + |
| 29 | + echo "$(tput bold)$(tput setaf 7)========================================$(tput sgr0)" |
| 30 | + echo "$(tput bold)$(tput setaf 7)Running example in $example_dir$(tput sgr0)" |
| 31 | + echo "$(tput bold)$(tput setaf 7)========================================$(tput sgr0)" |
| 32 | + |
| 33 | + if [ -d "$example_dir/node_modules" ]; then |
| 34 | + echo "$(tput bold)$(tput setaf 7)Skipping installation of dependencies$(tput sgr0)" |
| 35 | + else |
| 36 | + npm --prefix "$example_dir" ci |
| 37 | + fi |
| 38 | + |
| 39 | + echo "$(tput bold)$(tput setaf 7)Running tests$(tput sgr0)" |
| 40 | + npm --prefix "$example_dir" run test |
| 41 | +} |
| 42 | + |
| 43 | +run_cmd() { |
| 44 | + local example_dir="$1" |
| 45 | + shift |
| 46 | + if [ $# -eq 0 ]; then |
| 47 | + echo "No command provided" |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | + echo "$(tput bold)$(tput setaf 7)========================================$(tput sgr0)" |
| 51 | + echo "$(tput bold)$(tput setaf 7)Running '$*' in $example_dir$(tput sgr0)" |
| 52 | + echo "$(tput bold)$(tput setaf 7)========================================$(tput sgr0)" |
| 53 | + npm --prefix "$example_dir" "$@" |
| 54 | +} |
| 55 | + |
| 56 | +main() { |
| 57 | + if [ "${1-}" = "--help" ] || [ "${1-}" = "-h" ]; then |
| 58 | + usage |
| 59 | + fi |
| 60 | + |
| 61 | + declare -a examples=( |
| 62 | + examples/e2e |
| 63 | + examples/graphql |
| 64 | + examples/jest |
| 65 | + examples/messages |
| 66 | + examples/mocha |
| 67 | + examples/serverless |
| 68 | + examples/typescript |
| 69 | + examples/v3/e2e |
| 70 | + examples/v3/provider-state-injected |
| 71 | + examples/v3/run-specific-verifications |
| 72 | + examples/v3/todo-consumer |
| 73 | + examples/v3/typescript |
| 74 | + examples/v4/matchers |
| 75 | + examples/v4/plugins |
| 76 | + ) |
| 77 | + |
| 78 | + if [ $# -gt 0 ]; then |
| 79 | + for example in "${examples[@]}"; do |
| 80 | + run_cmd "$example" "$@" |
| 81 | + done |
| 82 | + else |
| 83 | + declare -a failed_examples=() |
| 84 | + for example in "${examples[@]}"; do |
| 85 | + run_example "$example" || failed_examples+=("$example") |
| 86 | + done |
| 87 | + |
| 88 | + for failed_example in "${failed_examples[@]}"; do |
| 89 | + echo "$(tput bold)$(tput setaf 1)Failed to run example in $failed_example$(tput sgr0)" |
| 90 | + done |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +main "$@" |
0 commit comments