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

Warn if ruby/rails/bundle binstub shebangs are bad #586

Merged
merged 2 commits into from
Sep 14, 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
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