1require 'rubygems/test_case'
2require 'rubygems/commands/which_command'
3
4class TestGemCommandsWhichCommand < Gem::TestCase
5
6  def setup
7    super
8    Gem::Specification.reset
9    @cmd = Gem::Commands::WhichCommand.new
10  end
11
12  def test_execute
13    util_foo_bar
14
15    @cmd.handle_options %w[foo_bar]
16
17    use_ui @ui do
18      @cmd.execute
19    end
20
21    assert_equal "#{@foo_bar.full_gem_path}/lib/foo_bar.rb\n", @ui.output
22    assert_equal '', @ui.error
23  end
24
25  def test_execute_directory
26    @cmd.handle_options %w[directory]
27
28    use_ui @ui do
29      assert_raises Gem::MockGemUi::TermError do
30        @cmd.execute
31      end
32    end
33
34    assert_equal '', @ui.output
35    assert_match %r%Can.t find ruby library file or shared library directory\n%,
36                 @ui.error
37  end
38
39  def test_execute_one_missing
40    # TODO: this test fails in isolation
41
42    util_foo_bar
43
44    @cmd.handle_options %w[foo_bar missinglib]
45
46    use_ui @ui do
47      @cmd.execute
48    end
49
50    assert_equal "#{@foo_bar.full_gem_path}/lib/foo_bar.rb\n", @ui.output
51    assert_match %r%Can.t find ruby library file or shared library missinglib\n%,
52                 @ui.error
53  end
54
55  def test_execute_missing
56    @cmd.handle_options %w[missinglib]
57
58    use_ui @ui do
59      assert_raises Gem::MockGemUi::TermError do
60        @cmd.execute
61      end
62    end
63
64    assert_equal '', @ui.output
65    assert_match %r%Can.t find ruby library file or shared library missinglib\n%,
66                 @ui.error
67  end
68
69  def util_foo_bar
70    files = %w[lib/foo_bar.rb lib/directory/baz.rb Rakefile]
71    @foo_bar = quick_spec 'foo_bar' do |gem|
72      gem.files = files
73    end
74
75    files.each do |file|
76      filename = File.join(@foo_bar.full_gem_path, file)
77      FileUtils.mkdir_p File.dirname filename
78      FileUtils.touch filename
79    end
80  end
81
82end
83
84