Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Ruby console automation a la Expect

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 bin
Octocat-spinner-32 examples
Octocat-spinner-32 lib
Octocat-spinner-32 script
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 History.txt
Octocat-spinner-32 README.org
Octocat-spinner-32 Rakefile
Octocat-spinner-32 greenletters.gemspec
Octocat-spinner-32 version.txt
README.org

Greenletters README

Synopsis

See introductory blog post HERE.

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."

Or, in Cucumber format:

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.
"""

What

Greenletters is a console interaction automation library similar to GNU Expect. You can use it to script interactions with command-line programs.

Why

Because Ruby’s built-in expect.rb is pretty underpowered and I wanted to drive command-line applications from Ruby, not TCL.

Who

Greenletters is by Avdi Grimm.

Where

http://github.com/avdi/greenletters

How

Greenletters uses the pty.rb library under the covers to create a UNIX pseudoterminal under Ruby’s control. Of course, this means that it is MacOSX/Linux/*NIX-only; Windows users need not apply.

The advantage of using a PTY is that any output - including output written to the console instead of STDOUT/STDERR - will be captured by Greenletters.

Cucumber

To use the Cucumber steps in your own feature files, put the following in your env.rb:

require 'greenletters'
require 'greenletters/cucumber_steps'

LICENSE

(The MIT License)

Copyright (c) 2010 Avdi Grimm

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.