[DRE-commits] [ruby-aruba] 08/98: Fixed problem with run_simple using positional arguments

Hideki Yamane henrich at moszumanska.debian.org
Tue Mar 22 12:20:33 UTC 2016


This is an automated email from the git hooks/post-receive script.

henrich pushed a commit to branch debian/sid
in repository ruby-aruba.

commit b632e2686a30fe3eaf334202d8f1776d02642d6b
Author: Dennis Günnewig <dg1 at ratiodata.de>
Date:   Tue Dec 8 10:15:39 2015 +0100

    Fixed problem with run_simple using positional arguments
---
 features/api/command/run_simple.feature | 207 ++++++++++++++++++++++++++++++++
 lib/aruba/api/command.rb                |  34 +++---
 2 files changed, 225 insertions(+), 16 deletions(-)

diff --git a/features/api/command/run_simple.feature b/features/api/command/run_simple.feature
new file mode 100644
index 0000000..37499b9
--- /dev/null
+++ b/features/api/command/run_simple.feature
@@ -0,0 +1,207 @@
+Feature: Run command
+
+  To run a command use the `#run`-method. There are some configuration options
+  which are relevant here:
+
+  - `fail_on_error`:
+
+    Given this option is `true`, `aruba` fails if the `command` fails to run - exit code <> 0.
+
+    For all other options see [run.feature](run.feature).
+    
+  Background:
+    Given I use a fixture named "cli-app"
+
+  Scenario: Require executable to succeed (by default value)
+    Given an executable named "bin/cli" with:
+    """bash
+    #!/bin/bash
+    exit 1
+    """
+    And a file named "spec/run_spec.rb" with:
+    """ruby
+    require 'spec_helper'
+
+    RSpec.describe 'Run command', :type => :aruba do
+      it { expect { run_simple('cli') }.to raise_error RSpec::Expectations::ExpectationNotMetError }
+    end
+    """
+    When I run `rspec`
+    Then the specs should all pass
+
+  Scenario: Require executable to succeed (set by option)
+    Given an executable named "bin/cli" with:
+    """bash
+    #!/bin/bash
+    exit 1
+    """
+    And a file named "spec/run_spec.rb" with:
+    """ruby
+    require 'spec_helper'
+
+    RSpec.describe 'Run command', :type => :aruba do
+      it { expect { run_simple('cli', fail_on_error: true) }.to raise_error }
+    end
+    """
+    When I run `rspec`
+    Then the specs should all pass
+
+  Scenario: Require executable to succeed (set by option, deprecated)
+    Given an executable named "bin/cli" with:
+    """bash
+    #!/bin/bash
+    exit 1
+    """
+    And a file named "spec/run_spec.rb" with:
+    """ruby
+    require 'spec_helper'
+
+    RSpec.describe 'Run command', :type => :aruba do
+      it { expect { run_simple('cli', true) }.to raise_error }
+    end
+    """
+    When I run `rspec`
+    Then the specs should all pass
+
+  Scenario: Ignore failure of executable (set by option)
+    Given an executable named "bin/cli" with:
+    """bash
+    #!/bin/bash
+    exit 1
+    """
+    And a file named "spec/run_spec.rb" with:
+    """ruby
+    require 'spec_helper'
+
+    RSpec.describe 'Run command', :type => :aruba do
+      it { expect { run_simple('cli', fail_on_error: false) }.not_to raise_error }
+    end
+    """
+    When I run `rspec`
+    Then the specs should all pass
+
+  Scenario: Ignore failure of executable (set by option, deprecated)
+    Given an executable named "bin/cli" with:
+    """bash
+    #!/bin/bash
+    exit 1
+    """
+    And a file named "spec/run_spec.rb" with:
+    """ruby
+    require 'spec_helper'
+
+    RSpec.describe 'Run command', :type => :aruba do
+      it { expect { run_simple('cli', false) }.not_to raise_error }
+    end
+    """
+    When I run `rspec`
+    Then the specs should all pass
+
+  Scenario: Command with long startup phase
+
+    If you have got a command with a long startup phase or use `ruby` together
+    with `bundler`, you should consider using the `startup_wait_time`-option.
+    Otherwise methods like `#send_signal` don't work since they require the
+    command to be running and have setup it's signal handler.
+
+    Given an executable named "bin/cli" with:
+    """bash
+    #!/usr/bin/env bash
+ 
+    function initialize_script {
+      sleep 2
+    }
+
+    function do_some_work {
+      echo "Hello, Aruba is working"
+    }
+
+    initialize_script
+    do_some_work
+
+    exit 0
+    """
+    And a file named "spec/run_spec.rb" with:
+    """ruby
+    require 'spec_helper'
+
+    RSpec.describe 'Run command', :type => :aruba, :exit_timeout => 1, :startup_wait_time => 2 do
+      before(:each) { run_simple('cli') }
+
+      it { expect(last_command_started).to be_successfully_executed }
+      it { expect(last_command_started).to have_output /Hello, Aruba is working/ }
+    end
+    """
+    When I run `rspec`
+    Then the specs should all pass
+
+  Scenario: Long running command
+
+    If you have got a "long running" command, you should consider using the
+    `exit_timeout`-option.
+
+    Given an executable named "bin/cli" with:
+    """bash
+    #!/usr/bin/env bash
+
+    function do_some_work {
+      sleep 2
+      echo "Hello, Aruba here"
+    }
+
+    do_some_work
+    """
+    And a file named "spec/run_spec.rb" with:
+    """ruby
+    require 'spec_helper'
+
+    RSpec.describe 'Run command', :type => :aruba, :exit_timeout => 3 do
+      before(:each) { run_simple('cli') }
+
+      it { expect(last_command_started).to be_successfully_executed }
+      it { expect(last_command_started).to have_output /Hello, Aruba here/ }
+    end
+    """
+    When I run `rspec`
+    Then the specs should all pass
+
+  Scenario: Sending signals to commands started with `#run_simple()`
+
+    Sending signals to a command which is started by
+    `#run_simple()` does not make sense. The command is stopped internally when
+    its exit status is checked.
+
+    Given an executable named "bin/cli" with:
+    """bash
+    #!/usr/bin/env bash
+
+    function initialize_script {
+      sleep 1
+    }
+
+    function cleanup_script {
+      sleep 1
+    }
+
+    function do_some_work {
+      echo "Hello, Aruba is working"
+    }
+
+    trap stop_script TERM
+
+    initialize_script
+    do_some_work
+    cleanup_script
+    exit 0
+    """
+    And a file named "spec/run_spec.rb" with:
+    """ruby
+    require 'spec_helper'
+
+    RSpec.describe 'Run command', :type => :aruba, :exit_timeout => 2, :startup_wait_time => 1 do
+      before(:each) { run_simple('cli') }
+      it { expect { last_command_started.send_signal 'HUP' }.to raise_error Aruba::CommandAlreadyStoppedError }
+    end
+    """
+    When I run `rspec`
+    Then the specs should all pass
diff --git a/lib/aruba/api/command.rb b/lib/aruba/api/command.rb
index 17edb46..40aaa79 100644
--- a/lib/aruba/api/command.rb
+++ b/lib/aruba/api/command.rb
@@ -144,19 +144,19 @@ module Aruba
         if args.last.is_a? Hash
           opts = args.pop
 
