guard-rubocop 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c0c295908d4ca4ca5f79bd5c4d85857dc2a05140
4
- data.tar.gz: 7cd9a0ff0f3858ab1a9182cb4b2645e2778a8b2c
3
+ metadata.gz: ae91b5efb5588f487c013e9ce9bd122b8e5a3e5d
4
+ data.tar.gz: eb7c6e0877ece705ffc2ec3d0678be31c99ffde0
5
5
  SHA512:
6
- metadata.gz: 84c30fe8fb9538d668bd2951ffd428dce9204d01f725f7ebb6df772652ec3223c767d9bc68a60d5f9f98b6b38aaaec44dc1cd7c9dfb9a84b8e72b8798141f5f5
7
- data.tar.gz: b1d22e396e96132d9749a87aae7af718dac2a7c2b425b1a803836a7fdfdc8b793c49b89fd8423b6c83aa191f00950bfe8cc06cb3b745db31955c267a991ed1ad
6
+ metadata.gz: c145d8df9c847803da47b8837347afaa4792fc5a41e484824635a424e384daa2c3694fa109e47797ca17f9e7e6c8c1dbc9bdee7e88fd0de76cc1c7e19d7ff1d7
7
+ data.tar.gz: c8a27a5a563029a367fb65d2fa1bb00508f14bb56dc73e834f6767ee172b6e30d47fe4e0a0ba8c2e05ce26d61d67e0b7cd12e82cf75208bfab47aed94701a929
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v0.2.0
4
+
5
+ * Add `:cli` option which allows to specify additional command line arguments
6
+ * Support JRuby and Rubinius in 1.9 modes
7
+
3
8
  ## v0.1.0
4
9
 
