Skip to content

Commit ccee631

Browse files
author
Timo Rößner
committed
Merge pull request whitesmith#113 from superiorlu/fix_and_refator_open_report_with_browser
add --no-browser options, refactor browser code
2 parents c3dcba7 + c175c7f commit ccee631

File tree

11 files changed

+18
-177
lines changed

11 files changed

+18
-177
lines changed

features/command_line_interface/options.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Feature: Rubycritic can be controlled using command-line options
2929
-m, --mode-ci Use CI mode (faster, but only analyses last commit)
3030
--deduplicate-symlinks De-duplicate symlinks based on their final target
3131
--suppress-ratings Suppress letter ratings
32-
-O, --open Open html report with browser: [:chrome, :chromium, :firefox, :safari]
32+
--no-browser Do not open html report with browser
3333
-v, --version Show gem's version
3434
-h, --help Show this message
3535

features/support/env.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize
1616
end
1717

1818
def rubycritic(args)
19-
run_simple("rubycritic #{args}", false)
19+
run_simple("rubycritic #{args} --no-browser", false)
2020
end
2121
end
2222

lib/rubycritic/browser.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
require 'rubycritic/platforms/linux'
2-
require 'rubycritic/platforms/darwin'
1+
require 'launchy'
32

43
module Rubycritic
54
class Browser
65
attr_reader :report_path
76

8-
SUPPORTS = Platforms::Linux::BROWSER_MAP.keys | Platforms::Darwin::BROWSER_MAP.keys
9-
107
def initialize(report_path)
118
@report_path = report_path
129
end
1310

1411
def open
15-
platform && platform.open(report_path)
16-
end
17-
18-
def platform
19-
@platform ||= [Platforms::Linux.new, Platforms::Darwin.new].find(&:can_open?)
12+
Launchy.open(report_path) do |exception|
13+
puts "Attempted to open #{report_path} and failed because #{exception}"
14+
end
2015
end
2116
end
2217
end

lib/rubycritic/cli/options.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,8 @@ def parse
4545
@suppress_ratings = true
4646
end
4747

48-
opts.on('-O ', '--open',
49-
Rubycritic::Browser::SUPPORTS,
50-
"Open html report with browser: #{Rubycritic::Browser::SUPPORTS}"
51-
) do |open_with|
52-
@open_with = open_with
48+
opts.on('--no-browser', 'Do not open html report with browser') do
49+
@no_browser = true
5350
end
5451

5552
opts.on_tail('-v', '--version', "Show gem's version") do
@@ -73,7 +70,7 @@ def to_h
7370
suppress_ratings: @suppress_ratings,
7471
help_text: @parser.help,
7572
minimum_score: @minimum_score || 0,
76-
open_with: @open_with
73+
no_browser: @no_browser
7774
}
7875
end
7976

lib/rubycritic/configuration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Rubycritic
22
class Configuration
33
attr_reader :root
44
attr_accessor :source_control_system, :mode, :format, :deduplicate_symlinks,
5-
:suppress_ratings, :open_with
5+
:suppress_ratings, :open_with, :no_browser
66

77
def set(options)
88
self.mode = options[:mode] || :default
@@ -11,6 +11,7 @@ def set(options)
1111
self.deduplicate_symlinks = options[:deduplicate_symlinks] || false
1212
self.suppress_ratings = options[:suppress_ratings] || false
1313
self.open_with = options[:open_with]
14+
self.no_browser = options[:no_browser]
1415
end
1516

1617
def root=(path)

lib/rubycritic/generators/html_report.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'fileutils'
2+
require 'rubycritic/configuration'
23
require 'rubycritic/generators/html/overview'
34
require 'rubycritic/generators/html/smells_index'
45
require 'rubycritic/generators/html/code_index'
@@ -17,7 +18,7 @@ def generate_report
1718
create_directories_and_files
1819
copy_assets_to_report_directory
1920
puts "New critique at #{report_location}"
20-
browser.open
21+
browser.open unless Config.no_browser
2122
end
2223

