Skip to content

Commit 864e32f

Browse files
committed
Use #run_on_modifications to suppress multiple run on a save with Vim
This closes #6.
1 parent b498bd2 commit 864e32f

File tree

2 files changed

+69
-59
lines changed

2 files changed

+69
-59
lines changed

lib/guard/rubocop.rb

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,15 @@ def start
3030

3131
def run_all
3232
UI.info 'Inspecting Ruby code style of all files'
33-
run
33+
inspect_with_rubocop
3434
end
3535

36-
def run_on_changes(paths)
37-
paths += @failed_paths if @options[:keep_failed]
38-
paths = clean_paths(paths)
39-
40-
return if paths.empty?
41-
42-
displayed_paths = paths.map { |path| smart_path(path) }
43-
UI.info "Inspecting Ruby code style: #{displayed_paths.join(' ')}"
36+
def run_on_additions(paths)
37+
run_partially(paths)
38+
end
4439

45-
run(paths)
40+
def run_on_modifications(paths)
41+
run_partially(paths)
4642
end
4743

4844
def reload
@@ -62,7 +58,19 @@ def clean_paths(paths)
6258

6359
private
6460

65-
def run(paths = [])
61+
def run_partially(paths)
62+
paths += @failed_paths if @options[:keep_failed]
63+
paths = clean_paths(paths)
64+
65+
return if paths.empty?
66+
67+
displayed_paths = paths.map { |path| smart_path(path) }
68+
UI.info "Inspecting Ruby code style: #{displayed_paths.join(' ')}"
69+
70+
inspect_with_rubocop(paths)
71+
end
72+
73+
def inspect_with_rubocop(paths = [])
6674
runner = Runner.new(@options)
6775
passed = runner.run(paths)
6876
@failed_paths = runner.failed_paths

spec/guard/rubocop_spec.rb

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -111,69 +111,71 @@
111111
end
112112
end
113113

114-
describe '#run_on_changes', :processes_after_running do
115-
subject { super().run_on_changes(changed_paths) }
116-
let(:changed_paths) do
117-
[
118-
'lib/guard/rubocop.rb',
119-
'spec/spec_helper.rb'
120-
]
121-
end
122-
123-
before do
124-
allow_any_instance_of(Guard::Rubocop::Runner).to receive(:run).and_return(true)
125-
allow_any_instance_of(Guard::Rubocop::Runner).to receive(:failed_paths).and_return([])
126-
end
114+
[:run_on_additions, :run_on_modifications].each do |method|
115+
describe "##{method}", :processes_after_running do
116+
subject { super().send(method, changed_paths) }
117+
let(:changed_paths) do
118+
[
119+
'lib/guard/rubocop.rb',
120+
'spec/spec_helper.rb'
121+
]
122+
end
127123

128-
it 'inspects changed files with rubocop' do
129-
expect_any_instance_of(Guard::Rubocop::Runner).to receive(:run)
130-
guard.run_on_changes(changed_paths)
131-
end
124+
before do
125+
allow_any_instance_of(Guard::Rubocop::Runner).to receive(:run).and_return(true)
126+
allow_any_instance_of(Guard::Rubocop::Runner).to receive(:failed_paths).and_return([])
127+
end
132128

133-
it 'passes cleaned paths to rubocop' do
134-
expect_any_instance_of(Guard::Rubocop::Runner).to receive(:run) do |paths|
135-
expect(paths).to eq([
136-
File.expand_path('some.rb'),
137-
File.expand_path('dir/another.rb')
138-
])
129+
it 'inspects changed files with rubocop' do
130+
expect_any_instance_of(Guard::Rubocop::Runner).to receive(:run)
131+
subject
139132
end
140-
guard.run_on_changes(changed_paths)
141-
end
142133

143-
context 'when cleaned paths are empty' do
144-
before do
145-
allow(guard).to receive(:clean_paths).and_return([])
134+
it 'passes cleaned paths to rubocop' do
135+
expect_any_instance_of(Guard::Rubocop::Runner).to receive(:run) do |paths|
136+
expect(paths).to eq([
137+
File.expand_path('some.rb'),
138+
File.expand_path('dir/another.rb')
139+
])
140+
end
141+
subject
146142
end
147143

148-
it 'does nothing' do
149-
expect_any_instance_of(Guard::Rubocop::Runner).not_to receive(:run)
150-
guard.run_on_changes(changed_paths)
144+
context 'when cleaned paths are empty' do
145+
before do
146+
allow(guard).to receive(:clean_paths).and_return([])
147+
end
148+
149+
it 'does nothing' do
150+
expect_any_instance_of(Guard::Rubocop::Runner).not_to receive(:run)
151+
subject
152+
end
151153
end
152-
end
153154

154-
let(:failed_path) { File.expand_path('Rakefile') }
155+
let(:failed_path) { File.expand_path('Rakefile') }
155156

156-
context 'when :keep_failed option is enabled' do
157-
let(:options) { { keep_failed: true } }
157+
context 'when :keep_failed option is enabled' do
158+
let(:options) { { keep_failed: true } }
158159

159-
it 'also inspects paths which are failed last time' do
160-
guard.failed_paths << failed_path
161-
expect_any_instance_of(Guard::Rubocop::Runner).to receive(:run) do |paths|
162-
expect(paths).to include failed_path
160+
it 'also inspects paths which are failed last time' do
161+
guard.failed_paths << failed_path
162+
expect_any_instance_of(Guard::Rubocop::Runner).to receive(:run) do |paths|
163+
expect(paths).to include failed_path
164+
end
165+
subject
163166
end
164-
guard.run_on_changes(changed_paths)
165167
end
166-
end
167168

168-
context 'when :keep_failed option is disabled' do
169-
let(:options) { { keep_failed: false } }
169+
context 'when :keep_failed option is disabled' do
170+
let(:options) { { keep_failed: false } }
170171

171-
it 'inspects just changed paths' do
172-
guard.failed_paths << failed_path
173-
expect_any_instance_of(Guard::Rubocop::Runner).to receive(:run) do |paths|
174-
expect(paths).to eq(changed_paths)
172+
it 'inspects just changed paths' do
173+
guard.failed_paths << failed_path
174+
expect_any_instance_of(Guard::Rubocop::Runner).to receive(:run) do |paths|
175+
expect(paths).to eq(changed_paths)
176+
end
177+
subject
175178
end
176-
guard.run_on_changes(changed_paths)
177179
end
178180
end
179181
end

0 commit comments

Comments
 (0)