Skip to content

Commit

Permalink
reordered completion loading order, added Bond.load_gems
Browse files Browse the repository at this point in the history
  • Loading branch information
cldwalker committed May 17, 2010
1 parent 354bed0 commit df11d0f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
11 changes: 7 additions & 4 deletions lib/bond.rb
Expand Up @@ -74,10 +74,10 @@ def spy(*args); M.spy(*args); end
# an incorrect completion. Default is false.
def config; M.config; end

# Starts Bond with a default set of completions that replace and improve irb's completion. Loads completion
# files in the following order: optional :gems completions, lib/bond/completion.rb, optional ~/.bondrc,
# lib/bond/completions/*.rb, optional ~/.bond/completions/*.rb and optional block. See Rc for the DSL to use
# in completion files and in the block. Options are Bond.config keys and the following:
# Starts Bond with a default set of completions that replace and improve irb's completion. Loads completions
# in this order: lib/bond/completion.rb, lib/bond/completions/*.rb and the following optional
# completions: gem completions from :gems, ~/.bondrc, ~/.bond/completions/*.rb and from block. See Rc for
# the DSL to use in completion files and in the block. Valid options are Bond.config keys and the following:
# [*:gems*] Array of gems which have their completions loaded from lib/bond/completions/#{gem}.rb.
# ==== Examples:
# Bond.start :gems=>%w{hirb}
Expand All @@ -86,6 +86,9 @@ def config; M.config; end
# end
def start(options={}, &block); M.start(options, &block); end

# Loads completions for gems that ship with them at lib/bond/completions/#{gem}.rb.
def load_gems(*gems); M.load_gems(*gems); end

# An Agent who saves all Bond.complete missions and executes the correct one when a completion is called.
def agent; M.agent; end

Expand Down
21 changes: 13 additions & 8 deletions lib/bond/m.rb
Expand Up @@ -57,28 +57,32 @@ def debrief(options={})
# See Bond.start
def start(options={}, &block)
debrief options
Array(options[:gems]).each {|e| load_gem_completion(e) }
load_completions
Rc.module_eval(&block) if block
true
end

def load_gem_completion(rubygem)
(file = find_gem_file(rubygem, File.join('bond', 'completions', "#{rubygem}.rb"))) &&
load_file(file)
def load_gem_completion(rubygem) #:nodoc:
(file = find_gem_file(rubygem, File.join('bond', 'completions', "#{rubygem}.rb"))) ?
load_file(file) : $stderr.puts("Bond Error: No completions found for gem '#{rubygem}'")
end

# Finds the full path to a gem's file relative it's load path directory. Returns nil if not found.
def find_gem_file(rubygem, file)
begin gem(rubygem); rescue Exception; end
(dir = $:.find {|e| File.exists?(File.join(e, file)) }) && File.join(dir, file)
end

def load_gems(*gems) #:nodoc:
gems.select {|e| load_gem_completion(e) }
end

def load_completions #:nodoc:
load_file File.join(File.dirname(__FILE__), 'completion.rb')
load_dir File.dirname(__FILE__)
load_gems *Array(config[:gems])
load_file(File.join(home,'.bondrc')) if File.exists?(File.join(home, '.bondrc'))
[File.dirname(__FILE__), File.join(home, '.bond')].each do |base_dir|
load_dir(base_dir)
end
load_dir File.join(home, '.bond')
end

# Loads a completion file in Rc namespace.
Expand All @@ -88,7 +92,8 @@ def load_file(file)
$stderr.puts "Bond Error: Completion file '#{file}' failed to load with:", e.message
end

def load_dir(base_dir) #:nodoc:
# Loads completion files in given directory.
def load_dir(base_dir)
if File.exists?(dir = File.join(base_dir, 'completions'))
Dir[dir + '/*.rb'].each {|file| load_file(file) }
end
Expand Down
54 changes: 35 additions & 19 deletions test/bond_test.rb
Expand Up @@ -35,28 +35,44 @@ def start(options={}, &block)
tab('blah all').should == ["all_quiet"]
end

describe "with :gems" do
it "attempts to load gem" do
M.expects(:gem).twice
start(:gems=>%w{one two})
end
after_all { M.debrief :readline_plugin=>valid_readline_plugin; M.reset }
end

describe "start with :gems" do
before {
M.stubs(:load_dir)
File.stubs(:exists?).returns(true)
}
it "attempts to load gem" do
M.stubs(:load_file)
M.expects(:gem).twice
start(:gems=>%w{one two})
end

it "rescues nonexistent gem" do
M.expects(:gem).raises(LoadError)
should.not.raise { start(:gems=>%w{blah}) }
end
it "rescues nonexistent gem" do
M.stubs(:load_file)
M.expects(:gem).raises(LoadError)
should.not.raise { start(:gems=>%w{blah}) }
end

it "rescues nonexistent method 'gem'" do
M.stubs(:load_file)
M.expects(:gem).raises(NoMethodError)
should.not.raise { start(:gems=>%w{blah}) }
end

it "rescues nonexistent method 'gem'" do
M.expects(:gem).raises(NoMethodError)
should.not.raise { start(:gems=>%w{blah}) }
end
it "prints error if gem completion not found" do
M.stubs(:load_file)
M.expects(:find_gem_file).returns(nil)
capture_stderr { start(:gems=>%w{invalid}) }.should =~ /No completions.*'invalid'/
end

it "loads completion file" do
M.expects(:gem)
File.expects(:exists?).returns(true)
File.expects(:read).with(File.join($:[0], 'bond', 'completions', 'awesome.rb')).returns('')
start(:gems=>%w{awesome})
end
it "loads gem completion file" do
File.expects(:read).returns('')
File.expects(:read).returns('')
File.expects(:read).with(File.join($:[0], 'bond', 'completions', 'awesome.rb')).returns('')
M.expects(:gem)
start(:gems=>%w{awesome})
end
after_all { M.debrief :readline_plugin=>valid_readline_plugin; M.reset }
end
Expand Down

0 comments on commit df11d0f

Please sign in to comment.