From dd17db2b2a60a22bf04008fc1ee40033b495bc22 Mon Sep 17 00:00:00 2001 From: Leonid Shevtsov Date: Tue, 19 Dec 2017 11:07:11 +0200 Subject: [PATCH 1/2] Add a Rake.application.running? predicate --- lib/rake/application.rb | 9 +++++++++ test/test_rake_application.rb | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/rake/application.rb b/lib/rake/application.rb index 8927d951b..35e4c1b56 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,13 @@ 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 + @running = false end # Initialize the command line parameters and app name. @@ -151,6 +154,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..8f0f066c5 100644 --- a/test/test_rake_application.rb +++ b/test/test_rake_application.rb @@ -634,4 +634,22 @@ def loader.make_dummy loader end + def test_running + 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 + end From 20776f141ed043f36757b8cb3f0a297acd6042cf Mon Sep 17 00:00:00 2001 From: Leonid Shevtsov Date: Wed, 2 May 2018 12:26:40 +0300 Subject: [PATCH 2/2] Ensure that Application#running? is unset after a task has failed --- lib/rake/application.rb | 1 + test/test_rake_application.rb | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/rake/application.rb b/lib/rake/application.rb index 35e4c1b56..21a055153 100644 --- a/lib/rake/application.rb +++ b/lib/rake/application.rb @@ -84,6 +84,7 @@ def run(argv = ARGV) load_rakefile top_level end + ensure @running = false end diff --git a/test/test_rake_application.rb b/test/test_rake_application.rb index 8f0f066c5..4b5fe9a71 100644 --- a/test/test_rake_application.rb +++ b/test/test_rake_application.rb @@ -634,7 +634,7 @@ def loader.make_dummy loader end - def test_running + def test_running_flag was_running = false @app.instance_eval do @@ -652,4 +652,19 @@ def test_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