Skip to content

Commit

Permalink
Support escape: true only in ruby_exe, by executing from a temporary …
Browse files Browse the repository at this point in the history
…file

* It was never used for ruby_cmd.
* Escaping with the shell is very tricky to do reliably.
* Fixes #11.
  • Loading branch information
eregon committed Sep 4, 2015
1 parent cc5fd36 commit 1e1b4af
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/mspec/helpers/ruby_exe.rb
@@ -1,5 +1,6 @@
require 'mspec/utils/ruby_name'
require 'mspec/guards/platform'
require 'mspec/helpers/tmp'

# The ruby_exe helper provides a wrapper for invoking the
# same Ruby interpreter as the one running the specs and
Expand Down Expand Up @@ -124,6 +125,13 @@ def ruby_exe(code, opts = {})
ENV[key] = value
end

escape = opts.delete(:escape)
if escape
tmpfile = tmp("rubyexe.rb")
File.open(tmpfile, "w") { |f| f.write(code) }
code = tmpfile
end

begin
platform_is_not :opal do
`#{ruby_cmd(code, opts)}`
Expand All @@ -134,25 +142,20 @@ def ruby_exe(code, opts = {})
key = key.to_s
ENV.delete key unless saved_env.key? key
end
File.delete tmpfile if escape
end
end
end

def ruby_cmd(code, opts = {})
body = code

if code and not File.exist?(code)
if opts[:escape]
heredoc_separator = "END_OF_RUBYCODE"
lines = code.lines
until lines.none? {|line| line.start_with? heredoc_separator }
heredoc_separator << heredoc_separator
end
if opts[:escape]
raise "escape: true is no longer supported in ruby_cmd, use ruby_exe or a fixture"
end

body = %Q!-e "`cat <<'#{heredoc_separator}'\n#{code}\n#{heredoc_separator}\n`"!
else
body = "-e #{code.inspect}"
end
if code and not File.exist?(code)
body = "-e #{code.inspect}"
end

[RUBY_EXE, ENV['RUBY_FLAGS'], opts[:options], body, opts[:args]].compact.join(' ')
Expand Down

0 comments on commit 1e1b4af

Please sign in to comment.