Working Remotely? Thinking about it?

Are you a member of a geographically dispersed team? Are you thinking of working remotely, or hiring remote developers? Wide Teams is my new blog and podcast for distributed teams. Check it out for getting started guides, tips and best practices, news, interviews, screencasts, and more all about working remotely and collaborating with wide-spread teams.

Greenletters: Painless automation and testing for command-line applications

2010 July 19
by avdi

539px-IBM-3279

Did you ever use Expect to automate a complex command-line procedure, like an FTP upload? Expect is handy – and very powerful – but for Ruby projects it sure would be nice to be able to automate console apps directly from Ruby.

You may not have known it, but Ruby actually ships with a tiny Expect clone called expect.rb. Unfortunately it’s more of a proof of concept than a fully functional automation tool.

Enter Greenletters. Greenletters begins to bring some of the power of Expect to Ruby, with a simple, straightforward API. For example, here’s a scripted interaction with the classic Colossal Cave Adventure:

  require 'greenletters'

  adv = Greenletters::Process.new("adventure", :transcript => $stdout)

  # Install a handler which may be triggered at any time
  adv.on(:output, /welcome to adventure/i) do |process, match_data|
    adv < < "no\n"
  end

  puts "Starting adventure..."
  adv.start!

  # Wait for the specified pattern before proceeding
  adv.wait_for(:output, /you are standing at the end of a road/i)
  adv << "east\n"
  adv.wait_for(:output, /inside a building/i)
  adv << "quit\n"
  adv.wait_for(:output, /really want to quit/i)
  adv << "yes\n"
  adv.wait_for(:exit)
  puts "Adventure has exited."

Greenletters also ships with some simple Cucumber steps, so you can immediately start using it to specify the behavior of your command-line apps:

    Given process activity is logged to "greenletters.log"
    Given a process "adventure" from command "adventure"
    Given I reply "no" to output "Would you like instructions?" from process "adventure"
    Given I reply "yes" to output "Do you really want to quit" from process "adventure"
    When I execute the process "adventure"
    Then I should see the following output from process "adventure":
    """
    You are standing at the end of a road before a small brick building.
    Around you is a forest.  A small stream flows out of the building and
    down a gully.
    """
    When I enter "east" into process "adventure"
    Then I should see the following output from process "adventure":
    """
    You are inside a building, a well house for a large spring.
    """

Want to give it a try? Then:

gem install greenletters

And check out the examples/ directory for inspiration. Let me know what you think!

Bookmark and Share
Creative Commons License
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

Related posts:

  1. Announcing Firetower – A command-line interface to Campfire
  2. Safely executing commands with user data
  3. The three line rule
  4. Simple Design & Testing Conference 2007
  5. Daemonic Emacs
  • Kurt
    What is the hardware in the photo?


  • That's an IBM 3270 terminal.
  • IBM 3270.
  • I tried it and my machine (ruby191) couldn't require 'pty'. What kind of dependency is that? #greenletters
  • pty is a Ruby standard library. What is your platform? As far as I know PTY (and thus, Greenletters) will not work under Win32.
blog comments powered by Disqus