Extract user acceptance test helpers in a module
This commit is contained in:
		@@ -2,4 +2,4 @@ Feature: debug CLI option
 | 
			
		||||
 | 
			
		||||
  Scenario: raises the logger level to DEBUG
 | 
			
		||||
    When I run uhwm with option -d
 | 
			
		||||
    Then the current output must match /log.+debug.+level/i
 | 
			
		||||
    Then the output must match /log.+debug.+level/i
 | 
			
		||||
 
 | 
			
		||||
@@ -2,4 +2,4 @@ Feature: ruby feature require CLI option
 | 
			
		||||
 | 
			
		||||
  Scenario: requires a ruby feature
 | 
			
		||||
    When I run uhwm with option -v -r abbrev
 | 
			
		||||
    Then the current output must match /load.+abbrev.+ruby feature/i
 | 
			
		||||
    Then the output must match /load.+abbrev.+ruby feature/i
 | 
			
		||||
 
 | 
			
		||||
@@ -2,4 +2,4 @@ Feature: verbose CLI option
 | 
			
		||||
 | 
			
		||||
  Scenario: raises the logger level to INFO
 | 
			
		||||
    When I run uhwm with option -v
 | 
			
		||||
    Then the current output must match /log.+info.+level/i
 | 
			
		||||
    Then the output must match /log.+info.+level/i
 | 
			
		||||
 
 | 
			
		||||
