James Lemieux | 2a2559e | 2018-01-28 02:56:27 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Tests for wrapper.sh. |
| 4 | |
| 5 | set -euo pipefail |
| 6 | |
| 7 | # The location of the script under test. |
| 8 | readonly WRAPPER="$(realpath "$(dirname "$0")/wrapper.sh")" |
| 9 | # The name of the tests to run. Each test correspond to a function in this file |
| 10 | # whose name is the name of the test prefixed by 'test'. |
| 11 | readonly TEST_NAMES=( |
| 12 | SuccessfulCase |
| 13 | FailedCase |
| 14 | FailedSignalCase |
| 15 | ) |
| 16 | |
| 17 | # Fails with an error message. |
| 18 | function fatal() { |
| 19 | echo 1>&2 "FATAL: $@" |
| 20 | exit 113 |
| 21 | } |
| 22 | |
| 23 | function withTestFiles() { |
| 24 | ( |
| 25 | echo '1' |
| 26 | echo '2' |
| 27 | echo '3' |
| 28 | echo '4' |
| 29 | echo '5' |
| 30 | echo '6' |
| 31 | echo '7' |
| 32 | ) >testfile |
| 33 | ( |
| 34 | echo 'module: 1' |
| 35 | echo 'module: 2' |
| 36 | echo 'module: 3' |
| 37 | echo 'module: 4' |
| 38 | echo 'module: 5' |
| 39 | echo 'module: 6' |
| 40 | echo 'module: 7' |
| 41 | ) >testfileWithModule |
| 42 | ( |
| 43 | echo 'module: 3' |
| 44 | echo 'module: 4' |
| 45 | echo 'module: 5' |
| 46 | echo 'module: 6' |
| 47 | echo 'module: 7' |
| 48 | ) >testfileWithModuleTruncated |
| 49 | } |
| 50 | |
| 51 | function expectNoWrapOutput() { |
| 52 | if [ "$(cat output-wrap)" != '' ]; then |
| 53 | echo 'Wrap should not generate any output' |
| 54 | diff testfile output || true |
| 55 | return 1 |
| 56 | fi |
| 57 | } |
| 58 | |
| 59 | function expectSavedOutput() { |
| 60 | if ! diff testfile output; then |
| 61 | echo 'Should have saved the correct output' |
| 62 | diff testfile output || true |
| 63 | return 1 |
| 64 | fi |
| 65 | } |
| 66 | |
| 67 | function expectFullOutputWithModule() { |
| 68 | if ! diff testfileWithModule output-eval; then |
| 69 | echo 'Should have printed the full output' |
| 70 | diff testfileWithModule output || true |
| 71 | return 1 |
| 72 | fi |
| 73 | } |
| 74 | |
| 75 | function expectTruncatedOutputWithModule() { |
| 76 | if ! diff testfileWithModuleTruncated output-eval; then |
| 77 | echo 'Should have printed the truncated output' |
| 78 | diff testfileWithModuleTruncated output || true |
| 79 | return 1 |
| 80 | fi |
| 81 | } |
| 82 | |
| 83 | function whenWrap() { |
| 84 | "$WRAPPER" module "$PWD/output" "$PWD/retval" 'wrap' "$@" \ |
| 85 | 2>/dev/null \ |
| 86 | >output-wrap |
| 87 | } |
| 88 | |
| 89 | function whenEval() { |
| 90 | "$WRAPPER" module "$PWD/output" "$PWD/retval" 'eval' "$@" \ |
| 91 | >output-eval 2>&1 |
| 92 | } |
| 93 | |
| 94 | function testSuccessfulCase() { |
| 95 | withTestFiles |
| 96 | ( |
| 97 | echo '#!/bin/bash' |
| 98 | echo |
| 99 | echo 'cat testfile' |
| 100 | ) >script.sh |
| 101 | chmod 755 script.sh |
| 102 | whenWrap "$PWD/script.sh" |
| 103 | expectNoWrapOutput |
| 104 | if ! whenEval; then |
| 105 | echo 'Should have run successfully' |
| 106 | return 1 |
| 107 | fi |
| 108 | expectSavedOutput |
| 109 | expectTruncatedOutputWithModule |
| 110 | } |
| 111 | |
| 112 | function testFailedCase() { |
| 113 | withTestFiles |
| 114 | ( |
| 115 | echo '#!/bin/bash' |
| 116 | echo |
| 117 | echo 'cat testfile' |
| 118 | echo 'exit 1' |
| 119 | ) >script.sh |
| 120 | chmod 755 script.sh |
| 121 | whenWrap "$PWD/script.sh" |
| 122 | expectNoWrapOutput |
| 123 | if whenEval; then |
| 124 | echo 'Should have failed to run' |
| 125 | return 1 |
| 126 | fi |
| 127 | expectSavedOutput |
| 128 | expectFullOutputWithModule |
| 129 | } |
| 130 | |
| 131 | function testFailedSignalCase() { |
| 132 | withTestFiles |
| 133 | ( |
| 134 | echo '#!/bin/bash' |
| 135 | echo |
| 136 | echo 'cat testfile' |
| 137 | echo 'kill -TERM $$' |
| 138 | echo 'echo Should not be printed' |
| 139 | ) >script.sh |
| 140 | chmod 755 script.sh |
| 141 | whenWrap "$PWD/script.sh" |
| 142 | expectNoWrapOutput |
| 143 | if whenEval; then |
| 144 | echo 'Should have failed to run' |
| 145 | return 1 |
| 146 | fi |
| 147 | expectSavedOutput |
| 148 | expectFullOutputWithModule |
| 149 | } |
| 150 | |
| 151 | function main() { |
| 152 | local result=0 |
| 153 | local tmp="$(mktemp -d)" |
| 154 | for test_name in "${TEST_NAMES[@]}"; do |
| 155 | mkdir -p "$tmp/$test_name" |
| 156 | cd "$tmp/$test_name" |
| 157 | echo -n "Running $test_name..." |
| 158 | test"$test_name" >log || { |
| 159 | echo "FAILED"; |
| 160 | sed -e "s/^/$test_name: /" <log |
| 161 | rm log |
| 162 | result=1; |
| 163 | continue; |
| 164 | } |
| 165 | echo "PASSED" |
| 166 | rm log |
| 167 | done |
| 168 | return "$result" |
| 169 | } |
| 170 | |
| 171 | |
| 172 | main "$@" |