From 4d460944c777b55882dad4c487dfe0a84ca71be1 Mon Sep 17 00:00:00 2001 From: sodacris Date: Mon, 2 Dec 2024 10:51:32 +0800 Subject: [PATCH] [rubygems/rubygems] Rework `Bundler.which` tests Refactor to use real test cases rather than mock. Add relative path tests wich `Dir.chdir`. https://github.com/rubygems/rubygems/commit/ed556a0a53 --- spec/bundler/bundler/bundler_spec.rb | 71 ++++++++++------------------ 1 file changed, 24 insertions(+), 47 deletions(-) diff --git a/spec/bundler/bundler/bundler_spec.rb b/spec/bundler/bundler/bundler_spec.rb index 27bcb92659..4db8c00e52 100644 --- a/spec/bundler/bundler/bundler_spec.rb +++ b/spec/bundler/bundler/bundler_spec.rb @@ -164,59 +164,36 @@ RSpec.describe Bundler do end describe "#which" do - let(:executable) { "executable" } + it "can detect relative path" do + script_path = bundled_app("tmp/test_command") + create_file(script_path, "#!/usr/bin/env ruby\n") - let(:path) do - if Gem.win_platform? - %w[C:/a C:/b C:/c C:/../d C:/e] - else - %w[/a /b c ../d /e] + result = Dir.chdir script_path.dirname.dirname do + Bundler.which("test_command") end + expect(result).to eq(nil) + + result = Dir.chdir script_path.dirname do + Bundler.which("test_command") + end + + expect(result).to eq("test_command") unless Gem.win_platform? + expect(result).to eq("test_command.bat") if Gem.win_platform? end - let(:expected) do - if Gem.win_platform? - "executable.exe" - else - "executable" - end + it "can detect absolute path" do + create_file("test_command", "#!/usr/bin/env ruby\n") + + ENV["PATH"] = bundled_app("test_command").parent.to_s + + result = Bundler.which("test_command") + expect(result).to eq(bundled_app("test_command").to_s) unless Gem.win_platform? + expect(result).to eq(bundled_app("test_command.bat").to_s) if Gem.win_platform? end - before do - ENV["PATH"] = path.join(File::PATH_SEPARATOR) - - allow(File).to receive(:file?).and_return(false) - allow(File).to receive(:executable?).and_return(false) - if expected - expect(File).to receive(:file?).with(expected).and_return(true) - expect(File).to receive(:executable?).with(expected).and_return(true) - end - end - - subject { described_class.which(executable) } - - shared_examples_for "it returns the correct executable" do - it "returns the expected file" do - expect(subject).to eq(expected) - end - end - - it_behaves_like "it returns the correct executable" - - context "when the executable in inside a quoted path" do - let(:expected) do - if Gem.win_platform? - "C:/e/executable.exe" - else - "/e/executable" - end - end - it_behaves_like "it returns the correct executable" - end - - context "when the executable is not found" do - let(:expected) { nil } - it_behaves_like "it returns the correct executable" + it "returns nil when not found" do + result = Bundler.which("test_command") + expect(result).to eq(nil) end end