Skip to content

Commit ead0ff7

Browse files
committed
Add RuboCop plugins and resolve its offenses
1 parent 03f1e65 commit ead0ff7

File tree

13 files changed

+661
-297
lines changed

13 files changed

+661
-297
lines changed

.rubocop.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
require:
2+
- rubocop-performance
3+
- rubocop-rake
4+
- rubocop-rspec
5+
16
AllCops:
27
TargetRubyVersion: 2.5
38
NewCops: enable
@@ -62,3 +67,6 @@ Style/StringLiterals:
6267

6368
Style/WordArray:
6469
Enabled: false
70+
71+
RSpec/NestedGroups:
72+
Enabled: false

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ group :development do
1111
gem "pry-byebug", "~> 3.9"
1212
gem "rake", "~> 13.0"
1313
gem "rubocop", "~> 1.13.0", require: false
14+
gem "rubocop-performance", "~> 1.11", require: false
15+
gem "rubocop-rake", "~> 0.5.0", require: false
16+
gem "rubocop-rspec", "~> 2.3.0", require: false
1417
end
1518

1619
group :test do

Rakefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Bundler::GemHelper.install_tasks
66

77
require "rspec/core/rake_task"
88

9-
task "default" => "spec"
10-
119
RSpec::Core::RakeTask.new do |t|
1210
t.pattern = "spec/**/*_spec.rb"
1311
t.rspec_opts = ["--colour", "--format", "documentation"]
@@ -17,4 +15,4 @@ require "rubocop/rake_task"
1715

1816
RuboCop::RakeTask.new
1917

20-
task "default" => "rubocop"
18+
task "default" => ["spec", "rubocop"]

lib/clamp/parameter/definition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def consume(arguments)
4646
def infer_attribute_name
4747
inferred_name = name.downcase.tr("-", "_").sub(ELLIPSIS_SUFFIX, "").sub(OPTIONAL) { Regexp.last_match(1) }
4848

49-
raise "cannot infer attribute_name from #{name.inspect}" unless inferred_name =~ VALID_ATTRIBUTE_NAME
49+
raise "cannot infer attribute_name from #{name.inspect}" unless inferred_name.match? VALID_ATTRIBUTE_NAME
5050

5151
inferred_name += "_list" if multivalued?
5252
inferred_name

spec/clamp/command_group_spec.rb

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,27 @@ def message
2929

3030
end
3131

32-
it "delegates to sub-commands" do
32+
describe "flip command" do
33+
before do
34+
command.run(["flip"])
35+
end
3336

34-
command.run(["flip"])
35-
expect(stdout).to match(/FLIPPED/)
37+
it "delegates to sub-commands" do
38+
expect(stdout).to match(/FLIPPED/)
39+
end
40+
end
3641

37-
command.run(["flop"])
38-
expect(stdout).to match(/FLOPPED/)
42+
describe "flop command" do
43+
before do
44+
command.run(["flop"])
45+
end
3946

47+
it "delegates to sub-commands" do
48+
expect(stdout).to match(/FLOPPED/)
49+
end
4050
end
4151

42-
context "executed with no subcommand" do
52+
context "when executed with no subcommand" do
4353

4454
it "triggers help" do
4555
expect do
@@ -56,10 +66,7 @@ def message
5666
end
5767

5868
it "lists subcommands" do
59-
help = command.help
60-
expect(help).to match(/Subcommands:/)
61-
expect(help).to match(/flip +flip it/)
62-
expect(help).to match(/flop +flop it/)
69+
expect(command.help).to match(/Subcommands:\n +flip +flip it\n +flop +flop it/)
6370
end
6471

6572
it "handles new lines in subcommand descriptions" do
@@ -95,13 +102,27 @@ def execute
95102

96103
end
97104

98-
it "responds to both aliases" do
105+
describe "the first alias" do
99106

100-
command.run(["say", "boo"])
101-
expect(stdout).to match(/boo/)
107+
before do
108+
command.run(["say", "boo"])
109+
end
102110

103-
command.run(["talk", "jive"])
104-
expect(stdout).to match(/jive/)
111+
it "responds to it" do
112+
expect(stdout).to match(/boo/)
113+
end
114+
115+
end
116+
117+
describe "the second alias" do
118+
119+
before do
120+
command.run(["talk", "jive"])
121+
end
122+
123+
it "responds to it" do
124+
expect(stdout).to match(/jive/)
125+
end
105126

106127
end
107128

@@ -167,7 +188,7 @@ def execute
167188

168189
end
169190

170-
context "executed with no subcommand" do
191+
context "when executed with no subcommand" do
171192

172193
it "invokes the default subcommand" do
173194
command.run([])
@@ -192,7 +213,7 @@ def execute
192213

193214
end
194215

