Skip to content

Commit

Permalink
Automatic session cleanup with a block; YastTui#example.
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed Oct 27, 2020
1 parent d3f08eb commit 387c970
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
12 changes: 4 additions & 8 deletions tests/libyui/menu_hotkeys_1177760_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
bug = "1177760" # https://bugzilla.suse.com/show_bug.cgi?id=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

@tui.ensure_no_session
@tui = YastTui.new
@tui.example("MenuBar1") do
ex.run
end
end

it "has hotkeys in menu items, boo##{bug}" do
Expand Down
37 changes: 31 additions & 6 deletions tests/libyui/rspec_tmux_tui.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "shellwords"

# Drive interactive TUI (textual user interface) with tmux.
# https://github.com/tmux/tmux
class TmuxTui
class Error < RuntimeError
end
Expand All @@ -10,31 +12,44 @@ def self.new_session(*args)

attr_reader :session_name

def initialize(shell_command,
xy: [80, 24], detach: true, remain_on_exit: true, session_name: nil)
# @param session_name [String]
def initialize(session_name: nil)
@session_name = session_name || new_session_name
end

# @param shell_command [String]
# @param xy [(Integer, Integer)]
# @param detach [Boolean]
# @param remain_on_exit [Boolean] useful if shell_command may unexpectedly
# fail quickly. In that case we can still capture the pane
# and read the error messages.
def new_session(shell_command,
xy: [80, 24], detach: true, remain_on_exit: true)

@shell_command = shell_command
@x, @y = xy
@detach = detach
@session_name = session_name || new_session_name

detach_args = @detach ? ["-d"] : []
# "remain-on-exit" is useful if shell_command may unexpectedly fail quickly.
# In that case we can still capture the pane and read the error messages.
remain_on_exit_args = if remain_on_exit
["set-hook", "-g", "session-created", "set remain-on-exit on", ";"]
else
[]
end

system "tmux",
tmux_ret = system "tmux",
* remain_on_exit_args,
"new-session",
"-s", @session_name,
"-x", @x.to_s,
"-y", @y.to_s,
* detach_args,
"sh", "-c", shell_command

return tmux_ret unless block_given?

yield
ensure_no_session
end

def new_session_name
Expand Down Expand Up @@ -91,3 +106,13 @@ def ensure_no_session
kill_session if has_session?
end
end

class YastTui < TmuxTui
def example(basename, &block)
basename += ".rb" unless basename.end_with? ".rb"
yast_ncurses = "#{__dir__}/yast_ncurses"
example_dir = "/usr/share/doc/packages/yast2-ycp-ui-bindings/examples"

new_session("#{yast_ncurses} #{example_dir}/#{basename}", &block)
end
end
7 changes: 4 additions & 3 deletions tests/libyui/table_sort_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
around(:each) do |ex|
yast_ncurses = "#{__dir__}/yast_ncurses"
@base = "table_sort"
@tui = TmuxTui.new_session "#{yast_ncurses} #{__dir__}/#{@base}.rb change-current-item"
ex.run
@tui.ensure_no_session
@tui = TmuxTui.new
@tui.new_session "#{yast_ncurses} #{__dir__}/#{@base}.rb change-current-item" do
ex.run
end
end

bug = "1165388" # https://bugzilla.suse.com/show_bug.cgi?id=1165388
Expand Down

0 comments on commit 387c970

Please sign in to comment.