Skip to content

Commit

Permalink
(PDK-1200) Fix bundle env handling with puppet-dev
Browse files Browse the repository at this point in the history
Bundler requires that you continue to pass in PUPPET_GEM_VERSION
when doing check, install, and exec. Previously we were only setting
the puppet gem env when doing a bundle check and updating the lock file.
  • Loading branch information
Bryan Jen committed Oct 30, 2018
1 parent 7a8ef54 commit 818b9e8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
3 changes: 3 additions & 0 deletions lib/pdk/cli/test/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ module PDK::CLI
puppet_env = PDK::CLI::Util.puppet_from_opts_or_env(opts)
PDK::Util::PuppetVersion.fetch_puppet_dev if opts.key?(:'puppet-dev')
PDK::Util::RubyVersion.use(puppet_env[:ruby_version])

opts.merge!(puppet_env[:gemset])

PDK::Util::Bundler.ensure_bundle!(puppet_env[:gemset])

exit_code = PDK::Test::Unit.invoke(report, opts)
Expand Down
5 changes: 4 additions & 1 deletion lib/pdk/tests/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ def self.invoke(report, options = {})
setup

tests = options.fetch(:tests)

environment = { 'CI_SPEC_OPTIONS' => '--format j' }
environment['PUPPET_GEM_VERSION'] = options[:puppet] if options[:puppet]
spinner_msg = options.key?(:parallel) ? _('Running unit tests in parallel.') : _('Running unit tests.')
result = rake(cmd(tests, options), spinner_msg, 'CI_SPEC_OPTIONS' => '--format j')
result = rake(cmd(tests, options), spinner_msg, environment)

json_result = if options.key?(:parallel)
PDK::Util.find_all_json_in(result[:stdout])
Expand Down
5 changes: 3 additions & 2 deletions lib/pdk/util/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ def self.ensure_bundle!(gem_overrides = nil)
bundle.update_lock!(with: gem_overrides, local: all_deps_available)

# If there are missing dependencies after updating the lockfile, let `bundle install`
# go out and get them.
unless bundle.installed?
# go out and get them. If the specified puppet gem version points to a remote location
# or local filepath, then run bundle install as well.
if !bundle.installed? || (gem_overrides[:puppet] && gem_overrides[:puppet].start_with?('file://', 'git://', 'https://'))
bundle.install!(gem_overrides)
end

Expand Down
44 changes: 41 additions & 3 deletions spec/acceptance/version_changer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,47 @@
end
end

describe command('pdk validate --puppet-dev') do
its(:stderr) { is_expected.to match(%r{Using Puppet file://}i) }
its(:exit_status) { is_expected.to eq(0) }
describe 'puppet-dev uses the correct puppet env' do
before(:all) do
File.open(File.join('manifests', 'init.pp'), 'w') do |f|
f.puts <<-PPFILE
# version_select
class version_select {
}
PPFILE
end

FileUtils.mkdir_p(File.join('spec', 'classes'))
File.open(File.join('spec', 'classes', 'version_select_spec.rb'), 'w') do |f|
f.puts <<-TESTFILE
require 'spec_helper'
describe 'version_select' do
context 'test env' do
it('has path') {
path = Gem::Specification.find_by_name('puppet').source.options.key?('path')
expect(path).to be true
}
end
end
TESTFILE
end
end

describe command('pdk validate --puppet-dev') do
its(:stderr) { is_expected.to match(%r{Using Puppet file://}i) }
its(:exit_status) { is_expected.to eq(0) }
end

describe command('pdk test unit') do
its(:exit_status) { is_expected.not_to eq(0) }
its(:stderr) { is_expected.not_to match(%r{Using Puppet file://}i) }
end

describe command('pdk test unit --puppet-dev') do
its(:exit_status) { is_expected.to eq(0) }
its(:stderr) { is_expected.to match(%r{Using Puppet file://}i) }
end
end
end
end
13 changes: 8 additions & 5 deletions spec/unit/pdk/cli/test/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

it { is_expected.not_to be_nil }

let(:ruby_version) { '2.4.3' }
let(:puppet_version) { '5.4.0' }

before(:each) do
allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).and_return(ruby_version: '2.4.3', gemset: { puppet: '5.4.0' })
allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).and_return(ruby_version: ruby_version, gemset: { puppet: puppet_version })
allow(PDK::Util::RubyVersion).to receive(:use)
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(hash_including(:puppet))
end
Expand Down Expand Up @@ -91,7 +94,7 @@

context 'when passed --clean-fixtures' do
it 'invokes the command with :clean-fixtures => true' do
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, :tests => anything, :'clean-fixtures' => true).once.and_return(0)
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, :puppet => puppet_version, :tests => anything, :'clean-fixtures' => true).once.and_return(0)
expect {
test_unit_cmd.run_this(['--clean-fixtures'])
}.to exit_zero
Expand All @@ -100,7 +103,7 @@

context 'when not passed --clean-fixtures' do
it 'invokes the command without :clean-fixtures' do
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, tests: anything).once.and_return(0)
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, puppet: puppet_version, tests: anything).once.and_return(0)
expect {
test_unit_cmd.run_this([])
}.to exit_zero
Expand Down Expand Up @@ -153,7 +156,7 @@
context 'with --puppet-dev' do
let(:puppet_env) do
{
ruby_version: '2.4.3',
ruby_version: ruby_version,
gemset: { puppet: 'file://path/to/puppet' },
}
end
Expand Down Expand Up @@ -196,7 +199,7 @@
let(:puppet_version) { '5.3' }
let(:puppet_env) do
{
ruby_version: '2.4.3',
ruby_version: ruby_version,
gemset: { puppet: '5.3.5' },
}
end
Expand Down

0 comments on commit 818b9e8

Please sign in to comment.