Skip to content

Commit bf8c2a3

Browse files
committed
Fix which command test isolation
1 parent 569c5c7 commit bf8c2a3

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

PR_DESCRIPTION.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# PR Description
2+
3+
## Title
4+
Fix gem which command test isolation
5+
6+
## Problem
7+
The `gem which` command reports the filesystem path for a requireable file; `test_execute_one_missing` verifies it prints the found path and errors on a missing file. The test fails when run in isolation because it relies on gem path/spec state set by other tests.
8+
9+
## Fix
10+
Set `GEM_HOME`/`GEM_PATH` to `@gemhome`, call `Gem.use_paths(@gemhome)`, and reset the spec cache before the test runs. Restore the env and gem paths in an ensure block and reset the spec cache afterward.
11+
12+
## Testing
13+
- `ruby -Ilib:test:bundler/lib test/rubygems/test_gem_commands_which_command.rb -n test_execute_one_missing`

test/rubygems/test_gem_commands_which_command.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ def test_execute_directory
3838
end
3939

4040
def test_execute_one_missing
41-
# TODO: this test fails in isolation
41+
original_gem_home = ENV["GEM_HOME"]
42+
original_gem_path = ENV["GEM_PATH"]
43+
44+
ENV["GEM_HOME"] = @gemhome
45+
ENV["GEM_PATH"] = @gemhome
46+
Gem.use_paths(@gemhome)
47+
Gem::Specification.reset
4248

4349
util_foo_bar
4450

@@ -53,6 +59,21 @@ def test_execute_one_missing
5359
assert_equal "#{@foo_bar.full_gem_path}/lib/foo_bar.rb\n", @ui.output
5460
assert_match(/Can.t find Ruby library file or shared library missinglib\n/,
5561
@ui.error)
62+
ensure
63+
if original_gem_home
64+
ENV["GEM_HOME"] = original_gem_home
65+
else
66+
ENV.delete("GEM_HOME")
67+
end
68+
69+
if original_gem_path
70+
ENV["GEM_PATH"] = original_gem_path
71+
else
72+
ENV.delete("GEM_PATH")
73+
end
74+
75+
Gem.use_paths(@gemhome)
76+
Gem::Specification.reset
5677
end
5778

5879
def test_execute_missing

0 commit comments

Comments
 (0)