Skip to content

Instantly share code, notes, and snippets.

@bestdan
Created December 6, 2023 13:28
Show Gist options
  • Save bestdan/bf8fda63883f101f7d775722fc4af431 to your computer and use it in GitHub Desktop.
Save bestdan/bf8fda63883f101f7d775722fc4af431 to your computer and use it in GitHub Desktop.
Easily audit the difference between two dart objects in a failed test
#!/usr/bin/env sh
# # These functions make it easy to look at the output of a dart
# # test with large non-matching objects.
#
# # To use:
# # One and done:
# auditDartTest test/example_test.dart 'this test fails'
#
# # Or, if you want to check intermediate outputs:
# # Create the reference json file
# runAndSaveDartTest test/example_test.dart 'this test fails'
# auditDartTestFailure temp_file.json
# ... output ...
# rm temp_file.json
runAndSaveDartTest(){
flutter test -r json $1 --name $2 > temp_file.json
}
auditDartTest(){
flutter test -r json $1 --name $2 > temp_file.json
auditDartTestFailure temp_file.json
rm temp_file.json
}
auditDartTestFailure(){
cat $1 | extractErrorMessage > temp_error_msg
extractObjects temp_error_msg > temp_objects
echo $( diffExpectedActual temp_objects )
rm temp_error_msg
rm temp_objects
}
extractErrorMessage(){
cat $1 | jq -s 'map(select(.message != null)) | .[] | {message} | join("")' | grep "EXCEPTION" | sed 's/.*Expected/Expected/' | sed 's/\\n\\nWhen.*//'
}
extractObjects(){
input=$(cat $1)
actual=$( echo -n $input | tr -d '\n' | sed 's/.*Actual/Actual/' | awk '{$1=$1};1' )
expected=$( echo -n $input | tr -d '\n' | sed 's/Actual.*//' | awk '{$1=$1};1' )
echo "$expected\n$actual\n"
}
diffExpectedActual(){
input=$(cat $1)
actual=$( echo "$input" | tail -n 1 )
expected=$( echo "$input" | head -n 1 )
git --no-pager diff --color=always $(echo "$expected" | git hash-object -w --stdin) $(echo "$actual" | git hash-object -w --stdin) --word-diff | tail -n +6
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment