0.0
The project is in a healthy, maintained state
An object-oriented interface for spawning a command
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.5
~> 1.24
~> 3.5.7
~> 0.9
~> 13.1
 Project Readme

About

test-cmd.rb provides an object-oriented interface for spawning a command.

Examples

Callbacks

The success and failure callbacks provide hooks for when a command exits successfully or unsuccessfully. The callbacks are passed an instance of Test::Cmd:

require "test-cmd"
cmd("ruby", "-e", "exit 0")
  .success { print "The command [#{_1.pid}] was successful", "\n" }
  .failure { print "The command [#{_1.pid}] was unsuccessful", "\n" }

Test::Unit

The following example demonstrates how tests might be written with test-unit from the standard library. The cmd method takes the name or path of a command, alongside any arguments:

require "test/unit"
require "test/cmd"

class CmdTest < Test::Unit::TestCase
  def test_ruby_stdout
    assert_equal "42\n", ruby("puts 42").stdout
  end

  def test_ruby_stderr
    assert_equal "42\n", ruby("warn 42").stderr
  end

  def test_ruby_success_exit_status
    assert_equal 0, ruby("exit 0").exit_status
  end

  def test_ruby_failure_exit_status
    assert_equal 1, ruby("exit 1").exit_status
  end

  private

  def ruby(code)
    cmd("ruby", "-e", code)
  end
end

IO#sync

Sometimes it can be neccessary to flush output to the operating system immediately, otherwise there can be unexpected results. Consider the following example, where the output will be bar\nfoo\n instead of foo\nbar\n:

##
# test.rb
fork do
  sleep(1)
  puts "bar"
end
puts "foo"
Process.wait

##
# cmd.rb
p cmd("ruby", "test.rb").stdout # => "bar\nfoo\n"

And with output flushed to the operating system immediately ($stdout.sync = true):

##
# test.rb
$stdout.sync = true
fork do
  sleep(1)
  puts "bar"
end
puts "foo"
Process.wait

##
# cmd.rb
p cmd("ruby", "test.rb").stdout # => "foo\nbar\n"

Documentation

A complete API reference is available at 0x1eef.github.io/x/test-cmd.rb

Install

Rubygems.org

test-cmd.rb can be installed via rubygems.org:

gem install test-cmd.rb

Sources

License

BSD Zero Clause
See LICENSE