Skip to content

Instantly share code, notes, and snippets.

@gisikw
Created June 9, 2010 01:47
Show Gist options
  • Save gisikw/430931 to your computer and use it in GitHub Desktop.
Save gisikw/430931 to your computer and use it in GitHub Desktop.
Examples for Interactive Aruba Steps
Feature: Advanced scripting
As a My script writer
I want to nest tasks inside of questions
So I can respond dynamically to the user's needs
@aruba_interactive
Scenario: Nesting questions
Given a file named "yes.template" with:
"""
The user picked yes
"""
And a file named "script.rb" with:
"""
ask "Are you sure you would like to apply this script?"
yes do
folder "foo"
file "foo.txt" => "yes.template"
ask "Would you like to create bar.txt?"
yes do
file "bar.txt" => "yes.template"
end
end
"""
When I run "../../bin/my script.rb" interactively
And I type "y"
And I type "y"
Then the file "foo.txt" should contain "The user picked yes"
Then the file "bar.txt" should contain "The user picked yes"
Scenario: Creating a file
Given a directory named "foo"
When I run "echo 'hello, world' > foo/bar.txt"
Then the file "foo/bar.txt" should contain "hello, world"
require 'open3'
Before '@aruba_interactive' do
@stdout_stream = nil
@stderr_stream = nil
@stdout = ""
@stdin = ""
@stdout_listener = Thread.new do
Thread.stop
loop do
@stdout << @stdout_stream.readpartial(1)
end
end
@stderr_listener = Thread.new do
Thread.stop
loop do
@stderr << @stderr_stream.readpartial(1)
end
end
end
When /^I run "([^\"]*)" interactively$/ do |arg1|
old_dir = Dir.pwd
Dir.chdir("tmp/aruba") unless Dir.pwd.split('/')[-1] == "aruba"
@stdin_stream, @stdout_stream, @stderr_stream = Open3.popen3(arg1)
Dir.chdir(old_dir)
sleep 1
@stdout_listener.run
@stderr_listener.run
end
When /^I type "([^\"]*)"$/ do |arg1|
@stdin_stream.puts arg1
sleep 0.5
end
Then /^the program should prompt "([^\"]*)"$/ do |arg1|
sleep 0.5
@stdout.should include(arg1)
end
Before '@aruba_interactive' do
@stdout_stream = nil
@stderr_stream = nil
@stdout = ""
@sterr = ""
@stdout_listener = Thread.new do
Thread.stop
loop do
@stdout << @stdout_stream.readpartial(1)
end
end
@stderr_listener = Thread.new do
Thread.stop
loop do
@stderr << @stderr_stream.readpartial(1)
end
end
end
When /^I run "([^\"]*)" interactively$/ do |arg1|
old_dir = Dir.pwd
Dir.chdir("tmp/aruba") unless Dir.pwd.split('/')[-1] == "aruba"
@stdin_stream, @stdout_stream, @stderr_stream = Open3.popen3(arg1)
Dir.chdir(old_dir)
sleep 1
@stdout_listener.run
@stderr_listener.run
end
When /^I type "([^\"]*)"$/ do |arg1|
@stdin_stream.puts arg1
sleep 0.5
end
Then /^the program should prompt "([^\"]*)"$/ do |arg1|
sleep 0.5
@stdout.should include(arg1)
end
require 'open3'
stdin, stdout, stderr = Open3.popen3('dc')
stdin.puts(5)
stdin.puts(10)
stdin.puts("+")
stdin.puts("p")
stdout.gets
#=> "15\n"
puts `ls`
#=> "app.rb config.ru views tmp ..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment