Minor Kernel cleanup

This commit is contained in:
Douglas Eichelberger 2023-03-14 10:23:29 -07:00
parent 1a86e001a5
commit a1748605ab
4 changed files with 25 additions and 34 deletions

View File

@ -351,16 +351,6 @@ module Kernel
# rubocop:enable Style/GlobalVars # rubocop:enable Style/GlobalVars
end end
sig { returns(String) }
def capture_stderr
old = $stderr
$stderr = StringIO.new
yield
$stderr.string
ensure
$stderr = old
end
def redirect_stdout(file) def redirect_stdout(file)
out = $stdout.dup out = $stdout.dup
$stdout.reopen(file) $stdout.reopen(file)
@ -428,17 +418,6 @@ module Kernel
@paths ||= ORIGINAL_PATHS.uniq.map(&:to_s) @paths ||= ORIGINAL_PATHS.uniq.map(&:to_s)
end end
def parse_author!(author)
match_data = /^(?<name>[^<]+?)[ \t]*<(?<email>[^>]+?)>$/.match(author)
if match_data
name = match_data[:name]
email = match_data[:email]
end
raise UsageError, "Unable to parse name and email." if name.blank? && email.blank?
{ name: name, email: email }
end
def disk_usage_readable(size_in_bytes) def disk_usage_readable(size_in_bytes)
if size_in_bytes >= 1_073_741_824 if size_in_bytes >= 1_073_741_824
size = size_in_bytes.to_f / 1_073_741_824 size = size_in_bytes.to_f / 1_073_741_824

View File

@ -200,19 +200,6 @@ describe "globally-scoped helper methods" do
end end
end end
specify "#parse_author!" do
parse_error_msg = /Unable to parse name and email/
expect(parse_author!("John Doe <john.doe@example.com>"))
.to eq({ name: "John Doe", email: "john.doe@example.com" })
expect { parse_author!("") }
.to raise_error(parse_error_msg)
expect { parse_author!("John Doe") }
.to raise_error(parse_error_msg)
expect { parse_author!("<john.doe@example.com>") }
.to raise_error(parse_error_msg)
end
specify "#disk_usage_readable" do specify "#disk_usage_readable" do
expect(disk_usage_readable(1)).to eq("1B") expect(disk_usage_readable(1)).to eq("1B")
expect(disk_usage_readable(1000)).to eq("1000B") expect(disk_usage_readable(1000)).to eq("1000B")

View File

@ -33,6 +33,19 @@ describe Utils do
end end
end end
specify ".parse_author!" do
parse_error_msg = /Unable to parse name and email/
expect(described_class.parse_author!("John Doe <john.doe@example.com>"))
.to eq({ name: "John Doe", email: "john.doe@example.com" })
expect { described_class.parse_author!("") }
.to raise_error(parse_error_msg)
expect { described_class.parse_author!("John Doe") }
.to raise_error(parse_error_msg)
expect { described_class.parse_author!("<john.doe@example.com>") }
.to raise_error(parse_error_msg)
end
describe ".pluralize" do describe ".pluralize" do
it "combines the stem with the default suffix based on the count" do it "combines the stem with the default suffix based on the count" do
expect(described_class.pluralize("foo", 0)).to eq("foos") expect(described_class.pluralize("foo", 0)).to eq("foos")

View File

@ -132,6 +132,18 @@ module Utils
"#{stem}#{suffix}" "#{stem}#{suffix}"
end end
sig { params(author: String).returns({ email: String, name: String }) }
def self.parse_author!(author)
match_data = /^(?<name>[^<]+?)[ \t]*<(?<email>[^>]+?)>$/.match(author)
if match_data
name = match_data[:name]
email = match_data[:email]
end
raise UsageError, "Unable to parse name and email." if name.blank? && email.blank?
{ name: T.must(name), email: T.must(email) }
end
# Makes an underscored, lowercase form from the expression in the string. # Makes an underscored, lowercase form from the expression in the string.
# #
# Changes '::' to '/' to convert namespaces to paths. # Changes '::' to '/' to convert namespaces to paths.