Skip to content

Commit

Permalink
Add API fallback to web command
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusRich committed Aug 13, 2019
1 parent 467205c commit c9e19cd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
21 changes: 19 additions & 2 deletions lib/rubygems/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ def open_page(gem, options)
begin
spec = Gem::Specification.find_by_name(gem)
rescue Gem::MissingSpecError => e
puts e.message
exit 1
spec = fetch_remote_spec(gem)

if spec.nil?
puts e.message
puts "Could not find '#{gem}' in rubygems.org too."
return
end
end

if options[:sourcecode]
Expand All @@ -26,6 +31,18 @@ def open_page(gem, options)
end
end

def fetch_remote_spec(gem)
dep = Gem::Dependency.new gem
found, _ = Gem::SpecFetcher.fetcher.spec_for_dependency dep
spec_tuple = found.first

if spec_tuple.nil? || spec_tuple.empty?
nil
else
spec_tuple.first
end
end

def get_info_from_metadata(spec, info)
uri = spec.metadata[info]

Expand Down
57 changes: 53 additions & 4 deletions test/rubygems/test_gem_commands_web_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,61 @@ def test_open_rubygems

def test_search_unexisting_gem
gem = "this-is-an-unexisting-gem"
assert_raises(SystemExit) do
assert_output(/Could not find '#{gem}'/) do
@cmd.handle_options [gem]
@cmd.execute
assert_output(/Could not find '#{gem}'/) do
@cmd.handle_options [gem]
@cmd.execute
end
end

def test_search_online_if_gem_is_not_installed
gem = "this-is-an-unexisting-gem"
exception = proc { raise Gem::MissingSpecError.new("error", nil) }
spec = MiniTest::Mock.new
spec.expect(:homepage, "https://bestgemever.example.io")
spec.expect(:nil?, false)

Gem::Specification.stub :find_by_name, exception do
@cmd.executor.stub :fetch_remote_spec, spec do
assert_output("https://bestgemever.example.io\n") do
@cmd.handle_options [gem]
@cmd.execute
end
end
end

spec.verify
end

def test_search_online_for_inexisting_gem
gem = "this-is-an-unexisting-gem"
exception = proc { raise Gem::MissingSpecError.new("error", nil) }

Gem::Specification.stub :find_by_name, exception, [gem] do
@cmd.executor.stub :fetch_remote_spec, nil do
assert_output(/Could not find '#{gem}' in rubygems.org too./) do
@cmd.handle_options [gem]
@cmd.execute
end
end
end
end

def test_fetch_remote_spec
found_spec = [[[util_spec(@gem), "sources"]], []]

Gem::SpecFetcher.fetcher.stub :spec_for_dependency, found_spec do
spec = @cmd.executor.fetch_remote_spec @gem
assert_equal @gem, spec.name
end
end

def test_fetch_unexisting_remote_spec
gem = "this-is-an-unexisting-gem"
not_found = [[], []]

Gem::SpecFetcher.fetcher.stub :spec_for_dependency, not_found do
assert_nil @cmd.executor.fetch_remote_spec gem
end
end

def test_open_browser_if_env_variable_is_set
Expand Down

0 comments on commit c9e19cd

Please sign in to comment.