Skip to content

Commit 511a968

Browse files
author
Glyn Normington
committed
Handle buildpack not in a git repository
When the buildpack is not in a git repository, issue an appropriate debug log message and prevent 'fatal' standard error messages from appearing. Improve test coverage of Buildpack class. [#55742000]
1 parent f7fe4dc commit 511a968

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

lib/java_buildpack/buildpack.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ def release
107107

108108
private
109109

110-
GIT_DIR = File.expand_path('../../.git', File.dirname(__FILE__)).freeze
111-
112110
COMPONENTS_CONFIG = '../../config/components.yml'.freeze
113111

114112
LIB_DIRECTORY = '.lib'
@@ -193,6 +191,10 @@ def self.framework_directory
193191
Pathname.new(File.expand_path('framework', File.dirname(__FILE__)))
194192
end
195193

194+
def self.git_dir
195+
File.expand_path('../../.git', File.dirname(__FILE__))
196+
end
197+
196198
def self.jre_directory
197199
Pathname.new(File.expand_path('jre', File.dirname(__FILE__)))
198200
end
@@ -202,8 +204,12 @@ def self.log_git_data(logger)
202204
# Call the debug method passing a parameter rather than a block so that, should the git command
203205
# become inaccessible to the buildpack at some point in the future, we find out before someone
204206
# happens to switch on debug logging.
205-
logger.debug("git remotes: #{`git --git-dir=#{GIT_DIR} remote -v`}")
206-
logger.debug("git HEAD commit: #{`git --git-dir=#{GIT_DIR} log HEAD^!`}")
207+
if system("git --git-dir=#{git_dir} status 2>/dev/null 1>/dev/null")
208+
logger.debug("git remotes: #{`git --git-dir=#{git_dir} remote -v`}")
209+
logger.debug("git HEAD commit: #{`git --git-dir=#{git_dir} log HEAD^!`}")
210+
else
211+
logger.debug('Buildpack is not stored in a git repository')
212+
end
207213
end
208214

209215
def self.lib_directory(app_dir)

spec/java_buildpack/buildpack_spec.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module JavaBuildpack
3333

3434
before do
3535
YAML.stub(:load_file).with(File.expand_path('config/logging.yml')).and_return(
36-
'default_log_level' => 'INFO'
36+
'default_log_level' => 'DEBUG'
3737
)
3838
YAML.stub(:load_file).with(File.expand_path('config/components.yml')).and_return(
3939
'containers' => ['Test::StubContainer1', 'Test::StubContainer2'],
@@ -49,6 +49,8 @@ module JavaBuildpack
4949

5050
Test::StubJre1.stub(:new).and_return(stub_jre1)
5151
Test::StubJre2.stub(:new).and_return(stub_jre2)
52+
53+
$stderr = StringIO.new
5254
end
5355

5456
it 'should raise an error if more than one container can run an application' do
@@ -125,6 +127,26 @@ module JavaBuildpack
125127
with_buildpack { |buildpack| buildpack.detect }
126128
end
127129

130+
it 'logs information about the git repository of a buildpack' do
131+
with_buildpack { |buildpack| buildpack.detect }
132+
standard_error = $stderr.string
133+
expect(standard_error).to match(/git remotes/)
134+
expect(standard_error).to match(/git HEAD commit/)
135+
end
136+
137+
it 'realises when buildpack is not stored in a git repository' do
138+
Dir.mktmpdir do |tmp_dir|
139+
Buildpack.stub(:git_dir).and_return(tmp_dir)
140+
with_buildpack { |buildpack| buildpack.detect }
141+
expect($stderr.string).to match(/Buildpack is not stored in a git repository/)
142+
end
143+
end
144+
145+
it 'handles exceptions correctly' do
146+
expect { with_buildpack { |buildpack| raise 'an exception' } }.to raise_error SystemExit
147+
expect($stderr.string).to match(/an exception/)
148+
end
149+
128150
def with_buildpack(&block)
129151
JavaBuildpack::Diagnostics::LoggerFactory.send :close # suppress warnings
130152
Dir.mktmpdir do |root|

0 commit comments

Comments
 (0)