tests for shell-specific diagnostic message

This commit is contained in:
Greg Nisbet 2016-05-22 18:02:39 -07:00
parent f0cc815d86
commit bf63c08d50
5 changed files with 35 additions and 12 deletions

View File

@ -13,9 +13,8 @@ module Homebrew
ENV.universal_binary if ARGV.build_universal? ENV.universal_binary if ARGV.build_universal?
shell_value = ARGV.value("shell") shell_value = ARGV.value("shell")
has_plain = ARGV.include?("--plain")
if has_plain if ARGV.include?("--plain")
shell = nil shell = nil
elsif shell_value.nil? elsif shell_value.nil?
# legacy behavior # legacy behavior

View File

@ -496,7 +496,7 @@ module Homebrew
<<-EOS.undent <<-EOS.undent
Homebrew's bin was not found in your PATH. Homebrew's bin was not found in your PATH.
Consider setting the PATH for example like so Consider setting the PATH for example like so
#{Utils::Shell.prepend_path_in_shell_profile("#{HOMEBREW_PREFIX}/bin:$PATH")} #{Utils::Shell.prepend_path_in_shell_profile("#{HOMEBREW_PREFIX}/bin")}
EOS EOS
end end
@ -511,7 +511,7 @@ module Homebrew
Homebrew's sbin was not found in your PATH but you have installed Homebrew's sbin was not found in your PATH but you have installed
formulae that put executables in #{HOMEBREW_PREFIX}/sbin. formulae that put executables in #{HOMEBREW_PREFIX}/sbin.
Consider setting the PATH for example like so Consider setting the PATH for example like so
#{Utils::Shell.prepend_path_in_shell_profile("#{HOMEBREW_PREFIX}/sbin:$PATH")} #{Utils::Shell.prepend_path_in_shell_profile("#{HOMEBREW_PREFIX}/sbin")}
EOS EOS
end end

View File

@ -238,7 +238,7 @@ class IntegrationCommandTests < Homebrew::TestCase
end end
def test_env_csh def test_env_csh
assert_match %r{setenv CMAKE_PREFIX_PATH #{Regexp.quote(HOMEBREW_PREFIX.to_s)}}, assert_match %r{setenv CMAKE_PREFIX_PATH #{Regexp.quote(HOMEBREW_PREFIX.to_s)};},
cmd("--env", "--shell=tcsh") cmd("--env", "--shell=tcsh")
end end

View File

@ -35,4 +35,25 @@ class ShellSmokeTest < Homebrew::TestCase
assert_equal "\\$", Utils::Shell.csh_quote("$") assert_equal "\\$", Utils::Shell.csh_quote("$")
assert_equal "word", Utils::Shell.csh_quote("word") assert_equal "word", Utils::Shell.csh_quote("word")
end end
def prepend_path_shell(shell, path, fragment)
original_shell = ENV["SHELL"]
ENV["SHELL"] = shell
prepend_message = Utils::Shell.prepend_path_in_shell_profile(path)
assert(
prepend_message.start_with?(fragment),
"#{shell}: expected #{prepend_message} to match #{fragment}"
)
ENV["SHELL"] = original_shell
end
def test_prepend_path_in_shell_profile()
prepend_path_shell "/bin/tcsh", "/path", "echo 'setenv PATH /path"
prepend_path_shell "/bin/bash", "/path", "echo 'export PATH=\"/path"
prepend_path_shell "/usr/local/bin/fish", "/path", "echo 'set -g fish_user_paths \"/path\" $fish_user_paths' >>"
end
end end

View File

@ -10,8 +10,10 @@ module Utils
}.freeze }.freeze
module Shell module Shell
UNSAFE_SHELL_CHAR = /([^A-Za-z0-9_\-.,:\/@\n])/
# take a path and heuristically convert it # take a path and heuristically convert it
# to a shell, return nil if there's no match # to a shell name, return nil if there's no match
def self.path_to_shell(path) def self.path_to_shell(path)
# we only care about the basename # we only care about the basename
shell_name = File.basename(path) shell_name = File.basename(path)
@ -34,7 +36,8 @@ module Utils
return "''" if str.empty? return "''" if str.empty?
str = str.dup str = str.dup
# anything that isn't a known safe character is padded # anything that isn't a known safe character is padded
str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/, "\\\\" + "\\1") str.gsub!(UNSAFE_SHELL_CHAR, "\\\\" + "\\1")
# newlines have to be specially quoted in csh
str.gsub!(/\n/, "'\\\n'") str.gsub!(/\n/, "'\\\n'")
str str
end end
@ -45,7 +48,7 @@ module Utils
return "''" if str.empty? return "''" if str.empty?
str = str.dup str = str.dup
# anything that isn't a known safe character is padded # anything that isn't a known safe character is padded
str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/, "\\\\" + "\\1") str.gsub!(UNSAFE_SHELL_CHAR, "\\\\" + "\\1")
str.gsub!(/\n/, "'\n'") str.gsub!(/\n/, "'\n'")
str str
end end
@ -61,7 +64,7 @@ module Utils
# and a literal \ can be included via \\ # and a literal \ can be included via \\
"set -gx #{key} \"#{sh_quote(value)}\"" "set -gx #{key} \"#{sh_quote(value)}\""
when :csh, :tcsh when :csh, :tcsh
"setenv #{key} #{csh_quote(value)}" "setenv #{key} #{csh_quote(value)};"
end end
end end
@ -72,12 +75,12 @@ module Utils
def self.prepend_path_in_shell_profile(path) def self.prepend_path_in_shell_profile(path)
case preferred_shell case preferred_shell
when :bash, :ksh, :sh, :zsh when :bash, :ksh, :sh, :zsh, nil
"echo 'export PATH=\"#{sh_quote(path)}:$PATH >> #{shell_profile}" "echo 'export PATH=\"#{sh_quote(path)}:$PATH'\" >> #{shell_profile}"
when :csh, :tcsh when :csh, :tcsh
"echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{shell_profile}" "echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{shell_profile}"
when :fish when :fish
"echo 'set -g fish_user_paths $fish_user_paths >> #{sh_quote(path)}' >> #{shell_profile}" "echo 'set -g fish_user_paths \"#{sh_quote(path)}\" $fish_user_paths' >> #{shell_profile}"
end end
end end
end end