Merge pull request #14973 from dduugg/kernel-cleanup

Minor Kernel cleanup
This commit is contained in:
Mike McQuaid 2023-03-14 15:05:53 -04:00 committed by GitHub
commit 0d5e291fe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 42 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

@ -182,14 +182,6 @@ describe "globally-scoped helper methods" do
expect(which_editor).to eq("vemate -w") expect(which_editor).to eq("vemate -w")
end end
specify "#capture_stderr" do
err = capture_stderr do
$stderr.print "test"
end
expect(err).to eq("test")
end
describe "#pretty_duration" do describe "#pretty_duration" do
it "converts seconds to a human-readable string" do it "converts seconds to a human-readable string" do
expect(pretty_duration(1)).to eq("1 second") expect(pretty_duration(1)).to eq("1 second")
@ -200,19 +192,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.