Skip to content

Commit

Permalink
Refactor the pre_condition hacks into an RSpec shared context.
Browse files Browse the repository at this point in the history
DRY!
  • Loading branch information
R. Tyler Croy committed Mar 10, 2014
1 parent e3ddd0e commit 6977815
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 47 deletions.
3 changes: 1 addition & 2 deletions spec/classes/jenkins_cli_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'spec_helper'

describe 'jenkins' do
describe 'jenkins', :type => :module do
let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'RedHat' } }
let(:pre_condition) { [] }

context 'cli' do
context 'default' do
Expand Down
3 changes: 1 addition & 2 deletions spec/classes/jenkins_config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'spec_helper'

describe 'jenkins' do
describe 'jenkins', :type => :module do
let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'RedHat' } }
let(:pre_condition) { [] }

context 'config' do
context 'default' do
Expand Down
2 changes: 1 addition & 1 deletion spec/classes/jenkins_firewall_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe 'jenkins' do
describe 'jenkins', :type => :module do
let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'RedHat' } }
let(:pre_condition) { ["define firewall($action, $state, $dport, $proto) {}"] }
let(:params) { { :configure_firewall => true } }
Expand Down
3 changes: 1 addition & 2 deletions spec/classes/jenkins_package_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'spec_helper'

describe 'jenkins' do
describe 'jenkins', :type => :module do
let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'RedHat' } }
let(:pre_condition) { [] }

describe 'package' do
context 'default' do
Expand Down
3 changes: 1 addition & 2 deletions spec/classes/jenkins_plugins_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'spec_helper'

describe 'jenkins' do
describe 'jenkins', :type => :module do
let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'RedHat' } }
let(:pre_condition) { [] }

context 'plugins' do
context 'default' do
Expand Down
3 changes: 1 addition & 2 deletions spec/classes/jenkins_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'spec_helper'

describe 'jenkins' do
describe 'jenkins', :type => :module do
let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'RedHat' } }
let(:pre_condition) { [] }

context 'proxy' do
context 'default' do
Expand Down
4 changes: 1 addition & 3 deletions spec/classes/jenkins_repo_debian_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'spec_helper'

describe 'jenkins' do
let(:pre_condition) { [] }

describe 'jenkins', :type => :module do
# Switching OS Family to prevent duplicate declaration
let(:facts) do
{
Expand Down
3 changes: 1 addition & 2 deletions spec/classes/jenkins_repo_el_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require 'spec_helper'

describe 'jenkins' do
describe 'jenkins', :type => :module do
# Switching OS Family to prevent duplicate declaration
let(:facts) { { :osfamily => 'Redhat', :operatingsystem => 'CentOS' } }
let(:pre_condition) { [] }

context 'repo::el' do
describe 'default' do
Expand Down
3 changes: 1 addition & 2 deletions spec/classes/jenkins_repo_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'spec_helper'

describe 'jenkins' do
let(:pre_condition) { [] }
describe 'jenkins', :type => :module do

describe 'repo' do
describe 'default' do
Expand Down
3 changes: 1 addition & 2 deletions spec/classes/jenkins_repo_suse_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require 'spec_helper'

describe 'jenkins' do
describe 'jenkins', :type => :module do
# Switching OS Family to prevent duplicate declaration
let(:facts) { { :osfamily => 'Suse', :operatingsystem => 'OpenSuSE' } }
let(:pre_condition) { [] }

context 'repo::suse' do
describe 'default' do
Expand Down
3 changes: 1 addition & 2 deletions spec/classes/jenkins_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'spec_helper'

describe 'jenkins' do
describe 'jenkins', :type => :module do
let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'RedHat' } }
let(:pre_condition) { [] }

context 'service' do
context 'default' do
Expand Down
3 changes: 1 addition & 2 deletions spec/classes/jenkins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

# Note, rspec-puppet determines the class name from the top level describe
# string.
describe 'jenkins' do
let(:pre_condition) { [] }
describe 'jenkins', :type => :module do
describe "on RedHat" do
let(:facts) do
{ :osfamily => 'RedHat', :operatingsystem => 'CentOS' }
Expand Down
33 changes: 33 additions & 0 deletions spec/helpers/rspechelpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'rubygems'
require 'rspec'


module Jenkins
module RSpecHelpers
def clear_facts
end

shared_context "custom fact example", :type => :fact do
# Need to make sure we clear out our facts at the start to make sure that
# we don't pick up some facts left over from rspec-puppet
#
# Also clear at the end to be a good citizen
around :each do
Facter.clear
Facter.clear_messages
end
end

shared_context "module pre-conditions", :type => :module do
let(:pre_condition) { [] }

before :each do
if pre_condition.instance_of? Array
pre_condition << 'class stdlib {}'
else
pre_condition = "class stdlib {} \n #{pre_condition}"
end
end
end
end
end
26 changes: 3 additions & 23 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,12 @@

$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../'))

require 'spec/helpers/rspechelpers'

RSpec.configure do |c|
# Override puppetlabs_spec_helper's stupid setting of mock_with to :mocha,
# which is a totally piece of garbage mocking library
c.mock_with :rspec

def clear_facts
Facter.clear
Facter.clear_messages
end

c.before(:each, :type => :fact) do
# Need to make sure we clear out our facts at the start to make sure that
# we don't pick up some facts left over from rspec-puppet
clear_facts
end
c.after(:each, :type => :fact) do
clear_facts
end

c.before :each do
if self.respond_to? :pre_condition
if pre_condition.instance_of? Array
pre_condition << 'class stdlib {}'
else
pre_condition = "class stdlib {} \n #{pre_condition}"
end
end
end
c.include(Jenkins::RSpecHelpers)
end

0 comments on commit 6977815

Please sign in to comment.