# Test/unit autorun fails to set exit status on failure

**URL:** <https://www.ruby-forum.com/t/test-unit-autorun-fails-to-set-exit-status-on-failure/104840>\
**Category:** Ruby\
**Created:** [May 23, 2007, 4:40am UTC](https://www.ruby-forum.com/t/test-unit-autorun-fails-to-set-exit-status-on-failure/104840 "2007-05-23T04:40:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [May 23, 2007, 4:40am UTC](https://www.ruby-forum.com/t/test-unit-autorun-fails-to-set-exit-status-on-failure/104840/1 "2007-05-23T04:40:59Z")

</div>

Consider the elementary unit test that fails…

=try.rb===============================================================  
require ‘test/unit’

# class TC\_OurTest \< Test::Unit::TestCase def test\_fail assert(false, ‘Assertion was false.’) end end

What is its exit status?

ruby -w try.rb  
echo $?  
0

If I look in  
/usr/local/lib/ruby/1.8/test/unit.rb  
I see at the end the following code…

* * *

## at\_exit do unless $! || Test::Unit.run? exit Test::Unit::AutoRunner.run end end

Now try this chunk of code…

==atexittoolate.rb====================================================

at\_exit do  
exit( 1)  
end

======================================================================  
ruby -w atexittoolate.rb;echo $?  
0

If I change that to…  
==atexittoolate.rb====================================================

# at\_exit do exit( 1) end exit(2)

ruby -w atexittoolate.rb;echo $?  
2

Bottom line: at\_exit seems to be too late (Under ruby-1.8.6 linux  
anyway) to set the exit status.

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [May 23, 2007, 5:25am UTC](https://www.ruby-forum.com/t/test-unit-autorun-fails-to-set-exit-status-on-failure/104840/2 "2007-05-23T05:25:01Z")

</div>

On 5/22/07, John C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Consider the elementary unit test that fails…
> 
> > 
> 
> Bottom line: at\_exit seems to be too late (Under ruby-1.8.6 linux  
> anyway) to set the exit status.

Hmmm …

$ ruby -v  
ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-cygwin]

$ cat tmp.rb  
at\_exit do  
puts “Calling exit handler”  
exit 2  
end

exit

$ ruby tmp.rb  
Calling exit handler

$ echo $?  
2

There is nothing in eval.c that prevents the at\_exit procs from  
calling exit again with a new status code. When the at\_exit proc is  
called, it is first popped off the stack and then run. When it calls  
exit at the end, the whole process starts again, and the next at\_exit  
proc is called which, too, can call exit with a new status code.

The status passed to the last exit call is the one that is returned.

Is there another at\_exit proc that is registered before the Test::Unit  
at\_ext (and therefore is called afterwards) that is setting the exit  
code to zero?

Now, this is all from the 1.8.5 source code. Perhaps something changed  
in 1.8.6 – but that seems a large-ish change.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [May 23, 2007, 5:47am UTC](https://www.ruby-forum.com/t/test-unit-autorun-fails-to-set-exit-status-on-failure/104840/3 "2007-05-23T05:47:03Z")

</div>

On 5/22/07, Tim P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now, this is all from the 1.8.5 source code. Perhaps something changed  
> in 1.8.6 – but that seems a large-ish change.

I think this is a bug in 1.8.6:  
[http://rubyforge.org/tracker/?func=detail&atid=1698&aid=9300&group\_id=426](http://rubyforge.org/tracker/?func=detail&atid=1698&aid=9300&group_id=426)

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [May 23, 2007, 6:15am UTC](https://www.ruby-forum.com/t/test-unit-autorun-fails-to-set-exit-status-on-failure/104840/4 "2007-05-23T06:15:05Z")

</div>

On 5/22/07, Gregory B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 5/22/07, Tim P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Now, this is all from the 1.8.5 source code. Perhaps something changed  
> > in 1.8.6 – but that seems a large-ish change.
> 
> I think this is a bug in 1.8.6:  
> [http://rubyforge.org/tracker/?func=detail&atid=1698&aid=9300&group\_id=426](http://rubyforge.org/tracker/?func=detail&atid=1698&aid=9300&group_id=426)

Thanks for the link. Nobu checked in a fix for this bug. Should be  
available in the next maintenance release.

TwP
