diff --git a/lib/rake/application.rb b/lib/rake/application.rb index 8927d951b..21a055153 100644 --- a/lib/rake/application.rb +++ b/lib/rake/application.rb @@ -56,6 +56,7 @@ def initialize @loaders = {} @default_loader = Rake::DefaultLoader.new @original_dir = Dir.pwd + @running = false @top_level_tasks = [] add_loader("rb", DefaultLoader.new) add_loader("rf", DefaultLoader.new) @@ -77,11 +78,14 @@ def initialize # +init+ on your application. Then define any tasks. Finally, # call +top_level+ to run your top level tasks. def run(argv = ARGV) + @running = true standard_exception_handling do init "rake", argv load_rakefile top_level end + ensure + @running = false end # Initialize the command line parameters and app name. @@ -151,6 +155,12 @@ def thread_pool # :nodoc: @thread_pool ||= ThreadPool.new(options.thread_pool_size || Rake.suggested_thread_count-1) end + # Is true, if the Rake application is currently running + # (in other words, that the +rake+ command line script was invoked) + def running? + @running + end + # internal ---------------------------------------------------------------- # Invokes a task with arguments that are extracted from +task_string+ diff --git a/test/test_rake_application.rb b/test/test_rake_application.rb index d17445c7e..4b5fe9a71 100644 --- a/test/test_rake_application.rb +++ b/test/test_rake_application.rb @@ -634,4 +634,37 @@ def loader.make_dummy loader end + def test_running_flag + was_running = false + + @app.instance_eval do + app = self + intern(Rake::Task, "default").enhance do + was_running = app.running? + end + end + + rakefile_default + + assert !@app.running? + @app.run %w[--rakelib=""] + assert !@app.running? + assert was_running + end + + def test_running_flag_false_after_abort + @app.instance_eval do + intern(Rake::Task, "default").enhance do + abort "Task execution failed" + end + end + + rakefile_default + + assert_raises(SystemExit) { + @app.run %w[--rakelib=""] + } + + assert !@app.running? + end end