class TTYtest::Tmux::Driver

Constants

COMMAND
DEFAULT_CONFING_FILE_PATH
REQUIRED_TMUX_VERSION
SLEEP_INFINITY
SOCKET_NAME

Public Class Methods

new( debug: false, command: COMMAND, socket_name: SOCKET_NAME, config_file_path: DEFAULT_CONFING_FILE_PATH ) click to toggle source
# File lib/ttytest/tmux/driver.rb, line 20
def initialize(
  debug: false,
  command: COMMAND,
  socket_name: SOCKET_NAME,
  config_file_path: DEFAULT_CONFING_FILE_PATH
)
  @debug = debug
  @tmux_cmd = command
  @socket_name = socket_name
  @config_file_path = config_file_path
end

Public Instance Methods

available?() click to toggle source
# File lib/ttytest/tmux/driver.rb, line 51
def available?
  @available ||= (Gem::Version.new(tmux_version) >= Gem::Version.new(REQUIRED_TMUX_VERSION))
end
new_terminal(cmd, width: 80, height: 24) click to toggle source
# File lib/ttytest/tmux/driver.rb, line 32
def new_terminal(cmd, width: 80, height: 24)
  cmd = "#{cmd}\n#{SLEEP_INFINITY}"

  session_name = "ttytest-#{SecureRandom.uuid}"
  tmux(*%W[-f #{@config_file_path} new-session -s #{session_name} -d -x #{width} -y #{height} #{cmd}])
  session = Session.new(self, session_name)
  Terminal.new(session)
end
tmux(*args) click to toggle source

@api private

# File lib/ttytest/tmux/driver.rb, line 42
def tmux(*args)
  ensure_available
  puts "tmux(#{args.inspect[1...-1]})" if debug?

  stdout, stderr, status = Open3.capture3(@tmux_cmd, '-L', SOCKET_NAME, *args)
  raise TmuxError, "tmux(#{args.inspect[1...-1]}) failed\n#{stderr}" unless status.success?
  stdout
end

Private Instance Methods

debug?() click to toggle source
# File lib/ttytest/tmux/driver.rb, line 57
def debug?
  @debug
end
ensure_available() click to toggle source
# File lib/ttytest/tmux/driver.rb, line 61
def ensure_available
  if !available?
    if !tmux_version
      raise TmuxError, "Running `tmux -V` to determine version failed. Is tmux installed?"
    else
      raise TmuxError, "tmux version #{tmux_version} does not meet requirement >= #{REQUIRED_TMUX_VERSION}"
    end
  end
end
tmux_version() click to toggle source
# File lib/ttytest/tmux/driver.rb, line 71
def tmux_version
  @tmux_version ||= `#{@tmux_cmd} -V`[/tmux (\d+.\d+)/, 1]
rescue Errno::ENOENT
  nil
end