195-
context "executed with no subcommand" do
216+
context "when executed with no subcommand" do
196217

197218
it "invokes the default subcommand" do
198219
command.run([])
@@ -203,24 +224,26 @@ def execute
203224

204225
end
205226

206-
context "declaring a default subcommand after subcommands" do
227+
context "when declaring a default subcommand after subcommands" do
207228

208-
it "is not supported" do
229+
let(:command) do
230+
Class.new(Clamp::Command) do
209231

210-
expect do
211-
Class.new(Clamp::Command) do
232+
subcommand "status", "Show status" do
212233

213-
subcommand "status", "Show status" do
234+
def execute
235+
puts "All good!"
236+
end
214237

215-
def execute
216-
puts "All good!"
217-
end
238+
end
218239

219-
end
240+
end
241+
end
220242

221-
self.default_subcommand = "status"
243+
it "is not supported" do
222244

223-
end
245+
expect do
246+
command.default_subcommand = "status"
224247
end.to raise_error(/default_subcommand must be defined before subcommands/)
225248

226249
end
@@ -251,18 +274,18 @@ def execute
251274

252275
it "allows the parameter to be specified first" do
253276
command.run(["dummy", "spit"])
254-
expect(stdout.strip).to eql "spat the dummy"
277+
expect(stdout.strip).to eq "spat the dummy"
255278
end
256279

257280
it "passes the parameter down the stack" do
258281
command.run(["money", "say", "loud"])
259-
expect(stdout.strip).to eql "MONEY"
282+
expect(stdout.strip).to eq "MONEY"
260283
end
261284

262285
it "shows parameter in usage help" do
263286
command.run(["stuff", "say", "loud", "--help"])
264287
rescue Clamp::HelpWanted => e
265-
expect(e.command.invocation_path).to eql("with THING say loud")
288+
expect(e.command.invocation_path).to eq "with THING say loud"
266289
end
267290

268291
end
@@ -336,7 +359,7 @@ def execute; end
336359

337360
it "only parses options once" do
338361
command.run(["--json", '{"a":"b"}', "woohoohoo"])
339-
expect(stdout).to eql "parsing!"
362+
expect(stdout).to eq "parsing!"
340363
end
341364

342365
end
@@ -376,19 +399,16 @@ def execute; end
376399
command_class.new("foo")
377400
end
378401

379-
it "should signal no such subcommand usage error" do
380-
expect { command.run(["foo"]) }.to raise_error(Clamp::UsageError) do |exception|
381-
expect(exception.message).to eq "No such sub-command 'foo'"
382-
end
402+
it "signals no such subcommand usage error" do
403+
expect { command.run(["foo"]) }.to raise_error(Clamp::UsageError, "No such sub-command 'foo'")
383404
end
384405

385-
it "should execute the subcommand missing method" do
406+
it "executes the subcommand missing method" do
386407
command.extend subcommand_missing
387-
expect { command.run(["foo"]) }.to raise_error(SystemExit)
388-
expect(stderr).to match(/there is no such thing/)
408+
expect { command.run(["foo"]) }.to raise_error(SystemExit, /there is no such thing/)
389409
end
390410

391-
it "should use the subcommand class returned from subcommand_missing" do
411+
it "uses the subcommand class returned from subcommand_missing" do
392412
command.extend subcommand_missing_with_return
393413
command.run(["foo"])
394414
expect(stdout).to match(/known subcommand/)
@@ -409,7 +429,7 @@ def execute
409429

410430
it "allows options after the subcommand" do
411431
command.run(%w[hop --direction south])
412-
expect(stdout).to eql "Hopping south\n"
432+
expect(stdout).to eq "Hopping south\n"
413433
end
414434

415435
end

spec/clamp/option_module_spec.rb renamed to spec/clamp/command_option_module_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def execute
3131

3232
it "accepts options from included module" do
3333
command.run(["--size", "42"])
34-
expect(stdout).to eql "size = 42\n"
34+
expect(stdout).to eq "size = 42\n"
3535
end
3636

3737
end

spec/clamp/option_reordering_spec.rb renamed to spec/clamp/command_option_reordering_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ def execute
4040

4141
it "still works" do
4242
command.run(%w[say foo])
43-
expect(stdout).to eql("foo\n")
43+
expect(stdout).to eq "foo\n"
4444
end
4545

4646
it "honours options after positional arguments" do
4747
command.run(%w[say blah --verbose])
48-
expect(stdout).to eql("blahblahblah\n")
48+
expect(stdout).to eq "blahblahblah\n"
4949
end
5050

5151
it "honours options declared on subcommands" do
5252
command.run(%w[say --loud blah])
53-
expect(stdout).to eql("BLAH\n")
53+
expect(stdout).to eq "BLAH\n"
5454
end
5555

5656
end

0 commit comments

Comments
 (0)