Skip to content

Commit

Permalink
Refactor away String#indent
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Jan 8, 2024
1 parent e74dc47 commit 02f2d35
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
22 changes: 11 additions & 11 deletions Library/Homebrew/test/dev-cmd/bottle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,11 @@ def install
bottle.sha256(catalina: "109c0cb581a7b5d84da36d84b221fb9dd0f8a927b3044d82611791c9907e202e")

expect(homebrew.bottle_output(bottle, nil)).to eq(
<<~RUBY.indent(2),
bottle do
root_url "https://example.com"
sha256 catalina: "109c0cb581a7b5d84da36d84b221fb9dd0f8a927b3044d82611791c9907e202e"
end
<<-RUBY,
bottle do
root_url "https://example.com"
sha256 catalina: "109c0cb581a7b5d84da36d84b221fb9dd0f8a927b3044d82611791c9907e202e"
end
RUBY
)
end
Expand All @@ -526,12 +526,12 @@ def install
bottle.sha256(catalina: "109c0cb581a7b5d84da36d84b221fb9dd0f8a927b3044d82611791c9907e202e")

expect(homebrew.bottle_output(bottle, "ExampleStrategy")).to eq(
<<~RUBY.indent(2),
bottle do
root_url "https://example.com",
using: ExampleStrategy
sha256 catalina: "109c0cb581a7b5d84da36d84b221fb9dd0f8a927b3044d82611791c9907e202e"
end
<<-RUBY,
bottle do
root_url "https://example.com",
using: ExampleStrategy
sha256 catalina: "109c0cb581a7b5d84da36d84b221fb9dd0f8a927b3044d82611791c9907e202e"
end
RUBY
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def install
Formulary.core_path(name).tap do |formula_path|
formula_path.write <<~RUBY
class #{Formulary.class_s(name)} < Formula
#{content.indent(2)}
#{content.gsub(/^(?!$)/, " ")}
end
RUBY
end
Expand Down
18 changes: 9 additions & 9 deletions Library/Homebrew/test/utils/ast/ast_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
describe Utils::AST do
describe ".stanza_text" do
let(:compound_license) do
<<~RUBY.chomp
license all_of: [
:public_domain,
"MIT",
"GPL-3.0-or-later" => { with: "Autoconf-exception-3.0" },
]
<<-RUBY
license all_of: [
:public_domain,
"MIT",
"GPL-3.0-or-later" => { with: "Autoconf-exception-3.0" },
]
RUBY
end

Expand All @@ -36,14 +36,14 @@
it "adds indent to stanza text if specified" do
expect(described_class.stanza_text(:revision, "revision 1", indent: 2)).to eq(" revision 1")
expect(described_class.stanza_text(:license, 'license "MIT"', indent: 2)).to eq(' license "MIT"')
expect(described_class.stanza_text(:license, compound_license, indent: 2)).to eq(compound_license.indent(2))
expect(described_class.stanza_text(:license, compound_license, indent: 2)).to eq(compound_license)
end

it "does not add indent if already indented" do
expect(described_class.stanza_text(:revision, " revision 1", indent: 2)).to eq(" revision 1")
expect(
described_class.stanza_text(:license, compound_license.indent(2), indent: 2),
).to eq(compound_license.indent(2))
described_class.stanza_text(:license, compound_license, indent: 2),
).to eq(compound_license)
end
end
end
8 changes: 4 additions & 4 deletions Library/Homebrew/test/utils/ast/formula_ast_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ class Foo < Formula

describe "#add_bottle_block" do
let(:bottle_output) do
<<~RUBY.chomp.indent(2)
bottle do
sha256 "f7b1fc772c79c20fddf621ccc791090bc1085fcef4da6cca03399424c66e06ca" => :sierra
end
<<-RUBY
bottle do
sha256 "f7b1fc772c79c20fddf621ccc791090bc1085fcef4da6cca03399424c66e06ca" => :sierra
end
RUBY
end

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/utils/ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def stanza_text(name, value, indent: nil)
value if (node.is_a?(SendNode) || node.is_a?(BlockNode)) && node.method_name == name
end
text ||= "#{name} #{value.inspect}"
text = text.indent(indent) if indent && !text.match?(/\A\n* +/)
text.gsub!(/^(?!$)/, " " * indent) if indent && !text.match?(/\A\n* +/)
text
end

Expand Down

0 comments on commit 02f2d35

Please sign in to comment.