Fix formula reporter inconsistencies

This commit is contained in:
Rylan Polster
2025-11-07 11:53:02 -05:00
parent 0236b2fc2d
commit ff719ba39a
3 changed files with 59 additions and 7 deletions
+2 -2
View File
@@ -216,7 +216,7 @@ module Homebrew
names_path = HOMEBREW_CACHE_API/"#{type}_names.txt"
if !names_path.exist? || regenerate
names_path.unlink if names_path.exist?
names_path.write(names.join("\n"))
names_path.write(names.sort.join("\n"))
return true
end
@@ -231,7 +231,7 @@ module Homebrew
"#{alias_name}|#{real_name}"
end
aliases_path.unlink if aliases_path.exist?
aliases_path.write(aliases_text.join("\n"))
aliases_path.write(aliases_text.sort.join("\n"))
return true
end
+18 -5
View File
@@ -785,14 +785,27 @@ class Reporter
api_dir_prefix_basename = T.must(api_dir_prefix).basename
diff_output.lines.filter_map do |line|
diff_hash = diff_output.lines.each_with_object({}) do |line, hash|
next if line.match?(header_regex)
next unless add_delete_characters.include?(line[0])
line.sub(/^\+/, "A #{api_dir_prefix_basename}/")
.sub(/^-/, "D #{api_dir_prefix_basename}/")
.sub(/$/, ".rb")
.chomp
name = line.chomp.delete_prefix("+").delete_prefix("-")
file = "#{api_dir_prefix_basename}/#{name}.rb"
hash[file] ||= 0
if line.start_with?("+")
hash[file] += 1
elsif line.start_with?("-")
hash[file] -= 1
end
end
diff_hash.filter_map do |file, count|
if count.positive?
"A #{file}"
elsif count.negative?
"D #{file}"
end
end.join("\n")
else
Utils.popen_read(
@@ -152,6 +152,45 @@ RSpec.describe Homebrew::Cmd::UpdateReport do
expect(tap.cask_tokens).to include("new-cask")
end
end
describe "#diff" do
context "when using the API" do
subject(:reporter) do
described_class.new(tap,
api_names_txt: Pathname("formula_names.txt"),
api_names_before_txt: Pathname("formula_names_before.txt"),
api_dir_prefix: HOMEBREW_CACHE/"api")
end
it "ignore lines that haven't changed" do
expect(Utils).to receive(:popen_read).and_return(<<~DIFF)
foo
+bar
-baz
DIFF
expect(reporter.send(:diff)).to eq(<<~DIFF.strip)
A api/bar.rb
D api/baz.rb
DIFF
end
it "handles moved lines" do
expect(Utils).to receive(:popen_read).and_return(<<~DIFF)
+baz
foo
+bar
+baz
-bar
-baz
DIFF
expect(reporter.send(:diff)).to eq(<<~DIFF.strip)
A api/baz.rb
DIFF
end
end
end
end
describe ReporterHub do