Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

(maint) Ensure handle_puppet_run_returned_exit_code can use a Range #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ Bundler/DuplicatedGem:
# When there are dependencies we have duplicate gems so they cant be ordered
Bundler/OrderedGems:
Enabled: false

# Useless detection on Windows
Layout/EndOfLine:
Enabled: false
2 changes: 1 addition & 1 deletion beaker-testmode_switcher.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'beaker/testmode_switcher/version'

Expand Down
2 changes: 1 addition & 1 deletion lib/beaker/testmode_switcher/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ module DSL
end
end

include Beaker::TestmodeSwitcher::DSL
include Beaker::TestmodeSwitcher::DSL # rubocop:disable Style/MixinUsage This usage is expected
9 changes: 7 additions & 2 deletions lib/beaker/testmode_switcher/runner_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ def get_acceptable_puppet_run_exit_codes(opts = {})

# If no option supplied, return all exit codes, as an array,
# as acceptable so beaker returns a detailed output
(0...256)
0...256
end

def handle_puppet_run_returned_exit_code(acceptable_exit_codes, returned_exit_code)
return if acceptable_exit_codes.include?(returned_exit_code)
raise UnacceptableExitCodeError, "Unacceptable exit code returned: #{returned_exit_code}. Acceptable code(s): #{acceptable_exit_codes.join(', ')}"
acceptable_exit_codes_string = if acceptable_exit_codes.is_a?(Array)
acceptable_exit_codes.join(', ')
else
acceptable_exit_codes_string.to_s
end
raise UnacceptableExitCodeError, "Unacceptable exit code returned: #{returned_exit_code}. Acceptable code(s): #{acceptable_exit_codes_string}"
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/beaker/testmode_switcher/local_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
expect(File.read(@target)).to eq "content\n"
end

it 'creates a file with mode' do
it 'creates a file with mode', unless: windows_platform? do
subject.create_remote_file_ex(@target, "content\n", mode: '0700')
mode = format("%o", File.stat(@target).mode)
mode = format("%o", File.stat(@target).mode) # rubocop:disable Style/FormatStringToken Safe conversion in this instance
expect(mode).to eq "100700"
end
end
Expand Down
26 changes: 17 additions & 9 deletions spec/beaker/testmode_switcher/runner_base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,53 @@
describe Beaker::TestmodeSwitcher::RunnerBase do
subject { Beaker::TestmodeSwitcher::RunnerBase.new }

context 'get_acceptable_puppet_run_exit_codes' do
context ':catch_changes option passed' do
describe 'get_acceptable_puppet_run_exit_codes' do
context 'when :catch_changes option passed' do
it 'exit code of 0 given' do
expect(subject.get_acceptable_puppet_run_exit_codes(catch_changes: true)).to eq([0])
end
end

context ':catch_changes option passed' do
context 'when :catch_changes option passed' do
it 'exit codes of 0 & 2 given' do
expect(subject.get_acceptable_puppet_run_exit_codes(catch_failures: true)).to eq([0, 2])
end
end

context ':catch_changes option passed' do
context 'when :catch_changes option passed' do
it 'exit codes 1, 4 & 6 given' do
expect(subject.get_acceptable_puppet_run_exit_codes(expect_failures: true)).to eq([1, 4, 6])
end
end

context ':catch_changes option passed' do
context 'when :catch_changes option passed' do
it 'exit code of 2 given' do
expect(subject.get_acceptable_puppet_run_exit_codes(expect_changes: true)).to eq([2])
end
end

context 'no options passed' do
context 'when no options passed' do
it 'exit codes 0 - 256 given' do
expect(subject.get_acceptable_puppet_run_exit_codes).to eq((0...256))
end
end
end

context 'handle_puppet_run_returned_exit_code' do
describe 'handle_puppet_run_returned_exit_code' do
it 'throws UnacceptableExitCodeError when unacceptable exit code given' do
expect { subject.handle_puppet_run_returned_exit_code([0, 2], 5) }.to raise_error(Beaker::TestmodeSwitcher::UnacceptableExitCodeError, /Unacceptable exit code returned/i)
end

it 'not throw UnacceptableExitCodeError when acceptable exit code given' do
expect { subject.handle_puppet_run_returned_exit_code([0, 2], 2) }.not_to raise_error(Beaker::TestmodeSwitcher::UnacceptableExitCodeError)
it 'throws UnacceptableExitCodeError when unacceptable exit code given for a range' do
expect { subject.handle_puppet_run_returned_exit_code((0..2), 5) }.to raise_error(Beaker::TestmodeSwitcher::UnacceptableExitCodeError, /Unacceptable exit code returned/i)
end

it 'not throw when acceptable exit code given' do
expect { subject.handle_puppet_run_returned_exit_code([0, 2], 2) }.not_to raise_error
end

it 'not throw when acceptable exit code given for a range' do
expect { subject.handle_puppet_run_returned_exit_code((0..10), 2) }.not_to raise_error
end
end
end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@

# automatically load any shared examples or contexts
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }

def windows_platform?
# Ruby only sets File::ALT_SEPARATOR on Windows and the Ruby standard
# requiring features to be initialized and without side effect.
!!File::ALT_SEPARATOR # rubocop:disable Style/DoubleNegation Completely expected in this instance
end
2 changes: 1 addition & 1 deletion spec/support/examples/a_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
methods.each do |name|
it "should implement #{name} instead of mixing in Beaker::TestmodeSwitcher::DSL" do
method = subject.class.instance_method(name)
expect(method.owner).to_not be(Beaker::TestmodeSwitcher::DSL)
expect(method.owner).not_to be(Beaker::TestmodeSwitcher::DSL)
end
end
end