Enable/fix optional Ruby frozen string literal usage

Combined with https://github.com/Homebrew/homebrew-test-bot/pull/247
this will test Homebrew's use of frozen strings in CI. After this we
will then enable it for Homebrew developers and eventually all Homebrew
users.
This commit is contained in:
Mike McQuaid 2019-04-18 17:33:02 +09:00
parent 513dfa20f6
commit 86f43f79ee
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70
13 changed files with 31 additions and 27 deletions

View File

@ -348,6 +348,9 @@ else
RUBY_DISABLE_OPTIONS="--disable=gems,rubyopt" RUBY_DISABLE_OPTIONS="--disable=gems,rubyopt"
fi fi
# Don't set this for anyone (yet)
unset HOMEBREW_FROZEN_STRING_LITERAL
if [[ -z "$HOMEBREW_RUBY_WARNINGS" ]] if [[ -z "$HOMEBREW_RUBY_WARNINGS" ]]
then then
export HOMEBREW_RUBY_WARNINGS="-W0" export HOMEBREW_RUBY_WARNINGS="-W0"
@ -499,5 +502,5 @@ else
# Unshift command back into argument list (unless argument list was empty). # Unshift command back into argument list (unless argument list was empty).
[[ "$HOMEBREW_ARG_COUNT" -gt 0 ]] && set -- "$HOMEBREW_COMMAND" "$@" [[ "$HOMEBREW_ARG_COUNT" -gt 0 ]] && set -- "$HOMEBREW_COMMAND" "$@"
{ update-preinstall "$@"; exec "$HOMEBREW_RUBY_PATH" $HOMEBREW_RUBY_WARNINGS "$RUBY_DISABLE_OPTIONS" "$HOMEBREW_LIBRARY/Homebrew/brew.rb" "$@"; } { update-preinstall "$@"; exec "$HOMEBREW_RUBY_PATH" $HOMEBREW_FROZEN_STRING_LITERAL $HOMEBREW_RUBY_WARNINGS "$RUBY_DISABLE_OPTIONS" "$HOMEBREW_LIBRARY/Homebrew/brew.rb" "$@"; }
fi fi

View File

@ -56,7 +56,7 @@ module Cask
end end
def self.installation_info(cask) def self.installation_info(cask)
install_info = "" install_info = +""
if cask.installed? if cask.installed?
cask.versions.each do |version| cask.versions.each do |version|
versioned_staged_path = cask.caskroom_path.join(version) versioned_staged_path = cask.caskroom_path.join(version)
@ -69,7 +69,7 @@ module Cask
end, end,
).concat(")\n") ).concat(")\n")
end end
install_info install_info.freeze
else else
"Not installed\n" "Not installed\n"
end end

View File

@ -389,9 +389,6 @@ module Homebrew
/usr/bin occurs before #{HOMEBREW_PREFIX}/bin /usr/bin occurs before #{HOMEBREW_PREFIX}/bin
This means that system-provided programs will be used instead of those This means that system-provided programs will be used instead of those
provided by Homebrew. The following tools exist at both paths: provided by Homebrew. The following tools exist at both paths:
EOS
message += <<~EOS
Consider setting your PATH so that #{HOMEBREW_PREFIX}/bin Consider setting your PATH so that #{HOMEBREW_PREFIX}/bin
occurs before /usr/bin. Here is a one-liner: occurs before /usr/bin. Here is a one-liner:

View File

@ -1,7 +1,7 @@
class IO class IO
def readline_nonblock(sep = $INPUT_RECORD_SEPARATOR) def readline_nonblock(sep = $INPUT_RECORD_SEPARATOR)
line = "" line = +""
buffer = "" buffer = +""
loop do loop do
break if buffer == sep break if buffer == sep
@ -10,10 +10,10 @@ class IO
line.concat(buffer) line.concat(buffer)
end end
line line.freeze
rescue IO::WaitReadable, EOFError => e rescue IO::WaitReadable, EOFError => e
raise e if line.empty? raise e if line.empty?
line line.freeze
end end
end end

View File

@ -87,12 +87,12 @@ module Superenv
end end
def determine_cccfg def determine_cccfg
s = "" s = +""
# Fix issue with sed barfing on unicode characters on Mountain Lion # Fix issue with sed barfing on unicode characters on Mountain Lion
s << "s" s << "s"
# Fix issue with >= Mountain Lion apr-1-config having broken paths # Fix issue with >= Mountain Lion apr-1-config having broken paths
s << "a" s << "a"
s s.freeze
end end
def effective_sysroot def effective_sysroot

View File

@ -17,10 +17,11 @@ module DiskUsageExtension
end end
def abv def abv
out = "" out = +""
compute_disk_usage compute_disk_usage
out << "#{number_readable(@file_count)} files, " if @file_count > 1 out << "#{number_readable(@file_count)} files, " if @file_count > 1
out << disk_usage_readable(@disk_usage).to_s out << disk_usage_readable(@disk_usage).to_s
out.freeze
end end
private private
@ -344,8 +345,8 @@ class Pathname
# Writes an exec script that sets environment variables # Writes an exec script that sets environment variables
def write_env_script(target, env) def write_env_script(target, env)
env_export = "" env_export = +""
env.each { |key, value| env_export += "#{key}=\"#{value}\" " } env.each { |key, value| env_export << "#{key}=\"#{value}\" " }
dirname.mkpath dirname.mkpath
write <<~SH write <<~SH
#!/bin/bash #!/bin/bash

View File

