Skip to content

Commit 4d46094

Browse files
soda92matzbot
authored andcommitted
[rubygems/rubygems] Rework Bundler.which tests
Refactor to use real test cases rather than mock. Add relative path tests wich `Dir.chdir`. ruby/rubygems@ed556a0a53
1 parent 55f2917 commit 4d46094

File tree

1 file changed

+19
-42
lines changed

1 file changed

+19
-42
lines changed

spec/bundler/bundler/bundler_spec.rb

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -164,59 +164,36 @@
164164
end
165165

166166
describe "#which" do
167-
let(:executable) { "executable" }
167+
it "can detect relative path" do
168+
script_path = bundled_app("tmp/test_command")
169+
create_file(script_path, "#!/usr/bin/env ruby\n")
168170

169-
let(:path) do
170-
if Gem.win_platform?
171-
%w[C:/a C:/b C:/c C:/../d C:/e]
172-
else
173-
%w[/a /b c ../d /e]
171+
result = Dir.chdir script_path.dirname.dirname do
172+
Bundler.which("test_command")
174173
end
175-
end
174+
expect(result).to eq(nil)
176175

177-
let(:expected) do
178-
if Gem.win_platform?
179-
"executable.exe"
180-
else
181-
"executable"
176+
result = Dir.chdir script_path.dirname do
177+
Bundler.which("test_command")
182178
end
183-
end
184-
185-
before do
186-
ENV["PATH"] = path.join(File::PATH_SEPARATOR)
187179

188-
allow(File).to receive(:file?).and_return(false)
189-
allow(File).to receive(:executable?).and_return(false)
190-
if expected
191-
expect(File).to receive(:file?).with(expected).and_return(true)
192-
expect(File).to receive(:executable?).with(expected).and_return(true)
193-
end
180+
expect(result).to eq("test_command") unless Gem.win_platform?
181+
expect(result).to eq("test_command.bat") if Gem.win_platform?
194182
end
195183

196-
subject { described_class.which(executable) }
184+
it "can detect absolute path" do
185+
create_file("test_command", "#!/usr/bin/env ruby\n")
197186

198-
shared_examples_for "it returns the correct executable" do
199-
it "returns the expected file" do
200-
expect(subject).to eq(expected)
201-
end
202-
end
203-
204-
it_behaves_like "it returns the correct executable"
187+
ENV["PATH"] = bundled_app("test_command").parent.to_s
205188

206-
context "when the executable in inside a quoted path" do
207-
let(:expected) do
208-
if Gem.win_platform?
209-
"C:/e/executable.exe"
210-
else
211-
"/e/executable"
212-
end
213-
end
214-
it_behaves_like "it returns the correct executable"
189+
result = Bundler.which("test_command")
190+
expect(result).to eq(bundled_app("test_command").to_s) unless Gem.win_platform?
191+
expect(result).to eq(bundled_app("test_command.bat").to_s) if Gem.win_platform?
215192
end
216193

217-
context "when the executable is not found" do
218-
let(:expected) { nil }
219-
it_behaves_like "it returns the correct executable"
194+
it "returns nil when not found" do
195+
result = Bundler.which("test_command")
196+
expect(result).to eq(nil)
220197
end
221198
end
222199

0 commit comments

Comments
 (0)