module RSpec::Core::ShellEscape

def conditionally_quote(id)

def conditionally_quote(id)
  return id if shell_allows_unquoted_ids?
  quote(id)
end

def escape(shell_command)

def escape(shell_command)
  Shellwords.escape(shell_command.to_s)
end

def quote(argument)

def quote(argument)
  "'#{argument.to_s.gsub("'", "\\\\'")}'"
end

def shell_allows_unquoted_ids?

def shell_allows_unquoted_ids?
  # Note: ENV['SHELL'] isn't necessarily the shell the user is currently running.
  # According to http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html:
  # "This variable shall represent a pathname of the user's preferred command language interpreter."
  #
  # It's the best we can easily do, though. We err on the side of safety (quoting
  # the id when not actually needed) so it's not a big deal if the user is actually
  # using a different shell.
  SHELLS_ALLOWING_UNQUOTED_IDS.include?(ENV['SHELL'].to_s.split('/').last)
end