Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

bats test cases won't stop #46

Closed
jzk opened this issue Mar 14, 2014 · 5 comments
Closed

bats test cases won't stop #46

jzk opened this issue Mar 14, 2014 · 5 comments

Comments

@jzk
Copy link

jzk commented Mar 14, 2014

I am newbie to bash shell programming. Just found this great tool thought give it a shot. one weird thing I met is when all the cases are done. It prints
"ok 1 xxxx
ok 2 xxx
ok 3 xxx"

and it just stops there. only ctrl+c can make it rest.

Some of cases are invoking shell scripts directly.
Would like to know is there anything need to know writing bash program so it can be tested by bats?
Thanks

@kernelp4nic
Copy link

Can you reproduce this behavior from outside bats? Calling your shellscripts directly from the command line?

@jzk
Copy link
Author

jzk commented Mar 15, 2014

nope, calling that script directly won't halt there. and weird thing is If I put invocation of that script in the second test case and make 1st and last one simple assertion. what happens is it prints ok 1, ok 2, and ok 3 and then pause there.

@jzk
Copy link
Author

jzk commented Mar 20, 2014

I just reproduced it with cleaner steps..
say I have an test cases like below

#!/usr/bin/env bats
@test "start Tomcat" {
  run bash -c "$TOMCAT_HOME/bin/catalina.sh start "
}

running $TOMCAT_HOME/bin/catalina.sh start directly from shell can return successfully.
But running this test case just hangs there after execution is done (Tomcat process is up and running already)

bats  --tap tomcat.bats
1..1
ok 1 start Tomcat

still trying to figure out what's wrong

@jzk
Copy link
Author

jzk commented Mar 20, 2014

Update:

I changed test cases to

#!/usr/bin/env bats
@test "start Tomcat" {
  run bash -ilc "$TOMCAT_HOME/bin/catalina.sh start "
}

Now it can exit successfully without hanging there. Anyone can explain this to me, thanks?

@mguillet
Copy link

I had a similar issue. I spend quite some time to figure out what was going on.

My setup is bit different but the test is the same: I'm trying to start a Java application leveraging a wrapper (http://wrapper.tanukisoftware.com/doc/english/download.jsp). This wrapper comes with a bash script to perform start, stop and restart operations on the java application.

I think this is related to a file descriptor not being closed. If you edit the bats bash file and transform the last line:

exec "$command" $count_flag $extended_syntax_flag "${filenames[@]}" | "$formatter"

to

 exec "$command" $count_flag $extended_syntax_flag "${filenames[@]}"

You'll loose the formatting but your test script will now exit successfully.

Bats is using the file descriptor 3, commands run by your test file will inherit this file descriptor (I think). If your catalina.sh script file is also using this file descriptor and/or transmit it to a process that stays alive, your bats file will not stop since the pipe to the bats formatter relates to non closed file descriptor. By the way, if you brutally kill Tomcat while your bats file is still running, bats will return successfully.

Sorry if this is not perfectly clear, I'm not an expert of these things and it's still a bit fuzzy to me.

Here is how I've solved it (applied to your example)

#!/usr/bin/env bats
@test "start Tomcat" {
  run bash -c "$TOMCAT_HOME/bin/catalina.sh start "  3>&-
}

The 3>&- closes the file descriptor 3 for the new bash your creating. One of your bash options -i or -l may have the same effect.

@sstephenson: do you think that bats should take care of closing file descriptor 3 (or not "transmitting it")?

yarikoptic pushed a commit to neurodebian/bats that referenced this issue Aug 6, 2019
yarikoptic added a commit to neurodebian/bats that referenced this issue Aug 6, 2019
Bats 1.1.0 - 2018-07-08

This is the first release with new features relative to the original Bats 0.4.0.

Added:
* The `-r, --recursive` flag to scan directory arguments recursively for
  `*.bats` files (sstephenson#109)
* The `contrib/rpm/bats.spec` file to build RPMs (sstephenson#111)

Changed:
* Travis exercises latest versions of Bash from 3.2 through 4.4 (sstephenson#116, sstephenson#117)
* Error output highlights invalid command line options (sstephenson#45, sstephenson#46, sstephenson#118)
* Replaced `echo` with `printf` (sstephenson#120)

Fixed:
* Fixed `BATS_ERROR_STATUS` getting lost when `bats_error_trap` fired multiple
  times under Bash 4.2.x (sstephenson#110)
* Updated `bin/bats` symlink resolution, handling the case on CentOS where
  `/bin` is a symlink to `/usr/bin` (sstephenson#113, sstephenson#115)

* tag 'v1.1.0': (198 commits)
  Bats 1.1.0
  bats: Replace echo with printf
  Extract `abort()` function
  travis: Remove `bats -c` wrapper
  travis: Enable build with default Linux image Bash
  Add Bash version test to Travis job.
  Revert "Re-add Bash version check to Docker image build"
  Re-add Bash version check to Docker image build
  Move timing test to Docker run for Linux jobs
  Remove version check from Docker image build
  Bash version via build matrix instead of script loop
  Fix merge error.
  Add return code storage for Bash version loop
  Add Bash version output during 'docker build'
  Clean up Docker image tags
  Add default value for Bash version
  Cover more Bash versions with Docker
  BATS_ROOT: Elide options to reset shell options
  BATS_ROOT: Restore comment noting issue sstephenson#113
  BATS_ROOT: Use `set -P`, remove `PWD` resolution
  ...
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants