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 reload! to lotus console #46

Merged
merged 1 commit into from
Jul 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/lotus/commands/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
module Lotus
module Commands
class Console
module Methods
def reload!
puts 'Reloading...'
Kernel.exec $0
end
end

ENGINES = {
'pry' => 'Pry',
'ripl' => 'Ripl',
Expand All @@ -20,6 +27,9 @@ def start
ARGV.shift until ARGV.empty?
require File.expand_path(options[:applications], Dir.pwd)

# Add convenience methods to the main:Object binding
TOPLEVEL_BINDING.eval('self').send(:include, Methods)

engine.start
end

Expand All @@ -28,6 +38,7 @@ def engine
end

private

def engine_lookup
(ENGINES.find {|_, klass| Object.const_defined?(klass) } || default_engine).first
end
Expand Down
54 changes: 54 additions & 0 deletions test/commands/console_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,58 @@ def remove_engine(engine)
end
end
end

describe 'convenience methods' do
before do
@old_main = TOPLEVEL_BINDING
@main = Minitest::Mock.new
Lotus::Utils::IO.silence_warnings { TOPLEVEL_BINDING = @main }
@main.expect(:eval, TOPLEVEL_BINDING, ['self'])

@engine = Minitest::Mock.new
@engine.expect(:start, nil)
end

after do
Lotus::Utils::IO.silence_warnings { TOPLEVEL_BINDING = @old_main }
end

it 'mixes convenience methods into the TOPLEVEL_BINDING' do
@main.expect(:include, true, [Lotus::Commands::Console::Methods])

opts[:applications] = 'test/fixtures/microservices/config/applications'
console.stub(:engine, @engine) { console.start }

@engine.verify
@main.verify
Copy link
Member

Choose a reason for hiding this comment

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

Also @engine.verify

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah right!

end
end
end

describe Lotus::Commands::Console::Methods do
describe '#reload!' do
before do
@binding = Class.new
@binding.send(:include, Lotus::Commands::Console::Methods)

@old_kernel = Kernel
Lotus::Utils::IO.silence_warnings { Kernel = Minitest::Mock.new }
Kernel.expect(:exec, true, [$0])
end

after do
Lotus::Utils::IO.silence_warnings { Kernel = @old_kernel }
end

it 're-executes the running process' do
begin
$stdout = StringIO.new
@binding.new.reload!
ensure
$stdout = STDOUT
end

Kernel.verify
end
end
end