2324
def browser

lib/rubycritic/platforms/base.rb

Lines changed: 0 additions & 46 deletions
This file was deleted.

lib/rubycritic/platforms/darwin.rb

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/rubycritic/platforms/linux.rb

Lines changed: 0 additions & 30 deletions
This file was deleted.

rubycritic.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
2626
spec.add_runtime_dependency 'reek', '3.9.1'
2727
spec.add_runtime_dependency 'parser', '~> 2.3'
2828
spec.add_runtime_dependency 'colorize'
29+
spec.add_runtime_dependency 'launchy', '2.4.3'
2930

3031
spec.add_development_dependency 'aruba'
3132
spec.add_development_dependency 'bundler', '~> 1.3'

test/lib/rubycritic/browser_test.rb

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,16 @@
11
require 'test_helper'
22
require 'rubycritic/browser'
3-
require 'rubycritic/platforms/linux'
4-
require 'rubycritic/platforms/darwin'
5-
require 'rubycritic/generators/html_report'
63

74
describe Rubycritic::Browser do
85
before do
96
@report_path = 'tmp/rubycritic/overview.html'
107
@browser = Rubycritic::Browser.new @report_path
118
end
129

13-
describe 'open report on different platform' do
14-
context 'on linux' do
15-
it 'open with default browser' do
16-
Rubycritic::Config.set
17-
Rubycritic::Browser.any_instance.stubs(:platform).returns(Rubycritic::Platforms::Linux.new)
18-
@linux = @browser.platform
19-
@linux.browser.must_equal 'firefox'
20-
end
21-
22-
it 'open with google chrome browser ' do
23-
Rubycritic::Config.stubs(:open_with).returns(:chrome)
24-
Rubycritic::Browser.any_instance.stubs(:platform).returns(Rubycritic::Platforms::Linux.new)
25-
@linux = @browser.platform
26-
@linux.browser.must_equal 'google-chrome'
27-
end
28-
29-
it 'open with chromium browser ' do
30-
Rubycritic::Config.stubs(:open_with).returns(:chromium)
31-
Rubycritic::Browser.any_instance.stubs(:platform).returns(Rubycritic::Platforms::Linux.new)
32-
@linux = @browser.platform
33-
@linux.browser.must_equal 'chromium-browser'
34-
end
35-
end
36-
37-
context 'on darwin' do
38-
it 'open with default browser' do
39-
Rubycritic::Browser.any_instance.stubs(:platform).returns(Rubycritic::Platforms::Darwin.new)
40-
@darwin = @browser.platform
41-
@darwin.browser.must_equal 'safari'
42-
end
43-
44-
it 'open with google chrome browser ' do
45-
Rubycritic::Config.stubs(:open_with).returns(:chrome)
46-
Rubycritic::Browser.any_instance.stubs(:platform).returns(Rubycritic::Platforms::Darwin.new)
47-
@darwin = @browser.platform
48-
@darwin.browser.must_equal 'Google Chrome'
49-
end
50-
51-
it 'open with firefox browser ' do
52-
Rubycritic::Config.stubs(:open_with).returns(:firefox)
53-
Rubycritic::Browser.any_instance.stubs(:platform).returns(Rubycritic::Platforms::Darwin.new)
54-
@darwin = @browser.platform
55-
@darwin.browser.must_equal 'firefox'
56-
end
57-
end
58-
59-
context 'on other platform' do
60-
it 'not open with browser' do
61-
Rubycritic::Browser.any_instance.stubs(:platform).returns(nil)
62-
Rubycritic::Browser.any_instance.stubs(:report_path).returns('tmp/rubycritic/overview.html')
63-
@browser.open.must_be_nil
64-
end
10+
describe '#open' do
11+
it 'should be open report with launch browser' do
12+
Launchy.stubs(:open).returns(true)
13+
@browser.open.must_equal true
6514
end
6615
end
6716
end

0 commit comments

Comments
 (0)