Skip to content

Commit

Permalink
Add reload! to lotus console
Browse files Browse the repository at this point in the history
This patch adds a `reload!` method during `lotus console` sessions, much
like what is available during a `rails console` session. `reload!` will
simply call `Kernel.exec $0` to re-execute the running process after
outputting a message that it is doing so.

Signed-off-by: David Celis <me@davidcel.is>
  • Loading branch information
davidcelis committed Jul 10, 2014
1 parent 18786b3 commit 380ac3d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/lotus/commands/console.rb
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
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
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

0 comments on commit 380ac3d

Please sign in to comment.