Skip to content

Fix the new rspec test that verifies puppetcore gems #564

@gavindidrichsen

Description

@gavindidrichsen

Add the following test file:

cat << EOL > spec/support/gemfile_spec.rb
# frozen_string_literal: true

require 'rspec-puppet'
require 'puppetlabs_spec_helper/puppet_spec_helper'
require 'puppetlabs_spec_helper/puppetlabs_spec/puppet_internals'

def param_value(subject, type, title, param)
  subject.resource(type, title).send(:parameters)[param.to_sym]
end

def verify_contents(subject, title, expected_lines)
  content = subject.resource('file', title).send(:parameters)[:content]
  expect(content.split("\n") & expected_lines).to match_array expected_lines.uniq
end

spec_path = File.expand_path(File.join(Dir.pwd, 'spec'))
fixture_path = File.join(spec_path, 'fixtures')

env_module_path = ENV.fetch('MODULEPATH', nil)
module_path = File.join(fixture_path, 'modules')

module_path = [module_path, env_module_path].join(File::PATH_SEPARATOR) if env_module_path
if ENV['SIMPLECOV'] == 'yes'
  begin
    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
  rescue LoadError
    raise 'Add the simplecov, simplecov-console, codecov gems to Gemfile to enable this task'
  end
end

# Add all spec lib dirs to LOAD_PATH
components = module_path.split(File::PATH_SEPARATOR).map do |dir|
  next unless Dir.exist? dir

  Dir.entries(dir).grep_v(/^\./).map { |f| File.join(dir, f, 'spec', 'lib') }
end
components.flatten.each do |d|
  $LOAD_PATH << d if FileTest.directory?(d) && !$LOAD_PATH.include?(d)
end

RSpec.configure do |c|
  c.formatter = 'RSpec::Github::Formatter' if ENV['GITHUB_ACTIONS'] == 'true'

  c.environmentpath = spec_path
  c.module_path = module_path

  # https://github.com/puppetlabs/rspec-puppet#strict_variables
  c.strict_variables = ENV['STRICT_VARIABLES'] != 'no'
  # https://github.com/puppetlabs/rspec-puppet#ordering
  c.ordering = ENV['ORDERING'] if ENV['ORDERING']

  c.before :each do
    if c.mock_framework.framework_name == :rspec
      allow(Puppet.features).to receive(:root?).and_return(true)
    else
      Puppet.features.stubs(:root?).returns(true)
    end
  end
end

EOL

And then run the tests and we see the following failure:

  puppetlabs-peadm git:(gavin_puppetcore_update) ✗ bundle exec rspec spec/support/gemfile_spec.rb 

An error occurred while loading ./spec/support/gemfile_spec.rb.
Failure/Error: require 'puppetlabs_spec_helper/module_spec_helper'

TypeError:
  no implicit conversion of nil into String
# ./vendor/bundle/ruby/3.2.0/gems/puppetlabs_spec_helper-6.0.3/lib/puppetlabs_spec_helper/module_spec_helper.rb:62:in `directory?'
# ./vendor/bundle/ruby/3.2.0/gems/puppetlabs_spec_helper-6.0.3/lib/puppetlabs_spec_helper/module_spec_helper.rb:62:in `block in <top (required)>'
# ./vendor/bundle/ruby/3.2.0/gems/puppetlabs_spec_helper-6.0.3/lib/puppetlabs_spec_helper/module_spec_helper.rb:61:in `each'
# ./vendor/bundle/ruby/3.2.0/gems/puppetlabs_spec_helper-6.0.3/lib/puppetlabs_spec_helper/module_spec_helper.rb:61:in `<top (required)>'
# ./spec/spec_helper.rb:7:in `<top (required)>'
# ./spec/support/gemfile_spec.rb:3:in `<top (required)>'
No examples found.

Finished in 0.00003 seconds (files took 4.23 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

➜  puppetlabs-peadm git:(gavin_puppetcore_update) ✗ 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions