diff options
Diffstat (limited to 'spec/bundler/bundler/bundler_spec.rb')
-rw-r--r-- | spec/bundler/bundler/bundler_spec.rb | 221 |
1 files changed, 70 insertions, 151 deletions
diff --git a/spec/bundler/bundler/bundler_spec.rb b/spec/bundler/bundler/bundler_spec.rb index aeadcf9720..6a2e435e54 100644 --- a/spec/bundler/bundler/bundler_spec.rb +++ b/spec/bundler/bundler/bundler_spec.rb @@ -4,6 +4,71 @@ require "bundler" require "tmpdir" RSpec.describe Bundler do + describe "#load_marshal" do + it "is a private method and raises an error" do + data = Marshal.dump(Bundler) + expect { Bundler.load_marshal(data) }.to raise_error(NoMethodError, /private method [`']load_marshal' called/) + end + + it "loads any data" do + data = Marshal.dump(Bundler) + expect(Bundler.send(:load_marshal, data)).to eq(Bundler) + end + end + + describe "#safe_load_marshal" do + it "fails on unexpected class" do + data = Marshal.dump(Bundler) + expect { Bundler.safe_load_marshal(data) }.to raise_error(Bundler::MarshalError) + end + + it "loads simple structure" do + simple_structure = { "name" => [:development] } + data = Marshal.dump(simple_structure) + expect(Bundler.safe_load_marshal(data)).to eq(simple_structure) + end + + it "loads Gem::Specification" do + gem_spec = Gem::Specification.new do |s| + s.name = "bundler" + s.version = Gem::Version.new("2.4.7") + s.installed_by_version = Gem::Version.new("0") + s.authors = ["André Arko", + "Samuel Giddins", + "Colby Swandale", + "Hiroshi Shibata", + "David Rodríguez", + "Grey Baker", + "Stephanie Morillo", + "Chris Morris", + "James Wen", + "Tim Moore", + "André Medeiros", + "Jessica Lynn Suttles", + "Terence Lee", + "Carl Lerche", + "Yehuda Katz"] + s.date = Time.utc(2023, 2, 15) + s.description = "Bundler manages an application's dependencies through its entire life, across many machines, systematically and repeatably" + s.email = ["team@bundler.io"] + s.homepage = "https://bundler.io" + s.metadata = { "bug_tracker_uri" => "https://github.com/rubygems/rubygems/issues?q=is%3Aopen+is%3Aissue+label%3ABundler", + "changelog_uri" => "https://github.com/rubygems/rubygems/blob/master/bundler/CHANGELOG.md", + "homepage_uri" => "https://bundler.io/", + "source_code_uri" => "https://github.com/rubygems/rubygems/tree/master/bundler" } + s.require_paths = ["lib"] + s.required_ruby_version = Gem::Requirement.new([">= 2.6.0"]) + s.required_rubygems_version = Gem::Requirement.new([">= 3.0.1"]) + s.rubygems_version = "3.4.7" + s.specification_version = 4 + s.summary = "The best way to manage your application's dependencies" + s.license = false + end + data = Marshal.dump(gem_spec) + expect(Bundler.safe_load_marshal(data)).to eq(gem_spec) + end + end + describe "#load_gemspec_uncached" do let(:app_gemspec_path) { tmp("test.gemspec") } subject { Bundler.load_gemspec_uncached(app_gemspec_path) } @@ -11,7 +76,7 @@ RSpec.describe Bundler do context "with incorrect YAML file" do before do File.open(app_gemspec_path, "wb") do |f| - f.write strip_whitespace(<<-GEMSPEC) + f.write <<~GEMSPEC --- {:!00 ao=gu\g1= 7~f GEMSPEC @@ -23,7 +88,7 @@ RSpec.describe Bundler do end end - context "with correct YAML file", :if => defined?(Encoding) do + context "with correct YAML file", if: defined?(Encoding) do it "can load a gemspec with unicode characters with default ruby encoding" do # spec_helper forces the external encoding to UTF-8 but that's not the # default until Ruby 2.0 @@ -34,7 +99,7 @@ RSpec.describe Bundler do $VERBOSE = verbose File.open(app_gemspec_path, "wb") do |file| - file.puts <<-GEMSPEC.gsub(/^\s+/, "") + file.puts <<~GEMSPEC # -*- encoding: utf-8 -*- Gem::Specification.new do |gem| gem.author = "André the Giant" @@ -74,7 +139,7 @@ RSpec.describe Bundler do end GEMSPEC end - expect(Bundler.rubygems).to receive(:validate).with have_attributes(:name => "validated") + expect(Bundler.rubygems).to receive(:validate).with have_attributes(name: "validated") subject end end @@ -82,7 +147,7 @@ RSpec.describe Bundler do context "with gemspec containing local variables" do before do File.open(app_gemspec_path, "wb") do |f| - f.write strip_whitespace(<<-GEMSPEC) + f.write <<~GEMSPEC must_not_leak = true Gem::Specification.new do |gem| gem.name = "leak check" @@ -159,24 +224,6 @@ RSpec.describe Bundler do end end - describe "#rm_rf" do - context "the directory is world writable" do - let(:bundler_ui) { Bundler.ui } - it "should raise a friendly error" do - allow(File).to receive(:exist?).and_return(true) - allow(::Bundler::FileUtils).to receive(:remove_entry_secure).and_raise(ArgumentError) - allow(File).to receive(:world_writable?).and_return(true) - message = <<EOF -It is a security vulnerability to allow your home directory to be world-writable, and bundler can not continue. -You should probably consider fixing this issue by running `chmod o-w ~` on *nix. -Please refer to https://ruby-doc.org/stdlib-2.1.2/libdoc/fileutils/rdoc/FileUtils.html#method-c-remove_entry_secure for details. -EOF - expect(bundler_ui).to receive(:warn).with(message) - expect { Bundler.send(:rm_rf, bundled_app) }.to raise_error(Bundler::PathError) - end - end - end - describe "#mkdir_p" do it "creates a folder at the given path" do install_gemfile <<-G @@ -189,22 +236,6 @@ EOF Bundler.mkdir_p(bundled_app.join("foo", "bar")) expect(bundled_app.join("foo", "bar")).to exist end - - context "when mkdir_p requires sudo" do - it "creates a new folder using sudo" do - expect(Bundler).to receive(:requires_sudo?).and_return(true) - expect(Bundler).to receive(:sudo).and_return true - Bundler.mkdir_p(bundled_app.join("foo")) - end - end - - context "with :no_sudo option" do - it "forces mkdir_p to not use sudo" do - expect(Bundler).to receive(:requires_sudo?).and_return(true) - expect(Bundler).to_not receive(:sudo) - Bundler.mkdir_p(bundled_app.join("foo"), :no_sudo => true) - end - end end describe "#user_home" do @@ -268,118 +299,6 @@ EOF end end - describe "#requires_sudo?" do - let!(:tmpdir) { Dir.mktmpdir } - let(:bundle_path) { Pathname("#{tmpdir}/bundle") } - - def clear_cached_requires_sudo - return unless Bundler.instance_variable_defined?(:@requires_sudo_ran) - Bundler.remove_instance_variable(:@requires_sudo_ran) - Bundler.remove_instance_variable(:@requires_sudo) - end - - before do - clear_cached_requires_sudo - allow(Bundler).to receive(:which).with("sudo").and_return("/usr/bin/sudo") - allow(Bundler).to receive(:bundle_path).and_return(bundle_path) - end - - after do - FileUtils.rm_rf(tmpdir) - clear_cached_requires_sudo - end - - subject { Bundler.requires_sudo? } - - context "bundle_path doesn't exist" do - it { should be false } - - context "and parent dir can't be written" do - before do - FileUtils.chmod(0o500, tmpdir) - end - - it { should be true } - end - - context "with unwritable files in a parent dir" do - # Regression test for https://github.com/rubygems/bundler/pull/6316 - # It doesn't matter if there are other unwritable files so long as - # bundle_path can be created - before do - file = File.join(tmpdir, "unrelated_file") - FileUtils.touch(file) - FileUtils.chmod(0o400, file) - end - - it { should be false } - end - end - - context "bundle_path exists" do - before do - FileUtils.mkdir_p(bundle_path) - end - - it { should be false } - - context "and is unwritable" do - before do - FileUtils.chmod(0o500, bundle_path) - end - - it { should be true } - end - end - - context "path writability" do - before do - FileUtils.mkdir_p("tmp/vendor/bundle") - FileUtils.mkdir_p("tmp/vendor/bin_dir") - end - after do - FileUtils.rm_rf("tmp/vendor/bundle") - FileUtils.rm_rf("tmp/vendor/bin_dir") - end - context "writable paths" do - it "should return false and display nothing" do - allow(Bundler).to receive(:bundle_path).and_return(Pathname("tmp/vendor/bundle")) - expect(Bundler.ui).to_not receive(:warn) - expect(Bundler.requires_sudo?).to eq(false) - end - end - context "unwritable paths" do - before do - FileUtils.touch("tmp/vendor/bundle/unwritable1.txt") - FileUtils.touch("tmp/vendor/bundle/unwritable2.txt") - FileUtils.touch("tmp/vendor/bin_dir/unwritable3.txt") - FileUtils.chmod(0o400, "tmp/vendor/bundle/unwritable1.txt") - FileUtils.chmod(0o400, "tmp/vendor/bundle/unwritable2.txt") - FileUtils.chmod(0o400, "tmp/vendor/bin_dir/unwritable3.txt") - end - it "should return true and display warn message" do - allow(Bundler).to receive(:bundle_path).and_return(Pathname("tmp/vendor/bundle")) - bin_dir = Pathname("tmp/vendor/bin_dir/") - - # allow File#writable? to be called with args other than the stubbed on below - allow(File).to receive(:writable?).and_call_original - - # fake make the directory unwritable - allow(File).to receive(:writable?).with(bin_dir).and_return(false) - allow(Bundler).to receive(:system_bindir).and_return(Pathname("tmp/vendor/bin_dir/")) - message = <<-MESSAGE.chomp -Following files may not be writable, so sudo is needed: - tmp/vendor/bin_dir/ - tmp/vendor/bundle/unwritable1.txt - tmp/vendor/bundle/unwritable2.txt -MESSAGE - expect(Bundler.ui).to receive(:warn).with(message) - expect(Bundler.requires_sudo?).to eq(true) - end - end - end - end - context "user cache dir" do let(:home_path) { Pathname.new(ENV["HOME"]) } |