Skip to content

Commit

Permalink
Make basic DSL for testing via tmux
Browse files Browse the repository at this point in the history
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
wincent committed Mar 8, 2016
1 parent 7bba61a commit e9f83a1
Showing 1 changed file with 59 additions and 13 deletions.
72 changes: 59 additions & 13 deletions test.rb
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

0 comments on commit e9f83a1

Please sign in to comment.