Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Rake.application.running? predicate #243

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/rake/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Copy link
Member

@yuki24 yuki24 Dec 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be wrapped with an ensure block:

def run(...)
  ..
ensure
  @running = false
end

end

# Initialize the command line parameters and app name.
Expand Down Expand Up @@ -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+
Expand Down
33 changes: 33 additions & 0 deletions test/test_rake_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test case where the task execution fails but it correctly sets @running back to false after failure?

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