From 86f43f79eeee4157c7c27f109216c4c5ed52da65 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 18 Apr 2019 17:33:02 +0900 Subject: [PATCH] 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. --- Library/Homebrew/brew.sh | 5 ++++- Library/Homebrew/cask/cmd/info.rb | 4 ++-- Library/Homebrew/diagnostic.rb | 3 --- Library/Homebrew/extend/io.rb | 8 ++++---- Library/Homebrew/extend/os/mac/extend/ENV/super.rb | 4 ++-- Library/Homebrew/extend/pathname.rb | 7 ++++--- Library/Homebrew/formula_installer.rb | 2 +- Library/Homebrew/patch.rb | 4 ++-- Library/Homebrew/sandbox.rb | 4 ++-- Library/Homebrew/shims/super/cc | 4 ++-- Library/Homebrew/utils.rb | 3 ++- Library/Homebrew/utils/formatter.rb | 8 +++++--- Library/Homebrew/utils/ruby.sh | 2 +- 13 files changed, 31 insertions(+), 27 deletions(-) diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index ba6592eba5..761738c9ea 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -348,6 +348,9 @@ else RUBY_DISABLE_OPTIONS="--disable=gems,rubyopt" fi +# Don't set this for anyone (yet) +unset HOMEBREW_FROZEN_STRING_LITERAL + if [[ -z "$HOMEBREW_RUBY_WARNINGS" ]] then export HOMEBREW_RUBY_WARNINGS="-W0" @@ -499,5 +502,5 @@ else # Unshift command back into argument list (unless argument list was empty). [[ "$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 diff --git a/Library/Homebrew/cask/cmd/info.rb b/Library/Homebrew/cask/cmd/info.rb index 29b5c541b6..9899e60060 100644 --- a/Library/Homebrew/cask/cmd/info.rb +++ b/Library/Homebrew/cask/cmd/info.rb @@ -56,7 +56,7 @@ module Cask end def self.installation_info(cask) - install_info = "" + install_info = +"" if cask.installed? cask.versions.each do |version| versioned_staged_path = cask.caskroom_path.join(version) @@ -69,7 +69,7 @@ module Cask end, ).concat(")\n") end - install_info + install_info.freeze else "Not installed\n" end diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index bb4c41991e..2560cfd7a7 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -389,9 +389,6 @@ module Homebrew /usr/bin occurs before #{HOMEBREW_PREFIX}/bin This means that system-provided programs will be used instead of those provided by Homebrew. The following tools exist at both paths: - EOS - - message += <<~EOS Consider setting your PATH so that #{HOMEBREW_PREFIX}/bin occurs before /usr/bin. Here is a one-liner: diff --git a/Library/Homebrew/extend/io.rb b/Library/Homebrew/extend/io.rb index 70ded984f7..4ddfecde27 100644 --- a/Library/Homebrew/extend/io.rb +++ b/Library/Homebrew/extend/io.rb @@ -1,7 +1,7 @@ class IO def readline_nonblock(sep = $INPUT_RECORD_SEPARATOR) - line = "" - buffer = "" + line = +"" + buffer = +"" loop do break if buffer == sep @@ -10,10 +10,10 @@ class IO line.concat(buffer) end - line + line.freeze rescue IO::WaitReadable, EOFError => e raise e if line.empty? - line + line.freeze end end diff --git a/Library/Homebrew/extend/os/mac/extend/ENV/super.rb b/Library/Homebrew/extend/os/mac/extend/ENV/super.rb index 0d8db8c8bb..6cc29ba1e2 100644 --- a/Library/Homebrew/extend/os/mac/extend/ENV/super.rb +++ b/Library/Homebrew/extend/os/mac/extend/ENV/super.rb @@ -87,12 +87,12 @@ module Superenv end def determine_cccfg - s = "" + s = +"" # Fix issue with sed barfing on unicode characters on Mountain Lion s << "s" # Fix issue with >= Mountain Lion apr-1-config having broken paths s << "a" - s + s.freeze end def effective_sysroot diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index 80d5b8e5aa..77c3ce6516 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -17,10 +17,11 @@ module DiskUsageExtension end def abv - out = "" + out = +"" compute_disk_usage out << "#{number_readable(@file_count)} files, " if @file_count > 1 out << disk_usage_readable(@disk_usage).to_s + out.freeze end private @@ -344,8 +345,8 @@ class Pathname # Writes an exec script that sets environment variables def write_env_script(target, env) - env_export = "" - env.each { |key, value| env_export += "#{key}=\"#{value}\" " } + env_export = +"" + env.each { |key, value| env_export << "#{key}=\"#{value}\" " } dirname.mkpath write <<~SH #!/bin/bash diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index acd834f511..afedd08de6 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -668,7 +668,7 @@ class FormulaInstaller s << "#{Emoji.install_badge} " if Emoji.enabled? s << "#{formula.prefix.resolved_path}: #{formula.prefix.abv}" s << ", built in #{pretty_duration build_time}" if build_time - s + s.freeze end def build_time diff --git a/Library/Homebrew/patch.rb b/Library/Homebrew/patch.rb index 3bb604bf82..94d037726d 100644 --- a/Library/Homebrew/patch.rb +++ b/Library/Homebrew/patch.rb @@ -84,7 +84,7 @@ class DATAPatch < EmbeddedPatch end def contents - data = "" + data = +"" path.open("rb") do |f| loop do line = f.gets @@ -94,7 +94,7 @@ class DATAPatch < EmbeddedPatch data << line end end - data + data.freeze end end diff --git a/Library/Homebrew/sandbox.rb b/Library/Homebrew/sandbox.rb index 3392cb018b..413347023f 100644 --- a/Library/Homebrew/sandbox.rb +++ b/Library/Homebrew/sandbox.rb @@ -178,13 +178,13 @@ class Sandbox end def add_rule(rule) - s = "(" + s = +"(" s << (rule[:allow] ? "allow" : "deny") s << " #{rule[:operation]}" s << " (#{rule[:filter]})" if rule[:filter] s << " (with #{rule[:modifier]})" if rule[:modifier] s << ")" - @rules << s + @rules << s.freeze end def dump diff --git a/Library/Homebrew/shims/super/cc b/Library/Homebrew/shims/super/cc index 9957789aba..f50dde4447 100755 --- a/Library/Homebrew/shims/super/cc +++ b/Library/Homebrew/shims/super/cc @@ -5,7 +5,7 @@ then echo "${0##*/}: The build tool has reset ENV; --env=std required." >&2 exit 1 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 require "pathname" @@ -404,7 +404,7 @@ def log(basename, argv, tool, args) adds = args - argv dels = argv - args - s = "" + s = +"" s << "#{basename} called with: #{argv.join(" ")}\n" s << "superenv removed: #{dels.join(" ")}\n" unless dels.empty? s << "superenv added: #{adds.join(" ")}\n" unless adds.empty? diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 97d1a825e4..2305367f89 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -156,12 +156,13 @@ def pretty_duration(s) m = s / 60 s %= 60 res = "#{m} #{"minute".pluralize(m)}" - return res if s.zero? + return res.freeze if s.zero? res << " " end res << "#{s} #{"second".pluralize(s)}" + res.freeze end def interactive_shell(f = nil) diff --git a/Library/Homebrew/utils/formatter.rb b/Library/Homebrew/utils/formatter.rb index a6d6e1002e..f8d012fb31 100644 --- a/Library/Homebrew/utils/formatter.rb +++ b/Library/Homebrew/utils/formatter.rb @@ -94,7 +94,7 @@ module Formatter gap_string = "".rjust(gap_size) - output = "" + output = +"" rows.times do |row_index| 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 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 - output + output.freeze end end diff --git a/Library/Homebrew/utils/ruby.sh b/Library/Homebrew/utils/ruby.sh index b1a2988505..e47c9df9fa 100644 --- a/Library/Homebrew/utils/ruby.sh +++ b/Library/Homebrew/utils/ruby.sh @@ -40,7 +40,7 @@ setup-ruby-path() { ruby_version_new_enough="true" elif [[ -n "$HOMEBREW_RUBY_PATH" && -z "$HOMEBREW_FORCE_VENDOR_RUBY" ]] 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 if [[ -z "$HOMEBREW_RUBY_PATH" || -n "$HOMEBREW_FORCE_VENDOR_RUBY" || "$ruby_version_new_enough" != "true" ]]