Re: exit call in script causes unit test to not run (newby)



2007/11/20, weathercoach@xxxxxxxxx <weathercoach@xxxxxxxxx>:
Hello.
I've been grappling with unit testing for a couple weeks now. I'm
a bourne shell scripter by day who is trying to expand my skill
set. I currently have a ruby script which I'm trying to create some
unit tests for. The unit tests have not been running and I've
finally traced the problem to this method. Specifically the "exit"
call. If I comment out the exit then my tests actually run.

# Cleanup any transient files and directories and then exit
def script_clean
FileUtils.rm_rf(WORKDIR) if File.exists?(WORKDIR)
# Everything is tidy we should leave
exit
end

The above method is called in 2 different scenarios. The first case
is during script execution if any Exceptions occur. Then the error
message is printed and the script_clean method is called to ensure the
process exits.

Here is an example of where the method is called

begin
File.open(ADMIN_FILE, "w+") {|f| f << ADMIN_CONTENTS }
rescue Exception => e
puts e ; script_clean
end

Additionally if the script runs through to the end without
encountering any errors then the last line is a call to the
script_clean method

script_clean

My tests are not triggering any Exceptions so it appears to be the
last line which calls "script_clean" to hamper any test execution.
Explicitly exiting when an uncorrectable error has happened is
something I commonly do in bourne shell scripts and it made sense to
carry over the practice in ruby.

I disagree. Ruby and Bourne Shell are two completely different
languages. Especially shell does not have exception handling. I
would especially not exit from unit tests but raise an exception.
Even if you use exit you can make sure your cleanup code is run by
placing it in END or a global "ensure" clause:

10:46:02 /cygdrive/c/SCMws/RKlemme/OPSC_Gold_bas_dev_R1.1.5_s11_pp/bas
$ ruby -e 'END{puts 1}; exit 2'
1
10:46:05 /cygdrive/c/SCMws/RKlemme/OPSC_Gold_bas_dev_R1.1.5_s11_pp/bas
$ ruby -e 'begin; exit 2;ensure puts 1; end'
1

Should I be terminating premature
and normal script execution in some other fashion that is more
compatible with test/unit.

Yes, raise an exception. Btw, exit does actually throw an exception
but maybe that's not caught by the test framework.

$ ruby -e 'begin; exit 2; rescue Exception => e; p e, e.class,
e.class.ancestors; end'
#<SystemExit: exit>
SystemExit
[SystemExit, Exception, Object, Kernel]

You usually do not want your complete test suite to terminate when a
single test fails.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

.



Relevant Pages

  • exit call in script causes unit test to not run (newby)
    ... I currently have a ruby script which I'm trying to create some ... If I comment out the exit then my tests actually run. ... something I commonly do in bourne shell scripts and it made sense to ... # Set PROTO constant based on command line arg ...
    (comp.lang.ruby)
  • Re: How to exit Ruby program properly
    ... The fact that it raises an exception is noted to allow other ... If you REALLY want to exit NOW, you can use Process.exit!, but generally ... How to exit Ruby program properly ... > What is the best way to exit from my .rb script? ...
    (comp.lang.ruby)
  • Re: How to exit Ruby program properly
    ... What is the best way to exit from my .rb script? ... I'm using Process.exit but the documentation implies that it will ... throw an exception. ...
    (comp.lang.ruby)
  • Re: Remove duplicate files [POSIX script]
    ... I added in a hurry before I posted the script. ... In addition to the algorithmic complexity one should also note that ...
    (comp.unix.shell)
  • Re: RFD: How To Recognize Bad Javascript Code
    ... error in the contents of one script element does not interfere with the successful interpretation/compilation of syntax error free code in other script elements within the same document. ... So any residual compatibility issue is really just in relation to very old web browsers; mostly the forth generation of browsers and their antecedents. ... javascript's try/catch construct is nearly totally useless for various reasons. ... In Java (and other languages with good exception handling support) you can be very specific about which exceptions you catch and handle. ...
    (comp.lang.javascript)