Skip to content

Commit

Permalink
RSpec test with TmuxTui
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed Oct 21, 2020
1 parent b60f2bb commit e2fa1a5
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ endforeach(test)
ADD_TEST("integration" ruby ${CMAKE_CURRENT_SOURCE_DIR}/integration/run.rb)
ADD_TEST("translations" rspec --format doc ${CMAKE_CURRENT_SOURCE_DIR}/integration/translations_spec.rb)

file(GLOB libyui_specs "libyui/*_spec.rb")
foreach(test ${libyui_specs})
ADD_TEST(${test} rspec --format doc ${test})
endforeach(test)

file(GLOB libyui_tests "libyui/*.test")
foreach(test ${libyui_tests})
ADD_TEST(${test} ${test})
Expand Down
36 changes: 36 additions & 0 deletions tests/libyui/menu_hotkeys_1177760_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require_relative "rspec_tmux_tui"

describe "Menu Item" do
bug = "1177760"
around(:each) do |ex|
@base = "menu_hotkeys_#{bug}"

yast_ncurses = "#{__dir__}/yast_ncurses"
example_dir = "/usr/share/doc/packages/yast2-ycp-ui-bindings/examples"
@tui = TmuxTui.new_session "#{yast_ncurses} #{example_dir}/MenuBar1.rb"

ex.run

if @tui.has_session?
@tui.kill_session
end
end

it "has hotkeys in menu items, boo##{bug}" do
@tui.await(/File.*Edit.*View/)
@tui.capture_pane_to("#{@base}-1-initial")

@tui.send_keys "M-V" # &View
@tui.capture_pane_to("#{@base}-2-view-menu-activated")

@tui.send_keys "M-N" # &Normal
@tui.capture_pane_to("#{@base}-3-normal-menu-item-activated")

# the label
expect(@tui.capture_pane).to include("Last Event")
# the output
expect(@tui.capture_pane).to include("view_normal")

@tui.send_keys "M-Q" # &Quit
end
end
76 changes: 76 additions & 0 deletions tests/libyui/rspec_tmux_tui.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require "shellwords"

class TmuxTui
class Error < RuntimeError
end

def self.new_session(*args)
new(*args)
end

attr_reader :session_name

def initialize(shell_command, x: 80, y: 24, detach: true, session_name: nil)
@shell_command = shell_command
@x = x
@y = y
@detach = detach
@session_name = session_name || new_session_name

system "tmux", "new-session",
"-s", @session_name,
"-x", @x.to_s,
"-y", @y.to_s,
*(@detach ? ["-d"] : [] ),
"sh", "-c", "#{@shell_command}; sleep 9999"
end

def new_session_name
"tmux-tui-#{rand 10000}"
end

# @return [String]
def capture_pane(color: false)
esc = color ? "-e" : ""
# FIXME: failure of the command?
`tmux capture-pane -t #{session_name.shellescape} -p #{esc}`
end

def capture_pane_to(filename)
txt = capture_pane(color: false)
esc = capture_pane(color: true)
File.write("#{filename}.txt", txt)
File.write("#{filename}.esc", esc)
end

def await(pattern)
sleeps = [0.1, 0.2, 0.2, 0.5, 1, 2, 2, 5]
txt = ""
sleeps.each do |sl|
txt = capture_pane
case txt
when pattern
sleep 0.1 # draw the rest of the screen
return
else
sleep sl
end
end
raise Error, "Timed out waiting for #{pattern.inspect}. Seen:\n#{txt}"
end

# @param keys [String] "C-X" for Ctrl-X, "M-X" for Alt-X, think "Meta";
# for details see:
# man tmux | less +/"^KEY BINDINGS"
def send_keys(keys)
system "tmux", "send-keys", "-t", session_name, keys
end

def has_session?
system "tmux", "has-session", "-t", session_name
end

def kill_session
system "tmux", "kill-session", "-t", session_name
end
end

0 comments on commit e2fa1a5

Please sign in to comment.