Skip to content

Commit

Permalink
Updates open default browser commands
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusRich committed Jul 7, 2019
1 parent 7a2507c commit 290f241
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 38 deletions.
54 changes: 26 additions & 28 deletions lib/rubygems/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,49 @@ module Gem
module Web
class Executor

def open_default_browser_cmd(local_os, version)
def open_default_browser_cmd(local_os, version, uri)
case local_os
when 'aix'
'defaultbrowser'
"defaultbrowser #{uri}"
when 'cygwin'
'cygstart'
"cygstart #{uri}"
when 'darwin'
'open'
"open #{uri}"
when 'macruby'
'open'
"open #{uri}"
when 'freebsd'
'xdg-open'
"xdg-open #{uri}"
when 'hpux'
''
""
when 'java'
''
"if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {" \
"Desktop.getDesktop().browse(new URI(#{uri}));" \
"}"
when 'dalvik'
''
""
when 'dotnet'
''
"System.Diagnostics.Process.Start(#{uri});"
when 'linux'
'xdg-open'
"xdg-open #{uri}"
when 'mingw32'
'start'
"start #{uri}"
when 'netbsdelf'
'xdg-open'
"xdg-open #{uri}"
when 'openbsd'
'xdg-open'
"xdg-open #{uri}"
when 'bitrig'
'xdg-open'
"xdg-open #{uri}"
when 'solaris'
if version < 11
'sdtwebclient'
"sdtwebclient #{uri}"
else
'xdg-open'
"xdg-open #{uri}"
end
else
''
""
end
end

attr_reader :open_browser_cmd

def initialize
local_os = Gem::Platform.local.os
version = Gem::Platform.local.version
@open_browser_cmd = open_default_browser_cmd(local_os, version)
end

def open_page(gem, options)
if options[:sourcecode]
find_page(gem, "source_code_uri")
Expand Down Expand Up @@ -114,8 +108,12 @@ def open_rubytoolbox(gem)
end

def open_default_browser(uri)
if !@open_browser_cmd.nil?
system(@open_browser_cmd, uri)
local_os = Gem::Platform.local.os
version = Gem::Platform.local.version
open_browser_cmd = open_default_browser_cmd(local_os, version, uri)

if !open_browser_cmd.empty?
system(open_browser_cmd)
else
puts "The command 'web' is not supported on your platform."
end
Expand Down
46 changes: 36 additions & 10 deletions test/rubygems/test_gem_commands_web_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ def test_open_browser_command
@mock.expect(:version, '')

Gem::Platform.stub :local, @mock do
executor = Gem::Web::Executor.new
assert_equal "open", executor.open_browser_cmd
open_browser_cmd = Gem::Web::Executor.new.open_default_browser_cmd(Gem::Platform.local.os,
Gem::Platform.local.version,
"http://github.com/ruby/ruby")
assert_match /open/, open_browser_cmd
end

@mock.verify
end

def test_default_option
@mock.expect(:call, true, [@cmd.executor.open_browser_cmd, "http://github.com/rails/rails"])
open_browser_cmd = Gem::Web::Executor.new.open_default_browser_cmd(Gem::Platform.local.os,
Gem::Platform.local.version,
"http://github.com/rails/rails")
@mock.expect(:call, true, [open_browser_cmd])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[rails]
Expand All @@ -35,7 +40,10 @@ def test_default_option
end

def test_open_the_documentation
@mock.expect(:call, true, [@cmd.executor.open_browser_cmd, "http://api.rubyonrails.org"])
open_browser_cmd = Gem::Web::Executor.new.open_default_browser_cmd(Gem::Platform.local.os,
Gem::Platform.local.version,
"http://api.rubyonrails.org")
@mock.expect(:call, true, [open_browser_cmd])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-d rails]
Expand All @@ -46,7 +54,10 @@ def test_open_the_documentation
end

def test_open_the_homepage
@mock.expect(:call, true, [@cmd.executor.open_browser_cmd, "http://rubyonrails.org"])
open_browser_cmd = Gem::Web::Executor.new.open_default_browser_cmd(Gem::Platform.local.os,
Gem::Platform.local.version,
"http://rubyonrails.org")
@mock.expect(:call, true, [open_browser_cmd])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-w rails]
Expand All @@ -57,7 +68,10 @@ def test_open_the_homepage
end

def test_open_the_source_code
@mock.expect(:call, true, [@cmd.executor.open_browser_cmd, "http://github.com/rails/rails"])
open_browser_cmd = Gem::Web::Executor.new.open_default_browser_cmd(Gem::Platform.local.os,
Gem::Platform.local.version,
"http://github.com/rails/rails")
@mock.expect(:call, true, [open_browser_cmd])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-c rails]
Expand All @@ -68,7 +82,10 @@ def test_open_the_source_code
end

def test_open_github
@mock.expect(:call, true, [@cmd.executor.open_browser_cmd, "http://github.com/rails/rails"])
open_browser_cmd = Gem::Web::Executor.new.open_default_browser_cmd(Gem::Platform.local.os,
Gem::Platform.local.version,
"http://github.com/rails/rails")
@mock.expect(:call, true, [open_browser_cmd])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-g rails]
Expand All @@ -79,7 +96,10 @@ def test_open_github
end

def test_open_rubygems
@mock.expect(:call, true, [@cmd.executor.open_browser_cmd, "https://rubygems.org/gems/rails"])
open_browser_cmd = Gem::Web::Executor.new.open_default_browser_cmd(Gem::Platform.local.os,
Gem::Platform.local.version,
"https://rubygems.org/gems/rails")
@mock.expect(:call, true, [open_browser_cmd])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-r rails]
Expand All @@ -90,7 +110,10 @@ def test_open_rubygems
end

def test_open_rubytoolbox
@mock.expect(:call, true, [@cmd.executor.open_browser_cmd, "https://www.ruby-toolbox.com/projects/rails"])
open_browser_cmd = Gem::Web::Executor.new.open_default_browser_cmd(Gem::Platform.local.os,
Gem::Platform.local.version,
"https://www.ruby-toolbox.com/projects/rails")
@mock.expect(:call, true, [open_browser_cmd])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-t rails]
Expand All @@ -109,7 +132,10 @@ def test_search_unexisting_gem
end

def test_open_rubygems_if_it_could_not_find_page
@mock.expect(:call, true, [@cmd.executor.open_browser_cmd, "https://rubygems.org/gems/rails"])
open_browser_cmd = Gem::Web::Executor.new.open_default_browser_cmd(Gem::Platform.local.os,
Gem::Platform.local.version,
"https://rubygems.org/gems/rails")
@mock.expect(:call, true, [open_browser_cmd])

@cmd.executor.stub :system, @mock do
assert_output("Did not find page for rails, opening RubyGems page instead.\n") do
Expand Down

0 comments on commit 290f241

Please sign in to comment.