|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script verifies that every example in the examples directory can be built |
| 4 | + |
| 5 | +# Check that PlatformIO is on the path |
| 6 | +if [[ "$(which pio)" == "" ]]; then |
| 7 | + echo "PlatformIO executable (pio) not found on PATH. Stop." |
| 8 | + echo "PATH=$PATH" |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +# In general, we want the script to fail if something unexpected happens. |
| 13 | +# This flag gets only revoked for the actual build process. |
| 14 | +set -e |
| 15 | + |
| 16 | +# Find the script and repository location based on the current script location |
| 17 | +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" |
| 18 | +REPODIR=$(cd "$(dirname $SCRIPTDIR/../../../..)" && pwd) |
| 19 | + |
| 20 | +# Some other directory definitions |
| 21 | +TMPDIR="$REPODIR/tmp" |
| 22 | +CIDIR="$REPODIR/extras/ci" |
| 23 | +EXAMPLEDIR="$REPODIR/examples" |
| 24 | + |
| 25 | +# Re-create build directory |
| 26 | +if [ -d "$TMPDIR" ]; then |
| 27 | + rm -r "$TMPDIR" |
| 28 | +fi |
| 29 | +mkdir -p "$TMPDIR" |
| 30 | + |
| 31 | +# Count failed tests |
| 32 | +FAILED_TESTS=0 |
| 33 | + |
| 34 | +# Lists for failed and successful tests for the summary |
| 35 | +EXAMPLES_SUCCESS=() |
| 36 | +EXAMPLES_FAILURE=() |
| 37 | + |
| 38 | +# For each example |
| 39 | +for EXAMPLE in "$EXAMPLEDIR"/*; do |
| 40 | + # Check that an .ino file exists |
| 41 | + EXAMPLENAME=$(basename $EXAMPLE) |
| 42 | + if [ -d "$EXAMPLE" ] && [ -f "$EXAMPLE/$EXAMPLENAME.ino" ]; then |
| 43 | + if [ "$#" -ne 1 ]; then |
| 44 | + # No arguments: Test all |
| 45 | + TEST_THIS_EXAMPLE=1 |
| 46 | + else |
| 47 | + # Has arguments: Test only examples listed as arguments |
| 48 | + TEST_THIS_EXAMPLE=0 |
| 49 | + for arg in "$@"; do |
| 50 | + [[ $arg == "$EXAMPLENAME" ]] && TEST_THIS_EXAMPLE=1 && break |
| 51 | + done |
| 52 | + fi |
| 53 | + # If the test should be executed |
| 54 | + if [[ "$TEST_THIS_EXAMPLE" == "1" ]]; then |
| 55 | + # We take the .ino file, rename it as main.cpp and add an Arduino.h include at the top |
| 56 | + MAINCPP="$TMPDIR/$EXAMPLENAME/src/main.cpp" |
| 57 | + INOFILE="$TMPDIR/$EXAMPLENAME/src/$EXAMPLENAME.ino" |
| 58 | + PROJECTDIR="$TMPDIR/$EXAMPLENAME" |
| 59 | + echo "Building $EXAMPLENAME" |
| 60 | + echo "------------------------------------------------------------" |
| 61 | + # (re-)create the project directory under tmp/ |
| 62 | + if [ -d "$PROJECTDIR" ] && [ "$PROJECTDIR" != "" ]; then |
| 63 | + rmdir -r "$PROJECTDIR" |
| 64 | + fi |
| 65 | + # Create the lib folder to link the current version of the library |
| 66 | + mkdir -p "$PROJECTDIR/lib" |
| 67 | + # Copy the project folder template from ci/templates/example-project |
| 68 | + cp -r "$CIDIR/templates/example-project"/* "$PROJECTDIR/" |
| 69 | + # Copy the source files |
| 70 | + cp -r "$EXAMPLEDIR/$EXAMPLENAME/." "$PROJECTDIR/src" |
| 71 | + # Create the library link |
| 72 | + ln -s "$REPODIR" "$PROJECTDIR/lib/pio-esp32-ci-demo" |
| 73 | + # Convert .ino to main.cpp |
| 74 | + echo "#include <Arduino.h>" > "$MAINCPP" |
| 75 | + cat "$INOFILE" >> "$MAINCPP" |
| 76 | + rm "$INOFILE" |
| 77 | + # Try building the application (+e as we want to test every example and get a |
| 78 | + # summary on what is working) |
| 79 | + set +e |
| 80 | + pio run -d "$TMPDIR/$EXAMPLENAME" |
| 81 | + SUCCESS=$? |
| 82 | + set -e |
| 83 | + # Evaluate this example |
| 84 | + if [[ "$SUCCESS" != "0" ]]; then |
| 85 | + FAILED_TESTS=$[ FAILED_TESTS + 1 ] |
| 86 | + EXAMPLES_FAILURE+=("$EXAMPLENAME") |
| 87 | + else |
| 88 | + EXAMPLES_SUCCESS+=("$EXAMPLENAME") |
| 89 | + fi |
| 90 | + fi # TEST_THIS_EXAMPLE |
| 91 | + fi # example dir exists and contains .ino |
| 92 | +done |
| 93 | + |
| 94 | +# Summarize the results |
| 95 | +echo "Summary" |
| 96 | +echo "------------------------------------------------------------" |
| 97 | +for exmpl in "${EXAMPLES_SUCCESS[@]}"; do |
| 98 | + printf " \u2714 $exmpl\n" |
| 99 | +done |
| 100 | + |
| 101 | +for exmpl in "${EXAMPLES_FAILURE[@]}"; do |
| 102 | + printf " \u274c $exmpl\n" |
| 103 | +done |
| 104 | + |
| 105 | +# Return the overall success |
| 106 | +if [[ "$FAILED_TESTS" != "0" ]]; then |
| 107 | + echo "$FAILED_TESTS Tests failed." |
| 108 | + exit 1 |
| 109 | +else |
| 110 | + echo "Success." |
| 111 | +fi |
0 commit comments