Skip to content

Commit 4d9678b

Browse files
committed
Removed collector totals and updated CLI actions
We can delegate to the reporter now that the reporter has all of this information. All CLI actions were updated to reflect these changes including dealing with the loading of commits where applicable since the analyzers won't do this by default anymore. Milestone: major
1 parent 9383964 commit 4d9678b

File tree

8 files changed

+51
-174
lines changed

8 files changed

+51
-174
lines changed

lib/git/lint/cli/actions/analyze/branch.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,27 @@ class Branch < Sod::Action
1515

1616
on %w[-b --branch]
1717

18-
def initialize(analyzer: Analyzer.new, **)
18+
def initialize(analyzer: Analyzer.new, loader: Git::Lint::Commits::Loader.new, **)
1919
super(**)
2020
@analyzer = analyzer
21+
@loader = loader
2122
end
2223

2324
def call(*)
24-
parse
25+
report
2526
rescue Errors::Base => error
26-
logger.error { error.message }
27-
kernel.abort
27+
logger.abort error.message
2828
end
2929

3030
private
3131

32-
attr_reader :analyzer
32+
attr_reader :analyzer, :loader
3333

34-
def parse
35-
analyzer.call do |collector, reporter|
34+
def report
35+
loader.call.bind do |commits|
36+
reporter = analyzer.call commits
3637
io.puts reporter
37-
kernel.abort if collector.errors?
38+
kernel.abort if reporter.errors?
3839
end
3940
end
4041
end

lib/git/lint/cli/actions/analyze/commit.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@ def initialize(analyzer: Analyzer.new, **)
2121
end
2222

2323
def call *arguments
24-
process arguments.unshift "-1"
24+
collect arguments.unshift("-1")
2525
rescue Errors::Base => error
26-
logger.error { error.message }
27-
kernel.abort
26+
logger.abort error.message
2827
end
2928

3029
private
3130

3231
attr_reader :analyzer
3332

34-
def process arguments
35-
analyzer.call commits: git.commits(*arguments) do |collector, reporter|
36-
io.puts reporter
37-
kernel.abort if collector.errors?
38-
end
33+
def collect arguments
34+
git.commits(*arguments).either -> commits { report commits },
35+
-> message { logger.abort message.chomp }
36+
end
37+
38+
def report commits
39+
reporter = analyzer.call commits
40+
41+
io.puts reporter
42+
kernel.abort if reporter.errors?
3943
end
4044
end
4145
end

lib/git/lint/cli/actions/hook.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ def initialize(analyzer: Analyzer.new, **)
2121
end
2222

2323
def call path
24-
analyzer.call commits: commits(path) do |collector, reporter|
25-
io.puts reporter
26-
kernel.abort if collector.errors?
27-
end
24+
reporter = analyzer.call read(path)
25+
26+
io.puts reporter
27+
kernel.abort if reporter.errors?
2828
end
2929

3030
private
3131

3232
attr_reader :analyzer
3333

34-
def commits(path) = git.uncommitted(path).fmap { |commit| [commit] }
34+
def read(path) = git.uncommitted(path).bind { |commit| [commit] }
3535
end
3636
end
3737
end

lib/git/lint/collector.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,6 @@ def clear = collection.clear && self
3131

3232
def empty? = collection.empty?
3333

34-
def warnings? = collection.values.flatten.any?(&:warning?)
35-
36-
def errors? = collection.values.flatten.any?(&:error?)
37-
38-
def issues? = collection.values.flatten.any?(&:invalid?)
39-
40-
def total_warnings = collection.values.flatten.count(&:warning?)
41-
42-
def total_errors = collection.values.flatten.count(&:error?)
43-
44-
def total_issues = collection.values.flatten.count(&:invalid?)
45-
46-
def total_commits = collection.keys.size
47-
4834
def to_h = collection
4935

5036
private

spec/lib/git/lint/cli/actions/analyze/branch_spec.rb

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
include_context "with Git repository"
1313

1414
describe "#call" do
15+
before { allow(logger).to receive(:abort) }
16+
1517
it "reports no issues with valid commits" do
1618
git_repo_dir.change_dir do
1719
`git switch --quiet --create test`
@@ -50,22 +52,14 @@
5052
end
5153
end
5254

53-
context "with failure" do
54-
subject(:action) { described_class.new analyzer: }
55-
56-
let(:analyzer) { instance_double Git::Lint::Analyzer }
55+
it "logs error for failure" do
56+
analyzer = instance_double Git::Lint::Analyzer
57+
action = described_class.new(analyzer:)
5758

58-
before { allow(analyzer).to receive(:call).and_raise(Git::Lint::Errors::Base, "Danger!") }
59-
60-
it "logs error" do
61-
action.call
62-
expect(logger.reread).to match(/🛑.+Danger!/)
63-
end
59+
allow(analyzer).to receive(:call).and_raise(Git::Lint::Errors::Base, "Danger!")
60+
action.call
6461

65-
it "aborts" do
66-
action.call
67-
expect(kernel).to have_received(:abort)
68-
end
62+
expect(logger).to have_received(:abort).with("Danger!")
6963
end
7064
end
7165
end

spec/lib/git/lint/cli/actions/analyze/commit_spec.rb

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
include_context "with Git repository"
1313

1414
describe "#call" do
15+
before { allow(logger).to receive(:abort) }
16+
1517
it "reports zero issues with valid SHA" do
1618
git_repo_dir.change_dir do
1719
`git switch --quiet --create test`
1820
`touch b.txt && git add . && git commit --no-verify --message "Added A" --message "Test."`
1921

20-
action.call "-1"
22+
action.call
2123

