From 0cf695f4ab127a540cb3623110e6089b7e07e366 Mon Sep 17 00:00:00 2001 From: tphoney Date: Thu, 14 Feb 2019 16:42:25 +0000 Subject: [PATCH] (MODULES-3958) enable rspec-mock and code coverage --- .sync.yml | 21 ++++++++------ spec/spec_helper.rb | 5 ++++ spec/spec_helper_local.rb | 29 +++++++++++++++++-- .../provider/ini_setting/inheritance_spec.rb | 16 +++++----- .../puppet/provider/ini_setting/ruby_spec.rb | 4 +-- 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/.sync.yml b/.sync.yml index 3c6c9099..b3136c3e 100644 --- a/.sync.yml +++ b/.sync.yml @@ -1,4 +1,12 @@ --- +.gitlab-ci.yml: + unmanaged: true + +.rubocop.yml: + require: + - rubocop-i18n + - rubocop-rspec + .travis.yml: docker_sets: - set: docker/centos-7 @@ -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 - \ No newline at end of file + +spec/spec_helper.rb: + mock_with: ':rspec' + coverage_report: true diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0d5efc0b..149ff033 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,7 @@ +RSpec.configure do |c| + c.mock_with :rspec +end + require 'puppetlabs_spec_helper/module_spec_helper' require 'rspec-puppet-facts' @@ -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 diff --git a/spec/spec_helper_local.rb b/spec/spec_helper_local.rb index e35501c5..2e897cb5 100644 --- a/spec/spec_helper_local.rb +++ b/spec/spec_helper_local.rb @@ -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 diff --git a/spec/unit/puppet/provider/ini_setting/inheritance_spec.rb b/spec/unit/puppet/provider/ini_setting/inheritance_spec.rb index 29667bf9..d3b87601 100644 --- a/spec/unit/puppet/provider/ini_setting/inheritance_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/inheritance_spec.rb @@ -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') } @@ -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 @@ -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 @@ -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 diff --git a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb index d506aad8..0e8a9383 100644 --- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb @@ -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 @@ -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' },