require File.dirname(__FILE__) + '/test_helper' class TestKernel < Test::Unit::TestCase context "backticks" do should "raise a runtime error with contents of stderr if command does not succeed" do exception = assert_raises(RuntimeError) do `echo "error text on stderr" >&2; exit -1;` end assert_match "failed with: 'error text on stderr'", exception.message end should "raise runtime error with contents of stderr if command is not found" do exception = assert_raises(RuntimeError) do `bad-cmd` end assert_match "failed with: 'No such file or directory - bad-cmd'", exception.message end should "return the contents of stdout + stderr if command succeeds" do assert_match "std out\nstd err output\n", `echo "std out"; echo "std err output" >&2; exit 0;` end end context "%x{} syntax" do should "raise a runtime error with contents of stderr if command does not succeed" do exception = assert_raises(RuntimeError) do %x{echo "error text on stderr" >&2; exit -1;} end assert_match "failed with: 'error text on stderr'", exception.message end should "return the contents of stdout + stderr if command succeeds" do assert_match "std out\nstd err output\n", %x{echo "std out"; echo "std err output" >&2; exit 0;} end end context "system" do should "raise a runtime error with contents of stderr if command does not succeed" do exception = assert_raises(RuntimeError) do system %{echo "error text on stderr" >&2; exit -1;} end assert_match "failed with: 'error text on stderr'", exception.message end should "return true if command succeeds" do assert_kind_of TrueClass, system("echo 'hi!';") end end context "system!" do should "not raise an exception, even when command fails" do assert_nothing_raised {system!("exit -1;")} end should "return false when command fails" do assert_kind_of FalseClass, system!("exit -1;") end should "return true if command succeeds" do assert_kind_of TrueClass, system!("echo 'hi!';") end end end