aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/caveats_spec.rb
blob: 96ed7ea17932ed3ea75019f5b5144dee9142b847 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
require "formula"
require "caveats"

describe Caveats do
  subject { described_class.new(f) }
  let(:f) { formula { url "foo-1.0" } }

  specify "#f" do
    expect(subject.f).to eq(f)
  end

  describe "#empty?" do
    it "returns true if the Formula has no caveats" do
      expect(subject).to be_empty
    end

    it "returns false if the Formula has caveats" do
      f = formula do
        url "foo-1.0"

        def caveats
          "something"
        end
      end

      expect(described_class.new(f)).not_to be_empty
    end
  end

  describe "#caveats" do
    context "when f.plist is not nil", :needs_macos do
      it "prints plist startup information when f.plist_startup is not nil" do
        f = formula do
          url "foo-1.0"
          def plist
            "plist_test.plist"
          end
          plist_options startup: true
        end
        expect(described_class.new(f).caveats).to include("startup")
      end

      it "prints plist login information when f.plist_startup is nil" do
        f = formula do
          url "foo-1.0"
          def plist
            "plist_test.plist"
          end
        end
        expect(described_class.new(f).caveats).to include("login")
      end

      it "gives information about restarting services after upgrade" do
        f = formula do
          url "foo-1.0"
          def plist
            "plist_test.plist"
          end
          plist_options startup: true
        end
        f_obj = described_class.new(f)
        plist_path = mktmpdir/"plist"
        FileUtils.touch plist_path
        allow(f_obj).to receive(:plist_path).and_return(plist_path)
        allow(plist_path).to receive(:symlink?).and_return(true)
        expect(f_obj.caveats).to include("restart #{f.full_name}")
        expect(f_obj.caveats).to include("sudo")
      end

      context "when plist_path is not a file nor symlinked and plist_startup is false" do
        let(:f) {
          formula do
            url "foo-1.0"
            def plist
              "plist_test.plist"
            end
          end
        }
        let(:f_obj) { described_class.new(f) }
        let(:caveats) { f_obj.caveats }
        let(:plist_path) { mktmpdir/"plist" }

        before do
          FileUtils.touch plist_path
          allow(f_obj).to receive(:plist_path).and_return(plist_path)
          allow(plist_path).to receive(:symlink?).and_return(true)
        end

        it "tells command to run after upgrade" do
          allow(Kernel).to receive(:system).with(any_args).and_return(true)
          expect(caveats).to include("restart #{f.full_name} after an upgrade")
        end

        it "tells command to run to start formula" do
          expect(caveats).to include("To start #{f.full_name}:")
        end
      end

      it "gives information about plist_manual" do
        f = formula do
          url "foo-1.0"
          def plist
            "plist_test.plist"
          end
          plist_options manual: "foo"
        end
        caveats = described_class.new(f).caveats

        expect(caveats).to include("background service")
        expect(caveats).to include(f.plist_manual)
      end

      it "warns about brew failing under tmux" do
        f = formula do
          url "foo-1.0"
          def plist
            "plist_test.plist"
          end
        end
        allow(ENV).to receive(:[]).with("TMUX").and_return(true)
        allow(Homebrew).to receive(:_system).with("/usr/bin/pbpaste").and_return(false)
        caveats = described_class.new(f).caveats

        expect(caveats).to include("WARNING:")
        expect(caveats).to include("tmux")
      end
    end

    context "when f.keg_only is not nil" do
      let(:f) {
        formula do
          url "foo-1.0"
          keg_only "some reason"
        end
      }
      let(:caveats) { described_class.new(f).caveats }

      it "tells formula is keg_only" do
        expect(caveats).to include("keg-only")
      end

      it "gives command to be run when f.bin is a directory" do
        Pathname.new(f.bin).mkpath
        expect(caveats).to include(f.opt_bin.to_s)
      end

      it "gives command to be run when f.sbin is a directory" do
        Pathname.new(f.sbin).mkpath
        expect(caveats).to include(f.opt_sbin.to_s)
      end

      context "when f.lib or f.include is a directory" do
        it "gives command to be run when f.lib is a directory" do
          Pathname.new(f.lib).mkpath
          expect(caveats).to include("-L#{f.opt_lib}")
        end

        it "gives command to be run when f.include is a directory" do
          Pathname.new(f.include).mkpath
          expect(caveats).to include("-I#{f.opt_include}")
        end

        it "gives PKG_CONFIG_PATH when f.lib/'pkgconfig' and f.share/'pkgconfig' are directories" do
          allow_any_instance_of(Object).to receive(:which).with(any_args).and_return(Pathname.new("blah"))

          Pathname.new(f.share/"pkgconfig").mkpath
          Pathname.new(f.lib/"pkgconfig").mkpath

          expect(caveats).to include("#{f.opt_lib}/pkgconfig")
          expect(caveats).to include("#{f.opt_share}/pkgconfig")
        end
      end
    end

    context "shell completions" do
      let(:f) {
        formula do
          url "foo-1.0"
        end
      }
      let(:caveats) { described_class.new(f).caveats }
      let(:path) { f.prefix.resolved_path }

      before do
        allow_any_instance_of(Pathname).to receive(:children).and_return([Pathname.new("child")])
        allow_any_instance_of(Object).to receive(:which).with(any_args).and_return(Pathname.new("shell"))
      end

      it "gives dir where bash completions have been installed" do
        (path/"etc/bash_completion.d").mkpath
        expect(caveats).to include(HOMEBREW_PREFIX/"etc/bash_completion.d")
      end

      it "gives dir where zsh completions have been installed" do
        (path/"share/zsh/site-functions").mkpath
        expect(caveats).to include(HOMEBREW_PREFIX/"share/zsh/site-functions")
      end

      it "gives dir where fish completions have been installed" do
        (path/"share/fish/vendor_completions.d").mkpath
        expect(caveats).to include(HOMEBREW_PREFIX/"share/fish/vendor_completions.d")
      end
    end

    context "python caveats" do
      before do
        (f.prefix.resolved_path/"lib/python2.7/site-packages").mkpath
      end

      context "when f is not keg_only" do
        let(:f) {
          formula do
            url "foo-1.0"
          end
        }
        let(:caveats) { described_class.new(f).caveats }
        let(:user_site_packages) { Language::Python.user_site_packages("python") }

        it "give commands to run when Homebrew's site-packages is not in Python sys.path" do
          expect(caveats).to include("Homebrew's site-packages is not\nin your Python sys.path")
          expect(caveats).to include(user_site_packages)
          expect(caveats).to include("import site")
        end

        it "gives commands to run when python pth files are installed" do
          allow(Homebrew).to receive(:_system).and_return(true)
          allow(Dir).to receive(:[]).with(any_args).and_return(["blah.pth"])
          expect(caveats).to include(".pth files to Homebrew's site-packages and your\nPython isn't configured")
          expect(caveats).to include(user_site_packages)
          expect(caveats).to include("import site")
        end
      end

      it "gives commands to run when formula is keg_only" do
        f = formula do
          url "foo-1.0"
          keg_only "some reason"
        end
        caveats = described_class.new(f).caveats
        homebrew_site_packages = Language::Python.homebrew_site_packages
        expect(caveats).to include("echo #{f.opt_prefix}/lib/python2.7/site-packages >> #{homebrew_site_packages/f.name}.pth")
      end
    end
  end
end