Ben Biddington

Whatever it is, it's not about "coding"

Posts Tagged ‘software-development

Rake — shell task failure

leave a comment »

For my current project, we’re running cucumber as one of our tasks. Our report generation runs afterwards — which we need to happen regardless of cucumber’s exit code (cucumber exits with code 1 when any tests fail).

Handling shell task failure

We noticed that if cucumber fails, rake exits immediately — without running our subsequent report tasks.

We’re running cucumber with rake’s ruby function — which runs the ruby interpreter in its own process by invoking a shell command.

Pseudo-stacktrace (irrelevant source lines omitted):

Shell::CommandProcessor.system(command, *opts)
Rake::Win32.rake_system(*cmd)*args, &block)
FileUtils.ruby(*args, &block)

The key to this is the behaviour of rake’s Examining the source shows:

If it is not supplied a block, and the shell command fails, and rake exits with error status.

Aside: For some reason, Kernel.system echoes errors.


This task will cause rake to exit immediately with status 1:

task 'this-one-fails' do

while the following task does not cause rake to terminate  normally, any subsequent task will still run:

path = File.expand_path(File.dirname(__FILE__) + ‘/output/cucumber-manual.txt’)
cuke_path = ‘C:/ruby/lib/ruby/gems/1.8/gems/cucumber-0.3.3/bin/cucumber’
ruby(“#{cuke_path} -h > ‘#{path}'”) do |success, exit_code|
puts “Exited with code: #{exit_code}” if !success
task 'this-one-succeeds' do
    ruby("xxx") do |success, exit_code|
        puts "Exited with code: #{exit_code.exitstatus}" unless success

Getting the last exit status

The $? variable contains the status of the last child process to terminate. This may be useful if steps subsequent to the failed one require this information.

Written by benbiddington

16 May, 2009 at 16:45

TeamCity, Rake and shell commands

leave a comment »

We encountered an interesting problem today. while getting automated acceptance tests running in TeamCity.

Why Rake? :

  • Rakefiles (rake‘s version of Makefiles) are completely defined in standard Ruby syntax. No XML files to edit. No quirky Makefile syntax to worry about (is that a tab or a space?)
  • Users can specify tasks with prerequisites.
  • Rake supports rule patterns to synthesize implicit tasks.
  • Flexible FileLists that act like arrays but know about manipulating file names and paths.
  • A library of prepackaged tasks to make building rakefiles easier.

What are we using it for?

Running cucumber.

Our rake file consists of a single task instructing the ruby interpreter to run cucumber with a set of fixtures — simple.

What were the symptoms?

We found that our build would “fail” as soon as we issued the shell command. The rest of the process works as expected — cucumber runs all the test and we get the results logged.

Finally we tracked this down to unexpected behaviour in the TeamCity rake runner.

What was the cause?

The TeamCity rake runner is designed to capture all output from rake, and redirect it to the appropriate TeamCity log.

The problem has arisen because we are issuing a shell command using Notice it’s calling rake_output_message, which does just this by default:

# Send the message to the default rake output (which is $stderr).
def rake_output_message(message)

The TeamCity rake runner has overridden this in an attempt to capture these messages. For some reason this implementation sends an error message to TeamCity.

This means that all shell commands fail when running under the TeamCity rake runner.

Why does rake write to $stderr by default?

Don’t know. And the method name is not very evocative: rake_output_message.

Perhaps it’s intended to be overridden.

EDIT, 07-05-2009: All of these issues have been resolved by upgrading to latest version — even the teamcity-deletes-rake-file wrongness.


Written by benbiddington

30 April, 2009 at 11:59