-          exit_timeout      = opts.fetch(:exit_timeout, aruba.config.exit_timeout)
-          io_wait_timeout   = opts.fetch(:io_wait_timeout, aruba.config.io_wait_timeout)
-          stop_signal       = opts.fetch(:stop_signal, aruba.config.stop_signal)
-          startup_wait_time = opts.fetch(:startup_wait_time, aruba.config.startup_wait_time)
+          exit_timeout      = opts[:exit_timeout].nil? ? aruba.config.exit_timeout : opts[:exit_timeout]
+          io_wait_timeout   = opts[:io_wait_timeout].nil? ? aruba.config.io_wait_timeout : opts[:io_wait_timeout]
+          stop_signal       = opts[:stop_signal].nil? ? aruba.config.stop_signal : opts[:stop_signal]
+          startup_wait_time = opts[:startup_wait_time].nil? ? aruba.config.startup_wait_time : opts[:startup_wait_time]
         else
           if args.size > 1
             Aruba.platform.deprecated("Please pass options to `#run` as named parameters/hash and don\'t use the old style, e.g. `#run('cmd', :exit_timeout => 5)`.")
           end
 
-          exit_timeout      = args[0] || aruba.config.exit_timeout
-          io_wait_timeout   = args[1] || aruba.config.io_wait_timeout
-          stop_signal       = args[2] || aruba.config.stop_signal
-          startup_wait_time = args[3] || aruba.config.startup_wait_time
+          exit_timeout      = args[0].nil? ? aruba.config.exit_timeout : args[0]
+          io_wait_timeout   = args[1].nil? ? aruba.config.io_wait_timeout : args[1]
+          stop_signal       = args[2].nil? ? aruba.config.stop_signal : args[2]
+          startup_wait_time = args[3].nil? ? aruba.config.startup_wait_time : args[3]
         end
 
         cmd = replace_variables(cmd)
@@ -257,10 +257,7 @@ module Aruba
 
         if args.last.is_a? Hash
           opts = args.pop
-
-          fail_on_error   = opts.fetch(:fail_on_error, false) || false
-          exit_timeout    = opts.fetch(:exit_timeout, aruba.config.exit_timeout) || aruba.config.exit_timeout
-          io_wait_timeout = opts.fetch(:io_wait_timeout, aruba.config.io_wait_timeout) || aruba.config.io_wait_timeout
+          fail_on_error = opts.delete(:fail_on_error) == true ? true : false
         else
           if args.size > 1
             # rubocop:disable Metrics/LineLength
@@ -268,12 +265,17 @@ module Aruba
             # rubocop:enable Metrics/LineLength
           end
 
-          fail_on_error     = args[0] || true
-          exit_timeout      = args[1] || aruba.config.exit_timeout
-          io_wait_timeout   = args[2] || aruba.config.io_wait_timeout
+          fail_on_error = args[0] == false ? false : true
+
+          opts = {
+            :exit_timeout      => (args[1] || aruba.config.exit_timeout),
+            :io_wait_timeout   => (args[2] || aruba.config.io_wait_timeout),
+            :stop_signal       => (args[3] || aruba.config.stop_signal),
+            :startup_wait_time => (args[4] || aruba.config.startup_wait_time)
+          }
         end
 
-        command = run(cmd, :exit_timeout => exit_timeout, :io_wait_timeout => io_wait_timeout)
+        command = run(cmd, opts)
         command.stop
 
         if Aruba::VERSION < '1'

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-aruba.git



More information about the Pkg-ruby-extras-commits mailing list