Automatically recording canned responses for stubbed shell commands

This brief post shows a shell script that can automatically record canned responses from a shell command, for example the aws command line, for later use in shUnit2 tests.

For more information, see my other posts on unit testing bash scripts in shUnit2.


Replace the command of interest with a shell function following this pattern:


#!/bin/bash log_file=/tmp/ aws() { if [ ! -e $log_file ] then echo 'aws() {' >> $log_file echo ' case "$*" in' >> $log_file else awk '/esac/{exit}{print}' $log_file > x ; mv x $log_file fi echo ' "'$*'")' >> $log_file echo " cat <<'EOF'" >> $log_file command aws $* | tee -a $log_file echo 'EOF' >> $log_file echo ' ;;' >> $log_file echo ' esac' >> $log_file echo '}' >> $log_file } aws ec2 describe-instances --instance-id i-0985e6cf081ec2424 --query 'Reservations[*].Instances[0].PrivateIpAddress' --output text aws ec2 describe-images --image-id ami-0001e562 --query 'Images[].OwnerId' --output text aws ec2 describe-images --image-id ami-0001e562 --query 'Images[].CreationDate' --output text read more