aboutsummaryrefslogtreecommitdiff
path: root/wrapper_test.sh
diff options
context:
space:
mode:
authorJames Lemieux <jplemieux@google.com>2018-01-28 02:56:27 -0800
committerJames Lemieux <jplemieux@google.com>2018-02-05 15:02:10 -0800
commit2a2559e5581c585721d342ffe6dceff6c5d7019e (patch)
treeb6f6360349e2d979893031712f909a22d07d42f1 /wrapper_test.sh
parentb0d9a6c2cb434aba94ba4be1c694e53588ddc399 (diff)
downloadrobolectric-shadows-2a2559e5581c585721d342ffe6dceff6c5d7019e.tar.gz
Create a run script for external/robolectric-shadows
This run script was copied from prebuilts/misc/common/robolectric/3.6.1 with small modifications. The 3.6.1 script only executes against public SDK versions which existed at the time of the robolectric 3.6.1 release. This script allows testing against the SDK currently under development. Bug: 72384841 Test: make Run_TeleService_robotests Change-Id: I038afbdfede230bc0050760f0419f5842139dde6
Diffstat (limited to 'wrapper_test.sh')
-rwxr-xr-xwrapper_test.sh172
1 files changed, 172 insertions, 0 deletions
diff --git a/wrapper_test.sh b/wrapper_test.sh
new file mode 100755
index 000000000..f6215f545
--- /dev/null
+++ b/wrapper_test.sh
@@ -0,0 +1,172 @@
+#!/bin/bash
+#
+# Tests for wrapper.sh.
+
+set -euo pipefail
+
+# The location of the script under test.
+readonly WRAPPER="$(realpath "$(dirname "$0")/wrapper.sh")"
+# The name of the tests to run. Each test correspond to a function in this file
+# whose name is the name of the test prefixed by 'test'.
+readonly TEST_NAMES=(
+ SuccessfulCase
+ FailedCase
+ FailedSignalCase
+)
+
+# Fails with an error message.
+function fatal() {
+ echo 1>&2 "FATAL: $@"
+ exit 113
+}
+
+function withTestFiles() {
+ (
+ echo '1'
+ echo '2'
+ echo '3'
+ echo '4'
+ echo '5'
+ echo '6'
+ echo '7'
+ ) >testfile
+ (
+ echo 'module: 1'
+ echo 'module: 2'
+ echo 'module: 3'
+ echo 'module: 4'
+ echo 'module: 5'
+ echo 'module: 6'
+ echo 'module: 7'
+ ) >testfileWithModule
+ (
+ echo 'module: 3'
+ echo 'module: 4'
+ echo 'module: 5'
+ echo 'module: 6'
+ echo 'module: 7'
+ ) >testfileWithModuleTruncated
+}
+
+function expectNoWrapOutput() {
+ if [ "$(cat output-wrap)" != '' ]; then
+ echo 'Wrap should not generate any output'
+ diff testfile output || true
+ return 1
+ fi
+}
+
+function expectSavedOutput() {
+ if ! diff testfile output; then
+ echo 'Should have saved the correct output'
+ diff testfile output || true
+ return 1
+ fi
+}
+
+function expectFullOutputWithModule() {
+ if ! diff testfileWithModule output-eval; then
+ echo 'Should have printed the full output'
+ diff testfileWithModule output || true
+ return 1
+ fi
+}
+
+function expectTruncatedOutputWithModule() {
+ if ! diff testfileWithModuleTruncated output-eval; then
+ echo 'Should have printed the truncated output'
+ diff testfileWithModuleTruncated output || true
+ return 1
+ fi
+}
+
+function whenWrap() {
+ "$WRAPPER" module "$PWD/output" "$PWD/retval" 'wrap' "$@" \
+ 2>/dev/null \
+ >output-wrap
+}
+
+function whenEval() {
+ "$WRAPPER" module "$PWD/output" "$PWD/retval" 'eval' "$@" \
+ >output-eval 2>&1
+}
+
+function testSuccessfulCase() {
+ withTestFiles
+ (
+ echo '#!/bin/bash'
+ echo
+ echo 'cat testfile'
+ ) >script.sh
+ chmod 755 script.sh
+ whenWrap "$PWD/script.sh"
+ expectNoWrapOutput
+ if ! whenEval; then
+ echo 'Should have run successfully'
+ return 1
+ fi
+ expectSavedOutput
+ expectTruncatedOutputWithModule
+}
+
+function testFailedCase() {
+ withTestFiles
+ (
+ echo '#!/bin/bash'
+ echo
+ echo 'cat testfile'
+ echo 'exit 1'
+ ) >script.sh
+ chmod 755 script.sh
+ whenWrap "$PWD/script.sh"
+ expectNoWrapOutput
+ if whenEval; then
+ echo 'Should have failed to run'
+ return 1
+ fi
+ expectSavedOutput
+ expectFullOutputWithModule
+}
+
+function testFailedSignalCase() {
+ withTestFiles
+ (
+ echo '#!/bin/bash'
+ echo
+ echo 'cat testfile'
+ echo 'kill -TERM $$'
+ echo 'echo Should not be printed'
+ ) >script.sh
+ chmod 755 script.sh
+ whenWrap "$PWD/script.sh"
+ expectNoWrapOutput
+ if whenEval; then
+ echo 'Should have failed to run'
+ return 1
+ fi
+ expectSavedOutput
+ expectFullOutputWithModule
+}
+
+function main() {
+ local result=0
+ local tmp="$(mktemp -d)"
+ for test_name in "${TEST_NAMES[@]}"; do
+ mkdir -p "$tmp/$test_name"
+ cd "$tmp/$test_name"
+ echo -n "Running $test_name..."
+ test"$test_name" >log || {
+ echo "FAILED";
+ sed -e "s/^/$test_name: /" <log
+ rm log
+ result=1;
+ continue;
+ }
+ echo "PASSED"
+ rm log
+ done
+ return "$result"
+}
+
+
+main "$@"