summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsodacris <wjxa20152015@gmail.com>2024-12-02 10:51:32 +0800
committergit <svn-admin@ruby-lang.org>2024-12-04 08:35:00 +0000
commit4d460944c777b55882dad4c487dfe0a84ca71be1 (patch)
tree06eba1cd9c372e8c0b4e5f15bae4988dc9b7d56e
parent55f2917cfda6a792e4e24e8b2b98e55ff4a400fd (diff)
[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
-rw-r--r--spec/bundler/bundler/bundler_spec.rb61
1 files changed, 19 insertions, 42 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
- end
+ expect(result).to eq(nil)
- let(:expected) do
- if Gem.win_platform?
- "executable.exe"
- else
- "executable"
+ result = Dir.chdir script_path.dirname do
+ Bundler.which("test_command")
end
- 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
+ expect(result).to eq("test_command") unless Gem.win_platform?
+ expect(result).to eq("test_command.bat") if Gem.win_platform?
end
- subject { described_class.which(executable) }
+ it "can detect absolute path" do
+ create_file("test_command", "#!/usr/bin/env ruby\n")
- 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"
+ ENV["PATH"] = bundled_app("test_command").parent.to_s
- 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"
+ 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
- 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