Skip to content

Instantly share code, notes, and snippets.

@eric
Created May 23, 2010 06:58
Show Gist options
  • Save eric/410708 to your computer and use it in GitHub Desktop.
Save eric/410708 to your computer and use it in GitHub Desktop.
$ turn exec-testing.rb
Loaded suite
TestCases
test: #{CLIENT} "*?{}[]" PASS
test: #{CLIENT} "something ; echo anything" PASS
test: #{CLIENT} "something with quotes" PASS
test: #{CLIENT} "something | cat" PASS
test: #{CLIENT} $PWD $USER "$HOME $SHELL" PASS
test: #{CLIENT} singlearg PASS
test: #{CLIENT} something * PASS
test: #{CLIENT} two args PASS
test: should spawn shell: #{CLIENT} && echo something PASS
test: should spawn shell: cd /tmp && #{CLIENT} something PASS
test: should spawn shell: cd /tmp ; #{CLIENT} something PASS
==============================================================================
pass: 11, fail: 0, error: 0
total: 11 tests with 11 assertions in 4.851785 seconds
==============================================================================
#!/usr/bin/env ruby
if ARGV[0] == 'client'
puts "#{$$}\nARGV: #{ARGV.inspect}"
exit
else
RUBY_BIN = File.join(RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"])
CLIENT = "#{RUBY_BIN} #{File.expand_path(__FILE__)} client"
require 'test/unit'
class TestCases < Test::Unit::TestCase
def self.exec_testcase(command, options = {})
should_spawn_shell = !!options[:should_spawn_shell]
command_name = command.gsub(CLIENT, '#{CLIENT}')
command_name = "should spawn shell: #{command_name}" if should_spawn_shell
define_method('test: ' + command_name) do
pids_matched, description = execute_client(command)
spawned_shell = !pids_matched
assert spawned_shell == should_spawn_shell, description
end
end
exec_testcase %{#{CLIENT} singlearg}
exec_testcase %{#{CLIENT} two args}
exec_testcase %{#{CLIENT} "something with quotes"}
exec_testcase %{#{CLIENT} "something | cat"}
exec_testcase %{#{CLIENT} "something ; echo anything"}
exec_testcase %{#{CLIENT} something *}
exec_testcase %{#{CLIENT} $PWD $USER "$HOME $SHELL"}
exec_testcase %{#{CLIENT} "*?{}[]"}
exec_testcase %{cd /tmp && #{CLIENT} something}, :should_spawn_shell => true
exec_testcase %{#{CLIENT} && echo something}, :should_spawn_shell => true
exec_testcase %{cd /tmp ; #{CLIENT} something}, :should_spawn_shell => true
def execute_client(command)
rd, wr = IO.pipe
pid = fork do
rd.close
$stdout.reopen(wr)
exec command
end
wr.close
child_out = rd.read
child_pid, child_argv = child_out.split(/\n/)
[ pid.to_s == child_pid, child_argv ]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment