aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/test/cask/artifact/zap_test.rb
blob: 489299eca3e62678c3af119381427bc715740edd (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
require "test_helper"

# TODO: test that zap removes an alternate version of the same Cask
describe Hbc::Artifact::Zap do
  let(:cask) { Hbc.load("with-installable") }

  let(:zap_artifact) {
    Hbc::Artifact::Zap.new(cask, command: Hbc::FakeSystemCommand)
  }

  before do
    shutup do
      TestHelper.install_without_artifacts(cask)
    end
  end

  describe "install_phase" do
    it "does nothing, because the install_phase method is a no-op" do
      shutup do
        zap_artifact.install_phase
      end
    end
  end

  describe "uninstall_phase" do
    it "does nothing, because the uninstall_phase method is a no-op" do
      shutup do
        zap_artifact.uninstall_phase
      end
    end
  end

  describe "zap_phase" do
    subject do
      shutup do
        zap_artifact.zap_phase
      end
    end

    describe "when using launchctl" do
      let(:cask) { Hbc.load("with-zap-launchctl") }
      let(:launchctl_list_cmd) { %w[/bin/launchctl list my.fancy.package.service] }
      let(:launchctl_remove_cmd) { %w[/bin/launchctl remove my.fancy.package.service] }
      let(:unknown_response) { "launchctl list returned unknown response\n" }
      let(:service_info) {
        <<-EOS.undent
          {
                  "LimitLoadToSessionType" = "Aqua";
                  "Label" = "my.fancy.package.service";
                  "TimeOut" = 30;
                  "OnDemand" = true;
                  "LastExitStatus" = 0;
                  "ProgramArguments" = (
                          "argument";
                  );
          };
        EOS
      }

      describe "when launchctl job is owned by user" do
        it "can zap" do
          Hbc::FakeSystemCommand.stubs_command(
            launchctl_list_cmd,
            service_info
          )

          Hbc::FakeSystemCommand.stubs_command(
            sudo(launchctl_list_cmd),
            unknown_response
          )

          Hbc::FakeSystemCommand.expects_command(launchctl_remove_cmd)

          subject
        end
      end

      describe "when launchctl job is owned by system" do
        it "can zap" do
          Hbc::FakeSystemCommand.stubs_command(
            launchctl_list_cmd,
            unknown_response
          )

          Hbc::FakeSystemCommand.stubs_command(
            sudo(launchctl_list_cmd),
            service_info
          )

          Hbc::FakeSystemCommand.expects_command(sudo(launchctl_remove_cmd))

          subject
        end
      end
    end

    describe "when using pkgutil" do
      let(:cask) { Hbc.load("with-zap-pkgutil") }
      let(:main_pkg_id) { "my.fancy.package.main" }
      let(:agent_pkg_id) { "my.fancy.package.agent" }
      let(:main_files) {
        %w[
          fancy/bin/fancy.exe
          fancy/var/fancy.data
        ]
      }
      let(:main_dirs) {
        %w[
          fancy
          fancy/bin
          fancy/var
        ]
      }
      let(:agent_files) {
        %w[
          fancy/agent/fancy-agent.exe
          fancy/agent/fancy-agent.pid
          fancy/agent/fancy-agent.log
        ]
      }
      let(:agent_dirs) {
        %w[
          fancy
          fancy/agent
        ]
      }
      let(:pkg_info_plist) {
        <<-EOS.undent
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
          <plist version="1.0">
          <dict>
                  <key>install-location</key>
                  <string>tmp</string>
                  <key>volume</key>
                  <string>/</string>
          </dict>
          </plist>
        EOS
      }

      it "can zap" do
        Hbc::FakeSystemCommand.stubs_command(
          %w[/usr/sbin/pkgutil --pkgs=my.fancy.package.*],
          "#{main_pkg_id}\n#{agent_pkg_id}"
        )

        [
          [main_pkg_id, main_files, main_dirs],
          [agent_pkg_id, agent_files, agent_dirs],
        ].each do |pkg_id, pkg_files, pkg_dirs|
          Hbc::FakeSystemCommand.stubs_command(
            %W[/usr/sbin/pkgutil --only-files --files #{pkg_id}],
            pkg_files.join("\n")
          )

          Hbc::FakeSystemCommand.stubs_command(
            %W[/usr/sbin/pkgutil --only-dirs --files #{pkg_id}],
            pkg_dirs.join("\n")
          )

          Hbc::FakeSystemCommand.stubs_command(
            %W[/usr/sbin/pkgutil --files #{pkg_id}],
            (pkg_files + pkg_dirs).join("\n")
          )

          Hbc::FakeSystemCommand.stubs_command(
            %W[/usr/sbin/pkgutil --pkg-info-plist #{pkg_id}],
            pkg_info_plist
          )

          Hbc::FakeSystemCommand.expects_command(sudo(%W[/usr/sbin/pkgutil --forget #{pkg_id}]))

          Hbc::FakeSystemCommand.expects_command(
            sudo(%w[/bin/rm -f --] + pkg_files.map { |path| Pathname("/tmp/#{path}") })
          )
        end

        subject
      end
    end

    describe "when using kext" do
      let(:cask) { Hbc.load("with-zap-kext") }
      let(:kext_id) { "my.fancy.package.kernelextension" }

      it "can zap" do
        Hbc::FakeSystemCommand.stubs_command(
          sudo(%W[/usr/sbin/kextstat -l -b #{kext_id}]), "loaded"
        )

        Hbc::FakeSystemCommand.expects_command(
          sudo(%W[/sbin/kextunload -b #{kext_id}])
        )

        subject
      end
    end

    describe "when using quit" do
      let(:cask) { Hbc.load("with-zap-quit") }
      let(:bundle_id) { "my.fancy.package.app" }
      let(:count_processes_script) {
        'tell application "System Events" to count processes ' +
          %Q{whose bundle identifier is "#{bundle_id}"}
      }
      let(:quit_application_script) {
        %Q{tell application id "#{bundle_id}" to quit}
      }

      it "can zap" do
        Hbc::FakeSystemCommand.stubs_command(
          sudo(%W[/usr/bin/osascript -e #{count_processes_script}]), "1"
        )

        Hbc::FakeSystemCommand.stubs_command(
          sudo(%W[/usr/bin/osascript -e #{quit_application_script}])
        )

        subject
      end
    end

    describe "when using signal" do
      let(:cask) { Hbc.load("with-zap-signal") }
      let(:bundle_id) { "my.fancy.package.app" }
      let(:signals) { %w[TERM KILL] }
      let(:unix_pids) { [12_345, 67_890] }
      let(:get_unix_pids_script) {
        'tell application "System Events" to get the unix id of every process ' +
          %Q{whose bundle identifier is "#{bundle_id}"}
      }

      it "can zap" do
        Hbc::FakeSystemCommand.stubs_command(
          sudo(%W[/usr/bin/osascript -e #{get_unix_pids_script}]), unix_pids.join(", ")
        )

        signals.each do |signal|
          Process.expects(:kill).with(signal, *unix_pids)
        end

        subject
      end
    end

    describe "when using delete" do
      let(:cask) { Hbc.load("with-zap-delete") }

      it "can zap" do
        Hbc::FakeSystemCommand.expects_command(
          sudo(%w[/bin/rm -rf --],
               Pathname.new("/permissible/absolute/path"),
               Pathname.new("~/permissible/path/with/tilde").expand_path)
        )

        subject
      end
    end

    describe "when using trash" do
      let(:cask) { Hbc.load("with-zap-trash") }

      it "can zap" do
        Hbc::FakeSystemCommand.expects_command(
          sudo(%w[/bin/rm -rf --],
               Pathname.new("/permissible/absolute/path"),
               Pathname.new("~/permissible/path/with/tilde").expand_path)
        )

        subject
      end
    end

    describe "when using rmdir" do
      let(:cask) { Hbc.load("with-zap-rmdir") }
      let(:dir_pathname) { Pathname(TestHelper.local_binary_path("empty_directory")) }

      it "can zap" do
        Hbc::FakeSystemCommand.expects_command(
          sudo(%w[/bin/rm -f --], dir_pathname.join(".DS_Store"))
        )

        Hbc::FakeSystemCommand.expects_command(
          sudo(%w[/bin/rmdir --], dir_pathname)
        )

        subject
      end
    end

    describe "when using script" do
      let(:cask) { Hbc.load("with-zap-script") }
      let(:script_pathname) { cask.staged_path.join("MyFancyPkg", "FancyUninstaller.tool") }

      it "can zap" do
        Hbc::FakeSystemCommand.expects_command(%w[/bin/chmod -- +x] + [script_pathname])

        Hbc::FakeSystemCommand.expects_command(
          sudo(cask.staged_path.join("MyFancyPkg", "FancyUninstaller.tool"), "--please")
        )

        subject
      end
    end

    describe "when using early_script" do
      let(:cask) { Hbc.load("with-zap-early-script") }
      let(:script_pathname) { cask.staged_path.join("MyFancyPkg", "FancyUninstaller.tool") }

      it "can zap" do
        Hbc::FakeSystemCommand.expects_command(%w[/bin/chmod -- +x] + [script_pathname])

        Hbc::FakeSystemCommand.expects_command(
          sudo(cask.staged_path.join("MyFancyPkg", "FancyUninstaller.tool"), "--please")
        )

        subject
      end
    end

    describe "when using login_item" do
      let(:cask) { Hbc.load("with-zap-login-item") }

      it "can zap" do
        Hbc::FakeSystemCommand.expects_command(
          ["/usr/bin/osascript", "-e", 'tell application "System Events" to delete every login ' \
                                       'item whose name is "Fancy"']
        )

        subject
      end
    end
  end
end