aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/cask/cli/search_spec.rb
blob: cd1a7bd43c849ef26e17f94796fb5c69d04968f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require_relative "shared_examples/invalid_option"

describe Hbc::CLI::Search, :cask do
  before(:each) do
    allow(Tty).to receive(:width).and_return(0)
  end

  it_behaves_like "a command that handles invalid options"

  it "lists the available Casks that match the search term" do
    allow(GitHub).to receive(:search_code).and_return([])

    expect {
      Hbc::CLI::Search.run("local")
    }.to output(<<~EOS).to_stdout.as_tty
      ==> Partial Matches
      local-caffeine
      local-transmission
    EOS
  end

  it "outputs a plain list when stdout is not a TTY" do
    allow(GitHub).to receive(:search_code).and_return([])

    expect {
      Hbc::CLI::Search.run("local")
    }.to output(<<~EOS).to_stdout
      local-caffeine
      local-transmission
    EOS
  end

  it "returns matches even when online search failed" do
    allow(GitHub).to receive(:search_code).and_raise(GitHub::Error.new("reason"))

    expect {
      Hbc::CLI::Search.run("local")
    }.to output(<<~EOS).to_stdout
      local-caffeine
      local-transmission
    EOS
    .and output(/^Warning: Error searching on GitHub: reason/).to_stderr
  end

  it "shows that there are no Casks matching a search term that did not result in anything" do
    expect {
      Hbc::CLI::Search.run("foo-bar-baz")
    }.to output(<<~EOS).to_stdout.as_tty
      No Cask found for "foo-bar-baz".
    EOS
  end

  it "doesn't output anything to non-TTY stdout when there are no matches" do
    expect { Hbc::CLI::Search.run("foo-bar-baz") }
      .to not_to_output.to_stdout
      .and not_to_output.to_stderr
  end

  it "lists all Casks available offline with no search term" do
    allow(GitHub).to receive(:search_code).and_raise(GitHub::Error.new("reason"))
    expect { Hbc::CLI::Search.run }
      .to output(/local-caffeine/).to_stdout.as_tty
      .and not_to_output.to_stderr
  end

  it "ignores hyphens in search terms" do
    expect {
      Hbc::CLI::Search.run("lo-cal-caffeine")
    }.to output(/local-caffeine/).to_stdout.as_tty
  end

  it "ignores hyphens in Cask tokens" do
    expect {
      Hbc::CLI::Search.run("localcaffeine")
    }.to output(/local-caffeine/).to_stdout.as_tty
  end

  it "accepts multiple arguments" do
    expect {
      Hbc::CLI::Search.run("local caffeine")
    }.to output(/local-caffeine/).to_stdout.as_tty
  end

  it "accepts a regexp argument" do
    expect {
      Hbc::CLI::Search.run("/^local-c[a-z]ffeine$/")
    }.to output(<<~EOS).to_stdout.as_tty
      ==> Regexp Matches
      local-caffeine
    EOS
  end

  it "returns both exact and partial matches" do
    expect {
      Hbc::CLI::Search.run("test-opera")
    }.to output(<<~EOS).to_stdout.as_tty
      ==> Exact Match
      test-opera
      ==> Partial Matches
      test-opera-mail
    EOS
  end

  it "does not search the Tap name" do
    expect {
      Hbc::CLI::Search.run("caskroom")
    }.to output(<<~EOS).to_stdout.as_tty
      No Cask found for "caskroom".
    EOS
  end

  it "doesn't highlight packages that aren't installed" do
    expect(Hbc::CLI::Search.highlight_installed("local-caffeine")).to eq("local-caffeine")
  end

  it "highlights installed packages" do
    Hbc::CLI::Install.run("local-caffeine")

    expect(Hbc::CLI::Search.highlight_installed("local-caffeine")).to eq(pretty_installed("local-caffeine"))
  end
end