Skip to content

Commit

Permalink
Rely on Shellwords.shellescape to escape
Browse files Browse the repository at this point in the history
  • Loading branch information
ekohl committed Aug 29, 2023
1 parent 2ec48bf commit c602c88
Showing 1 changed file with 1 addition and 21 deletions.
22 changes: 1 addition & 21 deletions spec/spec_helper_acceptance_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,5 @@ def postgresql_version

def psql(psql_cmd, user = 'postgres', exit_codes = [0, 1], &block)
psql = "psql #{psql_cmd}"
LitmusHelper.instance.run_shell("cd /tmp; su #{shellescape(user)} -c #{shellescape(psql)}", acceptable_exit_codes: exit_codes, &block)
end

def shellescape(str)
str = str.to_s

# An empty argument will be skipped, so return empty quotes.
return "''" if str.empty?

str = str.dup

# Treat multibyte characters as is. It is caller's responsibility
# to encode the string in the right encoding for the shell
# environment.
str.gsub!(%r{([^A-Za-z0-9_\-.,:/@\n])}, '\\\\\\1')

# A LF cannot be escaped with a backslash because a backslash + LF
# combo is regarded as line continuation and simply ignored.
str.gsub!(%r{\n}, "'\n'")

str
LitmusHelper.instance.run_shell("cd /tmp; su #{Shellwords.shellescape(user)} -c #{Shellwords.shellescape(psql)}", acceptable_exit_codes: exit_codes, &block)
end

0 comments on commit c602c88

Please sign in to comment.