Skip to content
Snippets Groups Projects
Commit bf63c08d authored by Greg Nisbet's avatar Greg Nisbet
Browse files

tests for shell-specific diagnostic message

parent f0cc815d
No related branches found
No related tags found
No related merge requests found
......@@ -13,9 +13,8 @@ module Homebrew
ENV.universal_binary if ARGV.build_universal?
shell_value = ARGV.value("shell")
has_plain = ARGV.include?("--plain")
if has_plain
if ARGV.include?("--plain")
shell = nil
elsif shell_value.nil?
# legacy behavior
......
......@@ -496,7 +496,7 @@ module Homebrew
<<-EOS.undent
Homebrew's bin was not found in your PATH.
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
end
......@@ -511,7 +511,7 @@ module Homebrew
Homebrew's sbin was not found in your PATH but you have installed
formulae that put executables in #{HOMEBREW_PREFIX}/sbin.
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
end
......
......@@ -238,7 +238,7 @@ class IntegrationCommandTests < Homebrew::TestCase
end
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")
end
......
......@@ -35,4 +35,25 @@ class ShellSmokeTest < Homebrew::TestCase
assert_equal "\\$", Utils::Shell.csh_quote("$")
assert_equal "word", Utils::Shell.csh_quote("word")
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
......@@ -10,8 +10,10 @@ module Utils
}.freeze
module Shell
UNSAFE_SHELL_CHAR = /([^A-Za-z0-9_\-.,:\/@\n])/
# 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)
# we only care about the basename
shell_name = File.basename(path)
......@@ -34,7 +36,8 @@ module Utils
return "''" if str.empty?
str = str.dup
# 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
end
......@@ -45,7 +48,7 @@ module Utils
return "''" if str.empty?
str = str.dup
# 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
end
......@@ -61,7 +64,7 @@ module Utils
# and a literal \ can be included via \\
"set -gx #{key} \"#{sh_quote(value)}\""
when :csh, :tcsh
"setenv #{key} #{csh_quote(value)}"
"setenv #{key} #{csh_quote(value)};"
end
end
......@@ -72,12 +75,12 @@ module Utils
def self.prepend_path_in_shell_profile(path)
case preferred_shell
when :bash, :ksh, :sh, :zsh
"echo 'export PATH=\"#{sh_quote(path)}:$PATH >> #{shell_profile}"
when :bash, :ksh, :sh, :zsh, nil
"echo 'export PATH=\"#{sh_quote(path)}:$PATH'\" >> #{shell_profile}"
when :csh, :tcsh
"echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{shell_profile}"
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment