Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(SDK-331) Use vendored Gemfile.lock when available and needed #215

Merged
merged 1 commit into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/pdk/util/bundler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'bundler'
require 'fileutils'
require 'tty-spinner'
require 'pdk/util'
require 'pdk/cli/exec'
Expand All @@ -22,8 +23,20 @@ def self.ensure_bundle!
end

unless bundle.locked?
unless bundle.lock!
raise PDK::CLI::FatalError, _('Unable to resolve Gemfile dependencies.')
if PDK::Util.package_install?
# In packaged installs, try to use vendored Gemfile.lock as a starting point.
# The 'bundle install' below will pick up any new dependencies.
vendored_gemfile_lock = File.join(PDK::Util.package_cachedir, 'Gemfile.lock')

if File.exist?(vendored_gemfile_lock)
PDK.logger.debug(_("No Gemfile.lock found in module, using vendored Gemfile.lock from '%{source}'") % { source: vendored_gemfile_lock })
FileUtils.cp(vendored_gemfile_lock, File.join(PDK::Util.module_root, 'Gemfile.lock'))
end
else
# In non-packaged installs, just let bundler resolve deps as normal.
unless bundle.lock!
raise PDK::CLI::FatalError, _('Unable to resolve Gemfile dependencies.')
end
end
end

Expand Down
5 changes: 4 additions & 1 deletion package-testing/tests/validate_a_new_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
end

step 'Validate the module' do
on(workstation, "cd #{module_name} && #{pdk_command(workstation, 'validate')}", accept_all_exit_codes: true) do |outcome|
on(workstation, "cd #{module_name} && #{pdk_command(workstation, 'validate --debug')}", accept_all_exit_codes: true) do |outcome|
assert_equal(0, outcome.exit_code,
"Validate on a new module should return 0. stderr was: #{outcome.stderr}")

assert_match(%r{using vendored gemfile\.lock}i, outcome.stdout, 'pdk should use vendored Gemfile.lock')

on(workstation, "test -f #{module_name}/Gemfile.lock", accept_all_exit_codes: true) do |lock_check_outcome|
assert_equal(0, lock_check_outcome.exit_code, 'pdk validate should have caused a Gemfile.lock file to be created')
end
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/pdk/util/bundler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def bundle_regex

context 'when there is no Gemfile.lock' do
before(:each) do
allow(described_class).to receive(:already_bundled?).and_return(false)

allow(File).to receive(:file?).with(%r{Gemfile$}).and_return(true)
allow(File).to receive(:file?).with(%r{Gemfile\.lock$}).and_return(false)

Expand All @@ -62,6 +64,24 @@ def bundle_regex
allow_command([bundle_regex, 'install', any_args])
end

context 'when part of a packaged installation' do
before(:each) do
allow(PDK::Util).to receive(:package_install?).and_return(true)
allow(File).to receive(:file?).with(%r{PDK_VERSION}).and_return(true)
allow(File).to receive(:exist?).with(bundle_regex).and_return(true)
end

it 'copies a Gemfile.lock from vendored location' do
allow(PDK::Util).to receive(:package_cachedir).and_return('/package/cachedir')
allow(File).to receive(:exist?).with('/package/cachedir/Gemfile.lock').and_return(true)

expect(logger).to receive(:debug).with(%r{using vendored gemfile\.lock}i)
expect(FileUtils).to receive(:cp).with('/package/cachedir/Gemfile.lock', %r{Gemfile\.lock$})

described_class.ensure_bundle!
end
end

it 'generates Gemfile.lock' do
expect_command([bundle_regex, 'lock', any_args], nil, %r{resolving gemfile}i)

Expand Down