Skip to content

Commit

Permalink
Merge pull request #586 from heroku/check-binstubs
Browse files Browse the repository at this point in the history
Warn if ruby/rails/bundle binstub shebangs are bad
  • Loading branch information
calebhearth authored Sep 14, 2017
2 parents c42a578 + ad7d764 commit c2ba079
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
15 changes: 15 additions & 0 deletions lib/language_pack/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,28 @@ def best_practice_warnings
end
end

def warn_bad_binstubs
Dir["bin/{rake,bundle,rails}"].select do |binstub|
begin
if File.file?(binstub)
shebang = File.open(binstub, &:readline)
if !shebang.match %r{\A#!/usr/bin/env ruby(.exe)?\z}
warn("Binstub #{binstub} contains shebang #{shebang}. This may cause issues if the program specified is unavailable.", inline: true)
end
end
rescue EOFError
end
end
end

def compile
instrument 'ruby.compile' do
# check for new app at the beginning of the compile
new_app?
Dir.chdir(build_path)
remove_vendor_bundle
warn_bundler_upgrade
warn_bad_binstubs
install_ruby
install_jvm
setup_language_pack_environment
Expand Down
1 change: 1 addition & 0 deletions spec/installers/heroku_ruby_installer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@
end

end

end
60 changes: 50 additions & 10 deletions spec/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

describe LanguagePack::Ruby do
describe "#install_binary" do
context "installing yarn" do
before do
@old_path = ENV['PATH']
@old_stack = ENV['STACK']
ENV['STACK'] = 'cedar-14'
end
before do
@old_path = ENV['PATH']
@old_stack = ENV['STACK']
ENV['STACK'] = 'cedar-14'
end

after do
ENV['PATH'] = @old_path
ENV['STACK'] = @old_stack
end
after do
ENV['PATH'] = @old_path
ENV['STACK'] = @old_stack
end

context "installing yarn" do
it "sets up PATH" do
Dir.mktmpdir do |build_path|
Dir.mktmpdir do |cache_path|
Expand All @@ -27,5 +27,45 @@
end

end
describe "#warn_bad_binstubs" do
it "warns about malformed shebangs" do
Dir.mktmpdir do |build_path|
Dir.mktmpdir do |cache_path|
Dir.chdir(build_path) do
Dir.mkdir("bin")
File.open("bin/rake", 'w') { |f| f.write("#!/usr/bin/env ruby") }
File.open("bin/rails", 'w') { |f| f.write("#!/usr/bin/env ruby.exe") }
File.open("bin/bundle", 'w') { |f| f.write("#!/usr/bin/env ruby1.7") }

ruby = LanguagePack::Ruby.new(build_path, cache_path)
output = capture(:stdout) { ruby.send(:warn_bad_binstubs) }

expect(output).to include(<<-WARNING)
###### WARNING:
Binstub bin/bundle contains shebang #!/usr/bin/env ruby1.7. This may cause issues if the program specified is unavailable.
WARNING
end
end
end
end

def capture(stream)
stream = stream.to_s
captured_stream = Tempfile.new(stream)
stream_io = eval("$#{stream}")
origin_stream = stream_io.dup
stream_io.reopen(captured_stream)

yield

stream_io.rewind
return captured_stream.read
ensure
captured_stream.close
captured_stream.unlink
stream_io.reopen(origin_stream)
end
end
end

end

0 comments on commit c2ba079

Please sign in to comment.