-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Still not testing anything meaningful, but you can see the beginnings of something useful and easy-enough-to-use here. If the `#wait_for` fails, an exception is thrown and the session is left running for debugging purposes.
- Loading branch information
Showing
1 changed file
with
59 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,61 @@ | ||
#!/usr/bin/env ruby | ||
|
||
%x{tmux new-session -d -s ferret-test} | ||
%x{tmux send-keys -t ferret-test 'vim -u NONE' Enter} | ||
%x{tmux send-keys -t ferret-test ':set nocompatible' Enter} | ||
%x{tmux send-keys -t ferret-test ':set rtp+=#{Dir.pwd}' Enter} | ||
%x{tmux send-keys -t ferret-test ':runtime! plugin/ferret.vim' Enter} | ||
|
||
%x{tmux send-keys -t ferret-test \\\\ a usr/bin/env\\\\ Space ruby Enter} | ||
|
||
sleep 1 | ||
%x{tmux capture-pane -t ferret-test} | ||
buffer = %x{tmux show-buffer} | ||
%x{tmux delete-buffer} | ||
puts buffer | ||
require 'shellwords' | ||
|
||
module DSL | ||
module Constants | ||
Backslash = '\\' | ||
Enter = 'Enter' | ||
Space = 'Space' | ||
end | ||
|
||
class << self | ||
def escape(string) | ||
Shellwords.shellescape(string) | ||
end | ||
end | ||
|
||
class Session | ||
WAIT_TIMEOUT = 5 | ||
|
||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
def send_keys(*args) | ||
escaped = args.map { |arg| DSL.escape(arg) } | ||
%x{tmux send-keys -t #{@name} #{escaped.join(' ')}} | ||
end | ||
|
||
def wait_for(pattern) | ||
buffer = nil | ||
start = Time.now | ||
while (Time.now - start < WAIT_TIMEOUT) | ||
%x{tmux capture-pane -t #{@name}} | ||
buffer = %x{tmux show-buffer} | ||
%x{tmux delete-buffer} | ||
return buffer if buffer =~ pattern | ||
sleep 0.25 | ||
end | ||
raise "Failed to find pattern `#{pattern.inspect}` in session `#{@name}`" | ||
end | ||
end | ||
|
||
def session(name, &block) | ||
escaped_name = DSL.escape(name) | ||
%x{tmux new-session -d -s #{DSL.escape(escaped_name)}} | ||
Session.new(escaped_name).instance_eval(&block) | ||
%x{tmux kill-session -t #{escaped_name}} | ||
end | ||
end | ||
self.extend(DSL) | ||
Object.instance_eval { include DSL::Constants } | ||
|
||
session('ferret-test') do |name| | ||
send_keys('vim -u NONE', Enter) | ||
send_keys(':set nocompatible', Enter) | ||
send_keys(":set rtp+=#{DSL.escape(Dir.pwd)}", Enter) | ||
send_keys(':runtime! plugin/ferret.vim', Enter) | ||
send_keys(Backslash, 'a', 'usr/bin/env', Backslash, Space, 'ruby', Enter) | ||
wait_for(/module DSL/) | ||
end |