Skip to content

Commit 1e7425b

Browse files
author
Guillaume Loubier
committed
Add only_git_changes option
This allows the users to setup the project to only spellcheck files that are tracked by git when using the run_all command.
1 parent 0a8d8f7 commit 1e7425b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/guard/codespell.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def start
3535
# @return [Object] the task result
3636
def run_all
3737
return warn_codespell_missing unless codespell
38+
return run_codespell(changed_files) if options[:only_git_changes]
3839

3940
run_codespell
4041
end
@@ -66,8 +67,9 @@ def run_on_modifications(paths)
6667
def run_codespell(paths = [])
6768
Guard::Compat::UI.info "Looking for typos with codespell"
6869
cmd = build_command(paths)
69-
Guard::Compat::UI.info "... running #{cmd}"
70+
Guard::Compat::UI.info "... running #{cmd}" if options[:debug]
7071
system(cmd)
72+
Guard::Compat::UI.info "Codespell check finished"
7173
end
7274

7375
def build_command(paths)
@@ -95,5 +97,12 @@ def warn_codespell_missing
9597
Guard::Compat::UI.warning "Spellcheck failed - guard-codespell cannot find the codespell library."
9698
Guard::Compat::UI.warning "Please run `pip install codespell` to resolve this issue."
9799
end
100+
101+
# Fetch a list of changed files from the result of git status
102+
def changed_files
103+
`git status --porcelain`.lines.map do |line|
104+
line.chomp.split(" ").last
105+
end
106+
end
98107
end
99108
end

spec/guard/codespell_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
require "guard/codespell"
55

66
RSpec.describe Guard::Codespell do
7-
subject(:guard) { described_class.new({}) }
7+
subject(:guard) { described_class.new(options) }
88

99
let(:paths) { %w[path/to/foo.rb path/to/bar/baz.js] }
10+
let(:options) { {} }
1011

1112
before do
1213
allow(guard).to receive(:run_codespell)
@@ -39,6 +40,17 @@
3940
guard.run_all
4041
expect(guard).to have_received(:run_codespell).with(no_args)
4142
end
43+
44+
context "when only_git_changes option is set to true" do
45+
let(:options) { { only_git_changes: true } }
46+
47+
it "calls run_codespell without any args" do
48+
allow(guard).to receive(:changed_files).and_return(paths)
49+
50+
guard.run_all
51+
expect(guard).to have_received(:run_codespell).with(paths)
52+
end
53+
end
4254
end
4355

4456
describe "#run_on_additions" do

0 commit comments

Comments
 (0)