Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Apr 17, 2023
1 parent 95bea08 commit f8b27e6
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Library/Homebrew/test/language/perl/shebang_spec.rb
Expand Up @@ -35,7 +35,7 @@
it "can be used to replace Perl shebangs" do
allow(Formulary).to receive(:factory)
allow(Formulary).to receive(:factory).with(perl_f.name).and_return(perl_f)
Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f), file
Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f), file.path

expected_shebang = if OS.mac?
"/usr/bin/perl#{MacOS.preferred_perl_version}"
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/test/language/python/shebang_spec.rb
Expand Up @@ -35,7 +35,7 @@
it "can be used to replace Python shebangs" do
allow(Formulary).to receive(:factory)
allow(Formulary).to receive(:factory).with(python_f.name).and_return(python_f)
Utils::Shebang.rewrite_shebang described_class.detected_python_shebang(f, use_python_from_path: false), file
Utils::Shebang.rewrite_shebang described_class.detected_python_shebang(f, use_python_from_path: false), file.path

expect(File.read(file)).to eq <<~EOS
#!#{HOMEBREW_PREFIX}/opt/python@3.11/bin/python3.11
Expand All @@ -46,7 +46,7 @@
end

it "can be pointed to a `python3` in PATH" do
Utils::Shebang.rewrite_shebang described_class.detected_python_shebang(f, use_python_from_path: true), file
Utils::Shebang.rewrite_shebang described_class.detected_python_shebang(f, use_python_from_path: true), file.path

expect(File.read(file)).to eq <<~EOS
#!/usr/bin/env python3
Expand Down
6 changes: 4 additions & 2 deletions Library/Homebrew/utils/inreplace.rb
Expand Up @@ -20,6 +20,8 @@ def initialize(errors)
end
end

module_function

# Sometimes we have to change a bit before we install. Mostly we
# prefer a patch, but if you need the {Formula#prefix prefix} of
# this formula in the patch you have to resort to `inreplace`,
Expand All @@ -45,7 +47,7 @@ def initialize(errors)
audit_result: T::Boolean,
).void
}
def self.inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter
def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter
after = after.to_s if after.is_a? Symbol

errors = {}
Expand All @@ -71,7 +73,7 @@ def self.inreplace(paths, before = nil, after = nil, audit_result = true) # rubo
end

# @api private
def self.inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false)
def inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false)
str = File.binread(path)
contents = StringInreplaceExtension.new(str)
replacement_pairs.each do |old, new|
Expand Down
7 changes: 7 additions & 0 deletions Library/Homebrew/utils/inreplace.rbi
@@ -0,0 +1,7 @@
# typed: strict

module Utils
module Inreplace
include Kernel
end
end
4 changes: 3 additions & 1 deletion Library/Homebrew/utils/shebang.rb
Expand Up @@ -8,6 +8,8 @@ module Utils
module Shebang
extend T::Sig

module_function

# Specification on how to rewrite a given shebang.
#
# @api private
Expand All @@ -31,7 +33,7 @@ def initialize(regex, max_length, replacement)
#
# @api public
sig { params(rewrite_info: RewriteInfo, paths: T::Array[T.any(String, Pathname)]).void }
def self.rewrite_shebang(rewrite_info, *paths)
def rewrite_shebang(rewrite_info, *paths)
paths.each do |f|
f = Pathname(f)
next unless f.file?
Expand Down
7 changes: 7 additions & 0 deletions Library/Homebrew/utils/shebang.rbi
@@ -0,0 +1,7 @@
# typed: strict

module Utils
module Shebang
include Kernel
end
end
22 changes: 12 additions & 10 deletions Library/Homebrew/utils/shell.rb
Expand Up @@ -5,10 +5,12 @@ module Utils
module Shell
extend T::Sig

module_function

# Take a path and heuristically convert it to a shell name,
# return `nil` if there's no match.
sig { params(path: String).returns(T.nilable(Symbol)) }
def self.from_path(path)
def from_path(path)
# we only care about the basename
shell_name = File.basename(path)
# handle possible version suffix like `zsh-5.2`
Expand All @@ -17,23 +19,23 @@ def self.from_path(path)
end

sig { params(default: String).returns(String) }
def self.preferred_path(default: "")
def preferred_path(default: "")
ENV.fetch("SHELL", default)
end

sig { returns(T.nilable(Symbol)) }
def self.preferred
def preferred
from_path(preferred_path)
end

sig { returns(T.nilable(Symbol)) }
def self.parent
def parent
from_path(`ps -p #{Process.ppid} -o ucomm=`.strip)
end

# Quote values. Quoting keys is overkill.
sig { params(key: String, value: String, shell: T.nilable(Symbol)).returns(T.nilable(String)) }
def self.export_value(key, value, shell = preferred)
def export_value(key, value, shell = preferred)
case shell
when :bash, :ksh, :mksh, :sh, :zsh
"export #{key}=\"#{sh_quote(value)}\""
Expand All @@ -49,7 +51,7 @@ def self.export_value(key, value, shell = preferred)

# Return the shell profile file based on user's preferred shell.
sig { returns(String) }
def self.profile
def profile
case preferred
when :bash
bash_profile = "#{Dir.home}/.bash_profile"
Expand All @@ -62,7 +64,7 @@ def self.profile
end

sig { params(variable: String, value: String).returns(T.nilable(String)) }
def self.set_variable_in_profile(variable, value)
def set_variable_in_profile(variable, value)
case preferred
when :bash, :ksh, :sh, :zsh, nil
"echo 'export #{variable}=#{sh_quote(value)}' >> #{profile}"
Expand All @@ -74,7 +76,7 @@ def self.set_variable_in_profile(variable, value)
end

sig { params(path: String).returns(T.nilable(String)) }
def self.prepend_path_in_profile(path)
def prepend_path_in_profile(path)
case preferred
when :bash, :ksh, :mksh, :sh, :zsh, nil
"echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{profile}"
Expand All @@ -99,7 +101,7 @@ def self.prepend_path_in_profile(path)
UNSAFE_SHELL_CHAR = %r{([^A-Za-z0-9_\-.,:/@~\n])}.freeze

sig { params(str: String).returns(String) }
def self.csh_quote(str)
def csh_quote(str)
# ruby's implementation of shell_escape
str = str.to_s
return "''" if str.empty?
Expand All @@ -113,7 +115,7 @@ def self.csh_quote(str)
end

sig { params(str: String).returns(String) }
def self.sh_quote(str)
def sh_quote(str)
# ruby's implementation of shell_escape
str = str.to_s
return "''" if str.empty?
Expand Down
7 changes: 7 additions & 0 deletions Library/Homebrew/utils/shell.rbi
@@ -0,0 +1,7 @@
# typed: strict

module Utils
module Shell
include Kernel
end
end

0 comments on commit f8b27e6

Please sign in to comment.