@@ -10,4 +10,4 @@ Feature: layout registration
 | 
			
		||||
      end
 | 
			
		||||
      """
 | 
			
		||||
    When I run uhwm with option -r./layout -l Layout
 | 
			
		||||
    Then the current output must contain current display
 | 
			
		||||
    Then the output must contain current display
 | 
			
		||||
 
 | 
			
		||||
@@ -5,4 +5,4 @@ Feature: checking if another window manager is running
 | 
			
		||||
    Given another window manager is running
 | 
			
		||||
    When I start uhwm
 | 
			
		||||
    Then the exit status must be 70
 | 
			
		||||
    And the current output must match /error.+other.+window.+manager/i
 | 
			
		||||
    And the output must match /error.+other.+window.+manager/i
 | 
			
		||||
 
 | 
			
		||||
@@ -1,20 +1,3 @@
 | 
			
		||||
def uhwm_wait_output message, timeout: 1
 | 
			
		||||
  Timeout.timeout(timeout) do
 | 
			
		||||
    loop do
 | 
			
		||||
      break if case message
 | 
			
		||||
        when Regexp then @process.stdout + @process.stderr =~ message
 | 
			
		||||
        when String then assert_partial_output_interactive message
 | 
			
		||||
      end
 | 
			
		||||
      sleep 0.1
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
rescue Timeout::Error
 | 
			
		||||
  fail [
 | 
			
		||||
    "expected `#{message}' not seen after #{timeout} seconds in:",
 | 
			
		||||
    "  ```\n  #{@process.stdout + @process.stderr}  ```"
 | 
			
		||||
  ].join "\n"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
Then /^the output must contain exactly the usage$/ do
 | 
			
		||||
  assert_exact_output <<-eoh, all_output
 | 
			
		||||
Usage: uhwm [options]
 | 
			
		||||
@@ -29,17 +12,13 @@ options:
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
Then /^the output must match \/([^\/]+)\/([a-z]*)$/ do |pattern, options|
 | 
			
		||||
  expect(@process.stdout).to match Regexp.new(pattern, options)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
Then /^the current output must match \/([^\/]+)\/([a-z]*)$/ do |pattern, options|
 | 
			
		||||
  uhwm_wait_output Regexp.new(pattern, options)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
Then /^the current output must contain:$/ do |content|
 | 
			
		||||
Then /^the output must contain:$/ do |content|
 | 
			
		||||
  uhwm_wait_output content.to_s
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
Then /^the current output must contain current display$/ do
 | 
			
		||||
Then /^the output must contain current display$/ do
 | 
			
		||||
  uhwm_wait_output ENV['DISPLAY']
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -1,16 +1,5 @@
 | 
			
		||||
def uhwm_run options = '-v'
 | 
			
		||||
  command = %w[uhwm]
 | 
			
		||||
  command << options if options
 | 
			
		||||
  @interactive = @process = run command.join ' '
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
def uhwm_run_wait_ready
 | 
			
		||||
  uhwm_run
 | 
			
		||||
  uhwm_wait_output 'Connected to'
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
Given /^another window manager is running$/ do
 | 
			
		||||
  expect(@other_wm).to be_alive
 | 
			
		||||
  expect(other_wm).to be_alive
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
Given /^uhwm is running$/ do
 | 
			
		||||
 
 | 
			
		||||
@@ -1,21 +1,8 @@
 | 
			
		||||
def x_key key
 | 
			
		||||
  fail "cannot simulate X key `#{key}'" unless system "xdotool key #{key}"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
def x_socket_check pid
 | 
			
		||||
  case RbConfig::CONFIG['host_os']
 | 
			
		||||
  when /linux/
 | 
			
		||||
    `netstat -xp 2> /dev/null`.lines.grep /\s+#{pid}\/ruby/
 | 
			
		||||
  else
 | 
			
		||||
    `sockstat -u`.lines.grep /\s+ruby.+\s+#{pid}/
 | 
			
		||||
  end.any?
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
When /^I press the default quit key binding$/ do
 | 
			
		||||
  x_key 'alt+q'
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
Then /^it must connect to X display$/ do
 | 
			
		||||
  uhwm_wait_output 'Connected to'
 | 
			
		||||
  expect(x_socket_check @process.pid).to be true
 | 
			
		||||
  expect(x_socket_check uhwm_pid).to be true
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
require 'aruba/cucumber'
 | 
			
		||||
require 'headless'
 | 
			
		||||
 | 
			
		||||
require 'uh/wm/testing/acceptance_helpers'
 | 
			
		||||
 | 
			
		||||
module Aruba
 | 
			
		||||
  class SpawnProcess
 | 
			
		||||
    def pid
 | 
			
		||||
@@ -9,15 +11,14 @@ module Aruba
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
World(Uh::WM::Testing::AcceptanceHelpers)
 | 
			
		||||
 | 
			
		||||
Headless.new.start
 | 
			
		||||
 | 
			
		||||
After do |scenario|
 | 
			
		||||
  @process and @process.terminate
 | 
			
		||||
  uhwm_ensure_stop
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
Around '@other_wm_running' do |scenario, block|
 | 
			
		||||
  @other_wm = ChildProcess.build('twm')
 | 
			
		||||
  @other_wm.start
 | 
			
		||||
  block.call
 | 
			
		||||
  @other_wm.stop
 | 
			
		||||
  with_other_wm { block.call }
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										69
									
								
								lib/uh/wm/testing/acceptance_helpers.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								lib/uh/wm/testing/acceptance_helpers.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,69 @@
 | 
			
		||||
module Uh
 | 
			
		||||
  module WM
 | 
			
		||||
    module Testing
 | 
			
		||||
      module AcceptanceHelpers
 | 
			
		||||
        def uhwm_run options = '-v'
 | 
			
		||||
          command = %w[uhwm]
 | 
			
		||||
          command << options if options
 | 
			
		||||
          @interactive = @process = run command.join ' '
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def uhwm_ensure_stop
 | 
			
		||||
          @process and @process.terminate
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def uhwm_pid
 | 
			
		||||
          @process.pid
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def uhwm_output
 | 
			
		||||
          @process.stdout
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def uhwm_wait_output message, timeout: 1
 | 
			
		||||
          Timeout.timeout(timeout) do
 | 
			
		||||
            loop do
 | 
			
		||||
              break if case message
 | 
			
		||||
                when Regexp then @process.stdout + @process.stderr =~ message
 | 
			
		||||
                when String then assert_partial_output_interactive message
 | 
			
		||||
              end
 | 
			
		||||
              sleep 0.1
 | 
			
		||||
            end
 | 
			
		||||
          end
 | 
			
		||||
        rescue Timeout::Error
 | 
			
		||||
          fail [
 | 
			
		||||
            "expected `#{message}' not seen after #{timeout} seconds in:",
 | 
			
		||||
            "  ```\n  #{@process.stdout + @process.stderr}  ```"
 | 
			
		||||
          ].join "\n"
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def uhwm_run_wait_ready
 | 
			
		||||
          uhwm_run
 | 
			
		||||
          uhwm_wait_output 'Connected to'
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def with_other_wm
 | 
			
		||||
          @other_wm = ChildProcess.build('twm').tap { |o| o.start }
 | 
			
		||||
          @other_wm.stop
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def other_wm
 | 
			
		||||
          @other_wm
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def x_key key
 | 
			
		||||
          fail "cannot simulate X key `#{key}'" unless system "xdotool key #{key}"
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def x_socket_check pid
 | 
			
		||||
          case RbConfig::CONFIG['host_os']
 | 
			
		||||
          when /linux/
 | 
			
		||||
            `netstat -xp 2> /dev/null`.lines.grep /\s+#{pid}\/ruby/
 | 
			
		||||
          else
 | 
			
		||||
            `sockstat -u`.lines.grep /\s+ruby.+\s+#{pid}/
 | 
			
		||||
          end.any?
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user