Skip to content

Commit

Permalink
Merge pull request puppetlabs#330 from tphoney/MODULES-3958
Browse files Browse the repository at this point in the history
(MODULES-3958) enable rspec-mock and code coverage
  • Loading branch information
david22swan authored Feb 18, 2019
2 parents 6e8d0d1 + 0cf695f commit d674268
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
21 changes: 12 additions & 9 deletions .sync.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
---
.gitlab-ci.yml:
unmanaged: true

.rubocop.yml:
require:
- rubocop-i18n
- rubocop-rspec

.travis.yml:
docker_sets:
- set: docker/centos-7
Expand Down Expand Up @@ -28,15 +36,10 @@ Gemfile:
- mingw
- x64_mingw

.gitlab-ci.yml:
unmanaged: true

Rakefile:
requires:
- puppet_pot_generator/rake_tasks

.rubocop.yml:
require:
- rubocop-i18n
- rubocop-rspec


spec/spec_helper.rb:
mock_with: ':rspec'
coverage_report: true
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
RSpec.configure do |c|
c.mock_with :rspec
end

require 'puppetlabs_spec_helper/module_spec_helper'
require 'rspec-puppet-facts'

Expand Down Expand Up @@ -34,6 +38,7 @@
end
c.filter_run_excluding(bolt: true) unless ENV['GEM_BOLT']
c.after(:suite) do
RSpec::Puppet::Coverage.report!(0)
end
end

Expand Down
29 changes: 27 additions & 2 deletions spec/spec_helper_local.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
RSpec.configure do |config|
config.mock_with :rspec
if ENV['COVERAGE'] == 'yes'
require 'simplecov'
require 'simplecov-console'
require 'codecov'

SimpleCov.formatters = [
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::Console,
SimpleCov::Formatter::Codecov,
]
SimpleCov.start do
track_files 'lib/**/*.rb'

add_filter '/spec'

# do not track vendored files
add_filter '/vendor'
add_filter '/.vendor'

# do not track gitignored files
# this adds about 4 seconds to the coverage check
# this could definitely be optimized
add_filter do |f|
# system returns true if exit status is 0, which with git-check-ignore means file is ignored
system("git check-ignore --quiet #{f.filename}")
end
end
end
16 changes: 8 additions & 8 deletions spec/unit/puppet/provider/ini_setting/inheritance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

# This is a reduced version of ruby_spec.rb just to ensure we can subclass as
# documented
$LOAD_PATH << './spec/fixtures/modules/inherit_ini_setting/lib'
provider_class = Puppet::Type.type(:inherit_ini_setting).provider(:ini_setting)
describe provider_class do
$LOAD_PATH << './spec/fixtures/inherit_ini_setting/lib'

describe Puppet::Type.type(:inherit_ini_setting).provider(:ini_setting) do
include PuppetlabsSpec::Files

let(:tmpfile) { tmpfilename('inherit_ini_setting_test') }
Expand All @@ -23,8 +23,8 @@ def validate_file(expected_content, tmpfile)
let(:orig_content) { '' }

it 'parses nothing when the file is empty' do
provider_class.stubs(:file_path).returns(tmpfile)
expect(provider_class.instances).to eq([])
allow(described_class).to receive(:file_path).and_return(tmpfile)
expect(described_class.instances).to eq([])
end

context 'when the file has contents' do
Expand All @@ -37,8 +37,8 @@ def validate_file(expected_content, tmpfile)
end

it 'parses the results' do
provider_class.stubs(:file_path).returns(tmpfile)
instances = provider_class.instances
allow(described_class).to receive(:file_path).and_return(tmpfile)
instances = described_class.instances
expect(instances.size).to eq(2)
# inherited version of namevar flattens the names
names = instances.map do |instance| instance.instance_variable_get(:@property_hash)[:name] end # rubocop:disable Style/BlockDelimiters
Expand All @@ -51,7 +51,7 @@ def validate_file(expected_content, tmpfile)
let(:orig_content) { '' }

it 'adds a value to the file' do
provider_class.stubs(:file_path).returns(tmpfile)
allow(described_class).to receive(:file_path).and_return(tmpfile)
resource = Puppet::Type::Inherit_ini_setting.new(setting: 'set_this', value: 'to_that')
provider = described_class.new(resource)
provider.create
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/puppet/provider/ini_setting/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def self.file_path
end
end
it 'returns [] when file is empty' do
child_one.stubs(:file_path).returns(emptyfile)
expect(child_one).to receive(:file_path).and_return(emptyfile)
expect(child_one.instances).to eq([])
end
child_two = Class.new(provider_class) do
Expand Down Expand Up @@ -90,7 +90,7 @@ def self.file_path
'/some/file/path'
end
end
child_three.stubs(:file_path).returns(tmpfile)
expect(child_three).to receive(:file_path).exactly(2).times.and_return(tmpfile)
expect(child_three.instances.size).to eq(7)
expected_array = [
{ name: 'section1/foo', value: 'foovalue' },
Expand Down

0 comments on commit d674268

Please sign in to comment.