ruby-changes:73843
From: Victor <ko1@a...>
Date: Mon, 3 Oct 2022 20:42:07 +0900 (JST)
Subject: [ruby-changes:73843] bc6c1e0e25 (master): [rubygems/rubygems] Copy template contents instead of file and perms
https://git.ruby-lang.org/ruby.git/commit/?id=bc6c1e0e25 From bc6c1e0e25a9dc80382e2ffb8559bbe171e0400e Mon Sep 17 00:00:00 2001 From: Victor Gama <hey@v...> Date: Wed, 21 Sep 2022 15:09:36 -0300 Subject: [rubygems/rubygems] Copy template contents instead of file and perms This allows the file to be created without copying permissions from Bundler's installation source. The previous behaviour was noticed after installing Ruby through brew, and using bundle init, which yielded a read-only Gemfile. https://github.com/rubygems/rubygems/commit/839a06851d --- lib/bundler/cli/init.rb | 6 +++++- spec/bundler/commands/init_spec.rb | 23 +++++++++++++++++++++++ spec/bundler/support/path.rb | 4 ++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/bundler/cli/init.rb b/lib/bundler/cli/init.rb index e4f8229c48..bc96507c29 100644 --- a/lib/bundler/cli/init.rb +++ b/lib/bundler/cli/init.rb @@ -32,7 +32,11 @@ module Bundler https://github.com/ruby/ruby/blob/trunk/lib/bundler/cli/init.rb#L32 file << spec.to_gemfile end else - FileUtils.cp(File.expand_path("../templates/#{gemfile}", __dir__), gemfile) + File.open(File.expand_path("../templates/#{gemfile}", __dir__), "r") do |template| + File.open(gemfile, "wb") do |destination| + IO.copy_stream(template, destination) + end + end end puts "Writing new #{gemfile} to #{SharedHelpers.pwd}/#{gemfile}" diff --git a/spec/bundler/commands/init_spec.rb b/spec/bundler/commands/init_spec.rb index eaf8fa170a..9c499b99a1 100644 --- a/spec/bundler/commands/init_spec.rb +++ b/spec/bundler/commands/init_spec.rb @@ -7,6 +7,29 @@ RSpec.describe "bundle init" do https://github.com/ruby/ruby/blob/trunk/spec/bundler/commands/init_spec.rb#L7 expect(bundled_app_gemfile).to be_file end + context "with a template with permission flags not matching current process umask" do + let(:template_file) do + gemfile = Bundler.preferred_gemfile_name + templates_dir.join(gemfile) + end + + let(:target_dir) { bundled_app("init_permissions_test") } + + around do |example| + old_chmod = File.stat(template_file).mode + FileUtils.chmod(old_chmod | 0o111, template_file) # chmod +x + example.run + FileUtils.chmod(old_chmod, template_file) + end + + it "honours the current process umask when generating from a template" do + FileUtils.mkdir(target_dir) + bundle :init, :dir => target_dir + generated_mode = File.stat(File.join(target_dir, "Gemfile")).mode & 0o111 + expect(generated_mode).to be_zero + end + end + context "when a Gemfile already exists" do before do create_file "Gemfile", <<-G diff --git a/spec/bundler/support/path.rb b/spec/bundler/support/path.rb index a39e46c78a..7443e78d52 100644 --- a/spec/bundler/support/path.rb +++ b/spec/bundler/support/path.rb @@ -312,6 +312,10 @@ module Spec https://github.com/ruby/ruby/blob/trunk/spec/bundler/support/path.rb#L312 source_root.join("tool/bundler") end + def templates_dir + lib_dir.join("bundler", "templates") + end + extend self end end -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/