Skip to content

Commit

Permalink
Merge pull request #131 from rodjek/pdk_util_bundler_unit_tests
Browse files Browse the repository at this point in the history
(maint) Extend unit tests for PDK::Util::Bundler
  • Loading branch information
James Stocks authored Jul 7, 2017
2 parents ff4bb66 + 45c891e commit 3ddb9ff
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/pdk/util/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def binstubs!(gems)
binstub_dir = File.join(File.dirname(gemfile), 'bin')
return true if gems.all? { |gem| File.file?(File.join(binstub_dir, gem)) }

command = bundle_command('binstubs', gems.join(' '), '--force')
command = bundle_command('binstubs', *gems, '--force')

result = command.execute!

Expand Down
106 changes: 104 additions & 2 deletions spec/util/bundler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
allow(PDK::Util).to receive(:module_root).and_return('/')
end

# @todo: untangle tests of PDK::Util::Bundler and
# PDK::Util::Bundler::BundleHelper

# TODO: deduplicate code in these two methods and extract them to a shared location
def allow_command(argv, result = nil)
result ||= { exit_code: 0, stdout: '', stderr: '' }
Expand Down Expand Up @@ -50,7 +53,9 @@ def bundle_regex
allow(File).to receive(:file?).with(%r{Gemfile$}).and_return(true)
allow(File).to receive(:file?).with(%r{Gemfile\.lock$}).and_return(false)

allow_command([bundle_regex, 'check', any_args], exit_code: 1)
allow_command([bundle_regex, 'check', any_args], exit_code: 1, stdout: 'check stdout', stderr: 'check stderr')
allow($stderr).to receive(:puts).with('check stdout')
allow($stderr).to receive(:puts).with('check stderr')
allow_command([bundle_regex, 'install', any_args])
end

Expand All @@ -59,14 +64,31 @@ def bundle_regex

described_class.ensure_bundle!
end

context 'and it fails to generate Gemfile.lock' do
before(:each) do
allow(described_class).to receive(:already_bundled?).and_return(false)
allow_command([bundle_regex, 'lock'], exit_code: 1, stdout: 'lock stdout', stderr: 'lock stderr')
allow($stderr).to receive(:puts).with('lock stdout')
allow($stderr).to receive(:puts).with('lock stderr')
end

it 'raises a FatalError' do
expect {
described_class.ensure_bundle!
}.to raise_error(PDK::CLI::FatalError, %r{unable to resolve gemfile dependencies}i)
end
end
end

context 'when there are missing gems' do
before(:each) do
allow(File).to receive(:file?).with(%r{Gemfile$}).and_return(true)
allow(File).to receive(:file?).with(%r{Gemfile\.lock$}).and_return(true)

allow_command([bundle_regex, 'check', any_args], exit_code: 1)
allow_command([bundle_regex, 'check', any_args], exit_code: 1, stdout: 'check stdout', stderr: 'check stderr')
allow($stderr).to receive(:puts).with('check stdout')
allow($stderr).to receive(:puts).with('check stderr')
end

it 'installs missing gems' do
Expand All @@ -76,6 +98,21 @@ def bundle_regex
described_class.ensure_bundle!
end

context 'and it fails to install the gems' do
before(:each) do
allow(described_class).to receive(:already_bundled?).and_return(false)
allow_command([bundle_regex, 'install', any_args], exit_code: 1, stdout: 'install stdout', stderr: 'install stderr')
allow($stderr).to receive(:puts).with('install stdout')
allow($stderr).to receive(:puts).with('install stderr')
end

it 'raises a FatalError' do
expect {
described_class.ensure_bundle!
}.to raise_error(PDK::CLI::FatalError, %r{unable to install missing gemfile dependencies}i)
end
end

it 'only attempts to install the gems once' do
expect(PDK::CLI::Exec::Command).not_to receive(:new)
expect(logger).to receive(:debug).with(%r{already been installed})
Expand All @@ -99,4 +136,69 @@ def bundle_regex
end
end
end

describe '.ensure_binstubs!' do
let(:gemfile) { '/path/to/Gemfile' }
let(:binstub_dir) { File.join(File.dirname(gemfile), 'bin') }
let(:gems) { %w[rspec pdk rake] }

before(:each) do
allow(PDK::Util).to receive(:find_upwards).and_return(gemfile)
end

context 'when the binstubs do not already exist' do
before(:each) do
gems.each { |gem| allow(File).to receive(:file?).with(File.join(binstub_dir, gem)).and_return(false) }
end

it 'generates the requested binstubs' do
expect_command([bundle_regex, 'binstubs', *gems, '--force'])

described_class.ensure_binstubs!(*gems)
end
end

context 'when all the requested binstubs exist' do
before(:each) do
gems.each { |gem| allow(File).to receive(:file?).with(File.join(binstub_dir, gem)).and_return(true) }
end

it 'does not regenerate the requested binstubs' do
expect(PDK::CLI::Exec::Command).not_to receive(:new).with(bundle_regex, 'binstubs', any_args)

described_class.ensure_binstubs!(*gems)
end
end

context 'when not all of the requested binstubs exist' do
before(:each) do
allow(File).to receive(:file?).with(File.join(binstub_dir, 'rake')).and_return(true)
allow(File).to receive(:file?).with(File.join(binstub_dir, 'rspec')).and_return(false)
allow(File).to receive(:file?).with(File.join(binstub_dir, 'pdk')).and_return(true)
end

it 'generates the requested binstubs' do
expect_command([bundle_regex, 'binstubs', *gems, '--force'])

described_class.ensure_binstubs!(*gems)
end
end

context 'when it fails to generate the binstubs' do
before(:each) do
gems.each { |gem| allow(File).to receive(:file?).with(File.join(binstub_dir, gem)).and_return(false) }
allow_command([bundle_regex, 'binstubs', *gems, '--force'], exit_code: 1, stdout: 'binstubs stdout', stderr: 'binstubs stderr')
allow($stderr).to receive(:puts).with('binstubs stdout')
allow($stderr).to receive(:puts).with('binstubs stderr')
end

it 'raises a fatal error' do
expect(logger).to receive(:error).with(a_string_matching(%r{failed to generate binstubs}i))

expect {
described_class.ensure_binstubs!(*gems)
}.to raise_error(PDK::CLI::FatalError, %r{unable to install requested binstubs}i)
end
end
end
end

0 comments on commit 3ddb9ff

Please sign in to comment.