Skip to content

Instantly share code, notes, and snippets.

@martinos
Created October 19, 2012 00:58
Show Gist options
  • Save martinos/3915664 to your computer and use it in GitHub Desktop.
Save martinos/3915664 to your computer and use it in GitHub Desktop.
Helper for testing executable in gem.
require 'open3'
def ruby_run(cmd, stdin_str)
bundler_gem = '-r bundler/setup' if Gem.loaded_specs.keys.include?('bundler')
libs = '-I ./lib'
full_cmd = %{ruby #{libs} #{bundler_gem} ./bin/#{cmd}}
stdout_str, stderr_str = ['', '']
exit_code = 0
Open3.popen3(full_cmd) do |stdin, stdout, stderr, wait_thr|
stdin.write stdin_str
stdin.close
stdout_str = stdout.read
stderr_str = stderr.read
exit_code = wait_thr.value
end
[stdout_str, stderr_str, exit_code]
end
ruby_run("rubyc map -o StringEncoder 'l.upcase'", "tata")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment