Skip to content

Commit

Permalink
Add CI support for panic detection
Browse files Browse the repository at this point in the history
panic-test.sh script under .traivs dir detect panic/unwrap calls, which
will panic the thread and cause process abortion.
  • Loading branch information
leonjia0112 committed Jan 29, 2019
1 parent 5ff1ed3 commit 063d0d9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ script:
- cargo fmt --all -- --check
- cargo build
- cargo test
- bash ./.travis/panic-test.sh ./.travis/banned_function.txt
2 changes: 2 additions & 0 deletions .travis/banned_function.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
unwrap()
panic!(.*)
37 changes: 37 additions & 0 deletions .travis/panic-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

# Check the unwrap and panic function presenting in src/ directory
# of the repo. Testing suite is excluded. If such a function presents
# return exit 1 for fail, otherwise return exit 0 for sucess.

# Getting the banned list from banned functions
banned_list=()
banned_function="$1"
while read -r line; do
banned_list=(${banned_list[@]} "$line")
done < "$banned_function"

output=""
catch_panic=$false

# Match panic call based on the given banned funciton list
for i in src/*; do
for func in "${banned_list[@]}"; do
tmp_line=$(sed '/#\[cfg(test)]/q' $i | grep -e "$func" | awk 'END {print NR}')
if [ "$tmp_line" -gt 0 ]; then
tmp_output=$(sed '/#\[cfg(test)]/q' $i | grep -n -e "$func")
output="${output}${tmp_output}\n"
catch_panic=$true
fi
done
done

# Output result
if [ $catch_panic ]; then
echo "exit 1. Please removed all the following panic calls."
printf "$output"
exit 1
else
echo "exit 0. Not panic violation occurs."
exit 0
fi

0 comments on commit 063d0d9

Please sign in to comment.