Skip to content

Instantly share code, notes, and snippets.

@sage-git
Last active May 17, 2018 01:36
Show Gist options
  • Save sage-git/66c73e70e3c76dadef955a8d2464947f to your computer and use it in GitHub Desktop.
Save sage-git/66c73e70e3c76dadef955a8d2464947f to your computer and use it in GitHub Desktop.
fork process in a BATS test script
# Aimed to test running script status.
# In this script, check the sleep process which is forked during a test script.
@test "count_process_extshell" {
echo "#!/bin/bash" > fork_shell.sh
echo "" >> fork_shell.sh
echo "sleep 1 &" >> fork_shell.sh
echo "ps ax | grep -E \"[0-9] sleep\" | wc -l" >> fork_shell.sh
ret=$(bash fork_shell.sh)
[ "$ret" = "1" ]
rm fork_shell.sh
}
@test "count_process_in_bats" {
sleep 1 &
ret=$(ps ax | grep -E "[0-9] sleep" | wc -l)
[ "$ret" = "1" ]
wait
}
@test "count_process_wait" {
sleep 1 &
ret=$(ps ax | grep -E "[0-9] sleep" | wc -l)
[ "$ret" = "1" ]
wait
ret=$(ps ax | grep -E "[0-9] sleep" | wc -l)
[ "$ret" = "0" ]
}
@test "count_process_kill" {
sleep 1 &
psnum=$(ps ax | grep -E "[0-9] sleep" | awk '{print $1}')
ret=$(ps ax | grep -E "[0-9] sleep" | wc -l)
[ "$ret" = "1" ]
kill $psnum
ret=$(ps ax | grep -E "[0-9] sleep" | wc -l)
[ "$ret" = "0" ]
wait
}
@sage-git
Copy link
Author

$ cat fork_shell.sh
#!/bin/bash

sleep 1 &
ps ax | grep -E "[0-9] sleep" | wc -l

@sage-git
Copy link
Author

$ bats -t fork_in_test.bats
1..4
ok 1 count_process_extshell
ok 2 count_process_in_bats
ok 3 count_process_wait
ok 4 count_process_kill

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment