|
5 | 5 | # These functions make it easier to write Bats test cases that validate the
|
6 | 6 | # behavior of long-running processes such as servers:
|
7 | 7 | #
|
8 |
| -# @test '$SUITE: my-server should start successfully' { |
| 8 | +# @test "$SUITE: my-server should start successfully" { |
9 | 9 | # skip_if_missing_background_utilities
|
10 | 10 | # run_in_background 'my-server'
|
11 | 11 | # wait_for_background_output 'my-server is now ready'
|
12 | 12 | # stop_background_run
|
13 | 13 | # assert_...
|
14 | 14 | # }
|
15 | 15 | #
|
| 16 | +# @test "$SUITE: my-test-script should start successfully" { |
| 17 | +# skip_if_missing_background_utilities |
| 18 | +# run_in_test_script_in_background 'my-test-script' \ |
| 19 | +# 'sleep 1' \ |
| 20 | +# 'echo "Hello, World!"' \ |
| 21 | +# 'sleep 10' |
| 22 | +# wait_for_background_output 'Hello, World!' |
| 23 | +# stop_background_run |
| 24 | +# assert_... |
| 25 | +# } |
| 26 | +# |
16 | 27 | # Call `skip_if_missing_background_utilities` at the beginning of each test case
|
17 | 28 | # to skip it if the host system lacks any of the process management utilities
|
18 | 29 | # required by the functions from this file.
|
@@ -64,7 +75,18 @@ run_in_background() {
|
64 | 75 | export BATS_BACKGROUND_RUN_OUTPUT
|
65 | 76 | BATS_BACKGROUND_RUN_OUTPUT="$BATS_TEST_ROOTDIR/background-run-output.txt"
|
66 | 77 | printf '' >"$BATS_BACKGROUND_RUN_OUTPUT"
|
67 |
| - "$@" >"$BATS_BACKGROUND_RUN_OUTPUT" 2>&1 & |
| 78 | + |
| 79 | + # Bats duplicates standard output as file descriptor 3 so that output from its |
| 80 | + # framework functions isn't captured along with any output from the code under |
| 81 | + # test. If the code under test contains a `sleep` or other blocking operation, |
| 82 | + # this file descriptor will be held open until the process becomes unblocked, |
| 83 | + # preventing Bats from exiting. Hence, we explicitly close file descriptor 3. |
| 84 | + # |
| 85 | + # Any other code running under Bats that opens a background process should |
| 86 | + # close this file descriptor as well. See: |
| 87 | + # - https://github.com/sstephenson/bats/issues/80 |
| 88 | + # - https://github.com/mbland/go-script-bash/issues/226 |
| 89 | + "$@" >"$BATS_BACKGROUND_RUN_OUTPUT" 2>&1 3>&- & |
68 | 90 | export BATS_BACKGROUND_RUN_PID="$!"
|
69 | 91 | restore_bats_shell_options
|
70 | 92 | }
|
|
0 commit comments