2224
expect(io.reread).to match(/.+1 commit inspected.*0 issues.+detected/m)
2325
end
@@ -28,7 +30,7 @@
2830
`git switch --quiet --create test`
2931
`touch a.txt && git add . && git commit --no-verify --message "Add A"`
3032

31-
action.call "-1"
33+
action.call
3234

3335
expect(io.reread).to match(
3436
/Commit Subject Prefix Error.+1 commit inspected.*1 issue.+detected/m
@@ -37,11 +39,16 @@
3739
end
3840

3941
it "reports zero issues when given no SHAs" do
42+
allow(logger).to receive(:abort)
43+
4044
git_repo_dir.change_dir do
4145
`rm -rf .git && git init`
4246
action.call
43-
expect(io.reread).to match(/.+0 commits inspected.*0 issues.+detected/m)
4447
end
48+
49+
expect(logger).to have_received(:abort).with(
50+
"fatal: your current branch 'main' does not have any commits yet"
51+
)
4552
end
4653

4754
it "aborts with invalid SHA" do
@@ -56,22 +63,14 @@
5663
end
5764
end
5865

59-
context "with failure" do
60-
subject(:action) { described_class.new analyzer: }
61-
62-
let(:analyzer) { instance_double Git::Lint::Analyzer }
66+
it "logs error for failure" do
67+
analyzer = instance_double Git::Lint::Analyzer
68+
action = described_class.new(analyzer:)
6369

64-
before { allow(analyzer).to receive(:call).and_raise(Git::Lint::Errors::Base, "Danger!") }
70+
allow(analyzer).to receive(:call).and_raise(Git::Lint::Errors::Base, "Danger!")
71+
action.call
6572

66-
it "logs error" do
67-
action.call
68-
expect(logger.reread).to match(/🛑.+Danger!/)
69-
end
70-
71-
it "aborts" do
72-
action.call
73-
expect(kernel).to have_received(:abort)
74-
end
73+
expect(logger).to have_received(:abort).with "Danger!"
7574
end
7675
end
7776
end

spec/lib/git/lint/cli/actions/hook_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
end
2727
end
2828

29-
it "aborts with errors" do
29+
it "aborts with invalid commit" do
3030
git_repo_dir.change_dir do
3131
action.call SPEC_ROOT.join("support/fixtures/commit-invalid.txt")
3232
expect(kernel).to have_received(:abort)

spec/lib/git/lint/collector_spec.rb

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -113,113 +113,6 @@
113113
end
114114
end
115115

116-
describe "#warnings?" do
117-
it "answers true with invalid analyzers at warn severity" do
118-
collector.add valid_analyzer
119-
collector.add warn_analyzer
120-
collector.add error_analyzer
121-
122-
expect(collector.warnings?).to be(true)
123-
end
124-
125-
it "answers false with no invalid analyzers at warn severity" do
126-
collector.add valid_analyzer
127-
collector.add error_analyzer
128-
129-
expect(collector.warnings?).to be(false)
130-
end
131-
end
132-
133-
describe "#errors?" do
134-
it "answers true with invalid analyzers at error severity" do
135-
collector.add valid_analyzer
136-
collector.add warn_analyzer
137-
collector.add error_analyzer
138-
139-
expect(collector.errors?).to be(true)
140-
end
141-
142-
it "answers false with no invalid analyzers at error severity" do
143-
collector.add valid_analyzer
144-
collector.add warn_analyzer
145-
146-
expect(collector.errors?).to be(false)
147-
end
148-
end
149-
150-
describe "#issues?" do
151-
it "answers true with invalid analyzers" do
152-
collector.add valid_analyzer
153-
collector.add warn_analyzer
154-
collector.add error_analyzer
155-
156-
expect(collector.issues?).to be(true)
157-
end
158-
159-
it "answers false with valid analyzers" do
160-
collector.add valid_analyzer
161-
expect(collector.issues?).to be(false)
162-
end
163-
end
164-
165-
describe "#total_warnings" do
166-
it "answers total warnings when invalid analyzers with warnings exist" do
167-
collector.add warn_analyzer
168-
expect(collector.total_warnings).to eq(1)
169-
end
170-
171-
it "answers zero warnings when analyzers without warnings exist" do
172-
collector.add valid_analyzer
173-
collector.add error_analyzer
174-
175-
expect(collector.total_warnings).to eq(0)
176-
end
177-
end
178-
179-
describe "#total_errors" do
180-
it "answers total errors when invalid analyzers with errors exist" do
181-
collector.add error_analyzer
182-
expect(collector.total_errors).to eq(1)
183-
end
184-
185-
it "answers zero errors when analyzers without errors exist" do
186-
collector.add valid_analyzer
187-
collector.add warn_analyzer
188-
189-
expect(collector.total_errors).to eq(0)
190-
end
191-
end
192-
193-
describe "#total_issues" do
194-
it "answers total issues when invalid analyzers exist" do
195-
collector.add valid_analyzer
196-
collector.add warn_analyzer
197-
collector.add error_analyzer
198-
199-
expect(collector.total_issues).to eq(2)
200-
end
201-
202-
it "answers zero issues when valid analyzers exist" do
203-
collector.add valid_analyzer
204-
expect(collector.total_issues).to eq(0)
205-
end
206-
207-
it "answers zero issues when no analyzers exist" do
208-
expect(collector.total_issues).to eq(0)
209-
end
210-
end
211-
212-
describe "#total_commits" do
213-
it "answers total commits" do
214-
collector.add valid_analyzer
215-
expect(collector.total_commits).to eq(1)
216-
end
217-
218-
it "answers zero with no commits" do
219-
expect(collector.total_commits).to eq(0)
220-
end
221-
end
222-
223116
describe "#to_h" do
224117
it "answers hash of commit and analyzers" do
225118
collector.add valid_analyzer

0 commit comments

Comments
 (0)