Module: RSpecSystem::Util

Included in:
NodeSet::Vagrant
Defined in:
lib/rspec-system/util.rb

Overview

A set of utilities that can be used as a mixin.

Instance Method Summary collapse

Instance Method Details

#shellescape(str) ⇒ String

This is the shellescape method from shellwords from ruby-2.0.0

Parameters:

  • str (String)

    string to escape

Returns:

  • (String)

    returns escaped string



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rspec-system/util.rb', line 7

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!(/([^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!(/\n/, "'\n'")

  return str
end