5
10
  * Update RuboCop dependency to 0.9 or later and earlier than 1.0
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  Guard::Rubocop allows you to automatically check Ruby code style with [RuboCop](https://github.com/bbatsov/rubocop) when files are modified.
6
6
 
7
- Tested on MRI 1.9 and MRI 2.0, according to RuboCop.
7
+ Tested on MRI 1.9, MRI 2.0, JRuby and Rubinius in 1.9 modes.
8
8
 
9
9
  ## Installation
10
10
 
@@ -42,10 +42,10 @@ Please read the [Guard usage documentation](https://github.com/guard/guard#readm
42
42
 
43
43
  ## Options
44
44
 
45
- You can pass some options in `Guardfile`:
45
+ You can pass some options in `Guardfile` like the following example:
46
46
 
47
47
  ```ruby
48
- guard :rubocop, all_on_start: false, notification: true do
48
+ guard :rubocop, all_on_start: false, cli: ['--format', 'clang', '--rails'] do
49
49
  # ...
50
50
  end
51
51
  ```
@@ -53,9 +53,14 @@ end
53
53
  ### Available Options
54
54
 
55
55
  ```ruby
56
- all_on_start: true # Check all files at Guard startup, default: true
57
- keep_failed: true # Keep failed files until they pass, default: true
58
- notification: :failed # Display Growl notification after each run
56
+ all_on_start: true # Check all files at Guard startup.
57
+ # default: true
58
+ cli: ['--rails'] # Pass arbitrary RuboCop CLI arguments.
59
+ # An array or string is acceptable.
60
+ # default: nil
61
+ keep_failed: true # Keep failed files until they pass.
62
+ # default: true
63
+ notification: :failed # Display Growl notification after each run.
59
64
  # true - Always notify
60
65
  # false - Never notify
61
66
  # :failed - Notify only when failed
data/lib/guard/rubocop.rb CHANGED
@@ -4,6 +4,8 @@ require 'guard'
4
4
  require 'guard/guard'
5
5
 
6
6
  module Guard
7
+ # This class gets API calls from `guard` and runs `rubocop` command via {Guard::Rubocop::Runner}.
8
+ # An instance of this class stays alive in a `guard` command session.
7
9
  class Rubocop < Guard
8
10
  autoload :Runner, 'guard/rubocop/runner'
9
11
 
@@ -15,7 +17,8 @@ module Guard
15
17
  @options = {
16
18
  all_on_start: true,
17
19
  keep_failed: true,
18
- notification: :failed
20
+ notification: :failed,
21
+ cli: nil
19
22
  }.merge(options)
20
23
 
21
24
  @failed_paths = []
@@ -2,9 +2,15 @@
2
2
 
3
3
  require 'json'
4
4
 
5
+ # rubocop:disable Documentation
6
+
5
7
  module Guard
6
8
  class Rubocop
9
+ # This class runs `rubocop` command, retrieves result and notifies.
10
+ # An instance of this class is intended to invoke `rubocop` only once in its lifetime.
7
11
  class Runner
12
+ # rubocop:enable Documentation
13
+
8
14
  def initialize(options)
9
15
  @options = options
10
16
  end
@@ -25,11 +31,43 @@ module Guard
25
31
 
26
32
  def build_command(paths)
27
33
  command = ['rubocop']
28
- command.concat(%w(--format progress)) # Keep default formatter for console.
34
+
35
+ unless include_formatter_for_console?(args_specified_by_user)
36
+ command.concat(%w(--format progress)) # Keep default formatter for console.
37
+ end
38
+
29
39
  command.concat(['--format', 'json', '--out', json_file_path])
40
+ command.concat(args_specified_by_user)
30
41
  command.concat(paths)
31
42
  end
32
43
 
44
+ def args_specified_by_user
45
+ @args_specified_by_user ||= begin
46
+ args = @options[:cli]
47
+ case args
48
+ when Array then args
49
+ when String then args.shellsplit
50
+ when NilClass then []
51
+ else fail ':cli option must be either an array or string'
52
+ end
53
+ end
54
+ end
55
+
56
+ def include_formatter_for_console?(cli_args)
57
+ index = -1
58
+ formatter_args = cli_args.group_by do |arg|
59
+ if %w(-f --format).include?(arg)
60
+ index += 1
61
+ end
62
+ index
63
+ end
64
+ formatter_args.delete(-1)
65
+
66
+ formatter_args.each_value.any? do |args|
67
+ args.none? { |a| %w(-o --out).include?(a) }
68
+ end
69
+ end
70
+
33
71
  def json_file_path
34
72
  @tempfile_path ||= begin
35
73
  # Just generate random tempfile path.
@@ -43,7 +81,8 @@ module Guard
43
81
  def result
44
82
  @result ||= begin
45
83
  File.open(json_file_path) do |file|
46
- JSON.load(file, nil, symbolize_names: true)
84
+ # Rubinius 2.0.0.rc1 does not support `JSON.load` with 3 args.
85
+ JSON.parse(file.read, symbolize_names: true)
47
86
  end
48
87
  end
49
88
  end
@@ -6,7 +6,7 @@ module Guard
6
6
  module RubocopVersion
7
7
  # http://semver.org/
8
8
  MAJOR = 0
9
- MINOR = 1
9
+ MINOR = 2
10
10
  PATCH = 0
11
11
  VERSION = [MAJOR, MINOR, PATCH].join('.')
12
12
  end
@@ -92,24 +92,147 @@ describe Guard::Rubocop::Runner do
92
92
  end
93
93
 
94
94
  describe '#build_command' do
95
+ subject(:build_command) { runner.build_command(paths) }
96
+ let(:options) { { cli: %w(--debug --rails) } }
95
97
  let(:paths) { %w(file1.rb file2.rb) }
96
98
 
97
- it 'adds args for the default formatter for console' do
98
- runner.build_command(paths)[0..2].should == %w(rubocop --format progress)
99
+ context 'when :cli option includes formatter for console' do
100
+ let(:options) { { cli: %w(--format simple) } }
101
+
102
+ it 'does not add args for the default formatter for console' do
103
+ build_command[0..2].should_not == %w(rubocop --format progress)
104
+ end
105
+ end
106
+
107
+ context 'when :cli option does not include formatter for console' do
108
+ let(:options) { { cli: %w(--format simple --out simple.txt) } }
109
+
110
+ it 'adds args for the default formatter for console' do
111
+ build_command[0..2].should == %w(rubocop --format progress)
112
+ end
99
113
  end
100
114
 
101
115
  it 'adds args for JSON formatter ' do
102
- runner.build_command(paths)[3..4].should == %w(--format json)
116
+ build_command[3..4].should == %w(--format json)
103
117
  end
104
118
 
105
119
  it 'adds args for output file path of JSON formatter ' do
106
- command = runner.build_command(paths)
107
- command[5].should == '--out'
108
- command[6].should_not be_empty
120
+ build_command[5].should == '--out'
121
+ build_command[6].should_not be_empty
122
+ end
123
+
124
+ it 'adds args specified by user' do
125
+ build_command[7..8].should == %w(--debug --rails)
109
126
  end
110
127
 
111
128
  it 'adds the passed paths' do
112
- runner.build_command(paths)[7..-1].should == %w(file1.rb file2.rb)
129
+ build_command[9..-1].should == %w(file1.rb file2.rb)
130
+ end
131
+
132
+ context 'when the value of :cli option is a string' do
133
+ let(:options) { { cli: '--debug --rails' } }
134
+
135
+ it 'handles' do
136
+ build_command[7..8].should == %w(--debug --rails)
137
+ end
138
+ end
139
+ end
140
+
141
+ describe '#args_specified_by_user' do
142
+ context 'when :cli option is nil' do
143
+ let(:options) { { cli: nil } }
144
+
145
+ it 'returns empty array' do
146
+ runner.args_specified_by_user.should == []
147
+ end
148
+ end
149
+
150
+ context 'when :cli option is an array' do
151
+ let(:options) { { cli: ['--out', 'output file.txt'] } }
152
+
153
+ it 'just returns the array' do
154
+ runner.args_specified_by_user.should == ['--out', 'output file.txt']
155
+ end
156
+ end
157
+
158
+ context 'when :cli option is a string' do
159
+ let(:options) { { cli: '--out "output file.txt"' } }
160
+
161
+ it 'returns an array from String#shellsplit' do
162
+ runner.args_specified_by_user.should == ['--out', 'output file.txt']
163
+ end
164
+ end
165
+
166
+ context 'when :cli option is other types' do
167
+ let(:options) { { cli: { key: 'value' } } }
168
+
169
+ it 'raises error' do
170
+ expect { runner.args_specified_by_user }.to raise_error
171
+ end
172
+ end
173
+ end
174
+
175
+ describe '#include_formatter_for_console?' do
176
+ subject(:include_formatter_for_console?) { runner.include_formatter_for_console?(args) }
177
+
178
+ context 'when the passed args include a -f/--format' do
179
+ context 'but does not include an -o/--output' do
180
+ let(:args) { %w(--format simple --debug) }
181
+
182
+ it 'returns true' do
183
+ include_formatter_for_console?.should be_true
184
+ end
185
+ end
186
+
187
+ context 'and include an -o/--output just after the -f/--format' do
188
+ let(:args) { %w(--format simple --out simple.txt) }
189
+
190
+ it 'returns false' do
191
+ include_formatter_for_console?.should be_false
192
+ end
193
+ end
194
+
195
+ context 'and include an -o/--output after the -f/--format across another arg' do
196
+ let(:args) { %w(--format simple --debug --out simple.txt) }
197
+
198
+ it 'returns false' do
199
+ include_formatter_for_console?.should be_false
200
+ end
201
+ end
202
+ end
203
+
204
+ context 'when the passed args include multiple -f/--format' do
205
+ context 'and all -f/--format have associated -o/--out' do
206
+ let(:args) { %w(--format simple --out simple.txt --format emacs --out emacs.txt) }
207
+
208
+ it 'returns false' do
209
+ include_formatter_for_console?.should be_false
210
+ end
211
+ end
212
+
213
+ context 'and any -f/--format has associated -o/--out' do
214
+ let(:args) { %w(--format simple --format emacs --out emacs.txt) }
215
+
216
+ it 'returns true' do
217
+ include_formatter_for_console?.should be_true
218
+ end
219
+ end
220
+
221
+ context 'and no -f/--format has associated -o/--out' do
222
+ let(:args) { %w(--format simple --format emacs) }
223
+
224
+ it 'returns true' do
225
+ include_formatter_for_console?.should be_true
226
+ end
227
+ end
228
+ end
229
+
230
+ context 'when the passed args do not include -f/--format' do
231
+ let(:args) { %w(--debug) }
232
+
233
+ it 'returns false' do
234
+ include_formatter_for_console?.should be_false
235
+ end
113
236
  end
114
237
  end
115
238
 
@@ -17,6 +17,7 @@ describe Guard::Rubocop, :silence_output do
17
17
  its([:all_on_start]) { should be_true }
18
18
  its([:keep_failed]) { should be_true }
19
19
  its([:notification]) { should == :failed }
20
+ its([:cli]) { should be_nil }
20
21
  end
21
22
  end
22
23
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuji Nakayama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-01 00:00:00.000000000 Z
11
+ date: 2013-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: guard