@ -668,7 +668,7 @@ class FormulaInstaller
s << "#{Emoji.install_badge} " if Emoji.enabled? s << "#{Emoji.install_badge} " if Emoji.enabled?
s << "#{formula.prefix.resolved_path}: #{formula.prefix.abv}" s << "#{formula.prefix.resolved_path}: #{formula.prefix.abv}"
s << ", built in #{pretty_duration build_time}" if build_time s << ", built in #{pretty_duration build_time}" if build_time
s s.freeze
end end
def build_time def build_time

View File

@ -84,7 +84,7 @@ class DATAPatch < EmbeddedPatch
end end
def contents def contents
data = "" data = +""
path.open("rb") do |f| path.open("rb") do |f|
loop do loop do
line = f.gets line = f.gets
@ -94,7 +94,7 @@ class DATAPatch < EmbeddedPatch
data << line data << line
end end
end end
data data.freeze
end end
end end

View File

@ -178,13 +178,13 @@ class Sandbox
end end
def add_rule(rule) def add_rule(rule)
s = "(" s = +"("
s << (rule[:allow] ? "allow" : "deny") s << (rule[:allow] ? "allow" : "deny")
s << " #{rule[:operation]}" s << " #{rule[:operation]}"
s << " (#{rule[:filter]})" if rule[:filter] s << " (#{rule[:filter]})" if rule[:filter]
s << " (with #{rule[:modifier]})" if rule[:modifier] s << " (with #{rule[:modifier]})" if rule[:modifier]
s << ")" s << ")"
@rules << s @rules << s.freeze
end end
def dump def dump

View File

@ -5,7 +5,7 @@ then
echo "${0##*/}: The build tool has reset ENV; --env=std required." >&2 echo "${0##*/}: The build tool has reset ENV; --env=std required." >&2
exit 1 exit 1
fi fi
exec "$HOMEBREW_RUBY_PATH" --disable=gems,did_you_mean,rubyopt -x "$0" "$@" exec "$HOMEBREW_RUBY_PATH" --enable-frozen-string-literal --disable=gems,did_you_mean,rubyopt -x "$0" "$@"
#!/usr/bin/env ruby -W0 #!/usr/bin/env ruby -W0
require "pathname" require "pathname"
@ -404,7 +404,7 @@ def log(basename, argv, tool, args)
adds = args - argv adds = args - argv
dels = argv - args dels = argv - args
s = "" s = +""
s << "#{basename} called with: #{argv.join(" ")}\n" s << "#{basename} called with: #{argv.join(" ")}\n"
s << "superenv removed: #{dels.join(" ")}\n" unless dels.empty? s << "superenv removed: #{dels.join(" ")}\n" unless dels.empty?
s << "superenv added: #{adds.join(" ")}\n" unless adds.empty? s << "superenv added: #{adds.join(" ")}\n" unless adds.empty?

View File

@ -156,12 +156,13 @@ def pretty_duration(s)
m = s / 60 m = s / 60
s %= 60 s %= 60
res = "#{m} #{"minute".pluralize(m)}" res = "#{m} #{"minute".pluralize(m)}"
return res if s.zero? return res.freeze if s.zero?
res << " " res << " "
end end
res << "#{s} #{"second".pluralize(s)}" res << "#{s} #{"second".pluralize(s)}"
res.freeze
end end
def interactive_shell(f = nil) def interactive_shell(f = nil)

View File

@ -94,7 +94,7 @@ module Formatter
gap_string = "".rjust(gap_size) gap_string = "".rjust(gap_size)
output = "" output = +""
rows.times do |row_index| rows.times do |row_index|
item_indices_for_row = row_index.step(objects.size - 1, rows).to_a item_indices_for_row = row_index.step(objects.size - 1, rows).to_a
@ -106,9 +106,11 @@ module Formatter
# don't add trailing whitespace to last column # don't add trailing whitespace to last column
last = objects.values_at(item_indices_for_row.last) last = objects.values_at(item_indices_for_row.last)
output.concat((first_n + last).join(gap_string)).concat("\n") output.concat((first_n + last)
.join(gap_string))
.concat("\n")
end end
output output.freeze
end end
end end

View File

@ -40,7 +40,7 @@ setup-ruby-path() {
ruby_version_new_enough="true" ruby_version_new_enough="true"
elif [[ -n "$HOMEBREW_RUBY_PATH" && -z "$HOMEBREW_FORCE_VENDOR_RUBY" ]] elif [[ -n "$HOMEBREW_RUBY_PATH" && -z "$HOMEBREW_FORCE_VENDOR_RUBY" ]]
then then
ruby_version_new_enough="$("$HOMEBREW_RUBY_PATH" --disable=gems -rrubygems -e "puts Gem::Version.new(RUBY_VERSION.to_s.dup) >= Gem::Version.new('$minimum_ruby_version')")" ruby_version_new_enough="$("$HOMEBREW_RUBY_PATH" --enable-frozen-string-literal --disable=gems,did_you_mean,rubyopt -rrubygems -e "puts Gem::Version.new(RUBY_VERSION.to_s.dup) >= Gem::Version.new('$minimum_ruby_version')")"
fi fi
if [[ -z "$HOMEBREW_RUBY_PATH" || -n "$HOMEBREW_FORCE_VENDOR_RUBY" || "$ruby_version_new_enough" != "true" ]] if [[ -z "$HOMEBREW_RUBY_PATH" || -n "$HOMEBREW_FORCE_VENDOR_RUBY" || "$ruby_version_new_enough" != "true" ]]